# README
httpheader
This is a Go package to parse and generate standard HTTP headers correctly.
It knows about complex headers like
Accept
,
Prefer
,
Link
.
Unlike many other implementations, it handles all the tricky bits of syntax like
quoted commas,
multiple header lines,
Unicode parameters.
It gives you convenient structures to work with, and can serialize them back
into HTTP.
This package is distributed under the MIT license, and hosted on GitHub. If it doesn't yet support a header that you need, feel free to open an issue there.
Installation
go get github.com/vfaronov/httpheader
Example
const request = `GET / HTTP/1.1
Host: api.example.com
User-Agent: MyApp/1.2.3 python-requests/2.22.0
Accept: text/*, application/json;q=0.8
Forwarded: for="198.51.100.30:14852";by="[2001:db8::ae:56]";proto=https
`
r, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(request)))
forwarded := httpheader.Forwarded(r.Header)
fmt.Println("received request from user at", forwarded[0].For.IP)
for _, product := range httpheader.UserAgent(r.Header) {
if product.Name == "MyApp" && product.Version < "2.0" {
fmt.Println("enabling compatibility mode for", product)
}
}
accept := httpheader.Accept(r.Header)
acceptJSON := httpheader.MatchAccept(accept, "application/json")
acceptXML := httpheader.MatchAccept(accept, "text/xml")
if acceptXML.Q > acceptJSON.Q {
fmt.Println("responding with XML")
}
// Output: received request from user at 198.51.100.30
// enabling compatibility mode for {MyApp 1.2.3 }
// responding with XML
# Packages
No description provided by the author
# Functions
Accept parses the Accept header from h (RFC 7231 Section 5.3.2).
AddForwarded is like SetForwarded but appends instead of replacing.
AddLink is like SetLink but appends instead of replacing.
AddVary appends the given names to the Vary header in h (RFC 7231 Section 7.1.4).
AddVia is like SetVia but appends instead of replacing.
AddWarning is like SetWarning but appends instead of replacing.
Allow parses the Allow header from h (RFC 7231 Section 7.4.1).
Authorization parses the Authorization header from h (RFC 7235 Section 4.2).
CacheControl parses the Cache-Control header from h (RFC 7234 Section 5.2).
ContentDisposition parses the Content-Disposition header from h (RFC 6266), returning the disposition type, the value of the 'filename' parameter (if any), and a map of any other parameters.
ContentType parses the Content-Type header from h (RFC 7231 Section 3.1.1.5), returning the media type/subtype and any parameters.
DecodeExtValue decodes the given ext-value (RFC 8187) into its text and language tag, both of which may be empty.
DeltaSeconds returns a Delta of the given number of seconds.
EncodeExtValue encodes text, which must be valid UTF-8, into an ext-value (RFC 8187) with the given lang tag.
Forwarded parses the Forwarded header from h (RFC 7239).
IfMatch parses the If-Match header from h (RFC 7232 Section 3.1).
IfNoneMatch parses the If-None-Match header from h (RFC 7232 Section 3.2).
Link parses the Link header from h (RFC 8288), resolving any relative Target and Anchor URLs against base, which is the URL that h was obtained from (http.Response's Request.URL).
Match returns true if serverTag is equivalent to any of clientTags by strong comparison (RFC 7232 Section 2.3.2), as necessary for interpreting the If-Match header.
MatchAccept searches accept for the element that most closely matches mediaType, according to precedence rules of RFC 7231 Section 5.3.2.
MatchWeak returns true if serverTag is equivalent to any of clientTags by weak comparison (RFC 7232 Section 2.3.2), as necessary for interpreting the If-None-Match header.
Prefer parses the Prefer header from h (RFC 7240), returning a map where keys are preference names.
PreferenceApplied parses the Preference-Applied header from h (RFC 7240 Section 3), returning a map where keys are preference names.
ProxyAuthenticate parses the Proxy-Authenticate header from h (RFC 7235 Section 4.3).
ProxyAuthorization parses the Proxy-Authorization header from h (RFC 7235 Section 4.4).
RetryAfter parses the Retry-After header from h (RFC 7231 Section 7.1.3).
Server parses the Server header from h (RFC 7231 Section 7.4.2).
SetAccept replaces the Accept header in h (RFC 7231 Section 5.3.2).
SetAllow replaces the Allow header in h (RFC 7231 Section 7.4.1).
SetAuthorization replaces the Authorization header in h (RFC 7235 Section 4.2).
SetCacheControl replaces the Cache-Control header in h.
SetContentDisposition replaces the Content-Disposition header in h (RFC 6266).
SetContentType replaces the Content-Type header in h (RFC 7231 Section 3.1.1.5).
SetETag replaces the ETag header in h (RFC 7232 Section 2.3).
SetForwarded replaces the Forwarded header in h (RFC 7239).
SetLink replaces the Link header in h (RFC 8288).
SetPrefer replaces the Prefer header in h (RFC 7240).
SetPreferenceApplied replaces the Preference-Applied header in h (RFC 7240 Section 3).
SetProxyAuthenticate replaces the Proxy-Authenticate header in h (RFC 7235 Section 4.3).
SetProxyAuthorization replaces the Proxy-Authorization header in h (RFC 7235 Section 4.4).
SetRetryAfter replaces the Retry-After header in h (RFC 7231 Section 7.1.3).
SetServer replaces the Server header in h (RFC 7231 Section 7.4.2).
SetUserAgent replaces the User-Agent header in h (RFC 7231 Section 5.5.3).
SetVary replaces the Vary header in h (RFC 7231 Section 7.1.4).
SetVia replaces the Via header in h (RFC 7230 Section 5.7.1).
SetWarning replaces the Warning header in h (RFC 7234 Section 5.5).
SetWWWAuthenticate replaces the WWW-Authenticate header in h (RFC 7235 Section 4.1).
UserAgent parses the User-Agent header from h (RFC 7231 Section 5.5.3).
Vary parses the Vary header from h (RFC 7231 Section 7.1.4), returning a map where keys are header names, canonicalized with http.CanonicalHeaderKey, and values are all true.
Via parses the Via header from h (RFC 7230 Section 5.7.1).
Warning parses the Warning header from h (RFC 7234 Section 5.5).
WWWAuthenticate parses the WWW-Authenticate header from h (RFC 7235 Section 4.1).
# Structs
An AcceptElem represents one element of the Accept header (RFC 7231 Section 5.3.2).
Auth represents an authentication challenge or credentials (RFC 7235 Section 2.1).
CacheDirectives represents directives of the Cache-Control header (RFC 7234 Section 5.2).
A Delta represents a numeric cache directive which may be either absent or a number of seconds.
An EntityTag is an opaque entity tag (RFC 7232 Section 2.3).
A ForwardedElem represents one element of the Forwarded header (RFC 7239).
A LinkElem represents a Web link (RFC 8288).
A Node represents a node identifier in a Forwarded header (RFC 7239 Section 6).
A Pref contains a preference's value and any associated parameters (RFC 7240).
A Product contains software information as found in the User-Agent and Server headers (RFC 7231 Section 5.5.3 and Section 7.4.2).
A ViaElem represents one element of the Via header (RFC 7230 Section 5.7.1).
A WarningElem represents one element of the Warning header (RFC 7234 Section 5.5).