oxy: github.com/vulcand/oxy/buffer Index | Files

package buffer

import "github.com/vulcand/oxy/buffer"

Package buffer provides http.Handler middleware that solves several problems when dealing with http requests:

Reads the entire request and response into buffer, optionally buffering it to disk for large requests. Checks the limits for the requests and responses, rejecting in case if the limit was exceeded. Changes request content-transfer-encoding from chunked and provides total size to the handlers.

Examples of a buffering middleware:

// sample HTTP handler
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  w.Write([]byte("hello"))
})

// Buffer will read the body in buffer before passing the request to the handler
// calculate total size of the request and transform it from chunked encoding
// before passing to the server
buffer.New(handler)

// This version will buffer up to 2MB in memory and will serialize any extra
// to a temporary file, if the request size exceeds 10MB it will reject the request
buffer.New(handler,
  buffer.MemRequestBodyBytes(2 * 1024 * 1024),
  buffer.MaxRequestBodyBytes(10 * 1024 * 1024))

// Will do the same as above, but with responses
buffer.New(handler,
  buffer.MemResponseBodyBytes(2 * 1024 * 1024),
  buffer.MaxResponseBodyBytes(10 * 1024 * 1024))

// Buffer will replay the request if the handler returns error at least 3 times
// before returning the response
buffer.New(handler, buffer.Retry(`IsNetworkError() && Attempts() <= 2`))

Index

Package Files

buffer.go threshold.go

Constants

const (
    // DefaultMemBodyBytes Store up to 1MB in RAM
    DefaultMemBodyBytes = 1048576
    // DefaultMaxBodyBytes No limit by default
    DefaultMaxBodyBytes = -1
    // DefaultMaxRetryAttempts Maximum retry attempts
    DefaultMaxRetryAttempts = 10
)

func CondSetter Uses

func CondSetter(condition bool, setter optSetter) optSetter

CondSetter Conditional setter. ex: Cond(a > 4, MemRequestBodyBytes(a))

func ErrorHandler Uses

func ErrorHandler(h utils.ErrorHandler) optSetter

ErrorHandler sets error handler of the server

func IsValidExpression Uses

func IsValidExpression(expr string) bool

IsValidExpression check if it's a valid expression

func Logger Uses

func Logger(l *log.Logger) optSetter

Logger defines the logger the buffer will use.

It defaults to logrus.StandardLogger(), the global logger used by logrus.

func MaxRequestBodyBytes Uses

func MaxRequestBodyBytes(m int64) optSetter

MaxRequestBodyBytes sets the maximum request body size in bytes

func MaxResponseBodyBytes Uses

func MaxResponseBodyBytes(m int64) optSetter

MaxResponseBodyBytes sets the maximum request body size in bytes

func MemRequestBodyBytes Uses

func MemRequestBodyBytes(m int64) optSetter

MemRequestBodyBytes bytes sets the maximum request body to be stored in memory buffer middleware will serialize the excess to disk.

func MemResponseBodyBytes Uses

func MemResponseBodyBytes(m int64) optSetter

MemResponseBodyBytes sets the maximum request body to be stored in memory buffer middleware will serialize the excess to disk.

func Retry Uses

func Retry(predicate string) optSetter

Retry provides a predicate that allows buffer middleware to replay the request if it matches certain condition, e.g. returns special error code. Available functions are:

Attempts() - limits the amount of retry attempts ResponseCode() - returns http response code IsNetworkError() - tests if response code is related to networking error

Example of the predicate:

`Attempts() <= 2 && ResponseCode() == 502`

type Buffer Uses

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

Buffer is responsible for buffering requests and responses It buffers large requests and responses to disk,

func New Uses

func New(next http.Handler, setters ...optSetter) (*Buffer, error)

New returns a new buffer middleware. New() function supports optional functional arguments

func (*Buffer) ServeHTTP Uses

func (b *Buffer) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Buffer) Wrap Uses

func (b *Buffer) Wrap(next http.Handler) error

Wrap sets the next handler to be called by buffer handler.

type SizeErrHandler Uses

type SizeErrHandler struct{}

SizeErrHandler Size error handler

func (*SizeErrHandler) ServeHTTP Uses

func (e *SizeErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)

Package buffer imports 11 packages (graph) and is imported by 17 packages. Updated 2019-01-23. Refresh now. Tools for package owners.