http

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultProtocolCacheTime = time.Second * 300

Variables

View Source
var (
	ErrContentRangeHeaderNotFound    = errors.New("Content-Range header not found")
	ErrContentRangeInvalidFormat     = errors.New("Content-Range invalid format")
	ErrContentRangeInvalidStartValue = errors.New("Content-Range invalid start value")
	ErrContentRangeInvalidEndValue   = errors.New("Content-Range invalid end value")
	ErrContentRangeInvalidTotalValue = errors.New("Content-Range invalid total value")
	ErrRangeHeaderNotFound           = errors.New("header Range not found")
	ErrRangeHeaderInvalidFormat      = errors.New("header Range invalid format")
	ErrRangeHeaderUnsatisfiable      = errors.New("header Range unsatisfiable")
)
View Source
var (
	Parse       = parser.Parse
	SingleRange = parser.SingleParse
)

Functions

func BuildHeaderRange

func BuildHeaderRange(start, end, totalSize uint64) string

BuildRange builds the Range header value.

func ClientIP

func ClientIP(remoteAddr string, header http.Header) string

func CopyHeader

func CopyHeader(dst, src http.Header)

CopyHeader copies all headers from the source http.Header to the destination http.Header. It iterates over each header key-value pair in the source and adds them to the destination.

func CopyHeadersWithout

func CopyHeadersWithout(dst, src http.Header, excludeKeys ...string)

CopyHeadersWithout copies all headers from the source http.Header to the destination http.Header, excluding the headers specified in excludeKeys. It creates a map of excluded keys for efficient lookup and skips those keys during the copy process.

- dst: The destination http.Header where the headers will be copied to. - src: The source http.Header from which the headers will be copied. - excludeKeys: A variadic list of header keys to be excluded from copying.

Example usage:

src := http.Header{
    "Content-Type": {"application/json"},
    "Content-Length": {"123"},
    "Authorization": {"Bearer token"},
}
dst := http.Header{}
CopyHeadersWithout(dst, src, "Authorization", "Content-Length")
// dst will now contain only "Content-Type": {"application/json"}

func CopyTrailer

func CopyTrailer(dst, src http.Header)

CopyTrailer copies all headers from the source http.Header to the destination http.Header, prefixing each header key with the http.TrailerPrefix. This function is useful for handling HTTP trailers, which are headers sent after the body of an HTTP message.

see https://pkg.go.dev/net/http#example-ResponseWriter-Trailers

- dst: The destination http.Header where the headers will be copied to. - src: The source http.Header from which the headers will be copied.

Example usage:

src := http.Header{
    "Example-Key": {"Example-Value"},
}
dst := http.Header{}
CopyTrailer(dst, src)
// dst will now contain "Trailer-Example-Key": {"Example-Value"}

func IsChunked

func IsChunked(h http.Header) bool

IsChunked checks if the Transfer-Encoding header is chunked.

see https://www.rfc-editor.org/rfc/rfc9112.html#name-chunked-transfer-coding

func NewBizError

func NewBizError(code int, headers http.Header) error

func ParseCacheTime

func ParseCacheTime(withKey string, src http.Header) (time.Duration, bool)

ParseCacheTime parses cache time from HTTP headers.

If withKey is empty, it will parse from standard Cache-Control and Expires headers. If withKey is provided, it will look for that specific header key to determine cache time.

It returns the parsed cache duration and a boolean indicating whether caching is allowed.

func PrintRoutes

func PrintRoutes(mux *http.ServeMux)

func RemoveHopByHopHeaders

func RemoveHopByHopHeaders(h http.Header)

RemoveHopByHopHeaders removes hop-by-hop headers.

func ResponseHeaderSize

func ResponseHeaderSize(code int, hdr http.Header) uint64

func Scheme

func Scheme(r *http.Request) string

func WithTracer

func WithTracer(req *http.Request) *http.Request

Types

type BizError

type BizError interface {
	error

	Code() int
	Headers() http.Header
}

func ParseBizError

func ParseBizError(err error) (BizError, bool)

type ContentRange

type ContentRange struct {
	Start   int64
	Length  int64
	ObjSize uint64
}

func ParseContentRange

func ParseContentRange(header http.Header) (ContentRange, error)

ParseContentRange parses the Content-Range header from an HTTP response.

func (*ContentRange) String

func (cr *ContentRange) String() string

type Parser

type Parser interface {
	Parse(header string, size uint64) ([]*Range, error)
	SingleParse(header string, size uint64) (*Range, error)
}

func NewUnsatisfiableParser

func NewUnsatisfiableParser() Parser

type Range

type Range struct {
	Start, End int64
}

func NewRequestRange

func NewRequestRange(start, end int64) *Range

func UnsatisfiableMultiRange

func UnsatisfiableMultiRange(header string) ([]*Range, error)

UnsatisfiableMultiRange ...

func (*Range) ContentRange

func (r *Range) ContentRange(size uint64) string

func (*Range) Length

func (r *Range) Length() int64

func (*Range) MimeHeader

func (r *Range) MimeHeader(contentType string, size uint64) textproto.MIMEHeader

func (*Range) RangeLength

func (r *Range) RangeLength(totalSize int64) int64

func (*Range) String

func (r *Range) String() string

type ResponseRecorder

type ResponseRecorder struct {
	http.Hijacker
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseRecorder

func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder

func (*ResponseRecorder) Hijack

func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker.

func (*ResponseRecorder) SentBytes

func (r *ResponseRecorder) SentBytes() uint64

func (*ResponseRecorder) Size

func (r *ResponseRecorder) Size() uint64

func (*ResponseRecorder) Status

func (r *ResponseRecorder) Status() int

func (*ResponseRecorder) Write

func (r *ResponseRecorder) Write(b []byte) (n int, err error)

func (*ResponseRecorder) WriteHeader

func (r *ResponseRecorder) WriteHeader(s int)

Directories

Path Synopsis
Package cachecontrol provides HTTP Cache-Control header parsing with some utility functions to quickly deal with directives values.
Package cachecontrol provides HTTP Cache-Control header parsing with some utility functions to quickly deal with directives values.

Jump to

Keyboard shortcuts

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