http

package module
v0.0.0-...-159a337 Latest Latest
Warning

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

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

README

taylz.io/http

Documentation

Index

Constants

View Source
const (
	MethodRouterCONNECT methodRouter = "CONNECT"
	MethodRouterDELETE  methodRouter = "DELETE"
	MethodRouterGET     methodRouter = "GET"
	MethodRouterHEAD    methodRouter = "HEAD"
	MethodRouterOPTIONS methodRouter = "OPTIONS"
	MethodRouterPOST    methodRouter = "POST"
	MethodRouterPUT     methodRouter = "PUT"
	MethodRouterTRACE   methodRouter = "TRACE"
)
View Source
const (
	// StatusOK = 200
	StatusOK = http.StatusOK
	// StatusPartialContent = 206
	StatusPartialContent = http.StatusPartialContent
	// StatusMovedPermanently = 301
	StatusMovedPermanently = http.StatusMovedPermanently
	// StatusNotModified = 304
	StatusNotModified = http.StatusNotModified
	// StatusBadRequest = 400
	StatusBadRequest = http.StatusBadRequest
	// StatusUnauthorized = 401
	StatusUnauthorized = http.StatusUnauthorized
	// StatusPaymentRequired = 402
	StatusPaymentRequired = http.StatusPaymentRequired
	// StatusForbidden = 403
	StatusForbidden = http.StatusForbidden
	// StatusNotFound = 404
	StatusNotFound = http.StatusNotFound
	// StatusMethodNotAllowed = 405
	StatusMethodNotAllowed = http.StatusMethodNotAllowed
	// StatusConflict = 409
	StatusConflict = http.StatusConflict
	// StatusPreconditionFailed = 412
	StatusPreconditionFailed = http.StatusPreconditionFailed
	// StatusRequestedRangeNotSatisfiable = 416
	StatusRequestedRangeNotSatisfiable = http.StatusRequestedRangeNotSatisfiable
	// StatusTooManyRequests = 429
	StatusTooManyRequests = http.StatusTooManyRequests
	// StatusInternalServerError = 500
	StatusInternalServerError = http.StatusInternalServerError
)

Variables

View Source
var (
	MethodMiddlewareCONNECT = middlewareMethod("CONNECT")
	MethodMiddlewareDELETE  = middlewareMethod("DELETE")
	MethodMiddlewareGET     = middlewareMethod("GET")
	MethodMiddlewareHEAD    = middlewareMethod("HEAD")
	MethodMiddlewareOPTIONS = middlewareMethod("OPTIONS")
	MethodMiddlewarePOST    = middlewareMethod("POST")
	MethodMiddlewarePUT     = middlewareMethod("PUT")
	MethodMiddlewareTRACE   = middlewareMethod("TRACE")
)
View Source
var (
	RouterMiddlewareCONNECT = routerRouterMiddleware(MethodRouterCONNECT)
	RouterMiddlewareDELETE  = routerRouterMiddleware(MethodRouterDELETE)
	RouterMiddlewareGET     = routerRouterMiddleware(MethodRouterGET)
	RouterMiddlewareHEAD    = routerRouterMiddleware(MethodRouterHEAD)
	RouterMiddlewareOPTIONS = routerRouterMiddleware(MethodRouterOPTIONS)
	RouterMiddlewarePOST    = routerRouterMiddleware(MethodRouterPOST)
	RouterMiddlewarePUT     = routerRouterMiddleware(MethodRouterPUT)
	RouterMiddlewareTRACE   = routerRouterMiddleware(MethodRouterTRACE)
)
View Source
var ErrRegexp = Error(StatusInternalServerError, "url path regexp mismatch")
View Source
var MarshalJSON = json.Marshal
View Source
var UnmarshalJSON = json.Unmarshal

Functions

func Error

func Error(code int, err string) statusError

Error creates a StatusError

func ListenAndServe

func ListenAndServe(addr string, handler Handler) error

ListenAndServe calls http.ListenAndServe

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error

ListenAndServe calls http.ListenAndServeTLS

func MustMarshalJSON

func MustMarshalJSON(v any) []byte

func NewMD5SaltHashFunc

func NewMD5SaltHashFunc(salt string) func(string) string

NewMD5SaltHashFunc creates a Func type which salts input to crypto/md5.Sum

func ParseRequestBody

func ParseRequestBody[T any](r *Request, parserFunc func([]byte, any) error) (*T, error)

func QueryEscape

func QueryEscape(s string) string

QueryEscape calls url.QueryEscape

func RealClientAddr

func RealClientAddr(r *Request) string

RealClientAddr returns the Client IP, using "X-Real-Ip", and then "X-Forwarded-For", before defaulting to RemoteAddr

func Redirect

func Redirect(w Writer, r *Request, url string, code int)

Redirect calls http.Redirect

func StringContains

func StringContains(s, substr string) bool

func WriteError

func WriteError(w Writer, r *Request, err error)

func WriteErrorStatusJSON

func WriteErrorStatusJSON(w Writer, err error)

Types

type AllOfRouters

type AllOfRouters []Router

AllOfRouters is a Router group that returns true when all Routers in the group return true

func (AllOfRouters) RouteHTTP

func (and AllOfRouters) RouteHTTP(r *Request) bool

type AnyOfRouters

type AnyOfRouters []Router

AnyOfRouters is a Router group that returns true when any Routers in the group returns true

func (AnyOfRouters) RouteHTTP

func (or AnyOfRouters) RouteHTTP(r *Request) bool
type Cookie = http.Cookie

Cookie = http.Cookie

type Dir

type Dir = http.Dir

Dir = http.Dir

type FileSystem

type FileSystem = http.FileSystem

FileSystem = http.FileSystem

type Fork

type Fork []Path

Fork is a Pather made of []Pather

func NewFork

func NewFork() *Fork

NewFork creates a Fork

func (*Fork) Add

func (f *Fork) Add(paths ...Path) ForkBuilder

Add appends Pathers to this Fork

func (*Fork) Branch

func (f *Fork) Branch(path string) ForkBuilder

func (*Fork) Handle

func (f *Fork) Handle(path string, h Handler) ForkBuilder

Handle is short for Path(RoutePathExact(path), h)

func (*Fork) HandleFunc

func (f *Fork) HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder

HandleFunc is short for Handle(path, HandlerFunc(hf))

func (*Fork) Path

func (f *Fork) Path(r Router, h Handler) ForkBuilder

Path is short for Add(Path{r, h})

func (*Fork) PathFunc

func (f *Fork) PathFunc(r Router, hf func(Writer, *Request)) ForkBuilder

PathFunc is short for Path(r, HandlerFunc(hf))

func (*Fork) PathFuncs

func (f *Fork) PathFuncs(rf func(*Request) bool, hf func(Writer, *Request)) ForkBuilder

PathFunc is short for PathFunc(RouterFunc(rf), hf)

func (*Fork) ServeHTTP

func (f *Fork) ServeHTTP(w Writer, r *Request)

func (*Fork) With

func (f *Fork) With(routers []RouterMiddleware, middlewares []Middleware) ForkBuilder

func (*Fork) WithMiddlewares

func (f *Fork) WithMiddlewares(middlewares ...Middleware) ForkBuilder

func (*Fork) WithRouters

func (f *Fork) WithRouters(routers ...RouterMiddleware) ForkBuilder

type ForkBuilder

type ForkBuilder interface {
	Add(...Path) ForkBuilder
	Branch(path string) ForkBuilder
	Path(Router, Handler) ForkBuilder
	PathFunc(Router, func(Writer, *Request)) ForkBuilder
	PathFuncs(func(*Request) bool, func(Writer, *Request)) ForkBuilder
	Handle(path string, h Handler) ForkBuilder
	HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder
	With([]RouterMiddleware, []Middleware) ForkBuilder
	WithMiddlewares(...Middleware) ForkBuilder
	WithRouters(...RouterMiddleware) ForkBuilder
}

type ForkFunc

type ForkFunc func(...Path)

func (ForkFunc) Add

func (f ForkFunc) Add(paths ...Path) ForkBuilder

func (ForkFunc) Branch

func (f ForkFunc) Branch(path string) ForkBuilder

func (ForkFunc) Handle

func (f ForkFunc) Handle(path string, h Handler) ForkBuilder

func (ForkFunc) HandleFunc

func (f ForkFunc) HandleFunc(path string, hf func(Writer, *Request)) ForkBuilder

func (ForkFunc) Path

func (f ForkFunc) Path(r Router, h Handler) ForkBuilder

func (ForkFunc) PathFunc

func (f ForkFunc) PathFunc(r Router, hf func(Writer, *Request)) ForkBuilder

func (ForkFunc) PathFuncs

func (f ForkFunc) PathFuncs(rf func(*Request) bool, hf func(Writer, *Request)) ForkBuilder

func (ForkFunc) With

func (f ForkFunc) With(routerMiddlewares []RouterMiddleware, middlewares []Middleware) ForkBuilder

func (ForkFunc) WithMiddlewares

func (f ForkFunc) WithMiddlewares(middlewares ...Middleware) ForkBuilder

func (ForkFunc) WithRouters

func (f ForkFunc) WithRouters(routers ...RouterMiddleware) ForkBuilder

type FuncRouter

type FuncRouter func(*Request) bool

FuncRouter is a func type Router

func (FuncRouter) RouteHTTP

func (f FuncRouter) RouteHTTP(r *Request) bool

RouteHTTP implements Router by calling f

type Handler

type Handler = http.Handler

Handler = http.Handler

func AddPrefix

func AddPrefix(prefix string, h Handler) Handler

func BufferedHandler

func BufferedHandler(bytes []byte) Handler

BufferedHandler returns a Handler that always writes the closured bytes

func FileServer

func FileServer(root FileSystem) Handler

FileServer calls http.FileServer

func IndexHandler

func IndexHandler(fs FileSystem) Handler

IndexHandler returns a Handler that maps every request to /index.html for injected FileSystem, without issuing a redirect

func StripPrefix

func StripPrefix(prefix string, h Handler) Handler

func Use

func Use(h Handler, m ...Middleware) Handler

func Using

func Using(ms []Middleware, h Handler) Handler

type HandlerFunc

type HandlerFunc = http.HandlerFunc

HandlerFunc = http.HandlerFunc

type HostRouter

type HostRouter string

HostRouter is a string type for matching Request.Host

func (HostRouter) RouteHTTP

func (host HostRouter) RouteHTTP(r *Request) bool

type JSON

type JSON = map[string]any

type Middleware

type Middleware = func(next Handler) Handler

Middleware is a consumer type that manipulates Handlers

func AddPrefixMiddleware

func AddPrefixMiddleware(prefix string) Middleware

func RegexpPathContextMiddleware

func RegexpPathContextMiddleware(regexp *Regexp) Middleware

func StripPrefixMiddleware

func StripPrefixMiddleware(prefix string) Middleware

type Path

type Path struct {
	Router  Router
	Handler Handler
}

Path is a struct with Router and Handler pointers

func NewPath

func NewPath(r Router, h Handler) Path

NewPath creates a Path

func NewPathFunc

func NewPathFunc(r Router, hf func(Writer, *Request)) Path

NewPathFunc is short for NewPath(r, HandlerFunc(hf))

func SinglePagePath

func SinglePagePath(fs FileSystem, cache bool) Path

func (Path) RouteHTTP

func (p Path) RouteHTTP(r *Request) bool

RouteHTTP implements Router by calling calling the internal Router

func (Path) ServeHTTP

func (p Path) ServeHTTP(w Writer, r *Request)

ServeHTTP implements Handler by calling calling the internal Handler

type PathMatchRouter

type PathMatchRouter string

PathMatchRouter is a string type that matches a path literal

func (PathMatchRouter) RouteHTTP

func (path PathMatchRouter) RouteHTTP(r *Request) bool

RouteHTTP implements Router by literally matching the request path

type PathPrefixRouter

type PathPrefixRouter string

PathPrefixRouter is a string type that matches a path prefix

func (PathPrefixRouter) RouteHTTP

func (prefix PathPrefixRouter) RouteHTTP(r *Request) bool

RouteHTTP implements Router by matching the path prefix

type Regexp

type Regexp = regexp.Regexp

func NewRegexp

func NewRegexp(str string) *Regexp

type Request

type Request = http.Request

Request = http.Request

type ResponseWriter

type ResponseWriter = http.ResponseWriter

Writer = http.ResponseWriter

type Router

type Router interface {
	RouteHTTP(*Request) bool
}

Router is an routing interface

func AnyRouter

func AnyRouter() Router

AnyRouter returns a Router that always returns true

func RegexpPathRouter

func RegexpPathRouter(regexp *Regexp) Router

func SinglePageRouter

func SinglePageRouter() Router

SinglePageRouter returns a Router that checks for Single Page App response

Request.Method is GET Request.URL.Path does not have a period (.) Request.Header["Accept"] contains "text/html"

func UseRouter

func UseRouter(r Router, rm ...RouterMiddleware) Router

func UsingRouter

func UsingRouter(mr []RouterMiddleware, r Router) Router

UsingRouter applies RouterMiddlewares to a Router

type RouterMiddleware

type RouterMiddleware = func(Router) Router

func PathPrefixRouterMiddleware

func PathPrefixRouterMiddleware(str string) RouterMiddleware

func PathRouterMiddleware

func PathRouterMiddleware(path string) RouterMiddleware

func RegexpPathRouterMiddleware

func RegexpPathRouterMiddleware(regexp *Regexp) RouterMiddleware

type StatusError

type StatusError interface {
	error
	StatusCode() int
}

StatusError is an error with a status code

type TLSRouter

type TLSRouter bool

TLSRouter is a bool type that matches Request.TLS != nil

func (TLSRouter) RouteHTTP

func (tls TLSRouter) RouteHTTP(r *Request) bool

RouteHTTP implements Router by matching Request.TLS != nil

type URL

type URL = url.URL

URL = url.URL

func ParseURL

func ParseURL(rawurl string) (*URL, error)

ParseURL calls url.Parse

type UserAgentRouter

type UserAgentRouter string

UserAgentRouter is a string type for matching Request.Header["User-Agent"]

func (UserAgentRouter) RouteHTTP

func (ua UserAgentRouter) RouteHTTP(r *Request) bool

RouteHTTP matches the first chars of Request.Header["User-Agent"]

type Writer

type Writer = ResponseWriter

Writer = ResponseWriter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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