httpio

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2020 License: MIT Imports: 14 Imported by: 0

README

httpcrud/httpio

The package httpio provides utility types and interfaces to complement the httpcrud package.

The main two types, RequestReader and ResponseWriter, both implement parts of the httpcrud.Handler interface and can therefore be utilized as composition blocks in a complete implementation of the interface.

Both, the RequestReader and ResponseWriter, by exporting a number of interface type fields, allow the user to split the implementation into smaller, more specific subtasks to read and write the requests and responses, respectively.

Documentation

Overview

The package httpio provides utility types and interfaces to complement the httpcrud package.

The main two types, RequestReader and ResponseWriter, both implement parts of the httpcrud.Handler interface and can therefore be utilized as composition blocks in a complete implementation of the interface.

Both, the RequestReader and ResponseWriter, by exporting a number of interface type fields, allow the user to split the implementation into smaller, more specific subtasks to read and write the requests and responses, respectively.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterHTMLTemplatesOnce

func RegisterHTMLTemplatesOnce(tmap map[string]*template.Template)

RegisterHTMLTemplatesOnce registers the given templates to be used by the HTML BodyWriter implementation. The RegisterHTMLTemplatesOnce function is intended be called once at program start up.

Types

type BearerToken

type BearerToken struct {
	// Pointer to the string which should be set to the bearer token.
	Val *string
}

BearerToken can be used to read the bearer token value from the Authorization header of the incoming HTTP request and set it to the string pointed to by Val.

func (BearerToken) ReadHeader

func (rr BearerToken) ReadHeader(header http.Header) error

ReadHeader implements the HeaderReader interface.

type BodyReader

type BodyReader interface {
	ReadBody(r *http.Request) error
}

BodyReader interface can be implemented to read the body of an incoming request.

type BodyWriter

type BodyWriter interface {
	WriteInit(w http.ResponseWriter) error
	WriteBody(w http.ResponseWriter, r *http.Request, statusCode int) error
}

BodyWriter interface can be implemented to write the body of an outgoing response.

type Bool

type Bool map[string]*bool

Bool is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the bools pointed to by the map's values.

func (Bool) ReadHeader

func (rr Bool) ReadHeader(header http.Header) error

Bool implements the HeaderReader interface.

func (Bool) ReadPath

func (rr Bool) ReadPath(params route.Params) error

Bool implements the PathReader interface.

func (Bool) ReadQuery

func (rr Bool) ReadQuery(query url.Values) error

Bool implements the QueryReader interface.

type CSV

type CSV struct {
	Stream StreamWriter
}

The CSV type implements the BodyWriter interface by using a StreamWriter.

func (CSV) WriteBody

func (c CSV) WriteBody(_ http.ResponseWriter, _ *http.Request, _ int) error

WriteBody flushes the underlying StreamWriter.

func (CSV) WriteInit

func (c CSV) WriteInit(w http.ResponseWriter) error

WriteInit opens the underlying StreamWriter.

type CSVWriter

type CSVWriter struct {
	// The list of field names.
	// SHOULD be set prior to the first call to WriteRow.
	Header []string
	// The file name to be used for the Content-Disposition header.
	// SHOULD be set prior to the first call to WriteRow.
	FileName string
	// The HTTP status code to be sent with the response.
	// SHOULD be set prior to the first call to WriteRow.
	//
	// Set by Open to 200 (Status OK) as default. If the embedding code needs
	// to send a different value it should implement its own Open method, invoke
	// the embedded Open method to set up the writer and *then* set the StatusCode
	// field to the desired value.
	StatusCode int
	// contains filtered or unexported fields
}

CSVWriter implements the StreamWriter interface and is intended to be embedded by user-defined structs that want to implement custom csv writers.

func (*CSVWriter) Flush

func (w *CSVWriter) Flush() error

Flush flushes the underlying csv.Writer and returns and error if it fails. Flush will be invoked by (CSV).WriteBody indirectly through the StreamWriter interface, if, however, the embedding code overrides this method by providing its own impelmentation then that implementation MUST invoke this method directly.

func (*CSVWriter) Open

func (w *CSVWriter) Open(rw http.ResponseWriter)

Open prepares the CSVWriter instance using the given http.ResponseWriter as the target into which the csv data will be written. Open will be invoked by (CSV).WriteInit indirectly through the StreamWriter interface, if, however, the embedding code overrides this method by providing its own impelmentation then that implementation MUST invoke this method directly.

func (*CSVWriter) WriteRow

func (w *CSVWriter) WriteRow(row []string) error

WriteRow writes the given row to the underlying http.ResponseWriter. This method SHOULD be invoked directly by the user code.

type CookieValues

type CookieValues map[string]*string

CookieValues can be used to read the values from the Cookie header of the incoming HTTP request and indirectly set them to the strings pointed to by the map's values, using the map's keys to match against the Cookies' names.

func (CookieValues) ReadHeader

func (rr CookieValues) ReadHeader(header http.Header) error

ReadHeader implements the HeaderReader interface.

type Float32

type Float32 map[string]*float32

Float32 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the float32s pointed to by the map's values.

func (Float32) ReadHeader

func (rr Float32) ReadHeader(header http.Header) error

Float32 implements the HeaderReader interface.

func (Float32) ReadPath

func (rr Float32) ReadPath(params route.Params) error

Float32 implements the PathReader interface.

func (Float32) ReadQuery

func (rr Float32) ReadQuery(query url.Values) error

Float32 implements the QueryReader interface.

type Float64

type Float64 map[string]*float64

Float64 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the float64s pointed to by the map's values.

func (Float64) ReadHeader

func (rr Float64) ReadHeader(header http.Header) error

Float64 implements the HeaderReader interface.

func (Float64) ReadPath

func (rr Float64) ReadPath(params route.Params) error

Float64 implements the PathReader interface.

func (Float64) ReadQuery

func (rr Float64) ReadQuery(query url.Values) error

Float64 implements the QueryReader interface.

type Form

type Form struct {
	// The value to be url encoded and sent in an HTTP response body or
	// a pointer to the value to be url decoded from an HTTP request's body.
	Val interface{}
}

The Form type implements both the BodyWriter and the BodyReader interfaces.

func (Form) ReadBody

func (f Form) ReadBody(r *http.Request) error

ReadBody implements the BodyReader interface by decoding the request's form body into the reciever's Val field.

func (Form) WriteBody

func (f Form) WriteBody(w http.ResponseWriter, r *http.Request, statusCode int) error

WriteBody implements the BodyWriter interface by form encoding the receiver's Val field and sending the result in the response's body.

func (Form) WriteInit

func (Form) WriteInit(_ http.ResponseWriter) error

WriteInit is a noop, required only to satisfy the BodyWriter interface.

type HTML

type HTML struct {
	// The name (map key) of the template as registered with RegisterTemplates.
	Name string
	// Data to be passed to the template's Execute method.
	Data interface{}
}

The HTML type implements the BodyWriter interface.

func (HTML) WriteBody

func (h HTML) WriteBody(w http.ResponseWriter, r *http.Request, code int) error

WriteBody implements the BodyWriter interface by executing an html template with the specified Name, passing it the provided Data, and then sending the result as the response's body.

func (HTML) WriteInit

func (HTML) WriteInit(_ http.ResponseWriter) error

WriteInit is a noop, required only to satisfy the BodyWriter interface.

type HeaderReader

type HeaderReader interface {
	ReadHeader(header http.Header) error
}

HeaderReader interface can be implemented to read an incoming request's header.

type HeaderReaderList

type HeaderReaderList []HeaderReader

HeaderReaderList can be used to read the HTTP headers of different types.

func (HeaderReaderList) ReadHeader

func (list HeaderReaderList) ReadHeader(header http.Header) error

ReadHeader implements the HeaderReader interface.

type HeaderWriter

type HeaderWriter interface {
	WriteHeader(header http.Header)
}

HeaderWriter interface can be implemented to write an outgoing response's header.

type IPAddress

type IPAddress struct {
	// Pointer to the string which should be set to the ip address.
	Val *string
}

IPAddress can be used to read the IP address value from the header of the incoming HTTP request and set it to the string pointed to by Val.

func (IPAddress) ReadHeader

func (rr IPAddress) ReadHeader(header http.Header) error

ReadHeader implements the HeaderReader interface.

type IPUA added in v0.0.3

type IPUA struct {
	// Pointer to the strings which should be set to the ip & user agent.
	IP, UA *string
}

IPUA can be used to read the IP address & User-Agent values from the header of the incoming HTTP request and set them to the strings pointed to by IP & UA.

func (IPUA) ReadHeader added in v0.0.3

func (a IPUA) ReadHeader(header http.Header) error

ReadHeader implements the HeaderReader interface.

type Int

type Int map[string]*int

Int is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the ints pointed to by the map's values.

func (Int) ReadHeader

func (rr Int) ReadHeader(header http.Header) error

Int implements the HeaderReader interface.

func (Int) ReadPath

func (rr Int) ReadPath(params route.Params) error

Int implements the PathReader interface.

func (Int) ReadQuery

func (rr Int) ReadQuery(query url.Values) error

Int implements the QueryReader interface.

type Int16

type Int16 map[string]*int16

Int16 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int16s pointed to by the map's values.

func (Int16) ReadHeader

func (rr Int16) ReadHeader(header http.Header) error

Int16 implements the HeaderReader interface.

func (Int16) ReadPath

func (rr Int16) ReadPath(params route.Params) error

Int16 implements the PathReader interface.

func (Int16) ReadQuery

func (rr Int16) ReadQuery(query url.Values) error

Int16 implements the QueryReader interface.

type Int32

type Int32 map[string]*int32

Int32 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int32s pointed to by the map's values.

func (Int32) ReadHeader

func (rr Int32) ReadHeader(header http.Header) error

Int32 implements the HeaderReader interface.

func (Int32) ReadPath

func (rr Int32) ReadPath(params route.Params) error

Int32 implements the PathReader interface.

func (Int32) ReadQuery

func (rr Int32) ReadQuery(query url.Values) error

Int32 implements the QueryReader interface.

type Int64

type Int64 map[string]*int64

Int64 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int64s pointed to by the map's values.

func (Int64) ReadHeader

func (rr Int64) ReadHeader(header http.Header) error

Int64 implements the HeaderReader interface.

func (Int64) ReadPath

func (rr Int64) ReadPath(params route.Params) error

Int64 implements the PathReader interface.

func (Int64) ReadQuery

func (rr Int64) ReadQuery(query url.Values) error

Int64 implements the QueryReader interface.

type Int8

type Int8 map[string]*int8

Int8 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the int8s pointed to by the map's values.

func (Int8) ReadHeader

func (rr Int8) ReadHeader(header http.Header) error

Int8 implements the HeaderReader interface.

func (Int8) ReadPath

func (rr Int8) ReadPath(params route.Params) error

Int8 implements the PathReader interface.

func (Int8) ReadQuery

func (rr Int8) ReadQuery(query url.Values) error

Int8 implements the QueryReader interface.

type JSON

type JSON struct {
	// The value to be json encoded and sent in an HTTP response body or
	// a pointer to the value to be json decoded from an HTTP request's body.
	Val interface{}
}

The JSON type implements both the BodyWriter and the BodyReader interfaces.

func (JSON) ReadBody

func (j JSON) ReadBody(r *http.Request) error

ReadBody implements the BodyReader interface by decoding the request's json body into the reciever's Val field.

func (JSON) WriteBody

func (j JSON) WriteBody(w http.ResponseWriter, r *http.Request, statusCode int) error

WriteBody implements the BodyWriter interface by json encoding the receiver's Val field and sending the result in the response's body.

func (JSON) WriteInit

func (JSON) WriteInit(_ http.ResponseWriter) error

WriteInit is a noop, required only to satisfy the BodyWriter interface.

type NoTemplateError

type NoTemplateError struct {
	// The provided template name.
	Name string
}

NoTemplateError is returned when no template with the given name was registered.

func (NoTemplateError) Error

func (e NoTemplateError) Error() string

type PathReader

type PathReader interface {
	ReadPath(params route.Params) error
}

PathReader interface can be implemented to read the route.Params from an incoming request's url path.

type PathReaderList

type PathReaderList []PathReader

PathReaderList can be used to read the path parameters of different types.

func (PathReaderList) ReadPath

func (list PathReaderList) ReadPath(params route.Params) error

ReadPath implements the PathReader interface.

type QueryReader

type QueryReader interface {
	ReadQuery(query url.Values) error
}

QueryReader interface can be implemented to read the query parameters from an incoming request's url.

type QueryReaderList

type QueryReaderList []QueryReader

QueryReaderList can be used to read the url query parameters of different types.

func (QueryReaderList) ReadQuery

func (list QueryReaderList) ReadQuery(query url.Values) error

ReadQuery implements the QueryReader interface.

type ReadError

type ReadError struct {
	// The original error.
	Err error
}

ReadError represents an error returned by a BodyReader.

func (ReadError) Error

func (e ReadError) Error() string

type Redirect

type Redirect struct {
	// The URL to which to redirect.
	URL string
	// The HTTP redirect status code.
	StatusCode int
}

The Redirect type implements the BodyWriter interface.

func (Redirect) WriteBody

func (re Redirect) WriteBody(w http.ResponseWriter, r *http.Request, _ int) error

WriteBody implements the BodyWriter interface by issuing a redirect to the specified URL with the specified StatusCode.

func (Redirect) WriteInit

func (re Redirect) WriteInit(_ http.ResponseWriter) error

WriteInit is a noop, required only to satisfy the BodyWriter interface.

type RequestDump

type RequestDump struct {
	// The pointer to which to set the request dump.
	Val *[]byte
	// Indicates whether to dump the request's body as well.
	Body bool
}

The RequestDump type implements the BodyReader interface.

func (RequestDump) ReadBody

func (d RequestDump) ReadBody(r *http.Request) error

ReadBody implements the BodyReader interface by dumping the request's payload and setting the reciever's Val pointer to the result.

type RequestReader

type RequestReader struct {
	// If set, will read the header from the incoming request.
	Header HeaderReader
	// If set, will read the query parameters from the incoming request's url.
	Query QueryReader
	// If set, will read the route.Params from the path of incoming request's url.
	Path PathReader
	// If set, will read the body from the incoming request.
	Body BodyReader
	// contains filtered or unexported fields
}

RequestReader is intended to be embedded as part of a Handler implementation to read the various data from the incoming HTTP request.

func (*RequestReader) GetContext

func (rr *RequestReader) GetContext() context.Context

GetContext is a convenience method that returns the underlying http request's context value.

func (*RequestReader) GetRequest

func (rr *RequestReader) GetRequest() *http.Request

GetRequest is a convenience method that returns the underlying http request.

func (*RequestReader) ReadRequest

func (rr *RequestReader) ReadRequest(r *http.Request, c context.Context) error

RequestReader implements the ReadRequest method of the Handler interface.

type ResponseWriter

type ResponseWriter struct {
	// If set, will write the header of the outgoing response.
	Header HeaderWriter
	// If set, will write the body of the outgoing response.
	Body BodyWriter
	// If set, will be used as the HTTP status code of the outgoing response.
	Status int
}

ResponseWriter is intended to be embedded as part of a Handler implementation to write the various data of the outgoing HTTP response.

func (*ResponseWriter) InitResponse

func (rw *ResponseWriter) InitResponse(w http.ResponseWriter) error

ResponseWriter implements the InitResponse method of the Handler interface.

func (*ResponseWriter) WriteResponse

func (rw *ResponseWriter) WriteResponse(w http.ResponseWriter, r *http.Request) error

ResponseWriter implements the WriteResponse method of the Handler interface.

type SetCookie

type SetCookie struct {
	// The cookie to be set.
	Val *http.Cookie
}

SetCookie can be used to set the Set-Cookie header of the outgoing response.

func (SetCookie) WriteHeader

func (rr SetCookie) WriteHeader(header http.Header)

WriteHeader implements the HeaderWriter interface.

type StreamWriter

type StreamWriter interface {
	// Open, intended to be invoked by (BodyWriter).WriteInit, should prepare
	// the stream for writing using the given http.ResponseWriter as the medium
	// for transferring the data.
	Open(w http.ResponseWriter)
	// Flush, intended to be invoked by (BodyWriter).WriteBody, should write
	// any buffered data to the underlying http.ResponseWriter and return an
	// error if the flush fails.
	Flush() error
}

StreamWriter interface can be used by BodyWriters

type String

type String map[string]*string

String is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the strings pointed to by the map's values.

func (String) ReadHeader

func (rr String) ReadHeader(header http.Header) error

String implements the HeaderReader interface.

func (String) ReadPath

func (rr String) ReadPath(params route.Params) error

String implements the PathReader interface.

func (String) ReadQuery

func (rr String) ReadQuery(query url.Values) error

String implements the QueryReader interface.

type Text

type Text struct {
	// The value to be sent in an HTTP response body.
	Val string
}

The Text type implements the BodyWriter interface.

func (Text) WriteBody

func (t Text) WriteBody(w http.ResponseWriter, r *http.Request, code int) error

WriteBody implements the BodyWriter interface by writing the Val field's contents to the response's body.

func (Text) WriteInit

func (Text) WriteInit(_ http.ResponseWriter) error

WriteInit is a noop, required only to satisfy the BodyWriter interface.

type Uint

type Uint map[string]*uint

Uint is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uints pointed to by the map's values.

func (Uint) ReadHeader

func (rr Uint) ReadHeader(header http.Header) error

Uint implements the HeaderReader interface.

func (Uint) ReadPath

func (rr Uint) ReadPath(params route.Params) error

Uint implements the PathReader interface.

func (Uint) ReadQuery

func (rr Uint) ReadQuery(query url.Values) error

Uint implements the QueryReader interface.

type Uint16

type Uint16 map[string]*uint16

Uint16 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint16s pointed to by the map's values.

func (Uint16) ReadHeader

func (rr Uint16) ReadHeader(header http.Header) error

Uint16 implements the HeaderReader interface.

func (Uint16) ReadPath

func (rr Uint16) ReadPath(params route.Params) error

Uint16 implements the PathReader interface.

func (Uint16) ReadQuery

func (rr Uint16) ReadQuery(query url.Values) error

Uint16 implements the QueryReader interface.

type Uint32

type Uint32 map[string]*uint32

Uint32 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint32s pointed to by the map's values.

func (Uint32) ReadHeader

func (rr Uint32) ReadHeader(header http.Header) error

Uint32 implements the HeaderReader interface.

func (Uint32) ReadPath

func (rr Uint32) ReadPath(params route.Params) error

Uint32 implements the PathReader interface.

func (Uint32) ReadQuery

func (rr Uint32) ReadQuery(query url.Values) error

Uint32 implements the QueryReader interface.

type Uint64

type Uint64 map[string]*uint64

Uint64 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint64s pointed to by the map's values.

func (Uint64) ReadHeader

func (rr Uint64) ReadHeader(header http.Header) error

Uint64 implements the HeaderReader interface.

func (Uint64) ReadPath

func (rr Uint64) ReadPath(params route.Params) error

Uint64 implements the PathReader interface.

func (Uint64) ReadQuery

func (rr Uint64) ReadQuery(query url.Values) error

Uint64 implements the QueryReader interface.

type Uint8

type Uint8 map[string]*uint8

Uint8 is a map that can be used to read the values, using the map's keys, from an incoming request's header, path or query parameters and indirectly set them to the uint8s pointed to by the map's values.

func (Uint8) ReadHeader

func (rr Uint8) ReadHeader(header http.Header) error

Uint8 implements the HeaderReader interface.

func (Uint8) ReadPath

func (rr Uint8) ReadPath(params route.Params) error

Uint8 implements the PathReader interface.

func (Uint8) ReadQuery

func (rr Uint8) ReadQuery(query url.Values) error

Uint8 implements the QueryReader interface.

type UserAgent

type UserAgent struct {
	// Pointer to the string which should be set to the user agent.
	Val *string
}

UserAgent can be used to read the value from the User-Agent header of the incoming HTTP request and set it to the string pointed to by Val.

func (UserAgent) ReadHeader

func (rr UserAgent) ReadHeader(header http.Header) error

ReadHeader implements the HeaderReader interface.

type WriteError

type WriteError struct {
	// The original error.
	Err error
}

WriteError represents an error returned by a BodyWriter.

func (WriteError) Error

func (e WriteError) Error() string

type XML

type XML struct {
	// The value to be xml encoded and sent in an HTTP response body or
	// a pointer to the value to be xml decoded from an HTTP request's body.
	Val interface{}
}

The XML type implements both the BodyWriter and the BodyReader interfaces.

func (XML) ReadBody

func (x XML) ReadBody(r *http.Request) error

ReadBody implements the BodyReader interface by decoding the request's xml body into the reciever's Val field.

func (XML) WriteBody

func (x XML) WriteBody(w http.ResponseWriter, r *http.Request, statusCode int) error

WriteBody implements the BodyWriter interface by xml encoding the receiver's Val field and sending the result in the response's body.

func (XML) WriteInit

func (XML) WriteInit(_ http.ResponseWriter) error

WriteInit is a noop, required only to satisfy the BodyWriter interface.

Jump to

Keyboard shortcuts

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