h1

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package h1 implements a zero-copy HTTP/1.1 parser.

Index

Constants

View Source
const MaxHeaderSize = 16 << 20 // 16MB

MaxHeaderSize is the maximum allowed size of HTTP headers in bytes.

Variables

View Source
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")
)

H1 parser sentinel errors.

Functions

This section is empty.

Types

type Parser

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

Parser is a zero-allocation HTTP/1.x request parser.

func NewParser

func NewParser() *Parser

NewParser returns a new Parser ready for use.

func (*Parser) ConsumeBody

func (p *Parser) ConsumeBody(n int)

ConsumeBody advances the parser position by n bytes.

func (*Parser) GetBody

func (p *Parser) GetBody(contentLength int64) []byte

GetBody returns a zero-copy slice of the request body for the given content length.

func (*Parser) ParseChunkedBody

func (p *Parser) ParseChunkedBody() ([]byte, int, error)

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

func (p *Parser) ParseRequest(req *Request) (int, error)

ParseRequest parses a complete HTTP/1.x request from the buffer into req.

func (*Parser) Remaining

func (p *Parser) Remaining() int

Remaining returns the number of unconsumed bytes in the buffer.

func (*Parser) Reset

func (p *Parser) Reset(buf []byte)

Reset reinitializes the parser with a new input buffer.

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
}

Request holds the parsed components of an HTTP/1.x request.

func (*Request) Reset

func (r *Request) Reset()

Reset clears all fields, reusing existing header slice capacity.

Jump to

Keyboard shortcuts

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