Documentation ¶
Overview ¶
Package httpextra implements routines and types that allow more control over incoming requests and outgoing responses.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ContentType ¶
type ContentType struct { Mime string Extension string Error string Marshal func(interface{}) ([]byte, error) Default bool }
ContentType represents a single content type that includes an extension, a error message, and if it's the default.
func DefaultContentType ¶
func DefaultContentType(contentTypes map[string]*ContentType) *ContentType
DefaultContentType returns the content type set to default, or if none are set the first one is returned.
func RequestContentType ¶
func RequestContentType(contentTypes map[string]*ContentType, req *http.Request) *ContentType
RequestContentType gets an acceptable response format from a request.
type ContentTypeHandler ¶
type ContentTypeHandler struct { ContentTypes map[string]*ContentType Handler http.Handler }
ContentTypeHandler is a http.Handler that ensures unsupported formats have the correct response.
func NewContentTypeHandler ¶
func NewContentTypeHandler(contentTypes map[string]*ContentType, handler http.Handler) *ContentTypeHandler
func (*ContentTypeHandler) ServeHTTP ¶
func (cth *ContentTypeHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP calls the handler if a requested response content type is supported, otherwise a 406 is returned.
type LogHandler ¶
LogHandler is a http.Handler that logs requests in Common Log Format.
func NewLogHandler ¶
func NewLogHandler(writer io.Writer, handler http.Handler) *LogHandler
func (*LogHandler) ServeHTTP ¶
func (lh *LogHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP logs the request and calls the handler.
type NotFoundHandler ¶
type NotFoundHandler struct {
ContentTypes map[string]*ContentType
}
NotFoundHandler is a http.Handler responding to 404s.
func NewNotFoundHandler ¶
func NewNotFoundHandler(contentTypes map[string]*ContentType) *NotFoundHandler
func (*NotFoundHandler) ServeHTTP ¶
func (nfh *NotFoundHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP responds with 404
type Response ¶
type Response struct { ContentTypes map[string]*ContentType RW http.ResponseWriter Req *http.Request }
Response represents a structure that marshals content to send to the client.
func (*Response) Send ¶
Send parses the response and if parsed successfully responds with the given status code, otherwise a 500 is sent with the formats default error message.
func (*Response) SendDefault ¶
SendDefault does the same as Send, except uses the default content type for the response format.
type ResponseLogger ¶
type ResponseLogger struct { ResponseWriter http.ResponseWriter Status int Length int }
ResponseLogger is a http.ResponseWriter, it keeps the state of the responses status code and content length.
func (*ResponseLogger) Header ¶
func (rl *ResponseLogger) Header() http.Header
Header returns the responses headers.
func (*ResponseLogger) Write ¶
func (rl *ResponseLogger) Write(b []byte) (int, error)
Write writes the response and keeps track of the content length.
func (*ResponseLogger) WriteHeader ¶
func (rl *ResponseLogger) WriteHeader(status int)
WriteHeader writes the header, keeping track of the status code.
type SlashHandler ¶
SlashHandler is a http.Handler that removes trailing slashes
func NewSlashHandler ¶
func NewSlashHandler(handler http.Handler) *SlashHandler
func (*SlashHandler) ServeHTTP ¶
func (sh *SlashHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP stripes a trailing slash and calls the handler