httputil

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderAccept         = "Accept"
	HeaderAcceptEncoding = "Accept-Encoding"
	// HeaderAllow is the name of the "Allow" header field used to list the set of methods
	// advertised as supported by the target resource. Returning an Allow header is mandatory
	// for status 405 (method not found) and useful for the OPTIONS method in responses.
	// See RFC 7231: https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1
	HeaderAllow               = "Allow"
	HeaderAuthorization       = "Authorization"
	HeaderContentDisposition  = "Content-Disposition"
	HeaderContentEncoding     = "Content-Encoding"
	HeaderContentLength       = "Content-Length"
	HeaderContentType         = "Content-Type"
	HeaderCookie              = "Cookie"
	HeaderSetCookie           = "Set-Cookie"
	HeaderIfModifiedSince     = "If-Modified-Since"
	HeaderLastModified        = "Last-Modified"
	HeaderLocation            = "Location"
	HeaderRetryAfter          = "Retry-After"
	HeaderUpgrade             = "Upgrade"
	HeaderVary                = "Vary"
	HeaderWWWAuthenticate     = "WWW-Authenticate"
	HeaderXForwardedFor       = "X-Forwarded-For"
	HeaderXForwardedProto     = "X-Forwarded-Proto"
	HeaderXForwardedProtocol  = "X-Forwarded-Protocol"
	HeaderXForwardedSsl       = "X-Forwarded-Ssl"
	HeaderXUrlScheme          = "X-Url-Scheme"
	HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
	HeaderXRealIP             = "X-Real-Ip"
	HeaderXRequestID          = "X-Request-Id"
	HeaderXCorrelationID      = "X-Correlation-Id"
	HeaderXRequestedWith      = "X-Requested-With"
	HeaderServer              = "Server"
	HeaderOrigin              = "Origin"
	HeaderCacheControl        = "Cache-Control"
	HeaderConnection          = "Connection"

	// Access control
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"

	// Security
	HeaderStrictTransportSecurity         = "Strict-Transport-Security"
	HeaderXContentTypeOptions             = "X-Content-Type-Options"
	HeaderXXSSProtection                  = "X-XSS-Protection"
	HeaderXFrameOptions                   = "X-Frame-Options"
	HeaderContentSecurityPolicy           = "Content-Security-Policy"
	HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
	HeaderXCSRFToken                      = "X-CSRF-Token"
	HeaderReferrerPolicy                  = "Referrer-Policy"
)

Headers from echo framework

View Source
const (
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + charsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + charsetUTF8
	MIMETextXML                          = "text/xml"
	MIMETextXMLCharsetUTF8               = MIMETextXML + "; " + charsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + charsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + charsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEOctetStream                      = "application/octet-stream"
)

MIME types

Variables

View Source
var ErrInvalidRedirectCode = errors.New("invalid redirect code")

Functions

func Blob

func Blob(w http.ResponseWriter, code int, contentType string, b []byte) error

func Decode

func Decode(r *http.Request, v interface{}) error

Decode detects the correct decoder for use on an HTTP request and marshals into a given interface.

func DecodeForm

func DecodeForm(r io.Reader, v interface{}) error

DecodeForm decodes a given reader into an interface using the form decoder.

func DecodeJSON

func DecodeJSON(r io.Reader, v interface{}) error

DecodeJSON decodes a given reader into an interface using the json decoder.

func DecodeXML

func DecodeXML(r io.Reader, v interface{}) error

DecodeXML decodes a given reader into an interface using the xml decoder.

func GetCookie

func GetCookie(r *http.Request, cookieName string) (*http.Cookie, error)

func HandleError

func HandleError(w http.ResponseWriter, resp Error)

func IsWebSocket

func IsWebSocket(r *http.Request) bool

func JSON

func JSON(w http.ResponseWriter, code int, data interface{}) error

func JSONBlob

func JSONBlob(w http.ResponseWriter, code int, data []byte) error

func NoContent

func NoContent(w http.ResponseWriter, code int) error

func OffsetLimit

func OffsetLimit(r *http.Request) (offset, limit int64)

func QueryParam

func QueryParam(r *http.Request, key string) string

func RealIP

func RealIP(r *http.Request) string

func Redirect

func Redirect(w http.ResponseWriter, code int, url string) error

func RemoveCookie

func RemoveCookie(w http.ResponseWriter, cookieName string, v CookieConfig)

func RewriteRequestURLTarget

func RewriteRequestURLTarget(req *http.Request, target *url.URL)

func Scheme

func Scheme(r *http.Request) string

func SetCookie

func SetCookie(w http.ResponseWriter, value string, cookieName string, v CookieConfig)

func SetCookieB64

func SetCookieB64(w http.ResponseWriter, body []byte, cookieName string, v CookieConfig) string

Types

type ContentType

type ContentType int

ContentType is an enumeration of common HTTP content types.

const (
	ContentTypeUnknown ContentType = iota
	ContentTypePlainText
	ContentTypeHTML
	ContentTypeJSON
	ContentTypeXML
	ContentTypeForm
	ContentTypeEventStream
)

ContentTypes handled by this package.

func GetRequestContentType

func GetRequestContentType(r *http.Request) ContentType

type CookieConfig

type CookieConfig struct {
	// Domain for defines the host to which the cookie will be sent.
	Domain string `cfg:"domain"`
	// Path that must exist in the requested URL for the browser to send the Cookie header.
	Path string `cfg:"path"`
	// MaxAge the number of seconds until the cookie expires.
	MaxAge int `cfg:"max_age"`
	// Secure to cookie only sent over HTTPS.
	Secure bool `cfg:"secure"`
	// SameSite for Lax 2, Strict 3, None 4.
	SameSite http.SameSite `cfg:"same_site"`
	// HttpOnly for true for not accessible by JavaScript.
	HttpOnly bool `cfg:"http_only"`
}

type Error

type Error struct {
	Msg string `json:"message,omitempty"`
	Err error  `json:"error,omitempty"`

	Code int `json:"-"`
}

func NewError

func NewError(message string, err error, code int) Error

func NewErrorAs

func NewErrorAs(err error) Error

func (Error) Error

func (e Error) Error() string

func (Error) MarshalJSON

func (e Error) MarshalJSON() ([]byte, error)

type Response

type Response struct {
	Msg string `json:"message"`
}

Jump to

Keyboard shortcuts

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