Documentation ¶
Overview ¶
Package httputils contains utilities for handling HTTP requests.
Index ¶
- Variables
- func AbsolutePath(r *http.Request) string
- func AddStatusDetails(s *status.Status, details ...proto.Message) *status.Status
- func AddStatusInfo(s *status.Status, name, msg string) *status.Status
- func CheckName(field, name string, regexps map[string]*regexp.Regexp) error
- func DecodeJSON(r io.Reader, v interface{}) error
- func DecodeJSONPB(r io.Reader, m proto.Message) error
- func DecodeProto(m proto.Message, b io.Reader) error
- func DecodeProtoReq(m proto.Message, req *http.Request) error
- func EncodeJSON(w io.Writer, v interface{}) error
- func EncodeJSONPB(w io.Writer, m proto.Message) error
- func EncodeProto(m proto.Message) (io.Reader, error)
- func FromError(err error) int
- func HTTPStatus(code codes.Code) int
- func IsHTTPClientError(code int) bool
- func IsHTTPError(code int) bool
- func IsHTTPRedirect(code int) bool
- func IsHTTPS(in string) bool
- func IsHTTPServerError(code int) bool
- func IsHTTPSuccess(code int) bool
- func IsJSON(str string) bool
- func IsLocalhost(in string) bool
- func LivenessCheckHandler(w http.ResponseWriter, r *http.Request)
- func MustDecodeJSONPBResp(t *testing.T, resp *http.Response, m proto.Message)
- func MustDecodeJSONResp(t *testing.T, resp *http.Response, v interface{})
- func MustDecodeProto(m proto.Message, b io.Reader)
- func MustEncodeProto(m proto.Message) io.Reader
- func MustNewReq(method, url string, body io.Reader) *http.Request
- func NewInfoStatus(code codes.Code, name, msg string) *status.Status
- func NewPageHandler(page string, additionalCSP *CSP) func(w http.ResponseWriter, r *http.Request)
- func QueryParam(r *http.Request, name string) string
- func QueryParamInt(r *http.Request, name string) int
- func QueryParamWithDefault(r *http.Request, name string, d string) string
- func RPCCode(code int) codes.Code
- func RedirectHTMLPage(dst string) string
- func RequesterIP(r *http.Request) string
- func StatusPath(list ...string) string
- func StopHandler(w http.ResponseWriter, r *http.Request)
- func TemplateFromFiles(paths ...string) (*template.Template, error)
- func TracingID(r *http.Request) string
- func WriteCorsHeaders(w http.ResponseWriter)
- func WriteError(w http.ResponseWriter, err error)
- func WriteHTMLResp(w http.ResponseWriter, b string, additionalCSP *CSP)
- func WriteNonProtoResp(w http.ResponseWriter, resp interface{})
- func WriteRedirect(w http.ResponseWriter, r *http.Request, redirect string)
- func WriteResp(w http.ResponseWriter, m proto.Message)
- type CSP
- type FakeWriter
- type Page
Constants ¶
This section is empty.
Variables ¶
var HTMLReplacer = strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
`"`, """,
"'", "'",
)
HTMLReplacer escape URL parameters for HTML. This is copied from http package.
var ( // NewStatus is temprorary aliases. NewStatus = status.New )
Functions ¶
func AbsolutePath ¶
AbsolutePath find the registered path in the mux router. eg. register "/path/{var}" in router, request to "/path/a" AbsolutePath(r) will return "/path/{var}".
func AddStatusDetails ¶
AddStatusDetails adds a details message to a status.
func AddStatusInfo ¶
AddStatusInfo returns a new status that includes an additional ErrorInfo entry.
func DecodeJSON ¶
DecodeJSON reads JSON from io.Reader and decodes it into an object.
func DecodeJSONPB ¶
DecodeJSONPB reads JSONPB from io.Reader and decodes it into an object.
func DecodeProto ¶
DecodeProto decodes a reader with JSON coded protobuffer message content.
func DecodeProtoReq ¶
DecodeProtoReq decodes a request with protobuffer message body.
func EncodeJSON ¶
EncodeJSON encodes an object into JSON and writes it to io.Writer.
func EncodeJSONPB ¶
EncodeJSONPB encodes an object into JSONPB and writes it to io.Writer.
func EncodeProto ¶
EncodeProto decodes a reader with JSON coded protobuffer message content.
func HTTPStatus ¶
HTTPStatus translates a codes.Code into an HTTP status.
func IsHTTPClientError ¶
IsHTTPClientError check if http code is client error code: 4xx
func IsHTTPError ¶
IsHTTPError check if http code is error code: 4xx or 5xx
func IsHTTPRedirect ¶
IsHTTPRedirect check if http code is redirect code: 3xx
func IsHTTPServerError ¶
IsHTTPServerError check if http code is server error code: 5xx
func IsHTTPSuccess ¶
IsHTTPSuccess check if http code is success code: 2xx See: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
func IsLocalhost ¶ added in v0.9.3
IsLocalhost checks if the url is hosting in local
func LivenessCheckHandler ¶
func LivenessCheckHandler(w http.ResponseWriter, r *http.Request)
LivenessCheckHandler implements an application liveness checker for Google App Engine Flex apps
func MustDecodeJSONPBResp ¶
MustDecodeJSONPBResp is the test helper for DecodeJSONPB. TODO: move to a test package.
func MustDecodeJSONResp ¶
MustDecodeJSONResp is the test helper for DecodeJSON. TODO: move to a test package.
func MustDecodeProto ¶
MustDecodeProto decodes a reader with JSON coded protobuffer message content.
func MustEncodeProto ¶
MustEncodeProto decodes a reader with JSON coded protobuffer message content.
func MustNewReq ¶
MustNewReq creates a request. Crashes on error.
func NewInfoStatus ¶
NewInfoStatus returns a standard RPC-style error message with ErrorInfo details.
func NewPageHandler ¶
NewPageHandler creates a new handler that serves the given HTML page.
func QueryParam ¶
QueryParam returns a URL query parameter value.
func QueryParamInt ¶
QueryParamInt returns a URL query parameter value of int type. Returns 0 if missing or invalid.
func QueryParamWithDefault ¶
QueryParamWithDefault returns a URL query parameter value. If not set or empty, the provided default value is returned.
func RedirectHTMLPage ¶
RedirectHTMLPage retuns the HTML page generated by http.Redirect. This is copied from http package.
func RequesterIP ¶
RequesterIP find the requester ip from http request.
func StatusPath ¶
StatusPath combines multiple path elements into one string path.
func StopHandler ¶
func StopHandler(w http.ResponseWriter, r *http.Request)
StopHandler will cause make the server exit.
func TemplateFromFiles ¶ added in v0.9.2
TemplateFromFiles constructs the html safe template from given file. name: name of temaplate. path: path of the template file.
func TracingID ¶
TracingID find the tracing id in the request See https://cloud.google.com/appengine/docs/flexible/python/reference/request-headers#app_engine-specific_headers
func WriteCorsHeaders ¶
func WriteCorsHeaders(w http.ResponseWriter)
WriteCorsHeaders writes CORS headers (https://www.w3.org/TR/cors) to the response.
func WriteError ¶
func WriteError(w http.ResponseWriter, err error)
WriteError writes an error to the response. Does nothing if status is nil.
func WriteHTMLResp ¶
func WriteHTMLResp(w http.ResponseWriter, b string, additionalCSP *CSP)
WriteHTMLResp writes a "text/html" type string to the ResponseWriter.
func WriteNonProtoResp ¶
func WriteNonProtoResp(w http.ResponseWriter, resp interface{})
WriteNonProtoResp writes a reponse. For protobuf message responses use WriteResp(w, resp) instead.
func WriteRedirect ¶
func WriteRedirect(w http.ResponseWriter, r *http.Request, redirect string)
WriteRedirect writes a redirect to the provider URL. If the provided URL is relative, it will be relative to the request URL.
Types ¶
type CSP ¶ added in v0.9.4
type CSP struct {
// contains filtered or unexported fields
}
CSP handles Content Security Policy headers. See https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
func CSPFromString ¶ added in v0.9.4
CSPFromString reads csp from header.
type FakeWriter ¶
FakeWriter is a fake HTTP response writer. See http.ResponseWriter.
func (*FakeWriter) Write ¶
func (w *FakeWriter) Write(b []byte) (int, error)
Write appends to the Body.
func (*FakeWriter) WriteHeader ¶
func (w *FakeWriter) WriteHeader(code int)
WriteHeader writes the HTTP status code.