httpext

package
v5.14.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: Apache-2.0, MIT Imports: 13 Imported by: 13

Documentation

Index

Constants

View Source
const (
	Gzip     string = "gzip"
	Compress string = "compress"
	Deflate  string = "deflate"
	Br       string = "br"
	Identity string = "identity"
	Any      string = "*"
)

Accept-Encoding values

View Source
const (
	UTF8     string = "utf-8"
	ISO88591 string = "iso-8859-1"
)

Charset values

View Source
const (
	Age                           string = "Age"
	AltSCV                        string = "Alt-Svc"
	Accept                        string = "Accept"
	AcceptCharset                 string = "Accept-Charset"
	AcceptPatch                   string = "Accept-Patch"
	AcceptRanges                  string = "Accept-Ranges"
	AcceptedLanguage              string = "Accept-Language"
	AcceptEncoding                string = "Accept-Encoding"
	Authorization                 string = "Authorization"
	CrossOriginResourcePolicy     string = "Cross-Origin-Resource-Policy"
	CacheControl                  string = "Cache-Control"
	Connection                    string = "Connection"
	ContentDisposition            string = "Content-Disposition"
	ContentEncoding               string = "Content-Encoding"
	ContentLength                 string = "Content-Length"
	ContentType                   string = "Content-Type"
	ContentLanguage               string = "Content-Language"
	ContentLocation               string = "Content-Location"
	ContentRange                  string = "Content-Range"
	Date                          string = "Date"
	DeltaBase                     string = "Delta-Base"
	ETag                          string = "ETag"
	Expires                       string = "Expires"
	Host                          string = "Host"
	IM                            string = "IM"
	IfMatch                       string = "If-Match"
	IfModifiedSince               string = "If-Modified-Since"
	IfNoneMatch                   string = "If-None-Match"
	IfRange                       string = "If-Range"
	IfUnmodifiedSince             string = "If-Unmodified-Since"
	KeepAlive                     string = "Keep-Alive"
	LastModified                  string = "Last-Modified"
	Link                          string = "Link"
	Pragma                        string = "Pragma"
	ProxyAuthenticate             string = "Proxy-Authenticate"
	ProxyAuthorization            string = "Proxy-Authorization"
	PublicKeyPins                 string = "Public-Key-Pins"
	RetryAfter                    string = "Retry-After"
	Referer                       string = "Referer"
	Server                        string = "Server"
	SetCookie                     string = "Set-Cookie"
	StrictTransportSecurity       string = "Strict-Transport-Security"
	Trailer                       string = "Trailer"
	TK                            string = "Tk"
	TransferEncoding              string = "Transfer-Encoding"
	Location                      string = "Location"
	Upgrade                       string = "Upgrade"
	Vary                          string = "Vary"
	Via                           string = "Via"
	Warning                       string = "Warning"
	WWWAuthenticate               string = "WWW-Authenticate"
	XForwardedFor                 string = "X-Forwarded-For"
	XForwardedHost                string = "X-Forwarded-Host"
	XForwardedProto               string = "X-Forwarded-Proto"
	XRealIP                       string = "X-Real-Ip"
	XContentTypeOptions           string = "X-Content-Type-Options"
	XFrameOptions                 string = "X-Frame-Options"
	XXSSProtection                string = "X-XSS-Protection"
	XDNSPrefetchControl           string = "X-DNS-Prefetch-Control"
	Allow                         string = "Allow"
	Origin                        string = "Origin"
	AccessControlAllowOrigin      string = "Access-Control-Allow-Origin"
	AccessControlAllowCredentials string = "Access-Control-Allow-Credentials"
	AccessControlAllowHeaders     string = "Access-Control-Allow-Headers"
	AccessControlAllowMethods     string = "Access-Control-Allow-Methods"
	AccessControlExposeHeaders    string = "Access-Control-Expose-Headers"
	AccessControlMaxAge           string = "Access-Control-Max-Age"
	AccessControlRequestHeaders   string = "Access-Control-Request-Headers"
	AccessControlRequestMethod    string = "Access-Control-Request-Method"
	TimingAllowOrigin             string = "Timing-Allow-Origin"
	UserAgent                     string = "User-Agent"
)

HTTP Header keys

View Source
const (
	ApplicationJSONNoCharset string = "application/json"
	ApplicationJSON          string = ApplicationJSONNoCharset + charsetUTF8
	ApplicationJavaScript    string = "application/javascript"
	ApplicationXMLNoCharset  string = "application/xml"
	ApplicationXML           string = ApplicationXMLNoCharset + charsetUTF8
	ApplicationForm          string = "application/x-www-form-urlencoded"
	ApplicationProtobuf      string = "application/protobuf"
	ApplicationMsgpack       string = "application/msgpack"
	ApplicationWasm          string = "application/wasm"
	ApplicationPDF           string = "application/pdf"
	ApplicationOctetStream   string = "application/octet-stream"
	TextHTMLNoCharset               = "text/html"
	TextHTML                 string = TextHTMLNoCharset + charsetUTF8
	TextPlainNoCharset              = "text/plain"
	TextPlain                string = TextPlainNoCharset + charsetUTF8
	TextMarkdownNoCharset    string = "text/markdown"
	TextMarkdown             string = TextMarkdownNoCharset + charsetUTF8
	TextCSSNoCharset         string = "text/css"
	TextCSS                  string = TextCSSNoCharset + charsetUTF8
	TextCSV                  string = "text/csv"
	ImagePNG                 string = "image/png"
	ImageGIF                 string = "image/gif"
	ImageSVG                 string = "image/svg+xml"
	ImageJPEG                string = "image/jpeg"
	MultipartForm            string = "multipart/form-data"
)

Mime Type values for the Content-Type HTTP header

View Source
const (
	// QualityValueFormat is a format string helper for Quality Values
	QualityValueFormat = "%s;q=%1.3g"
)

Variables

View Source
var (
	// DefaultFormDecoder of this package, which is configurable
	DefaultFormDecoder FormDecoder = form.NewDecoder()

	// DefaultFormEncoder of this package, which is configurable
	DefaultFormEncoder FormEncoder = form.NewEncoder()
)

Functions

func AcceptedLanguages

func AcceptedLanguages(r *http.Request) (languages []string)

AcceptedLanguages returns an array of accepted languages denoted by the Accept-Language header sent by the browser

func Attachment

func Attachment(w http.ResponseWriter, r io.Reader, filename string) (err error)

Attachment is a helper method for returning an attachement file to be downloaded, if you with to open inline see function Inline

func ClientIP

func ClientIP(r *http.Request) (clientIP string)

ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.

func Decode

func Decode(r *http.Request, qp QueryParamsOption, maxMemory int64, v interface{}) (err error)

Decode takes the request and attempts to discover it's content type via the http headers and then decode the request body into the provided struct. Example if header was "application/json" would decode using json.NewDecoder(ioext.LimitReader(r.Body, maxMemory)).Decode(v).

This default to parsing query params if includeQueryParams=true and no other content type matches.

NOTE: when includeQueryParams=true query params will be parsed and included eg. route /user?test=true 'test' is added to parsed XML and replaces any values that may have been present

func DecodeForm

func DecodeForm(r *http.Request, qp QueryParamsOption, v interface{}) (err error)

DecodeForm parses the requests form data into the provided struct.

The Content-Type and http method are not checked.

NOTE: when QueryParamsOption=QueryParams the query params will be parsed and included eg. route /user?test=true 'test' is added to parsed Form.

func DecodeJSON

func DecodeJSON(r *http.Request, qp QueryParamsOption, maxMemory int64, v interface{}) (err error)

DecodeJSON decodes the request body into the provided struct and limits the request size via an ioext.LimitReader using the maxMemory param.

The Content-Type e.g. "application/json" and http method are not checked.

NOTE: when includeQueryParams=true query params will be parsed and included eg. route /user?test=true 'test' is added to parsed JSON and replaces any values that may have been present

func DecodeMultipartForm

func DecodeMultipartForm(r *http.Request, qp QueryParamsOption, maxMemory int64, v interface{}) (err error)

DecodeMultipartForm parses the requests form data into the provided struct.

The Content-Type and http method are not checked.

NOTE: when includeQueryParams=true query params will be parsed and included eg. route /user?test=true 'test' is added to parsed MultipartForm.

func DecodeQueryParams

func DecodeQueryParams(r *http.Request, v interface{}) (err error)

DecodeQueryParams takes the URL Query params flag.

func DecodeXML

func DecodeXML(r *http.Request, qp QueryParamsOption, maxMemory int64, v interface{}) (err error)

DecodeXML decodes the request body into the provided struct and limits the request size via an ioext.LimitReader using the maxMemory param.

The Content-Type e.g. "application/xml" and http method are not checked.

NOTE: when includeQueryParams=true query params will be parsed and included eg. route /user?test=true 'test' is added to parsed XML and replaces any values that may have been present

func Inline

func Inline(w http.ResponseWriter, r io.Reader, filename string) (err error)

Inline is a helper method for returning a file inline to be rendered/opened by the browser

func IsRetryableStatusCode added in v5.4.0

func IsRetryableStatusCode(code int) bool

IsRetryableStatusCode returns if the provided status code is considered retryable.

func JSON

func JSON(w http.ResponseWriter, status int, i interface{}) error

JSON marshals provided interface + returns JSON + status code

func JSONBytes

func JSONBytes(w http.ResponseWriter, status int, b []byte) (err error)

JSONBytes returns provided JSON response with status code

func JSONP

func JSONP(w http.ResponseWriter, status int, i interface{}, callback string) error

JSONP sends a JSONP response with status code and uses `callback` to construct the JSONP payload.

func JSONStream added in v5.2.0

func JSONStream(w http.ResponseWriter, status int, i interface{}) error

JSONStream uses json.Encoder to stream the JSON reponse body.

This differs from the JSON helper which unmarshalls into memory first allowing the capture of JSON encoding errors.

func QualityValue

func QualityValue(v string, qv float32) string

QualityValue accepts a values to add/concatenate a quality values to and the quality values itself.

func XML

func XML(w http.ResponseWriter, status int, i interface{}) error

XML marshals provided interface + returns XML + status code

func XMLBytes

func XMLBytes(w http.ResponseWriter, status int, b []byte) (err error)

XMLBytes returns provided XML response with status code

Types

type FormDecoder

type FormDecoder interface {
	Decode(interface{}, url.Values) error
}

FormDecoder is the type used for decoding a form for use

type FormEncoder added in v5.1.0

type FormEncoder interface {
	Encode(interface{}) (url.Values, error)
}

FormEncoder is the type used for encoding form data

type QueryParamsOption

type QueryParamsOption uint8

QueryParamsOption represents the options for including query parameters during Decode helper functions

const (
	QueryParams QueryParamsOption = iota
	NoQueryParams
)

QueryParamsOption's

Jump to

Keyboard shortcuts

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