httputil

package
v0.0.0-...-b178995 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2020 License: MIT Imports: 22 Imported by: 1

Documentation

Index

Constants

View Source
const ResponseFormatHeader = "BlobStash-API-Response-Format"

Variables

View Source
var BasicRealm = "Authorization Required"

BasicRealm is used when setting the WWW-Authenticate response header.

Functions

func BasicAuthFunc

func BasicAuthFunc(username string, password string) func(*http.Request) bool

BasicAuthFunc implements a checker for HTTP Basic Authentication scheme

func CheckBewit

func CheckBewit(r *http.Request) error

Check will try to authenticate the `bewit` parameter for the given request

func Error

func Error(w http.ResponseWriter, err error)

Error is an shortcut for `WriteJSONError(w, http.StatusInternalServerError, err.Error())`

func ExpvarsMiddleware

func ExpvarsMiddleware(m *expvar.Map) func(next http.Handler) http.Handler

func GetIpAddress

func GetIpAddress(r *http.Request) string

Return the IP Address from the `*http.Request`. Try the `X-Real-Ip`, `X-Forwarded-For` headers first.

func GetSessionID

func GetSessionID(r *http.Request) string

GetSessionID returns the client "session ID" (set via the `BlobStash-Session-ID` header). Returns an empty string if it's missing.

func HeaderLog

func HeaderLog(w http.ResponseWriter, msg string)

HeaderLog append a debug message that will be outputted in the `BlobStash-Debug` header

func LoggerMiddleware

func LoggerMiddleware(logger log.Logger) func(next http.Handler) http.Handler

LoggerMiddleware logs HTTP requests and adds some debug headers

func MarshalAndWrite

func MarshalAndWrite(r *http.Request, w http.ResponseWriter, data interface{}, writeOptions ...func(http.ResponseWriter)) bool

func NewBewit

func NewBewit(url *url.URL, ttl time.Duration) error

New returns a `bewit` token valid for the given TTL

func NewSnappyResponseWriter

func NewSnappyResponseWriter(rw http.ResponseWriter, r *http.Request) *snappyResponseWriter

NewSnappyResponseWriter returns a `http.ResponseWriter` wrapper which can encode the output with Snappy if requested by the client. If snappy isn't enabled, it will act like a regular `http.ResponseWriter` `Close` must be call so the `*snappy.Writer` instance can be put back in the `sync.Pool`

func Read

func Read(r *http.Request) ([]byte, error)

func RecoverHandler

func RecoverHandler(h http.Handler) http.Handler

RecoverHandler catches all the "paniced" errors and display a JSON error

func SetAttachment

func SetAttachment(fname string, r *http.Request, w http.ResponseWriter)

SetAttachment will set the "Content-Disposition" header if the "dl" query parameter is set

func SetHawkKey

func SetHawkKey(bkey []byte)

func SetNoCache

func SetNoCache(w http.ResponseWriter)

Set the `Cache-control` header to `no-cache` in order to prevent the browser to cache the response

func Unmarshal

func Unmarshal(req *http.Request, out interface{}) error

func WithStatusCode

func WithStatusCode(status int) func(http.ResponseWriter)

func Write

func Write(r *http.Request, w http.ResponseWriter, data []byte, writeOptions ...func(http.ResponseWriter))

func WriteEncoded

func WriteEncoded(r *http.Request, w http.ResponseWriter, data []byte, writeOptions ...func(http.ResponseWriter))

Same as Write, but assume data is already snappy encoded

func WriteJSON

func WriteJSON(w http.ResponseWriter, data interface{})

WriteJSON marshal and output the data as JSON with the right content-type

func WriteJSONError

func WriteJSONError(w http.ResponseWriter, status int, msg string)

WriteJSONError is an helper to output a {"error": <msg>} JSON payload with the given status code

Types

type PublicError

type PublicError struct {
	Err error
}

Wrapping an error in PublicError will make the RecoverHandler display the error message instead of the default status text.

func (*PublicError) Error

func (pe *PublicError) Error() string

Error implements the Error interface

func (*PublicError) Status

func (pe *PublicError) Status() int

Status implements the PublicErrorer interface with a 500 status code

type PublicErrorer

type PublicErrorer interface {
	Status() int
	Error() string
}

PublicErrorer is the interface for "displayable" error by the RecoverHandler

func NewPublicErrorFmt

func NewPublicErrorFmt(msg string, args ...interface{}) PublicErrorer

NewPublicError is a shortcut for initializing a `PublicError` with `fmt.Errorf`

type Query

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

func NewQuery

func NewQuery(q url.Values) *Query

func (*Query) Get

func (q *Query) Get(key string) string

func (*Query) GetBoolDefault

func (q *Query) GetBoolDefault(key string, defaultval bool) (bool, error)

func (*Query) GetDefault

func (q *Query) GetDefault(key, defaultval string) string

func (*Query) GetInt

func (q *Query) GetInt(key string, defaultval, maxval int) (int, error)

func (*Query) GetInt64Default

func (q *Query) GetInt64Default(key string, defaultval int64) (int64, error)

func (*Query) GetIntDefault

func (q *Query) GetIntDefault(key string, defaultval int) (int, error)

Directories

Path Synopsis
Package bewit implement a subset of the Hawk[1] authentication scheme (Single URI Authorization).
Package bewit implement a subset of the Hawk[1] authentication scheme (Single URI Authorization).

Jump to

Keyboard shortcuts

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