response

package
v0.0.0-...-288c4de Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package response contains helpers for rendering LXD HTTP responses.

Index

Constants

This section is empty.

Variables

View Source
var EmptySyncResponse = &syncResponse{success: true, metadata: make(map[string]any)}

EmptySyncResponse represents an empty syncResponse.

Functions

func Init

func Init(d bool)

Init sets the debug variable to the provided value.

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true if the error is considered a Not Found error.

func Upgrade

func Upgrade(hijackedConn net.Conn, protocolName string) error

Upgrade takes a hijacked HTTP connection and sends the HTTP 101 Switching Protocols headers for protocolName.

Types

type FileResponseEntry

type FileResponseEntry struct {
	// Required.
	Identifier string
	Filename   string

	// Read from a filesystem path.
	Path string

	// Read from a file.
	File         io.ReadSeeker
	FileSize     int64
	FileModified time.Time

	// Optional.
	Cleanup func()
}

FileResponseEntry represents a file response entry.

type Response

type Response interface {
	Render(w http.ResponseWriter) error
	String() string
}

Response represents an API response.

func BadRequest

func BadRequest(err error) Response

BadRequest returns a bad request response (400) with the given error.

func Conflict

func Conflict(err error) Response

Conflict returns a conflict response (409) with the given error.

func DevLxdErrorResponse

func DevLxdErrorResponse(err error, rawResponse bool) Response

DevLxdErrorResponse returns an error response. If rawResponse is true, a api.ResponseRaw will be sent instead of a minimal devLxdResponse.

func DevLxdResponse

func DevLxdResponse(code int, content any, contentType string, rawResponse bool) Response

DevLxdResponse represents a devLxdResponse. If rawResponse is true, a api.ResponseRaw will be sent instead of a minimal devLxdResponse.

func ErrorResponse

func ErrorResponse(code int, msg string) Response

ErrorResponse returns an error response with the given code and msg.

func FileResponse

func FileResponse(r *http.Request, files []FileResponseEntry, headers map[string]string) Response

FileResponse returns a new file response.

func Forbidden

func Forbidden(err error) Response

Forbidden returns a forbidden response (403) with the given error.

func ForwardedResponse

func ForwardedResponse(client lxd.InstanceServer, request *http.Request) Response

ForwardedResponse takes a request directed to a node and forwards it to another node, writing back the response it gegs.

func InternalError

func InternalError(err error) Response

InternalError returns an internal error response (500) with the given error.

func ManualResponse

func ManualResponse(hook func(w http.ResponseWriter) error) Response

ManualResponse creates a new manual response responder.

func NotFound

func NotFound(err error) Response

NotFound returns a not found response (404) with the given error.

func NotImplemented

func NotImplemented(err error) Response

NotImplemented returns a not implemented response (501) with the given error.

func PreconditionFailed

func PreconditionFailed(err error) Response

PreconditionFailed returns a precondition failed response (412) with the given error.

func SmartError

func SmartError(err error) Response

SmartError returns the right error message based on err. It uses the stdlib errors package to unwrap the error and find the cause.

func SyncResponse

func SyncResponse(success bool, metadata any) Response

SyncResponse returns a new syncResponse with the success and metadata fields set to the provided values.

func SyncResponseETag

func SyncResponseETag(success bool, metadata any, etag any) Response

SyncResponseETag returns a new syncResponse with an etag.

func SyncResponseHeaders

func SyncResponseHeaders(success bool, metadata any, headers map[string]string) Response

SyncResponseHeaders returns a new syncResponse with headers.

func SyncResponseLocation

func SyncResponseLocation(success bool, metadata any, location string) Response

SyncResponseLocation returns a new syncResponse with a location.

func SyncResponsePlain

func SyncResponsePlain(success bool, compress bool, metadata string) Response

SyncResponsePlain return a new syncResponse with plaintext.

func SyncResponseRedirect

func SyncResponseRedirect(address string) Response

SyncResponseRedirect returns a new syncResponse with a location, indicating a permanent redirect.

func Unauthorized

func Unauthorized(err error) Response

Unauthorized return an unauthorized response (401) with the given error.

func Unavailable

func Unavailable(err error) Response

Unavailable return an unavailable response (503) with the given error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL