http

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
)
View Source
const (
	StatusContinue           = http.StatusContinue
	StatusSwitchingProtocols = http.StatusSwitchingProtocols
	StatusProcessing         = http.StatusProcessing
	StatusEarlyHints         = http.StatusEarlyHints

	StatusOK                   = http.StatusOK
	StatusCreated              = http.StatusCreated
	StatusAccepted             = http.StatusAccepted
	StatusNonAuthoritativeInfo = http.StatusNonAuthoritativeInfo
	StatusNoContent            = http.StatusNoContent
	StatusResetContent         = http.StatusResetContent
	StatusPartialContent       = http.StatusPartialContent
	StatusMultiStatus          = http.StatusMultiStatus
	StatusAlreadyReported      = http.StatusAlreadyReported
	StatusIMUsed               = http.StatusIMUsed

	StatusMultipleChoices  = http.StatusMultipleChoices
	StatusMovedPermanently = http.StatusMovedPermanently
	StatusFound            = http.StatusFound
	StatusSeeOther         = http.StatusSeeOther
	StatusNotModified      = http.StatusNotModified
	StatusUseProxy         = http.StatusUseProxy

	StatusTemporaryRedirect = http.StatusTemporaryRedirect
	StatusPermanentRedirect = http.StatusPermanentRedirect

	StatusBadRequest                   = http.StatusBadRequest
	StatusUnauthorized                 = http.StatusUnauthorized
	StatusPaymentRequired              = http.StatusPaymentRequired
	StatusForbidden                    = http.StatusForbidden
	StatusNotFound                     = http.StatusNotFound
	StatusMethodNotAllowed             = http.StatusMethodNotAllowed
	StatusNotAcceptable                = http.StatusNotAcceptable
	StatusProxyAuthRequired            = http.StatusProxyAuthRequired
	StatusRequestTimeout               = http.StatusRequestTimeout
	StatusConflict                     = http.StatusConflict
	StatusGone                         = http.StatusGone
	StatusLengthRequired               = http.StatusLengthRequired
	StatusPreconditionFailed           = http.StatusPreconditionFailed
	StatusRequestEntityTooLarge        = http.StatusRequestEntityTooLarge
	StatusRequestURITooLong            = http.StatusRequestURITooLong
	StatusUnsupportedMediaType         = http.StatusUnsupportedMediaType
	StatusRequestedRangeNotSatisfiable = http.StatusRequestedRangeNotSatisfiable
	StatusExpectationFailed            = http.StatusExpectationFailed
	StatusTeapot                       = http.StatusTeapot
	StatusMisdirectedRequest           = http.StatusMisdirectedRequest
	StatusUnprocessableEntity          = http.StatusUnprocessableEntity
	StatusLocked                       = http.StatusLocked
	StatusFailedDependency             = http.StatusFailedDependency
	StatusTooEarly                     = http.StatusTooEarly
	StatusUpgradeRequired              = http.StatusUpgradeRequired
	StatusPreconditionRequired         = http.StatusPreconditionRequired
	StatusTooManyRequests              = http.StatusTooManyRequests
	StatusRequestHeaderFieldsTooLarge  = http.StatusRequestHeaderFieldsTooLarge
	StatusUnavailableForLegalReasons   = http.StatusUnavailableForLegalReasons

	StatusInternalServerError           = http.StatusInternalServerError
	StatusNotImplemented                = http.StatusNotImplemented
	StatusBadGateway                    = http.StatusBadGateway
	StatusServiceUnavailable            = http.StatusServiceUnavailable
	StatusGatewayTimeout                = http.StatusGatewayTimeout
	StatusHTTPVersionNotSupported       = http.StatusHTTPVersionNotSupported
	StatusVariantAlsoNegotiates         = http.StatusVariantAlsoNegotiates
	StatusInsufficientStorage           = http.StatusInsufficientStorage
	StatusLoopDetected                  = http.StatusLoopDetected
	StatusNotExtended                   = http.StatusNotExtended
	StatusNetworkAuthenticationRequired = http.StatusNetworkAuthenticationRequired
)
View Source
const (
	DefaultMaxHeaderBytes      = http.DefaultMaxHeaderBytes
	DefaultMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
	TimeFormat                 = http.TimeFormat
	TrailerPrefix              = http.TrailerPrefix
)

Variables

View Source
var (
	ErrNotSupported         = http.ErrNotSupported
	ErrUnexpectedTrailer    = http.ErrUnexpectedTrailer // Deprecated
	ErrMissingBoundary      = http.ErrMissingBoundary
	ErrNotMultipart         = http.ErrNotMultipart
	ErrHeaderTooLong        = http.ErrHeaderTooLong        // Deprecated
	ErrShortBody            = http.ErrShortBody            // Deprecated
	ErrMissingContentLength = http.ErrMissingContentLength // Deprecated

	ErrBodyNotAllowed  = http.ErrBodyNotAllowed
	ErrHijacked        = http.ErrHijacked
	ErrContentLength   = http.ErrContentLength
	ErrWriteAfterFlush = http.ErrWriteAfterFlush // Deprecated

	ServerContextKey    = http.ServerContextKey
	LocalAddrContextKey = http.LocalAddrContextKey

	DefaultClient         = http.DefaultClient
	DefaultServeMux       = http.DefaultServeMux
	ErrAbortHandler       = http.ErrAbortHandler
	ErrBodyReadAfterClose = http.ErrBodyReadAfterClose
	ErrHandlerTimeout     = http.ErrHandlerTimeout
	ErrLineTooLong        = http.ErrLineTooLong
	ErrMissingFile        = http.ErrMissingFile
	ErrNoCookie           = http.ErrNoCookie
	ErrNoLocation         = http.ErrNoLocation
	ErrSchemeMismatch     = http.ErrSchemeMismatch
	ErrServerClosed       = http.ErrServerClosed
	ErrSkipAltProtocol    = http.ErrSkipAltProtocol
	ErrUseLastResponse    = http.ErrUseLastResponse
	NoBody                = http.NoBody
)
View Source
var WithJar = WithCookieJar

WithJar is just an alias to WithCookieJar

Functions

This section is empty.

Types

type CheckRedirectFn

type CheckRedirectFn = func(*http.Request, []*http.Request) error

type Client

type Client interface {
	CloseIdleConnections()
	Do(*http.Request) (ClientResponse, error)
	Get(string) (ClientResponse, error)
	Head(string) (ClientResponse, error)
	Post(string, string, io.Reader) (ClientResponse, error)
	PostForm(string, url.Values) (ClientResponse, error)
}

Client is an interface for the net/http.Client struct

func NewClient

func NewClient(options ...ClientOption) Client

NewClient creates a Client with default values

type ClientOption

type ClientOption func(cl *http.Client)

ClientOption allows you to set options on a client in the NewClient constructor

func WithCheckRedirect

func WithCheckRedirect(f func(*http.Request, []*http.Request) error) ClientOption

Set the net/http.Client CheckRedirect

func WithCookieJar

func WithCookieJar(j CookieJar) ClientOption

Set the net/http.Client CookieJar

func WithTimeout

func WithTimeout(t time.Duration) ClientOption

Set the net/http.Client Timeout

func WithTransport

func WithTransport(t RoundTripper) ClientOption

Set the net/http.Client Transport

type ClientRequest

type ClientRequest interface {
	Write(io.Writer) error
	WriteProxy(io.Writer) error
}

ClientRequest provides an interface to a net/http.Request for use on a client making an http request.

func NewClientRequest

func NewClientRequest(method, url string, body io.Reader, options ...ClientRequestOption) (ClientRequest, error)

func NewClientRequestWithContext

func NewClientRequestWithContext(ctx context.Context, method, url string, body io.Reader, options ...ClientRequestOption) (ClientRequest, error)

func ReadRequest

func ReadRequest(r *bufio.Reader) (ClientRequest, error)

ReadRequest is a "constructor" that constructs a Request from a Reader

type ClientRequestOption

type ClientRequestOption func(req *http.Request)

ClientRequestOption allows you to set options on a request in the NewClientRequest constructor

func WithBody

func WithBody(r io.ReadCloser) ClientRequestOption

Set the net/http.Request Body

func WithClose

func WithClose(v bool) ClientRequestOption

Set the net/http.Request Close

func WithContentLength

func WithContentLength(l int64) ClientRequestOption

Set the net/http.Request ContentLength

func WithForm

func WithForm(form url.Values) ClientRequestOption

Set the net/http.Request Form

func WithGetBody

func WithGetBody(f func() (io.ReadCloser, error)) ClientRequestOption

Set the net/http.Request GetBody

func WithHeader

func WithHeader(name string, values ...string) ClientRequestOption

Set a an individual net/http.Request Header

func WithHeaders

func WithHeaders(headers Header) ClientRequestOption

Set the net/http.Request Header map

func WithHost

func WithHost(host string) ClientRequestOption

Set the net/http.Request Host

func WithMultipartForm

func WithMultipartForm(form *multipart.Form) ClientRequestOption

Set the net/http.Request MultipartForm

func WithPostForm

func WithPostForm(form url.Values) ClientRequestOption

Set the net/http.Request PostForm

func WithRemoteAddr

func WithRemoteAddr(addr string) ClientRequestOption

Set the net/http.RequestForm RemoteAddr

func WithRequestURI

func WithRequestURI(uri string) ClientRequestOption

Set the net/http.RequestForm RequestURI

func WithTrailer

func WithTrailer(t Header) ClientRequestOption

Set the net/http.RequestForm Trailer

func WithTransferEncoding

func WithTransferEncoding(encodings []string) ClientRequestOption

Set the net/http.Request TransferEncoding

type ClientResponse

type ClientResponse interface {
	// Simple pass-through methods:
	Cookies() []*Cookie
	Location() (*url.URL, error)
	ProtoAtLeast(int, int) bool
	Write(io.Writer) error

	// Methods allowing access to member variables:
	Status() string
	StatusCode() int
	Proto() string
	ProtoMajor() int
	ProtoMinor() int
	Header() Header
	Body() io.ReadCloser
	ContentLength() int64
	TransferEncoding() []string
	Close() bool
	Uncompressed() bool
	Trailer() Header
	Request() ClientRequest
	TLS() *tls.ConnectionState
}

ClientResponse is an interface to a Response received by the client.

type ConnState

type ConnState = http.ConnState
type Cookie = http.Cookie

type CookieJar

type CookieJar = http.CookieJar

type Dir

type Dir = http.Dir

type File

type File = http.File

type FileSystem

type FileSystem = http.FileSystem

type Flusher

type Flusher = http.Flusher

type HTTP

type HTTP interface {
	// ClientResponse constructors:
	Get(string) (ClientResponse, error)
	Head(string) (ClientResponse, error)
	Post(string, string, io.Reader) (ClientResponse, error)
	PostForm(string, url.Values) (ClientResponse, error)
	ReadResponse(*bufio.Reader, *http.Request) (ClientResponse, error)

	CanonicalHeaderKey(string) string
	DetectContentType([]byte) string
	Error(ResponseWriter, string, int)
	Handle(string, http.Handler)
	HandleFunc(string, func(ResponseWriter, *http.Request))
	ListenAndServe(string, http.Handler) error
	ListenAndServeTLS(string, string, string, http.Handler) error
	MaxBytesReader(ResponseWriter, io.ReadCloser, int64) io.ReadCloser
	NotFound(ResponseWriter, *http.Request)
	ParseHTTPVersion(string) (int, int, bool)
	ParseTime(string) (time.Time, error)
	ProxyFromEnvironment(*http.Request) (*url.URL, error)
	ProxyURL(*url.URL) func(*http.Request) (*url.URL, error)
	Redirect(ResponseWriter, *http.Request, string, int)
	Serve(net.Listener, http.Handler) error
	ServeContent(ResponseWriter, *http.Request, string, time.Time, io.ReadSeeker)
	ServeFile(ResponseWriter, *http.Request, string)
	ServeFileFS(ResponseWriter, *http.Request, fs.FS, string)
	ServeTLS(net.Listener, http.Handler, string, string) error
	SetCookie(ResponseWriter, *Cookie)
	StatusText(int) string
}

HTTP is an interface for the functions in the net/http package

func NewHTTP

func NewHTTP() HTTP

NewHTTP creates a new HTTP instance.

type Handler

type Handler = http.Handler

HTTP2Config is being added in 1.24.0. When it's a little more baked, I'll add it here: type HTTP2Config = http.HTTP2Config

type HandlerFunc

type HandlerFunc = http.HandlerFunc
type Header = http.Header

type Hijacker

type Hijacker = http.Hijacker

type MaxBytesError

type MaxBytesError = http.MaxBytesError

type PushOptions

type PushOptions = http.PushOptions

Protocols is being added in 1.24.0. When it's a little more baked, I'll add it here: type Protocols = http.Protocols

type Pusher

type Pusher = http.Pusher

type ResponseWriter

type ResponseWriter = http.ResponseWriter

type RoundTripper

type RoundTripper = http.RoundTripper

type SameSite

type SameSite = http.SameSite

type Server

type Server interface {
	Close() error
	ListenAndServe() error
	ListenAndServeTLS(string, string) error
	RegisterOnShutdown(func())
	Serve(net.Listener) error
	ServeTLS(net.Listener, string, string) error
	SetKeepAlivesEnabled(bool)
	Shutdown(context.Context) error
}

Server is an interface for the net/http.Server struct

func NewServer

func NewServer(options ...ServerOption) Server

NewServer creates a Server with default values

type ServerOption

type ServerOption func(svr *http.Server)

ServerOption allows you to set options on a server in the NewServer constructor

func WithAddr

func WithAddr(addr string) ServerOption

Set the net/http.Server Addr

func WithBaseContext

func WithBaseContext(f func(net.Listener) context.Context) ServerOption

Set the net/http.Server BaseContext

func WithConnContext

func WithConnContext(f func(context.Context, net.Conn) context.Context) ServerOption

Set the net/http.Server ConnContext

func WithConnState

func WithConnState(f func(net.Conn, ConnState)) ServerOption

Set the net/http.Server ConnState Note that the function will be called with the underlying connection, not an interface.

func WithDisableGeneralOptionsHandler

func WithDisableGeneralOptionsHandler(v bool) ServerOption

Set the net/http.Server DisableGeneralOptionsHandler

func WithErrorLog

func WithErrorLog(l *log.Logger) ServerOption

Set the net/http.Server ErrorLog

func WithHandler

func WithHandler(handler Handler) ServerOption

Set the net/http.Server Handler

func WithIdleTimeout

func WithIdleTimeout(t time.Duration) ServerOption

Set the net/http.Server IdleTimeout

func WithMaxHeaderBytes

func WithMaxHeaderBytes(n int) ServerOption

Set the net/http.Server MaxHeaderBytes

func WithReadHeaderTimeout

func WithReadHeaderTimeout(t time.Duration) ServerOption

Set the net/http.Server ReadHeaderTimeout

func WithReadTimeout

func WithReadTimeout(t time.Duration) ServerOption

Set the net/http.Server ReadTimeout

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) ServerOption

Set the net/http.Server TLSConfig

func WithTLSNextProto

func WithTLSNextProto(m map[string]func(*http.Server, *tls.Conn, http.Handler)) ServerOption

Set the net/http.Server TLSNextProto TODO: Explore the possibility of replacing the function parameters with interfaces

func WithWriteTimeout

func WithWriteTimeout(t time.Duration) ServerOption

Set the net/http.Server WriteTimeout

type ServerRequest

type ServerRequest interface {
	Method() string
	URL() *url.URL
	Proto() string
	ProtoMajor() int
	ProtoMinor() int
	Header() Header
	Body() io.ReadCloser
	GetBody() func() (io.ReadCloser, error)
	ContentLength() int64
	TransferEncoding() []string
	Host() string
	Form() url.Values
	PostForm() url.Values
	MultipartForm() *multipart.Form
	Trailer() Header
	RemoteAddr() string
	RequestURI() string
	TLS() *tls.ConnectionState
	Pattern() string
}

ServerRequest provides an interface to a net/http.Request for use on a server receiving an http request.

Jump to

Keyboard shortcuts

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