Documentation ¶
Overview ¶
Package httputil contains a bunch of HTTP utility code, some generic, and some Camlistore-specific.
Index ¶
- Constants
- func BadRequestError(conn http.ResponseWriter, errorMessage string, args ...interface{})
- func BaseURL(urlStr string, req *http.Request) (string, error)
- func BasicAuth(req *http.Request) (username, password string, err error)
- func CloseBody(rc io.ReadCloser)
- func DecodeJSON(res *http.Response, dest interface{}) error
- func ErrorRouting(conn http.ResponseWriter, req *http.Request)
- func ForbiddenError(conn http.ResponseWriter, errorMessage string, args ...interface{})
- func IsGet(r *http.Request) bool
- func IsLocalhost(req *http.Request) bool
- func IsWebsocketUpgrade(req *http.Request) bool
- func MustGet(req *http.Request, param string) string
- func MustGetBlobRef(req *http.Request, param string) blob.Ref
- func OptionalInt(req *http.Request, param string) int
- func PathBase(req *http.Request) string
- func PathSuffix(req *http.Request) string
- func Recover(rw http.ResponseWriter, req *http.Request)
- func RecoverJSON(rw http.ResponseWriter, req *http.Request)
- func RequestEntityTooLargeError(conn http.ResponseWriter)
- func RequestTargetPort(req *http.Request) int
- func ReturnJSON(rw http.ResponseWriter, data interface{})
- func ReturnJSONCode(rw http.ResponseWriter, code int, data interface{})
- func ServeError(conn http.ResponseWriter, req *http.Request, err error)
- func ServeJSONError(rw http.ResponseWriter, err interface{})
- type InvalidMethodError
- type InvalidParameterError
- type MissingParameterError
- type PrefixHandler
- type ServerError
- type StatsTransport
Constants ¶
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 BaseURL ¶
BaseURL returns the base URL (scheme + host and optional port + blobserver prefix) that should be used for requests (and responses) subsequent to req. The returned URL does not end in a trailing slash. The scheme and host:port are taken from urlStr if present, or derived from req otherwise. The prefix part comes from urlStr.
func BasicAuth ¶
BasicAuth parses the Authorization header on req If absent or invalid, an error is returned.
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 DecodeJSON ¶
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 IsLocalhost ¶
IsLocalhost reports whether the requesting connection is from this machine and has the same owner as this process.
func IsWebsocketUpgrade ¶
func MustGet ¶
MustGet returns a non-empty GET (or HEAD) parameter param and panics with a special error as caught by a deferred httputil.Recover.
func MustGetBlobRef ¶
MustGetBlobRef returns a non-nil BlobRef from req, as given by param. If it doesn't, it panics with a value understood by Recover or RecoverJSON.
func OptionalInt ¶
OptionalInt returns the integer in req given by param, or 0 if not present. If the form value is not an integer, it panics with a a value understood by Recover or RecoverJSON.
func PathSuffix ¶
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 RequestTargetPort ¶
RequestTargetPort returns the port targetted by the client in req. If not present, it returns 80, or 443 if TLS is used.
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 ServeJSONError ¶
func ServeJSONError(rw http.ResponseWriter, err interface{})
ServeJSONError sends a JSON error response to rw for the provided error value.
Types ¶
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 ¶
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 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
type StatsTransport ¶
type StatsTransport struct { // Transport optionally specifies the transport to use. // If nil, http.DefaultTransport is used. Transport http.RoundTripper // If VerboseLog is true, HTTP request summaries are logged. VerboseLog bool // contains filtered or unexported fields }
StatsTransport wraps another RoundTripper (or uses the default one) and counts the number of HTTP requests performed.
func (*StatsTransport) Requests ¶
func (t *StatsTransport) Requests() int