httpmiddleware

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: MIT Imports: 36 Imported by: 1

README

Http Middlewares

Go http middleware package that can be used with standard go http handlers along with httpmux package

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AuthorizationKey  = "authorization"
	AuthorizationType = "bearer"
	AuthnCtxKey       = &ContextKey{Name: "AuthnCtx"}
)

Functions

func Bytes

func Bytes(b []byte) string

Bytes gets a hash for the given slice of bytes.

func CORS

func CORS(next http.Handler) http.Handler

CORS creates a new cors handler with passed options.

func CSSResponseWriter

func CSSResponseWriter(next http.Handler) http.Handler

func CleanPath

func CleanPath(next http.Handler) http.Handler

CleanPath middleware will clean out double slash mistakes from a user's request path. For example, if a user requests /users//1 or //users////1 will both be treated as: /users/1

func ContentEncoding

func ContentEncoding(ce string, charsets ...string) bool

func ETag

func ETag(next http.Handler) http.Handler

func FileResponseWriter

func FileResponseWriter(next http.Handler) http.Handler

func GetAuthTokenFromCookie

func GetAuthTokenFromCookie(r *http.Request) (string, error)

GetAuthTokenFromCookie tries to retrieve the token from the cookie named "authorizationKey".

func GetAuthTokenFromHeader

func GetAuthTokenFromHeader(r *http.Request) (string, error)

GetAuthTokenFromHeader tries to retrieve the token from the request header: "Authorization: BEARER T".

func GetRequestId

func GetRequestId(r *http.Request) string

func GetUrlFormat

func GetUrlFormat(r *http.Request) string

GetUrlFormat - gets the url format processed by this middleware

func Gzip

func Gzip(next http.Handler) http.Handler

Gzip returns a middleware which compresses HTTP response using gzip compression scheme.

func HtmlResponseWriter

func HtmlResponseWriter(next http.Handler) http.Handler

func IsPrivateAddress

func IsPrivateAddress(address string) (bool, error)

func JavascriptResponseWriter

func JavascriptResponseWriter(next http.Handler) http.Handler

func JsonResponseWriter

func JsonResponseWriter(next http.Handler) http.Handler

func KeycloakAuthorization

func KeycloakAuthorization(next http.Handler) http.Handler

KeycloakAuthorization middleware checks for Authorization header containing a Bearer token

func MsgpackResponseWriter

func MsgpackResponseWriter(next http.Handler) http.Handler

func NoCache

func NoCache(next http.Handler) http.Handler

func ProtobufResponseWriter

func ProtobufResponseWriter(next http.Handler) http.Handler

func RealIP

func RealIP(next http.Handler) http.Handler

func Skip

func Skip() http.Handler

func String

func String(b string) string

String gets a hash for the given string.

func TextResponseWriter

func TextResponseWriter(next http.Handler) http.Handler

func TimeTakenLog

func TimeTakenLog(next http.Handler) http.Handler

TimeTakenLog middleware logs time taken for each request including the RequestId This middleware needs to be used after RequestId middleware in order to obtain the RequestId value first.

func TrueIp

func TrueIp(r *http.Request) string

func UrlFormat

func UrlFormat(next http.Handler) http.Handler

UrlFormat is a middleware that parses the url extension from a request path and stores it on the context. The middleware will trim the suffix from the routing path and continue routing. Routers should not include a url parameter for the suffix when using this httpmiddleware.

Example

     http://domain/list/cars/1 - will store ""
     http://domain/list/cars/2.json - will store "json"
     http://domain/list/cars/3.xml - will store "xml"

 func routes() http.MiddlewareFunc {
   r := NewMux()
   r.Use(httpmiddleware.URLFormat)

   r.Handler("/cars/:id", ListCars)

   return r
 }

 func ListCars(w http.ResponseWriter, r *http.request) {
	  urlFormat := httpmiddleware.GetUrlFormat(r)

	  switch urlFormat {
	  case "json":
	  	render.JSON(w, r, cars)
	  case "xml:"
	  	render.XML(w, r, cars)
	  default:
	  	render.JSON(w, r, cars)
	  }
}

func XmlResponseWriter

func XmlResponseWriter(next http.Handler) http.Handler

func YamlResponseWriter

func YamlResponseWriter(next http.Handler) http.Handler

Types

type ContextKey

type ContextKey struct {
	Name string
}

func (*ContextKey) String

func (k *ContextKey) String() string

type GzipWriter

type GzipWriter struct {
	io.Writer
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*GzipWriter) Flush

func (w *GzipWriter) Flush() error

func (*GzipWriter) Hijack

func (w *GzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*GzipWriter) Write

func (w *GzipWriter) Write(b []byte) (int, error)

type MiddlewareFunc

type MiddlewareFunc func(next http.Handler) http.Handler

MiddlewareFunc custom type to mark httpmiddleware.

func AllowContentEncoding

func AllowContentEncoding(contentEncoding ...string) MiddlewareFunc

AllowContentEncoding enforces a whitelist of request Content-Encoding otherwise responds with a 415 Unsupported Media Type status.

func AsMiddlewareFunc

func AsMiddlewareFunc(handler http.Handler) MiddlewareFunc

func Authorize

func Authorize(tokenMaker token.Builder, lookupTokenFunc func(r *http.Request) (string, error)) MiddlewareFunc

Authorize middleware for authorization.

func BasicAuth

func BasicAuth(realm string, credentials map[string]string) MiddlewareFunc

BasicAuth implements a simple middleware handler for adding basic http auth to a route.

func CORSWithConfig

func CORSWithConfig(config *cors.Config) MiddlewareFunc

CORSWithConfig creates a new cors handler with passed options.

func CheckUrlParams

func CheckUrlParams(expectedParams []string) MiddlewareFunc

CheckUrlParams checks if url params come as expected to proceed processing the request

func ContentCharset

func ContentCharset(charsets ...string) MiddlewareFunc

ContentCharset generates a handler that writes a 415 Unsupported Media Type response if none of the charsets match. An empty charset will allow requests with no Content-Type header or specified charset.

func ContentType

func ContentType(contentTypes ...string) MiddlewareFunc

ContentType enforces a whitelist of request Content-Types otherwise responds with a 415 Unsupported Media Type status.

func FileServer

func FileServer(urlPrefix, rootPath, defaultFile string, allowDirectoryListing bool) MiddlewareFunc

func GzipLevel

func GzipLevel(level int) MiddlewareFunc

GzipLevel returns a middleware which compresses HTTP response using gzip compression scheme using the level specified

func Health

func Health(endpoint, message string) MiddlewareFunc

Health endpoint middleware to check status on the server. Allows to pass a default message

func Maybe

func Maybe(mw func(http.Handler) http.Handler, maybeFn func(r *http.Request) bool) MiddlewareFunc

Maybe middleware will allow you to change the flow of the middleware stack execution depending on return value of maybeFn(request). This is useful for example if you'd like to skip a middleware handler if a request does not satisfy the maybeFn logic.

func RateLimiter

func RateLimiter(limit int, burst int) MiddlewareFunc

func Redirect

func Redirect(status int, path string) MiddlewareFunc

func RequestID

func RequestID(requestIdHeader string) MiddlewareFunc

func Rewrite

func Rewrite(old, new string) MiddlewareFunc

func SetHeader

func SetHeader(key, value string) MiddlewareFunc

SetHeader is a convenience handler to set a response header key/value

func Static

func Static(path string, handler http.Handler) MiddlewareFunc

Static routes a static GET request at the middleware level.

type Middlewares

type Middlewares []MiddlewareFunc

func (Middlewares) Append

func (m Middlewares) Append(handlers ...MiddlewareFunc) (chain Middlewares)

func (Middlewares) Then

func (m Middlewares) Then(next http.Handler) http.Handler

func (Middlewares) ThenFunc

func (m Middlewares) ThenFunc(fn http.HandlerFunc) http.Handler

Directories

Path Synopsis
Package nosurf implements an HTTP handler that mitigates Cross-Site request Forgery Attacks.
Package nosurf implements an HTTP handler that mitigates Cross-Site request Forgery Attacks.

Jump to

Keyboard shortcuts

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