Documentation ¶
Overview ¶
Package httputils is a collection of small utility functions for net/http.
Index ¶
- func MIMETypeMatches(mime string, types []string) bool
- func Negotiate(header http.Header, name string, offers ...string) (match string)
- func Newest(t ...time.Time) time.Time
- func NewestModTime(info ...os.FileInfo) time.Time
- func RequestLogf(r *http.Request, format string, v ...interface{})
- func Shutdown(ctx context.Context, srvs ...*http.Server) error
- func TCPKeepAliveListener(ln *net.TCPListener, period time.Duration) net.Listener
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MIMETypeMatches ¶
MIMETypeMatches returns whether a MIME type is found within a given list.
MIME types are compared in a case-insensitive manner.
A MIME type without any subtype will match any more precise MIME type, i.e. image/* will match image/png, image/svg, image/gif and any other image types.
Any directives that may be present in the MIME type will be skipped when comparing, i.e. text/html will match 'text/html; charset=utf-8'.
func Negotiate ¶
Negotiate performs HTTP content negotiation via the Accept, Accept-Charset, Accept-Encoding, and Accept-Language HTTP headers.
The highest ranking match is returned, or an empty string if none found.
It does not resolve wildcard MIME subtypes when used with the Accept header. If this is important to you, include the relevant wildcards in offers.
It does not resolve language subtypes, such as en-US to en. If this is important to you, include the relevant subtype in offers.
It also does not handle the rejected identity (identity;q=0 or *;q=0 or */*;q=0), instead returning an empty string.
func Newest ¶
Newest takes multiple time.Time and returns the newest time.
It is useful for selecting the newest modification time for templated resources.
func NewestModTime ¶
NewestModTime takes multiple os.FileInfo and returns the newest ModTime.
Any of info may be nil.
It is useful for selecting the newest modification time for templated resources.
func RequestLogf ¶
RequestLogf prints a message to the logger attached to the *http.Server that served the request. If nil, logging goes to os.Stderr via the log package's standard logger.
Arguments are handled in the manner of fmt.Printf.
func TCPKeepAliveListener ¶
TCPKeepAliveListener sets TCP keep-alive timeouts on accepted connections.
If period is zero, it defaults to three minutes.
It is an unexported net/http type that is used by net/http.ListenAndServe and net/http.ListenAndServeTLS so dead TCP connections (e.g. closing laptop mid-download) eventually go away.
Types ¶
This section is empty.