httputil

package
v0.0.0-...-c858694 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 11 Imported by: 6

Documentation

Overview

Package httputil is a custom HTTP framework created specifically for home.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowMethods

func AllowMethods(req *http.Request, allowed ...string) error

AllowMethods returns nil if req.Method is one of allowed methods, or an error of type httperror.Method otherwise.

func ErrorHandleMaybe

func ErrorHandleMaybe(
	w http.ResponseWriter, req *http.Request,
	users interface {
		// GetAuthenticated fetches the currently authenticated user,
		// or User{UserSpec: UserSpec{ID: 0}} if there is no authenticated user.
		GetAuthenticated(context.Context) (users.User, error)
	},

	maybeHandler func(w http.ResponseWriter, req *http.Request) error,
) (ok bool)

ErrorHandleMaybe factors error handling out of the HTTP maybe handler. If users is nil, it treats all requests as made by an unauthenticated user.

func ErrorHandler

func ErrorHandler(
	users interface {
		// GetAuthenticated fetches the currently authenticated user,
		// or User{UserSpec: UserSpec{ID: 0}} if there is no authenticated user.
		GetAuthenticated(context.Context) (users.User, error)
	},
	handler func(w http.ResponseWriter, req *http.Request) error,
) http.Handler

ErrorHandler factors error handling out of the HTTP handler. If users is nil, it treats all requests as made by an unauthenticated user.

func GzipHandler

func GzipHandler(handler http.Handler) http.Handler

GzipHandler applies gzip compression on top of handler, unless handler has already handled it (i.e., the "Content-Encoding" header is set).

func SetCookie

func SetCookie(w HeaderWriter, cookie *http.Cookie)

SetCookie adds a Set-Cookie header to the provided HeaderWriter's headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.

Types

type HeaderWriter

type HeaderWriter interface {
	// Header returns the header map that will be sent by
	// WriteHeader. The Header map also is the mechanism with which
	// Handlers can set HTTP trailers.
	//
	// Changing the header map after a call to WriteHeader (or
	// Write) has no effect unless the modified headers are
	// trailers.
	//
	// There are two ways to set Trailers. The preferred way is to
	// predeclare in the headers which trailers you will later
	// send by setting the "Trailer" header to the names of the
	// trailer keys which will come later. In this case, those
	// keys of the Header map are treated as if they were
	// trailers. See the example. The second way, for trailer
	// keys not known to the Handler until after the first Write,
	// is to prefix the Header map keys with the TrailerPrefix
	// constant value. See TrailerPrefix.
	//
	// To suppress implicit response headers (such as "Date"), set
	// their value to nil.
	Header() http.Header
}

HeaderWriter interface is used to construct an HTTP response header and trailer.

Jump to

Keyboard shortcuts

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