response

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderContentType        = "Content-Type"
	HeaderContentTypeOptions = "X-Content-Type-Options"
)

Variables

View Source
var ErrNotHttp2Request = errors.New("rejected, not a HTTP/2 request")

Functions

This section is empty.

Types

type EventGenerator

type EventGenerator func(ctx context.Context, lastEventId string) <-chan ServerSentEvent

The EventGenerator is a function that received a cancellation context.Context and the last event id received by the client and returns a channel of ServerSentEvent objects

type ExecutableTemplate

type ExecutableTemplate interface {
	Execute(wr io.Writer, data any) error
	ExecuteTemplate(wr io.Writer, name string, data any) error
}

type HttpCompressResponse

type HttpCompressResponse struct {
	// contains filtered or unexported fields
}

HttpCompressResponse implements response.HttpResponse and provides gzip HTTP compression

func NewHttpCompressResponse

func NewHttpCompressResponse(httpResponse HttpResponse, compressionLevel int) (*HttpCompressResponse, error)

NewHttpCompressResponse creates a new HttpResponse that provide gzip compression for an HTTP responses as long as the 'Accept-Encoding' request header specify it The compressionLevel should be: gzip.DefaultCompression, gzip.NoCompression or any integer value between gzip.BestSpeed and gzip.BestCompression inclusive

func (*HttpCompressResponse) Cookies

func (r *HttpCompressResponse) Cookies() HttpCookies

func (*HttpCompressResponse) Headers

func (r *HttpCompressResponse) Headers() HttpHeaders

func (*HttpCompressResponse) StatusCode

func (r *HttpCompressResponse) StatusCode() int

func (*HttpCompressResponse) Write

type HttpCookies

type HttpCookies map[string]*http.Cookie

HttpCookies is a map with custom cookies

func NewEmptyHttpCookie added in v1.1.0

func NewEmptyHttpCookie() HttpCookies

NewEmptyHttpCookie returns an instance of HttpCookies from the pool

func NewHttpCookie added in v1.1.0

func NewHttpCookie(cookies ...*http.Cookie) HttpCookies

NewHttpCookie returns an instance of HttpCookies from the pool and populates it with the cookies argument

func (HttpCookies) Add

func (c HttpCookies) Add(cookies ...*http.Cookie)

func (HttpCookies) Clear added in v1.1.0

func (c HttpCookies) Clear()

Clear all the data from the map

func (HttpCookies) Release added in v1.1.0

func (c HttpCookies) Release()

Release put this object in the pool for further re-usage, cleaning it beforehand

type HttpHandlerAdaptorResponse

type HttpHandlerAdaptorResponse struct {
	// contains filtered or unexported fields
}

HttpHandlerAdaptorResponse adapts the http.HandlerFunc and http.Handler to a response.HttpResponse

func HandlerAdaptor

func HandlerAdaptor(handler http.Handler) *HttpHandlerAdaptorResponse

HandlerAdaptor adapt a http.Handler to HttpResponse

func HandlerFuncAdaptor

func HandlerFuncAdaptor(handlerFunc http.HandlerFunc) *HttpHandlerAdaptorResponse

HandlerFuncAdaptor adapt a http.HandlerFunc to HttpResponse

func (*HttpHandlerAdaptorResponse) Cookies

func (*HttpHandlerAdaptorResponse) Headers

func (*HttpHandlerAdaptorResponse) StatusCode

func (r *HttpHandlerAdaptorResponse) StatusCode() int

func (*HttpHandlerAdaptorResponse) Write

type HttpHeaders added in v1.1.0

type HttpHeaders map[string]string

HttpHeaders is a map with custom headers

func NewHttpHeaders added in v1.1.0

func NewHttpHeaders() HttpHeaders

NewHttpHeaders returns an instance of HttpHeaders from the pool

func (HttpHeaders) Clear added in v1.1.0

func (h HttpHeaders) Clear()

Clear all the data from the map

func (HttpHeaders) Release added in v1.1.0

func (h HttpHeaders) Release()

Release put this object in the pool for further re-usage, cleaning it beforehand

func (HttpHeaders) Set added in v1.1.0

func (h HttpHeaders) Set(key string, val string)

type HttpHeadersResponse

type HttpHeadersResponse struct {
	HttpStatusCode int
	ContentType    string
	HttpHeaders    HttpHeaders
	HttpCookies    HttpCookies
}

HttpHeadersResponse implements response.HttpResponse and provides HTTP headers write

func InternalServerErrorHttpResponse

func InternalServerErrorHttpResponse() *HttpHeadersResponse

InternalServerErrorHttpResponse writes the http.StatusInternalServerError HTTP status code to the client

func (*HttpHeadersResponse) Cookies

func (r *HttpHeadersResponse) Cookies() HttpCookies

func (*HttpHeadersResponse) Headers

func (r *HttpHeadersResponse) Headers() HttpHeaders

func (*HttpHeadersResponse) StatusCode

func (r *HttpHeadersResponse) StatusCode() int

func (*HttpHeadersResponse) Write

type HttpHijackConnectionResponse added in v1.0.0

type HttpHijackConnectionResponse struct {
	// contains filtered or unexported fields
}

HttpHijackConnectionResponse implements response.HttpResponse and provides hijacking capability for the current TCP connection

func NewHttpHijackConnectionResponse added in v1.0.0

func NewHttpHijackConnectionResponse(hijackCallbackFunc func(net.Conn, *bufio.ReadWriter, error)) *HttpHijackConnectionResponse

NewHttpHijackConnectionResponse creates a new HttpResponse that hijack the current TCP connection, giving the full access of it to the application This response might be useful in websockets implementations, for example

func (*HttpHijackConnectionResponse) Cookies added in v1.0.0

func (*HttpHijackConnectionResponse) Headers added in v1.0.0

func (*HttpHijackConnectionResponse) StatusCode added in v1.0.0

func (r *HttpHijackConnectionResponse) StatusCode() int

func (*HttpHijackConnectionResponse) Write added in v1.0.0

type HttpJsonResponse

type HttpJsonResponse struct {
	HttpHeadersResponse
	Payload any
}

HttpJsonResponse implements response.HttpResponse and provides automatic conversion of an object to JSON

func JsonErrorHttpResponse

func JsonErrorHttpResponse(statusCode int, err error) *HttpJsonResponse

JsonErrorHttpResponse creates an error JSON response

func JsonErrorHttpResponseWithCookies

func JsonErrorHttpResponseWithCookies(statusCode int, err error, cookies HttpCookies) *HttpJsonResponse

JsonErrorHttpResponseWithCookies creates an error JSON response with custom cookies The cookies, if present, once will be written to output will be added in the pool for re-use

func JsonErrorHttpResponseWithHeaders

func JsonErrorHttpResponseWithHeaders(statusCode int, err error, headers HttpHeaders) *HttpJsonResponse

JsonErrorHttpResponseWithHeaders creates an error JSON response with custom headers The headers, if present, once will be written to output will be added in the pool for re-use

func JsonErrorHttpResponseWithHeadersAndCookies

func JsonErrorHttpResponseWithHeadersAndCookies(statusCode int, err error, headers HttpHeaders, cookies HttpCookies) *HttpJsonResponse

JsonErrorHttpResponseWithHeadersAndCookies creates an error JSON response with custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func JsonHttpResponse

func JsonHttpResponse(statusCode int, payload any) *HttpJsonResponse

JsonHttpResponse creates a JSON response with a specific status code

func JsonHttpResponseOK

func JsonHttpResponseOK(payload any) *HttpJsonResponse

JsonHttpResponseOK creates a 200 success JSON response

func JsonHttpResponseWithCookies

func JsonHttpResponseWithCookies(statusCode int, payload any, cookies HttpCookies) *HttpJsonResponse

JsonHttpResponseWithCookies creates a JSON response with a specific status code and cookies

func JsonHttpResponseWithHeaders

func JsonHttpResponseWithHeaders(statusCode int, payload any, headers HttpHeaders) *HttpJsonResponse

JsonHttpResponseWithHeaders creates a JSON response with a specific status code and headers

func JsonHttpResponseWithHeadersAndCookies

func JsonHttpResponseWithHeadersAndCookies(statusCode int, payload any, headers HttpHeaders, cookies HttpCookies) *HttpJsonResponse

JsonHttpResponseWithHeadersAndCookies creates a JSON response with a specific status code, custom headers and cookies

func (*HttpJsonResponse) Write

type HttpRawResponse

type HttpRawResponse struct {
	HttpHeadersResponse
	WriteFunc RawWriterFunc
}

HttpRawResponse implements response.HttpResponse and exposes the http.ResponseWriter for custom write

func RawWriterHttpResponse

func RawWriterHttpResponse(contentType string, writeFunc RawWriterFunc) *HttpRawResponse

RawWriterHttpResponse creates a 200 success reader response with a specific content type

func RawWriterHttpResponseWithCookies

func RawWriterHttpResponseWithCookies(statusCode int, contentType string, cookies HttpCookies, writeFunc RawWriterFunc) *HttpRawResponse

RawWriterHttpResponseWithCookies creates a 200 success reader response with custom cookies The cookies, if present, once will be written to output will be added in the pool for re-use

func RawWriterHttpResponseWithHeaders

func RawWriterHttpResponseWithHeaders(statusCode int, contentType string, headers HttpHeaders, writeFunc RawWriterFunc) *HttpRawResponse

RawWriterHttpResponseWithHeaders creates a 200 success reader response with custom headers The headers, if present, once will be written to output will be added in the pool for re-use

func RawWriterHttpResponseWithHeadersAndCookies

func RawWriterHttpResponseWithHeadersAndCookies(statusCode int, contentType string, headers HttpHeaders, cookies HttpCookies, writeFunc RawWriterFunc) *HttpRawResponse

RawWriterHttpResponseWithHeadersAndCookies creates a 200 success reader response with custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func (*HttpRawResponse) Write

type HttpRedirectResponse

type HttpRedirectResponse struct {
	HttpHeadersResponse
	Url string
}

HttpRedirectResponse implements response.HttpResponse for writing a redirect URL

func RedirectHttpResponse

func RedirectHttpResponse(url string) *HttpRedirectResponse

RedirectHttpResponse creates a redirect response with the status code 302

func RedirectHttpResponseMovedPermanently

func RedirectHttpResponseMovedPermanently(url string) *HttpRedirectResponse

RedirectHttpResponseMovedPermanently creates a redirect response with the status code 301

func RedirectHttpResponseSeeOther

func RedirectHttpResponseSeeOther(url string) *HttpRedirectResponse

RedirectHttpResponseSeeOther creates a redirect response with the status code 303

func (*HttpRedirectResponse) Write

type HttpResponse

type HttpResponse interface {
	// StatusCode returns the response status code
	StatusCode() int
	// Headers returns the response headers
	Headers() HttpHeaders
	// Cookies returns the response cookies
	Cookies() HttpCookies
	// Write the response to the client
	Write(w http.ResponseWriter, mc path.MatchingContext) error
}

HttpResponse describes the methods that a custom response should implement

type HttpSSEResponse

type HttpSSEResponse struct {
	HttpHeadersResponse
	EventGenerator EventGenerator
}

HttpSSEResponse implements response.HttpResponse and provides support for SSE

func SSEHttpResponse

func SSEHttpResponse(ew EventGenerator) *HttpSSEResponse

SSEHttpResponse creates a SSE response

func SSEHttpResponseWithHeaders

func SSEHttpResponseWithHeaders(ew EventGenerator, headers HttpHeaders) *HttpSSEResponse

SSEHttpResponseWithHeaders creates a SSE response with custom headers

func SSEHttpResponseWithHeadersAndCookies

func SSEHttpResponseWithHeadersAndCookies(ew EventGenerator, headers HttpHeaders, cookies HttpCookies) *HttpSSEResponse

SSEHttpResponseWithHeadersAndCookies creates a SSE response with custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func (*HttpSSEResponse) Write

type HttpStreamResponse

type HttpStreamResponse struct {
	HttpHeadersResponse
	Reader io.Reader
}

HttpStreamResponse implements response.HttpResponse and writes a sequence of bytes as a response

func StreamHttpResponse

func StreamHttpResponse(contentType string, reader io.Reader) *HttpStreamResponse

StreamHttpResponse creates a 200 success reader response with a specific content type

func StreamHttpResponseWithCookies

func StreamHttpResponseWithCookies(statusCode int, contentType string, cookies HttpCookies, reader io.Reader) *HttpStreamResponse

StreamHttpResponseWithCookies creates a 200 success reader response with custom cookies The cookies, if present, once will be written to output will be added in the pool for re-use

func StreamHttpResponseWithHeaders

func StreamHttpResponseWithHeaders(statusCode int, contentType string, headers HttpHeaders, reader io.Reader) *HttpStreamResponse

StreamHttpResponseWithHeaders creates a 200 success reader response with custom headers The headers, if present, once will be written to output will be added in the pool for re-use

func StreamHttpResponseWithHeadersAndCookies

func StreamHttpResponseWithHeadersAndCookies(statusCode int, contentType string, headers HttpHeaders, cookies HttpCookies, reader io.Reader) *HttpStreamResponse

StreamHttpResponseWithHeadersAndCookies creates a 200 success reader response with custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func (*HttpStreamResponse) Write

type HttpTemplateResponse

type HttpTemplateResponse struct {
	HttpHeadersResponse
	Template ExecutableTemplate
	Name     string
	Data     any
}

HttpTemplateResponse implements response.HttpResponse and renders a template as a response

func TemplateHttpResponseNotFound

func TemplateHttpResponseNotFound(template ExecutableTemplate, templateName string, templateData any) *HttpTemplateResponse

TemplateHttpResponseNotFound creates a 404 HTML response

func TemplateHttpResponseOK

func TemplateHttpResponseOK(template ExecutableTemplate, templateName string, templateData any) *HttpTemplateResponse

TemplateHttpResponseOK creates a 200 success HTML response

func TemplateHttpResponseWithCookies

func TemplateHttpResponseWithCookies(template ExecutableTemplate, statusCode int, templateName string, templateData any, cookies HttpCookies) *HttpTemplateResponse

TemplateHttpResponseWithCookies creates an HTML response with custom cookies The cookies, if present, once will be written to output will be added in the pool for re-use

func TemplateHttpResponseWithHeaders

func TemplateHttpResponseWithHeaders(template ExecutableTemplate, statusCode int, templateName string, templateData any, headers HttpHeaders) *HttpTemplateResponse

TemplateHttpResponseWithHeaders creates an HTML response with custom headers The headers, if present, once will be written to output will be added in the pool for re-use

func TemplateHttpResponseWithHeadersAndCookies

func TemplateHttpResponseWithHeadersAndCookies(template ExecutableTemplate, statusCode int, templateName string, templateData any, headers HttpHeaders, cookies HttpCookies) *HttpTemplateResponse

TemplateHttpResponseWithHeadersAndCookies creates an HTML response with custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func (*HttpTemplateResponse) Write

type HttpTextResponse

type HttpTextResponse struct {
	HttpHeadersResponse
	Payload string
}

HttpTextResponse implements response.HttpResponse and writes a string as a response

func HtmlHttpResponse

func HtmlHttpResponse(statusCode int, payload string) *HttpTextResponse

HtmlHttpResponse creates an HTML response with a specific status code

func HtmlHttpResponseOK

func HtmlHttpResponseOK(payload string) *HttpTextResponse

HtmlHttpResponseOK creates a 200 success HTML response

func HtmlHttpResponseWithHeaders

func HtmlHttpResponseWithHeaders(statusCode int, payload string, headers HttpHeaders) *HttpTextResponse

HtmlHttpResponseWithHeaders creates an HTML response with a specific status code and headers The headers, if present, once will be written to output will be added in the pool for re-use

func HtmlResponseWithHeadersAndCookies

func HtmlResponseWithHeadersAndCookies(statusCode int, payload string, headers HttpHeaders, cookies HttpCookies) *HttpTextResponse

HtmlResponseWithHeadersAndCookies creates a plain text response with a specific status code, custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func PlainTextHttpResponse

func PlainTextHttpResponse(statusCode int, payload string) *HttpTextResponse

PlainTextHttpResponse creates a plain text response with a specific status code

func PlainTextHttpResponseOK

func PlainTextHttpResponseOK(payload string) *HttpTextResponse

PlainTextHttpResponseOK creates a 200 success plain text response

func PlainTextHttpResponseWithHeaders

func PlainTextHttpResponseWithHeaders(statusCode int, payload string, headers HttpHeaders) *HttpTextResponse

PlainTextHttpResponseWithHeaders creates a plain text response with a specific status code and headers The headers, if present, once will be written to output will be added in the pool for re-use

func PlainTextResponseWithHeadersAndCookies

func PlainTextResponseWithHeadersAndCookies(statusCode int, payload string, headers HttpHeaders, cookies HttpCookies) *HttpTextResponse

PlainTextResponseWithHeadersAndCookies creates a plain text response with a specific status code, custom headers and cookies The headers and cookies, if present, once will be written to output will be added in the pool for re-use

func (*HttpTextResponse) Write

type NilTemplate added in v1.0.0

type NilTemplate struct {
}

NilTemplate implements ExecutableTemplate and can be used when you use static resources without templating

func (NilTemplate) Execute added in v1.0.0

func (t NilTemplate) Execute(wr io.Writer, data any) error

func (NilTemplate) ExecuteTemplate added in v1.0.0

func (t NilTemplate) ExecuteTemplate(wr io.Writer, name string, data any) error

type RawWriterFunc

type RawWriterFunc func(w io.Writer) error

type ServerSentEvent

type ServerSentEvent struct {
	// A string identifying the type of event described
	Name string
	// The event ID to set the EventSource object's last event ID value
	Id string
	// The data field for the message
	Data []string
	// The reconnection time in millis. If the connection to the server is lost, the browser will wait for the specified time before attempting to reconnect.
	Retry int
}

ServerSentEvent defines the server-sent event fields. More about server-sent events can be found here: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

func (ServerSentEvent) String

func (evt ServerSentEvent) String() string

Jump to

Keyboard shortcuts

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