go-agent: github.com/sqreen/go-agent/sdk/middleware/sqhttp Index | Files

package sqhttp

import "github.com/sqreen/go-agent/sdk/middleware/sqhttp"

Index

Package Files

http.go

func Middleware Uses

func Middleware(next http.Handler) http.Handler

Middleware is Sqreen's middleware function for `net/http` to monitor and protect received requests. In protection mode, it can block and redirect requests according to its IP address or identified user using `Identify()` and `MatchSecurityResponse()` methods during from the request handler.

SDK methods can be called from request handlers by using the request event record. It can be accessed using `sdk.FromContext()` on a request context. The middleware function stores it into the request context.

Usage example:

fn := func(w http.ResponseWriter, r *http.Request) {
	// Get the request record.
	sqreen := sdk.FromContext(r.Context())

	// Example of sending a custom event.
	sqreen.TrackEvent("my.event")

	// Example of globally identifying a user and checking if the request
	// should be aborted.
	uid := sdk.EventUserIdentifiersMap{"uid": "my-uid"}
	sqUser := sqreen.ForUser(uid)
	sqUser.Identify() // Globally associate this user to the current request
	if match, _ := sqUser.MatchSecurityResponse(); match {
		// Return to stop further handling the request and let Sqreen's
		// middleware apply and abort the request.
		return
	}
	// Not blocked.
	fmt.Fprintf(w, "OK")
}
http.Handle("/foo", sqhttp.Middleware(http.HandlerFunc(fn)))

type AbortRequestError Uses

type AbortRequestError struct {
    Message string
}

AbortRequestError is returned by handlers when some security response was triggered and handled the response. The request handling should therefore stop.

func (AbortRequestError) Error Uses

func (AbortRequestError) Error() string

type Handler Uses

type Handler interface {
    ServeHTTP(w http.ResponseWriter, r *http.Request) error
}

Handler is equivalent to http.Handler but returns an error when the request should no longer be handled.

func MiddlewareWithError Uses

func MiddlewareWithError(next Handler) Handler

MiddlewareWithError is a helper middleware to define other middlewares for other frameworks thanks to the error returned by the handlers in order to know if a request is being aborted.

type HandlerFunc Uses

type HandlerFunc func(http.ResponseWriter, *http.Request) error

HandlerFunc is equivalent to http.HandlerFunc but returns an error when the request should no longer be handled.

func (HandlerFunc) ServeHTTP Uses

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error

ServeHTTP calls f(w, r).

type ResponseWriter Uses

type ResponseWriter = responseWriter

Package sqhttp imports 4 packages (graph) and is imported by 3 packages. Updated 2019-07-26. Refresh now. Tools for package owners.