httputil

package
v0.0.0-...-d9fd4a4 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2015 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package httputil contains a bunch of HTTP utility code

Index

Constants

View Source
const (
	PathBaseHeader   = "X-Prefixhandler-Pathbase"
	PathSuffixHeader = "X-Prefixhandler-Pathsuffix"
)

Variables

This section is empty.

Functions

func BadRequestError

func BadRequestError(conn http.ResponseWriter, errorMessage string, args ...interface{})

func CloseBody

func CloseBody(rc io.ReadCloser)

CloseBody should be used to close an http.Response.Body.

It does a final little Read to maybe see EOF (to trigger connection re-use) before calling Close.

func ConvertTime

func ConvertTime(value string) reflect.Value

func DecodeJSON

func DecodeJSON(res *http.Response, dest interface{}) error

DecodeJSON decodes the JSON in res.Body into dest and then closes res.Body. It defensively caps the JSON at 8 MB for now.

func ErrorRouting

func ErrorRouting(conn http.ResponseWriter, req *http.Request)

func ForbiddenError

func ForbiddenError(conn http.ResponseWriter, errorMessage string, args ...interface{})

func FormValue

func FormValue(r *http.Request, key string) (string, error)

func FormValues

func FormValues(r *http.Request, key string) ([]string, error)

func IsGet

func IsGet(r *http.Request) bool

IsGet reports whether r.Method is a GET or HEAD request.

func MustGet

func MustGet(req *http.Request, param string) string

MustGet returns a non-empty GET (or HEAD) parameter param and panics with a special error as caught by a deferred httputil.Recover.

func OffsetCount

func OffsetCount(r *http.Request) (int, int)

func ParseBool

func ParseBool(r *http.Request, key string) (bool, error)

func ParseInt

func ParseInt(r *http.Request, key string) (int, error)

func ParseIntArray

func ParseIntArray(r *http.Request, key string) ([]int, error)

func ParseString

func ParseString(r *http.Request, key string) (string, error)

func ParseUint

func ParseUint(r *http.Request, key string) (uint, error)

func PathBase

func PathBase(req *http.Request) string

PathBase returns a Request's base path, if it went via a PrefixHandler.

func PathSuffix

func PathSuffix(req *http.Request) string

PathSuffix returns a Request's suffix path, if it went via a PrefixHandler.

func Recover

func Recover(rw http.ResponseWriter, req *http.Request)

Recover is meant to be used at the top of handlers with "defer" to catch errors from MustGet, etc:

func handler(rw http.ResponseWriter, req *http.Request) {
    defer httputil.Recover(rw, req)
    id := req.MustGet("id")
    ....

Recover will send the proper HTTP error type and message (e.g. a 400 Bad Request for MustGet)

func RecoverJSON

func RecoverJSON(rw http.ResponseWriter, req *http.Request)

RecoverJSON is like Recover but returns with a JSON response.

func RequestEntityTooLargeError

func RequestEntityTooLargeError(conn http.ResponseWriter)

func ReturnJSON

func ReturnJSON(rw http.ResponseWriter, data interface{})

func ReturnJSONCode

func ReturnJSONCode(rw http.ResponseWriter, code int, data interface{})

func ServeError

func ServeError(conn http.ResponseWriter, req *http.Request, err error)

func ServeJSONCodeError

func ServeJSONCodeError(rw http.ResponseWriter, err interface{}, code int)

ServeJSONCodeError sends a JSON error response to rw for the provided error value.

func ServeJSONError

func ServeJSONError(rw http.ResponseWriter, err interface{})

ServeJSONError sends a JSON error response to rw for the provided error value.

func SetBool

func SetBool(r *http.Request, m map[string]interface{}, key string) error

func SetInt

func SetInt(r *http.Request, m map[string]interface{}, key string) error

func SetIntArray

func SetIntArray(r *http.Request, m map[string]interface{}, key string) error

func SetString

func SetString(r *http.Request, m map[string]interface{}, key string) error

func SetUint

func SetUint(r *http.Request, m map[string]interface{}, key string) error

Types

type DataErrResponse

type DataErrResponse struct {
	Error map[string]string
}

type InvalidMethodError

type InvalidMethodError struct{}

An InvalidMethodError is returned when an HTTP handler is invoked with an unsupported method.

func (InvalidMethodError) Error

func (InvalidMethodError) Error() string

func (InvalidMethodError) HTTPCode

func (InvalidMethodError) HTTPCode() int

type InvalidParameterError

type InvalidParameterError string

An InvalidParameterError represents an invalid HTTP parameter. The underlying string is the invalid parameter name, not value.

func (InvalidParameterError) Error

func (p InvalidParameterError) Error() string

func (InvalidParameterError) HTTPCode

func (InvalidParameterError) HTTPCode() int

type MissingParameterError

type MissingParameterError string

A MissingParameterError represents a missing HTTP parameter. The underlying string is the missing parameter name.

func (MissingParameterError) Error

func (p MissingParameterError) Error() string

func (MissingParameterError) HTTPCode

func (MissingParameterError) HTTPCode() int

type PrefixHandler

type PrefixHandler struct {
	Prefix  string
	Handler http.Handler
}

PrefixHandler wraps another Handler and verifies that all requests' Path begin with Prefix. If they don't, a 500 error is returned. If they do, the headers PathBaseHeader and PathSuffixHeader are set on the request before proxying to Handler. PathBaseHeader is just the value of Prefix. PathSuffixHeader is the part of the path that follows Prefix.

func (*PrefixHandler) ServeHTTP

func (p *PrefixHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type Request

type Request http.Request

func NewRequest

func NewRequest(method string, uri string, body interface{}, params url.Values) (*Request, error)

NewRequest wraps http.Request

func (*Request) Do

func (req *Request) Do() (*Response, error)

type Response

type Response http.Response

func (*Response) ToErr

func (res *Response) ToErr() (*DataErrResponse, error)

func (*Response) ToJSON

func (res *Response) ToJSON(i interface{}) error

type ServerError

type ServerError string

A ServerError is a generic 500 error.

func (ServerError) Error

func (e ServerError) Error() string

func (ServerError) HTTPCode

func (ServerError) HTTPCode() int

Jump to

Keyboard shortcuts

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