http

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT, Apache-2.0 Imports: 29 Imported by: 24

Documentation

Index

Constants

View Source
const (
	HeaderUserAgent                   = "User-Agent"
	HeaderXForwardedFor               = "X-Forwarded-For"
	HeaderXAccelBuffering             = "X-Accel-Buffering"
	HeaderAuth                        = "Auth"
	HeaderContentType                 = "Content-Type"
	HeaderTrace                       = "Tracing"
	HeaderTraceID                     = "Tracing-ID"
	HeaderTraceBin                    = "Tracing-Bin"
	HeaderAuthorization               = "Authorization"
	HeaderCookie                      = "Cookie"
	HeaderCookieValueToken            = "token"
	HeaderCookieValueDel              = "del"
	HeaderContentDisposition          = "Content-Disposition"
	HeaderContentEncoding             = "Content-Encoding"
	HeaderReferer                     = "Referer"
	HeaderAccept                      = "Accept"
	HeaderAcceptCharset               = "Accept-Charset"
	HeaderAcceptLanguage              = "Accept-Language"
	HeaderAcceptEncoding              = "Accept-Encoding"
	HeaderCacheControl                = "Cache-Control"
	HeaderSetCookie                   = "Set-Cookie"
	HeaderTrailer                     = "Trailer"
	HeaderTransferEncoding            = "Transfer-Encoding"
	HeaderTransferEncodingChunked     = "chunked"
	HeaderTE                          = "TE"
	HeaderLastModified                = "Last-Modified"
	HeaderContentLength               = "Content-Length"
	HeaderAccessControlRequestMethod  = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
	HeaderOrigin                      = "Origin"
	HeaderConnection                  = "Connection"
	HeaderRange                       = "Range"
	HeaderAllow                       = "Allow"
	HeaderHost                        = "Host"
	HeaderVia                         = "Via"
	HeaderDate                        = "Date"
	HeaderExpect                      = "Expect"
	HeaderFrom                        = "From"
	HeaderPragma                      = "Pragma"
	HeaderWarning                     = "Warning"
	HeaderContentRange                = "Content-Range"
	HeaderAcceptRanges                = "Accept-Ranges"
	HeaderXForwardedHost              = "X-Forwarded-Host"
)
View Source
const (
	HeaderGrpcTraceBin = "Grpc-Trace-Bin"
	HeaderGrpcInternal = "Grpc-Internal"
	HeaderGrpcStatus   = "Grpc-Status"
	HeaderGrpcMsg      = "Grpc-Msg"
	HeaderGrpcMessage  = "Grpc-Message"
	HeaderErrorCode    = "Error-Code"
	HeaderErrorMsg     = "Error-Msg"
	HeaderErrorMessage = "Error-Message"
)
View Source
const (
	HeaderDeviceInfo = "Device-Info"
	HeaderAppInfo    = "App-Info"
	HeaderLocation   = "Location"
	HeaderArea       = "Area"
	HeaderInternal   = "Internal"
)
View Source
const (
	// ContentTypeHtml is the  string of text/html response header's content type value.
	ContentTypeHtml = "text/html"
	ContentTypeCss  = "text/css"
	// ContentTypeText header value for Text data.
	ContentTypeText = "text/plain"
	// ContentTypeTextXml header value for XML data.
	ContentTypeTextXml = "text/xml"
	// ContentTypeTextMarkdown custom key/content type, the real is the text/html.
	ContentTypeTextMarkdown = "text/markdown"
	// ContentTypeTextYaml header value for YAML plain text.
	ContentTypeTextYaml = "text/yaml"

	// ContentTypeMultipart header value for post multipart form data.
	ContentTypeMultipart = "multipart/form-data"

	// ContentTypeOctetStream header value for binary data.
	ContentTypeOctetStream = "application/octet-stream"
	// ContentTypeWebassembly header value for web assembly files.
	ContentTypeWebassembly = "application/wasm"
	// ContentTypeJson header value for JSON data.
	ContentTypeJson = "application/json"
	// ContentTypeJsonProblem header value for JSON API problem error.
	// Read more at: https://tools.ietf.org/html/rfc7807
	ContentTypeJsonProblem = "application/problem+json"
	// ContentTypeXmlProblem header value for XML API problem error.
	// Read more at: https://tools.ietf.org/html/rfc7807
	ContentTypeXmlProblem = "application/problem+xml"
	ContentTypeJavascript = "application/javascript"
	// ContentTypeXml obsolete header value for XML.
	ContentTypeXml = "application/xml"
	// ContentTypeYaml header value for YAML data.
	ContentTypeYaml = "application/yaml"
	// ContentTypeProtobuf header value for Protobuf messages data.
	ContentTypeProtobuf  = "application/protobuf"
	ContentTypeXProtobuf = "application/x-protobuf"
	// ContentTypeMsgPack header value for MsgPack data.
	ContentTypeMsgPack = "application/msgpack"
	// ContentTypeForm header value for post form data.
	ContentTypeForm = "application/x-www-form-urlencoded"

	// ContentTypeGrpc Content-Type header value for gRPC.
	ContentTypeGrpc      = "application/grpc"
	ContentTypeGrpcWeb   = "application/grpc-web"
	ContentTypePdf       = "application/pdf"
	ContentTypeJsonUtf8  = "application/json;charset=utf-8"
	ContentTypeFormParam = "application/x-www-form-urlencoded;param=value"

	ContentTypeImagePng              = "image/png"
	ContentTypeImageJpeg             = "image/jpeg"
	ContentTypeImageGif              = "image/gif"
	ContentTypeImageBmp              = "image/bmp"
	ContentTypeImageWebp             = "image/webp"
	ContentTypeImageAvif             = "image/avif"
	ContentTypeImageHeif             = "image/heif"
	ContentTypeImageSvg              = "image/svg+xml"
	ContentTypeImageTiff             = "image/tiff"
	ContentTypeImageXIcon            = "image/x-icon"
	ContentTypeImageVndMicrosoftIcon = "image/vnd.microsoft.icon"

	ContentTypeCharsetUtf8 = "charset=UTF-8"

	ContentTypeTextEventStream = "text/event-stream"
)
View Source
const (
	FormDataFieldTmpl = `form-data; name="%s"`
	FormDataFileTmpl  = `form-data; name="%s"; filename="%s"`
	AttachmentTmpl    = `attachment; filename="%s"`
)
View Source
const (
	BestCompression    = gzip.BestCompression
	BestSpeed          = gzip.BestSpeed
	DefaultCompression = gzip.DefaultCompression
	NoCompression      = gzip.NoCompression
)
View Source
const (
	CacheControlNoCache = "no-cache"
)
View Source
const (
	ConnectionKeepAlive = "keep-alive"
)
View Source
const (
	TransferEncodingChunked = "chunked"
)

Variables

View Source
var (
	DefaultExcludedExtensions = NewExcludedExtensions([]string{
		".png", ".gif", ".jpeg", ".jpg",
	})
	DefaultGzipOptions = &GzipOptions{
		ExcludedExtensions: DefaultExcludedExtensions,
	}
)

Functions

func BasicAuth

func BasicAuth(username, password string) string

BasicAuth returns the result.

func CheckFileSize added in v1.7.10

func CheckFileSize(f multipart.File, uploadMaxSize int) bool

CheckFileSize reports whether the condition holds.

func CopyHttpHeader

func CopyHttpHeader(dst, src http.Header)

CopyHttpHeader performs the operation.

func Director added in v1.7.6

func Director() *httputil.ReverseProxy

Director returns the result.

func DirectorServer added in v1.7.6

func DirectorServer(addr string) error

DirectorServer performs the operation.

func FormatAttachment added in v1.11.2

func FormatAttachment(filename string) string

FormatAttachment formats or converts the value.

func FormatContentDisposition

func FormatContentDisposition(filename string) string

FormatContentDisposition formats or converts the value.

func FormatContentRange

func FormatContentRange(start, end, total int64) string

FormatContentRange formats or converts the value.

func FormatFormDataField added in v1.11.2

func FormatFormDataField(name string) string

FormatFormDataField formats or converts the value.

func FormatFormDataFile added in v1.11.2

func FormatFormDataFile(name, filename string) string

FormatFormDataFile formats or converts the value.

func FormatRange

func FormatRange(start, end int64) string

FormatRange formats or converts the value.

func GetContentLength

func GetContentLength(header http.Header) int64

GetContentLength returns the value.

func GetFileExt added in v1.7.10

func GetFileExt(file *multipart.FileHeader) (string, error)

GetFileExt returns the value.

func GetFileSize added in v1.7.10

func GetFileSize(f multipart.File) int

GetFileSize returns the value.

func GetToken

func GetToken(header http.Header) string

GetToken returns the value.

func GzipBody added in v1.7.8

func GzipBody(r *http.Request) (io.ReadCloser, error)

GzipBody performs the operation.

func HandleDebug added in v1.7.8

func HandleDebug(mux *http.ServeMux, prefix string)

HandleDebug executes the operation.

func HandleStack added in v1.7.8

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

HandleStack executes the operation.

func HeaderIntoHttpHeader

func HeaderIntoHttpHeader(header Header, httpHeader http.Header)

HeaderIntoHttpHeader performs the operation.

func HttpHeaderIntoHeader added in v1.3.9

func HttpHeaderIntoHeader(httpHeader http.Header, header Header)

HttpHeaderIntoHeader performs the operation.

func NewGzipHandler added in v1.7.8

func NewGzipHandler(level int, options *GzipOptions) *gzipHandler

NewGzipHandler creates and returns a new instance.

func ParseContentDisposition

func ParseContentDisposition(header string) (string, error)

ParseContentDisposition parses the input.

func ParseContentRange

func ParseContentRange(rangeHeader string) (start int64, end int64, total int64, err error)

ParseContentRange parses the input.

func ParseDisposition

func ParseDisposition(disposition string) (mediatype string, params map[string]string, err error)

ParseDisposition parses the input.

func ParseRange

func ParseRange(header string) (int64, int64, error)

ParseRange parses the input.

func Rewrite added in v1.7.6

func Rewrite() *httputil.ReverseProxy

Rewrite returns the result.

func RewriteServer added in v1.7.6

func RewriteServer(addr string) error

RewriteServer performs the operation.

func SetBasicAuth

func SetBasicAuth(header http.Header, username, password string)

SetBasicAuth updates or inserts a value.

func Tunneling added in v1.7.6

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

Tunneling performs the operation.

func Upload added in v1.7.6

func Upload(dir string) http.HandlerFunc

Upload returns the result.

func UseMiddleware added in v1.3.10

func UseMiddleware(handler http.Handler, middlewares ...Middleware) http.Handler

UseMiddleware returns the result.

Types

type ExcludedExtensions added in v1.7.8

type ExcludedExtensions map[string]bool

Using map for better lookup performance

func NewExcludedExtensions added in v1.7.8

func NewExcludedExtensions(extensions []string) ExcludedExtensions

NewExcludedExtensions creates and returns a new instance.

func (ExcludedExtensions) Contains added in v1.7.8

func (e ExcludedExtensions) Contains(target string) bool

Contains reports whether the condition holds.

type ExcludedPaths added in v1.7.8

type ExcludedPaths []string

func NewExcludedPaths added in v1.7.8

func NewExcludedPaths(paths []string) ExcludedPaths

NewExcludedPaths creates and returns a new instance.

func (ExcludedPaths) Contains added in v1.7.8

func (e ExcludedPaths) Contains(requestURI string) bool

Contains reports whether the condition holds.

type ExcludedPathsRegex added in v1.7.8

type ExcludedPathsRegex []*regexp.Regexp

func NewExcludedPathsRegex added in v1.7.8

func NewExcludedPathsRegex(regexes []string) ExcludedPathsRegex

NewExcludedPathsRegex creates and returns a new instance.

func (ExcludedPathsRegex) Contains added in v1.7.8

func (e ExcludedPathsRegex) Contains(requestURI string) bool

Contains reports whether the condition holds.

type File added in v1.7.10

type File struct {
	http.File
}

type FileInfo added in v1.7.10

type FileInfo struct {
	Body io.ReadCloser
	// contains filtered or unexported fields
}

func FetchFile added in v1.7.10

func FetchFile(url string, options ...func(r *http.Request)) (*FileInfo, error)

FetchFile performs the operation.

func FetchFileByRequest added in v1.7.10

func FetchFileByRequest(r *http.Request) (*FileInfo, error)

FetchFileByRequest performs the operation.

func (*FileInfo) IsDir added in v1.7.10

func (f *FileInfo) IsDir() bool

IsDir reports whether the condition holds.

func (*FileInfo) ModTime added in v1.7.10

func (f *FileInfo) ModTime() time.Time

ModTime returns the result.

func (*FileInfo) Mode added in v1.7.10

func (f *FileInfo) Mode() fs.FileMode

Mode returns the result.

func (*FileInfo) Name added in v1.7.10

func (f *FileInfo) Name() string

Name returns the result.

func (*FileInfo) Size added in v1.7.10

func (f *FileInfo) Size() int64

Size returns the number of elements.

func (*FileInfo) Sys added in v1.7.10

func (f *FileInfo) Sys() any

Sys returns the result.

type FileWatchInfo added in v1.7.10

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

func (*FileWatchInfo) Do added in v1.7.10

func (c *FileWatchInfo) Do()

Do executes the operation.

type FileWatchInfos added in v1.7.10

type FileWatchInfos map[string]*FileWatchInfo

type FileWatcher added in v1.7.10

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

func NewFileWatcher added in v1.7.10

func NewFileWatcher(interval time.Duration) *FileWatcher

NewFileWatcher creates and returns a new instance.

func (*FileWatcher) Add added in v1.7.10

func (w *FileWatcher) Add(url string, callback func(file *FileInfo), opts ...func(r *http.Request)) error

Add updates or inserts a value.

func (*FileWatcher) Close added in v1.7.10

func (w *FileWatcher) Close()

Close closes and releases resources.

func (*FileWatcher) Remove added in v1.7.10

func (w *FileWatcher) Remove(url string) error

Remove removes or resets state.

func (*FileWatcher) Update added in v1.7.10

func (w *FileWatcher) Update(interval time.Duration)

Update updates or inserts a value.

type Fs added in v1.7.10

type Fs = http.FileSystem

type GzipOptions added in v1.7.8

type GzipOptions struct {
	ExcludedExtensions ExcludedExtensions
	ExcludedPaths      ExcludedPaths
	ExcludedPathsRegex ExcludedPathsRegex
	Handler            http.HandlerFunc
}
type Header interface {
	Add(key, value string)
	Set(key, value string)
	Get(key string) string
	Values(key string) []string
	Range(func(key, value string))
}

type HttpHeader

type HttpHeader http.Header

func (HttpHeader) Add

func (h HttpHeader) Add(k, v string)

Add updates or inserts a value.

func (HttpHeader) Get

func (h HttpHeader) Get(k string) string

Get returns the value.

func (HttpHeader) IntoHttpHeader

func (h HttpHeader) IntoHttpHeader(header http.Header)

IntoHttpHeader performs the operation.

func (HttpHeader) Range

func (h HttpHeader) Range(f func(key, value string))

Range performs the operation.

func (HttpHeader) Set

func (h HttpHeader) Set(k, v string)

Set updates or inserts a value.

func (HttpHeader) ToHttpHeader added in v1.3.13

func (h HttpHeader) ToHttpHeader() http.Header

ToHttpHeader converts the value.

func (HttpHeader) Values

func (h HttpHeader) Values(k string) []string

Values returns the result.

type IntoHttpHeader

type IntoHttpHeader interface {
	IntoHttpHeader(header http.Header)
}

type MapHeader

type MapHeader map[string]string

func (MapHeader) Add

func (h MapHeader) Add(k, v string)

Add updates or inserts a value.

func (MapHeader) Get

func (h MapHeader) Get(k string) string

Get returns the value.

func (MapHeader) IntoHttpHeader

func (h MapHeader) IntoHttpHeader(header http.Header)

IntoHttpHeader performs the operation.

func (MapHeader) Range

func (h MapHeader) Range(f func(key, value string))

Range performs the operation.

func (MapHeader) Set

func (h MapHeader) Set(k, v string)

Set updates or inserts a value.

func (MapHeader) ToHttpHeader added in v1.3.13

func (h MapHeader) ToHttpHeader() http.Header

ToHttpHeader converts the value.

func (MapHeader) Values

func (h MapHeader) Values(k string) []string

Values returns the result.

type Middleware added in v1.3.10

type Middleware func(http.Handler) http.Handler

type MiddlewareContext added in v1.3.11

type MiddlewareContext struct {
	Request        *http.Request
	ResponseWriter http.ResponseWriter
	// contains filtered or unexported fields
}

func NewMiddlewareContext added in v1.3.11

func NewMiddlewareContext(handler http.Handler, handlers ...MiddlewareContextHandler) *MiddlewareContext

NewMiddlewareContext creates and returns a new instance.

func (*MiddlewareContext) Next added in v1.3.11

func (m *MiddlewareContext) Next()

Next performs the operation.

func (*MiddlewareContext) ServeHTTP added in v1.3.11

func (m *MiddlewareContext) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP executes the operation.

func (*MiddlewareContext) Use added in v1.3.11

Use performs the operation.

type MiddlewareContextHandler added in v1.3.19

type MiddlewareContextHandler func(*MiddlewareContext)

type Record added in v1.7.2

type Record struct {
	ContentType string
	Body        *bytes.Buffer
	Raw         []byte
	Value       any
}

func (*Record) RecordBody added in v1.7.7

func (rw *Record) RecordBody(raw []byte, v any)

RecordBody performs the operation.

type RecordBodyer added in v1.11.0

type RecordBodyer interface {
	RecordBody(raw []byte, v any)
}

type Recorder added in v1.7.2

type Recorder struct {
	RequestRecorder
	ResponseRecorder
}

func NewRecorder

func NewRecorder(w http.ResponseWriter, r *http.Request) *Recorder

NewRecorder returns an initialized Recorder.

func (*Recorder) Reset added in v1.7.2

func (rw *Recorder) Reset()

Reset removes or resets state.

type RequestRecorder added in v1.7.2

type RequestRecorder struct {
	Record
	// contains filtered or unexported fields
}

func (*RequestRecorder) Close added in v1.7.7

func (rw *RequestRecorder) Close() error

Close closes and releases resources.

func (*RequestRecorder) Read added in v1.7.7

func (rw *RequestRecorder) Read(b []byte) (int, error)

Read performs the operation.

type ResponseRecorder

type ResponseRecorder struct {
	Record

	StatusCode int
	// contains filtered or unexported fields
}

func (*ResponseRecorder) Flush

func (rw *ResponseRecorder) Flush()

Flush implements http.Flusher. To test whether Flush was called, see rw.Flushed.

func (*ResponseRecorder) Header

func (rw *ResponseRecorder) Header() http.Header

Header returns the result.

func (*ResponseRecorder) Write

func (rw *ResponseRecorder) Write(buf []byte) (int, error)

Write performs the operation.

func (*ResponseRecorder) WriteHeader

func (rw *ResponseRecorder) WriteHeader(statusCode int)

WriteHeader implements http.ResponseWriter.

type SliceHeader

type SliceHeader []string

func NewHeader

func NewHeader() *SliceHeader

NewHeader creates and returns a new instance.

func (*SliceHeader) Add

func (h *SliceHeader) Add(k, v string)

Add updates or inserts a value.

func (SliceHeader) Clone

func (h SliceHeader) Clone() SliceHeader

Clone returns the result.

func (*SliceHeader) Get

func (h *SliceHeader) Get(k string) string

Get returns the value.

func (SliceHeader) IntoHttpHeader

func (h SliceHeader) IntoHttpHeader(header http.Header)

IntoHttpHeader performs the operation.

func (*SliceHeader) Range

func (h *SliceHeader) Range(f func(key, value string))

Range performs the operation.

func (*SliceHeader) Set

func (h *SliceHeader) Set(k, v string)

Set updates or inserts a value.

func (SliceHeader) ToHttpHeader added in v1.3.13

func (h SliceHeader) ToHttpHeader() http.Header

ToHttpHeader converts the value.

func (*SliceHeader) Values

func (h *SliceHeader) Values(k string) []string

Values returns the result.

type Unwrapper added in v1.7.2

type Unwrapper interface {
	Unwrap() http.ResponseWriter
}

Directories

Path Synopsis
v2

Jump to

Keyboard shortcuts

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