response

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2021 License: BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package response provides encapsulated HTTP responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

func NewBuilder

func NewBuilder() *Builder

func (*Builder) Body

func (builder *Builder) Body() body.Body

Body returns the associated HTTP response body.

The caller must not read from or modify the returned Body. Call Body.Copy() and operate on the copy when performing such actions.

func (*Builder) Build

func (builder *Builder) Build() *Response

Build returns a new Response with the properties specified on this Builder.

A Body MUST already have been specified. The Response takes ownership of the Body.

If the Content-Type header has not been specified, then the Content-Type header is automatically populated as "application/octet-stream".

If the Content-Length header has not been specified AND the Body has a non-negative BytesRemaining(), then the Content-Length header is automatically populated from BytesRemaining().

After calling this method, the Builder is reset to an empty state and is ready to build another Response. Only the PageGenerator is retained.

func (*Builder) Copy

func (builder *Builder) Copy() (*Builder, error)

Copy creates a copy of this Builder, duplicating the Body as needed.

func (*Builder) Err

func (builder *Builder) Err() error

Err returns the associated Go error, or nil if not set.

func (*Builder) ErrorPage

func (builder *Builder) ErrorPage(code int, err error) *Builder

ErrorPage populates this Builder with a generated error response.

func (*Builder) Headers

func (builder *Builder) Headers() http.Header

Headers returns the associated HTTP response headers.

The caller is allowed to mutate the returned map and its contents.

func (*Builder) PageGenerator

func (builder *Builder) PageGenerator() PageGenerator

PageGenerator returns the associated PageGenerator instance, or DefaultPageGenerator if not set.

func (*Builder) RedirectPage

func (builder *Builder) RedirectPage(code int, location string) *Builder

RedirectPage populates this Builder with a generated redirect response.

func (*Builder) Status

func (builder *Builder) Status() int

Status returns the associated HTTP status code, or 0 if not set.

func (*Builder) WithBody

func (builder *Builder) WithBody(b body.Body) *Builder

WithBody associates the given Body with this Builder, taking ownership of it.

The given value MUST NOT be nil.

func (*Builder) WithContentEncoding

func (builder *Builder) WithContentEncoding(str string) *Builder

WithContentEncoding sets the Content-Encoding header.

func (*Builder) WithContentLanguage

func (builder *Builder) WithContentLanguage(str string) *Builder

WithContentLanguage sets the Content-Language header.

func (*Builder) WithContentType

func (builder *Builder) WithContentType(str string) *Builder

WithContentType sets the Content-Type header.

func (*Builder) WithDigest

func (builder *Builder) WithDigest(algo string, sum []byte) *Builder

WithDigest adds the given Digest header.

func (*Builder) WithETag

func (builder *Builder) WithETag(tag string, isStrong bool) *Builder

WithETag sets the ETag header.

func (*Builder) WithError

func (builder *Builder) WithError(err error) *Builder

WithError associates the given Go error with this Builder.

The given value MAY be nil.

func (*Builder) WithHeader

func (builder *Builder) WithHeader(name string, value string, appendToExisting bool) *Builder

WithHeader adds the given header to the HTTP response.

func (*Builder) WithHeaders

func (builder *Builder) WithHeaders(hdrs http.Header) *Builder

WithHeaders associates the given HTTP response headers with this Builder.

This method makes a deep copy of the given map and its contents, so that no references to it are retained.

func (*Builder) WithJSON

func (builder *Builder) WithJSON(v interface{}) *Builder

WithJSON converts the given value to a JSON Body, then associates it with this Builder.

This method also adds the header "Content-Type: application/json".

func (*Builder) WithPageGenerator

func (builder *Builder) WithPageGenerator(gen PageGenerator) *Builder

WithPageGenerator associates the given PageGenerator instance with this Builder. This affects future calls to RedirectPage or ErrorPage.

The given value MUST NOT be nil.

func (*Builder) WithPrettyJSON

func (builder *Builder) WithPrettyJSON(v interface{}) *Builder

WithPrettyJSON converts the given value to a human-formatted JSON Body, then associates it with this Builder.

This method also adds the header "Content-Type: application/json".

func (*Builder) WithProto

func (builder *Builder) WithProto(msg proto.Message) *Builder

WithProto converts the given value to a binary protobuf Body, then associates it with this Builder.

This method also adds the header "Content-Type: application/vnd.google.protobuf".

func (*Builder) WithProtoText

func (builder *Builder) WithProtoText(msg proto.Message, o *prototext.MarshalOptions) *Builder

WithProtoText converts the given value to a text protobuf Body, then associates it with this Builder.

This method also adds the header "Content-Type: text/plain; charset=utf-8".

func (*Builder) WithStatus

func (builder *Builder) WithStatus(code int) *Builder

WithStatus associates the given HTTP status code with this Builder.

The given value MUST lie between 200 and 999 inclusive.

func (*Builder) WithoutHeader

func (builder *Builder) WithoutHeader(name string) *Builder

WithoutHeader removes the given header from the HTTP response.

type PageGenerator

type PageGenerator interface {
	GenerateRedirectPage(code int, location string) (http.Header, body.Body)
	GenerateErrorPage(code int, err error) (http.Header, body.Body)
}
var DefaultPageGenerator PageGenerator = &defaultPageGenerator{}

type Response

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

Response represents an HTTP response.

func (*Response) Body

func (resp *Response) Body() body.Body

Body returns this Response's Body.

The caller must not read from or modify the returned Body. Call Body.Copy() and operate on the copy when performing such actions.

func (*Response) Copy

func (resp *Response) Copy() (*Response, error)

Copy returns a copy of this Response.

func (*Response) Err

func (resp *Response) Err() error

Err returns the Go error which provoked this Response, if any.

func (*Response) Headers

func (resp *Response) Headers() http.Header

Headers returns the HTTP headers of the response.

The caller MUST NOT modify the returned map or its contents.

func (*Response) Serve

func (resp *Response) Serve(w http.ResponseWriter) error

Serve serves the Response via the given ResponseWriter, consuming its Body.

func (*Response) Status

func (resp *Response) Status() int

Status returns the HTTP status code of the response.

The returned value lies between 200 and 999 inclusive.

func (*Response) String

func (resp *Response) String() string

String returns a programmer-friendly string description.

type Writer

type Writer interface {
	http.ResponseWriter
	http.Pusher
	MaybeWriteHeader(int)
	Status() int
	BytesWritten() int64
	SawError() bool
	Unwrap() http.ResponseWriter
}

func NewWriter

func NewWriter(w http.ResponseWriter, r *http.Request) Writer

Jump to

Keyboard shortcuts

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