Documentation
¶
Overview ¶
Package acceptable is a library that handles headers for content negotiation in web applications written in Go. Content negotiation is specified by RFC (http://tools.ietf.org/html/rfc7231) and, less formally, by Ajax (https://en.wikipedia.org/wiki/XMLHttpRequest).
Accept ¶
from https://tools.ietf.org/html/rfc7231#section-5.3.2:
The "Accept" header field can be used by user agents to specify response media types that are acceptable. Accept header fields can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.
A request without any Accept header field implies that the user agent will accept any media type in response.
If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response, or disregard the header field by treating the response as if it is not subject to content negotiation.
Accept-Language ¶
from https://tools.ietf.org/html/rfc7231#section-5.3.5:
The "Accept-Language" header field can be used by user agents to indicate the set of natural languages that are preferred in the response.
A request without any Accept-Language header field implies that the user agent will accept any language in response.
If the header field is present in a request and none of the available representations for the response have a matching language tag, the origin server can either disregard the header field by treating the response as if it is not subject to content negotiation or honor the header field by sending a 406 (Not Acceptable) response. However, the latter is not encouraged, as doing so can prevent users from accessing content that they might be able to use (with translation software, for example).
package header provides parsing rules for content negotiation headers according to RFC-7231.
For "Accept-Language", "Accept-Encoding" or "Accept-Charset" use the Parse function.
For "Accept" use the ParseMediaRanges function. This has more complex attributes and rules.
Index ¶
Constants ¶
const ( Accept = "Accept" AcceptLanguage = "Accept-Language" XRequestedWith = "X-Requested-With" XMLHttpRequest = "XMLHttpRequest" )
const ( // DefaultQuality is the default quality of a media range without explicit "q" // https://tools.ietf.org/html/rfc7231#section-5.3.1 DefaultQuality float64 = 1.0 //e.g text/html;q=1 // NotAcceptable is the value indicating that its item is not acceptable // https://tools.ietf.org/html/rfc7231#section-5.3.1 NotAcceptable float64 = 0.0 //e.g text/foo;q=0 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ContentType ¶
ContentType is a media type as defined in RFC-2045, RFC-2046, RFC-2231 (https://tools.ietf.org/html/rfc2045, https://tools.ietf.org/html/rfc2046, https://tools.ietf.org/html/rfc2231)
func ContentTypeOf ¶
func ContentTypeOf(typ, subtype string, paramKV ...string) ContentType
ContentTypeOf builds a content type value with optional parameters. The parameters are passed in as literal strings, e.g. "charset=utf-8".
func (ContentType) String ¶
func (ct ContentType) String() string
type MediaRange ¶
type MediaRange struct {
ContentType
Quality float64
}
MediaRange is a media range value and associated quality between 0.0 and 1.0. There may also be parameters (e.g. "charset") and extension values.
func (MediaRange) String ¶
func (mr MediaRange) String() string
func (MediaRange) StrongerThan ¶
func (mr MediaRange) StrongerThan(other MediaRange) bool
StrongerThan compares a media range with another value, using the precedence rules.
func (MediaRange) Value ¶
func (mr MediaRange) Value() string
Value gets the conjoined type and subtype string, plus any parameters (but not extensions).
type MediaRanges ¶
type MediaRanges []MediaRange
MediaRanges holds a slice of media ranges.
func ParseMediaRanges ¶
func ParseMediaRanges(acceptHeader string) MediaRanges
ParseMediaRanges splits a prioritised "Accept" header value and sorts the parts based on quality values and precedence rules. These are returned in order with the most preferred first.
A request without any Accept header field implies that the user agent will accept any media type in response. If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response or disregard the header field by treating the response as if it is not subject to content negotiation.
func (MediaRanges) String ¶
func (mrs MediaRanges) String() string
func (MediaRanges) WithDefault ¶
func (mrs MediaRanges) WithDefault() MediaRanges
type Offer ¶
type Offer struct {
ContentType
Language string
Processor func() error
}
Offer holds information about one particular resource representation that can potentially provide an acceptable response.
func BestMatch ¶
func BestMatch(mrs MediaRanges, languages PrecedenceValues, available ...Offer) *Offer
BestMatch finds the content type and language that best matches the accepted media ranges and languages. The result contains the best match, based on the rules of RFC-7231. Whenever the result is nil, the response should be 406-Not Acceptable. If there are no available offers, the response will always be nil.
func BestRequestMatch ¶
BestRequestMatch finds the content type and language that best matches the accepted media ranges and languages contained in request headers. The result contains the best match, based on the rules of RFC-7231. Whenever the result is nil, the response should be 406-Not Acceptable. If there are no available offers, the response will always be nil.
type PrecedenceValue ¶
PrecedenceValue is a value and associate quality between 0.0 and 1.0
func (PrecedenceValue) String ¶
func (pv PrecedenceValue) String() string
type PrecedenceValues ¶
type PrecedenceValues []PrecedenceValue
PrecedenceValues holds a slice of precedence values.
func Parse ¶
func Parse(acceptXyzHeader string) PrecedenceValues
Parse splits a prioritised "Accept-Language", "Accept-Encoding" or "Accept-Charset" header value and sorts the parts. These are returned in order with the most preferred first.
func (PrecedenceValues) String ¶
func (pvs PrecedenceValues) String() string
func (PrecedenceValues) WithDefault ¶
func (pvs PrecedenceValues) WithDefault() PrecedenceValues