httpextra

package module
v0.0.0-...-1981397 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2013 License: MIT Imports: 7 Imported by: 1

README

HTTPExtra

GoDoc

HTTPExtra provides handlers and other structures extending the capabilities of the standard HTTP package.

Install

go get github.com/larzconwell/httpextra

License

MIT licensed, see here

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

func ParseForm

func ParseForm(contentTypes map[string]*ContentType, rw http.ResponseWriter, req *http.Request) (url.Values, bool)

ParseForm parses the request form, handling errors.

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

type LogHandler struct {
	Writer  io.Writer
	Handler http.Handler
}

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

func (res *Response) Send(data interface{}, status int)

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

func (res *Response) SendDefault(data interface{}, status int)

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

type SlashHandler struct {
	Handler http.Handler
}

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

Jump to

Keyboard shortcuts

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