Documentation
¶
Overview ¶
Package http has helpers for HTTP servers, such as signed cookies.
Index ¶
Constants ¶
const MinCookieKeyLength = 32
MinCookieKeyLength is the shortest key NewCookieSigner accepts.
Variables ¶
This section is empty.
Functions ¶
func ListenAndServe ¶ added in v0.0.2
ListenAndServe listens on server's Addr, or ":http" if it's empty, and then serves it as Serve does.
func Serve ¶ added in v0.0.2
Serve serves server on listener until ctx is done, then shuts it down gracefully. In-flight requests get a few seconds to finish before their connections are closed.
Before serving, Serve sets server's ReadHeaderTimeout and IdleTimeout to sensible defaults if neither they nor ReadTimeout are set. It sets no read or write timeout, as they'd cut off slow uploads and long-lived responses such as server-sent events.
Serve returns nil after a clean shutdown, or the error that stopped it otherwise.
func ServeLocal ¶ added in v0.0.2
ServeLocal serves handler over plain HTTP on address, as Serve does, for running a service locally without its usual network layer.
Requests are rejected with 421 Misdirected Request unless their Host is address, or localhost, 127.0.0.1 or [::1] on its port, ignoring case. This stops pages on other sites from reaching the service through DNS rebinding. If address has port 0, the port actually listened on is used.
Types ¶
type CookieSigner ¶
type CookieSigner struct {
// contains filtered or unexported fields
}
CookieSigner keeps small values in cookies signed with HMAC-SHA256, so a server can trust what comes back without keeping a store. Values are signed but not encrypted: the client can read them, but not change them.
The signature covers the cookie's name as well as its value, so a value sealed for one cookie is not accepted for another.
func NewCookieSigner ¶
func NewCookieSigner(key []byte) (*CookieSigner, error)
NewCookieSigner returns a CookieSigner using key, which must be at least MinCookieKeyLength bytes. Changing the key invalidates every value sealed with the old one.
func (*CookieSigner) Open ¶
func (s *CookieSigner) Open(name, value string, v any) bool
Open checks that value was sealed for the cookie called name and decodes it into v. It reports false if the signature or encoding is wrong, in which case v may have been partly written.