httputil

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httputil contains utilities for HTTP clients and servers.

Index

Constants

This section is empty.

Variables

View Source
var HopByHopHeaders = []string{
	"Connection",
	"Keep-Alive",
	"Proxy-Authenticate",
	"Proxy-Authorization",
	"Te",
	"Trailers",
	"Transfer-Encoding",
	"Upgrade",
	"Host",
}

HopByHopHeaders are hop-by-hop headers that should not be forwarded by proxies (RFC 7230).

View Source
var TransportHeaders = []string{
	"Content-Length",
	"Date",
	"Accept-Encoding",
	"User-Agent",
	"Transfer-Encoding",
	"Time-To-Live",
	"If-Match",
	"If-None-Match",
	"Range",
	"If-Range",
	"Content-Range",
}

TransportHeaders are headers added by the HTTP transport layer that should not be cached.

Functions

func CheckConditionals

func CheckConditionals(r *http.Request, etag string) int

CheckConditionals evaluates RFC 7232 If-Match and If-None-Match precondition headers on r against etag. It returns 0 when all preconditions pass, otherwise the HTTP status the caller should send: 412 Precondition Failed for a failed If-Match, or 304 Not Modified for a satisfied If-None-Match. It is for callers that serve a body directly (not via ServeCacheHit) and need the status code.

func ConditionalOptions

func ConditionalOptions(r *http.Request) []client.RequestOption

ConditionalOptions extracts conditional-request and range options from an incoming request, for forwarding to a cache Open or Stat. Range/If-Range are honoured by Open and ignored by Stat.

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, r *http.Request, status int, msg string, args ...any)

ErrorResponse creates an error response with the given code and format, and also logs a message.

func Errorf

func Errorf(status int, format string, args ...any) error

func FilterHeaders

func FilterHeaders(headers http.Header, skip ...string) http.Header

FilterHeaders returns a copy of headers with the specified header keys removed.

func ServeCacheHit

func ServeCacheHit(w http.ResponseWriter, headers http.Header, body io.ReadCloser, openErr error, opts ...ServeOption) (handled bool, n int64, err error)

ServeCacheHit writes the outcome of a cache Open to w. headers and body are the Open return values and openErr its error. It handles the success and conditional cases: a nil error streams the body (always closing it), a satisfied If-None-Match (ErrNotModified) writes 304 with the stored headers, and a failed If-Match (ErrPreconditionFailed) writes 412. It returns handled=false for any other error (e.g. os.ErrNotExist) so the caller can map it to its own status, and n is the number of body bytes written (0 for bodiless responses).

func ServeCacheStat

func ServeCacheStat(w http.ResponseWriter, headers http.Header, statErr error) (handled bool)

ServeCacheStat answers a metadata-only (HEAD) request from the outcome of a cache Stat. It mirrors ServeCacheHit without a body: success writes 200 with the stored headers, ErrNotModified writes 304 with headers, and ErrPreconditionFailed writes 412. It returns handled=false for any other error so the caller can map it to its own status.

Types

type HTTPError

type HTTPError struct {
	// contains filtered or unexported fields
}

func (HTTPError) Error

func (h HTTPError) Error() string

func (HTTPError) Unwrap

func (h HTTPError) Unwrap() error

func (HTTPError) WriteHTTP

func (h HTTPError) WriteHTTP(w http.ResponseWriter, r *http.Request)

WriteHTTP writes this error as an HTTP response.

type HTTPResponder

type HTTPResponder interface {
	error
	WriteHTTP(http.ResponseWriter, *http.Request)
}

HTTPResponder is an error that knows how to write itself as an HTTP response.

type ServeOption added in v0.3.0

type ServeOption func(*serveConfig)

ServeOption configures ServeCacheHit.

func WithResponseDecorator added in v0.3.0

func WithResponseDecorator(fn func(w http.ResponseWriter, stored http.Header)) ServeOption

WithResponseDecorator registers fn to run after the stored headers have been copied to the response but before the status line is written, on the 200, 206 and 416 paths. It lets endpoints that delegate range/conditional resolution to the cache still override or augment the response (e.g. force a Content-Type or advertise endpoint-specific metadata) uniformly across full and partial responses. fn is given the stored headers (so it can branch on, say, a Content-Range) and must not write the body or call WriteHeader.

Jump to

Keyboard shortcuts

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