http

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package http has helpers for HTTP servers, such as signed cookies.

Index

Constants

View Source
const MinCookieKeyLength = 32

MinCookieKeyLength is the shortest key NewCookieSigner accepts.

Variables

This section is empty.

Functions

func ListenAndServe added in v0.0.2

func ListenAndServe(ctx context.Context, server *http.Server) error

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

func Serve(ctx context.Context, server *http.Server, listener net.Listener) error

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

func ServeLocal(ctx context.Context, address string, handler http.Handler) error

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.

func (*CookieSigner) Read

func (s *CookieSigner) Read(r *http.Request, name string, v any) bool

Read opens the cookie called name from r into v, reporting false if it is missing or not validly sealed.

func (*CookieSigner) Seal

func (s *CookieSigner) Seal(name string, v any) (string, error)

Seal encodes v as JSON and signs it for the cookie called name. The result is safe to use as a cookie value.

Jump to

Keyboard shortcuts

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