Documentation
¶
Overview ¶
Package h1 implements a zero-copy HTTP/1.1 parser.
Index ¶
- Constants
- Variables
- func LowerInPlace(b []byte)
- func UnsafeLowerHeader(raw []byte) string
- func UnsafeString(b []byte) string
- type Parser
- func (p *Parser) ConsumeBody(n int)
- func (p *Parser) GetBody(contentLength int64) []byte
- func (p *Parser) ParseChunkedBody() ([]byte, int, error)
- func (p *Parser) ParseRequest(req *Request) (int, error)
- func (p *Parser) Remaining() int
- func (p *Parser) Reset(buf []byte)
- func (p *Parser) SetZeroCopy(enabled bool)
- type Request
Constants ¶
const MaxHeaderSize = 16 << 20 // 16MB
MaxHeaderSize is the maximum allowed size of HTTP headers in bytes.
Variables ¶
var ( ErrBufferExhausted = errors.New("buffer exhausted") ErrInvalidRequestLine = errors.New("invalid request line") ErrInvalidHeader = errors.New("invalid header line") ErrMissingHost = errors.New("missing Host header") ErrUnsupportedVersion = errors.New("unsupported HTTP version") ErrHeadersTooLarge = errors.New("headers too large") ErrInvalidContentLength = errors.New("invalid content-length") ErrDuplicateContentLength = errors.New("duplicate content-length with conflicting values") )
H1 parser sentinel errors.
Functions ¶
func LowerInPlace ¶ added in v1.1.0
func LowerInPlace(b []byte)
LowerInPlace lowercases ASCII bytes in-place.
func UnsafeLowerHeader ¶ added in v1.1.0
UnsafeLowerHeader returns an interned string for common HTTP header names, or lowercases the name in-place and returns a zero-copy unsafe string. The caller must ensure the byte slice outlives the returned string.
func UnsafeString ¶ added in v1.1.0
UnsafeString returns a string that shares the underlying byte slice memory. The caller must ensure the byte slice is not modified while the string is in use.
Types ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a zero-allocation HTTP/1.x request parser.
func (*Parser) ConsumeBody ¶
ConsumeBody advances the parser position by n bytes.
func (*Parser) GetBody ¶
GetBody returns a zero-copy slice of the request body for the given content length.
func (*Parser) ParseChunkedBody ¶
ParseChunkedBody parses a single chunk from chunked transfer encoding. Returns a zero-copy slice into the parser buffer, bytes consumed, or error. Returns (nil, 0, nil) when more data is needed.
func (*Parser) ParseRequest ¶
ParseRequest parses a complete HTTP/1.x request from the buffer into req.
func (*Parser) SetZeroCopy ¶ added in v1.1.0
SetZeroCopy enables zero-copy header parsing. When enabled, the parser only populates RawHeaders (not Headers). The caller must convert RawHeaders to strings — typically via unsafe.String for zero allocation.
type Request ¶
type Request struct {
Method string
Path string
Version string
Headers [][2]string
RawHeaders [][2][]byte
Host string
ContentLength int64
ChunkedEncoding bool
KeepAlive bool
HeadersComplete bool
BodyRead int64
ExpectContinue bool
}
Request holds the parsed components of an HTTP/1.x request.