httputil

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: MIT Imports: 20 Imported by: 4

Documentation

Index

Constants

View Source
const (
	Unused = 0

	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + charsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + charsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationFormCharsetUTF8       = MIMEApplicationForm + "; " + charsetUTF8
	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"
)
View Source
const (
	HeaderAccept                        = "Accept"
	HeaderAcceptEncoding                = "Accept-Encoding"
	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"
	HeaderUpgrade                       = "Upgrade"
	HeaderVary                          = "Vary"
	HeaderWWWAuthenticate               = "WWW-Authenticate"
	HeaderXForwardedProto               = "X-Forwarded-Proto"
	HeaderXHTTPMethodOverride           = "X-HTTP-Method-Override"
	HeaderXForwardedFor                 = "X-Forwarded-For"
	HeaderXRealIP                       = "X-Real-IP"
	HeaderServer                        = "Server"
	HeaderOrigin                        = "Origin"
	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"

	HeaderStrictTransportSecurity = "Strict-Transport-Security"
	HeaderXContentTypeOptions     = "X-Content-Type-Options"
	HeaderXXSSProtection          = "X-XSS-Protection"
	HeaderXFrameOptions           = "X-Frame-Options"
	HeaderContentSecurityPolicy   = "Content-Security-Policy"
	HeaderXCSRFToken              = "X-CSRF-Token"
)

Variables

View Source
var (
	ErrUnableToMarshalForm = errors.New("unable to marshal form")
	ErrSign                = errors.New("failed to verify sign")
)

Functions

func FormResponse

func FormResponse(w http.ResponseWriter, value any, options ...ResponseOptions) error

func JSONResponse

func JSONResponse(w http.ResponseWriter, value any, options ...ResponseOptions) error

func LimitListener added in v0.0.18

func LimitListener(l net.Listener, n int) net.Listener

LimitListener returns a Listener that accepts at most n simultaneous connections from the provided Listener.

func Listen added in v0.0.18

func Listen(addr, path string, handler http.Handler, keepalive time.Duration) (*http.Server, net.Listener, error)

Listen creates a http server and registers a handler

Wraps a netutil.ConnHandler handler as a websocket http.Handler as following:

import "golang.org/x/net/websocket"

websocket.Handler(func(conn *websocket.Conn) {
	handler(netutil.IP(conn.Request()), conn)
})

func PingHandler

func PingHandler(w http.ResponseWriter, r *http.Request)

func PostFormJSON

func PostFormJSON(httpc *http.Client, url_ string, data url.Values, res any) error

func Response

func Response(w http.ResponseWriter, body any, options ...ResponseOptions) error

func TextResponse

func TextResponse(w http.ResponseWriter, value string, options ...ResponseOptions) error

func VerifySign

func VerifySign(signer Signer, key, keyField, signField string, args url.Values) error

func XMLResponse

func XMLResponse(w http.ResponseWriter, value any, options ...ResponseOptions) error

Types

type Config

type Config struct {
	Address           string            `json:"address"`
	StaticDir         string            `json:"static_dir"`
	StaticPath        string            `json:"static_path"`
	ConnTimeout       time.Duration     `json:"conn_timeout"`
	ReadHeaderTimeout time.Duration     `json:"read_header_timeout"`
	ReadTimeout       time.Duration     `json:"read_timeout"`
	WriteTimeout      time.Duration     `json:"write_timeout"`
	MaxConns          int               `json:"max_conns"`
	Headers           map[string]string `json:"headers"`
}

type FormMarshaler

type FormMarshaler interface {
	MarshalForm() ([]byte, error)
}

type HTTPServer

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

HTTPServer ...

func NewHTTPServer

func NewHTTPServer(cfg Config) *HTTPServer

func (*HTTPServer) Addr added in v0.0.18

func (httpd *HTTPServer) Addr() string

func (*HTTPServer) Handle

func (httpd *HTTPServer) Handle(pattern string, handler http.Handler, middlewares ...Middleware)

func (*HTTPServer) HandleFunc

func (httpd *HTTPServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request), middlewares ...Middleware)

func (*HTTPServer) JSONResponse

func (httpd *HTTPServer) JSONResponse(w http.ResponseWriter, r *http.Request, data any, options ...ResponseOptions) error

func (*HTTPServer) Listen added in v0.0.18

func (httpd *HTTPServer) Listen() (net.Listener, error)

func (*HTTPServer) ListenAndServe

func (httpd *HTTPServer) ListenAndServe() error

func (*HTTPServer) NumHandling

func (httpd *HTTPServer) NumHandling() int64

func (*HTTPServer) Serve added in v0.0.18

func (httpd *HTTPServer) Serve(l net.Listener) error

func (*HTTPServer) Shutdown

func (httpd *HTTPServer) Shutdown(ctx context.Context) error

type MarshalFunc

type MarshalFunc func(any) ([]byte, error)

type Middleware

type Middleware interface {
	Apply(http.Handler) http.Handler
}

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

MiddlewareFunc implements Middleware interface

func (MiddlewareFunc) Apply

func (m MiddlewareFunc) Apply(h http.Handler) http.Handler

type Pager

type Pager struct {
	Total int64 `json:"total"`
	Curr  int   `json:"curr"`
	Limit int   `json:"limit"`
	List  any   `json:"list"`
}

type ResponseOptions

type ResponseOptions func(opts *responseOptions)

func WithAcceptType

func WithAcceptType(acceptType string) ResponseOptions

func WithContentType

func WithContentType(contentType string) ResponseOptions

func WithStatus

func WithStatus(status int) ResponseOptions

type Result

type Result struct {
	Response   *http.Response
	StatusCode int
	Data       []byte
	Error      error
}

func Get

func Get(url string) Result

func PostForm

func PostForm(url string, values url.Values) Result

func (Result) Ok

func (result Result) Ok() bool

func (Result) Status

func (result Result) Status() string

type Signer

type Signer func(key, keyField, signField string, args url.Values) (string, error)

func HashSigner

func HashSigner(hasher hash.Hash) Signer

func MD5Signer

func MD5Signer() Signer

func SHA256Signer

func SHA256Signer() Signer

Jump to

Keyboard shortcuts

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