home: github.com/shurcooL/home/httputil Index | Files

package httputil

import "github.com/shurcooL/home/httputil"

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

Index

Package Files

doc.go handler.go header.go method.go

func AllowMethods Uses

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 Uses

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 Uses

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 Uses

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 Uses

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.

type HeaderWriter Uses

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.

Package httputil imports 11 packages (graph) and is imported by 8 packages. Updated 2020-08-11. Refresh now. Tools for package owners.