frame

package
v1.2.4 Latest Latest
Warning

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

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

Documentation

Overview

Package frame provides HTTP/2 frame type definitions, parsing, writing, and HPACK integration.

Index

Constants

View Source
const ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"

ClientPreface is the HTTP/2 client connection preface string.

Variables

This section is empty.

Functions

func ValidateClientPreface

func ValidateClientPreface(buf []byte) bool

ValidateClientPreface checks whether buf begins with a valid HTTP/2 client preface.

Types

type Flags

type Flags uint8

Flags represents HTTP/2 frame flags.

const (
	FlagAck        Flags = 0x1
	FlagEndStream  Flags = 0x1
	FlagEndHeaders Flags = 0x4
	FlagPadded     Flags = 0x8
	FlagPriority   Flags = 0x20
)

HTTP/2 frame flag constants as defined in RFC 7540 Section 6.

type Frame

type Frame struct {
	Type     Type
	Flags    Flags
	StreamID uint32
	Payload  []byte
}

Frame represents a generic HTTP/2 frame.

type HeaderDecoder

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

HeaderDecoder decodes HTTP headers using HPACK.

func NewHeaderDecoder

func NewHeaderDecoder(maxSize uint32) *HeaderDecoder

NewHeaderDecoder creates a new header decoder.

func (*HeaderDecoder) Decode

func (d *HeaderDecoder) Decode(data []byte) ([][2]string, error)

Decode decodes HPACK-encoded headers.

type HeaderEncoder

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

HeaderEncoder encodes HTTP headers using HPACK.

func NewHeaderEncoder

func NewHeaderEncoder() *HeaderEncoder

NewHeaderEncoder creates a new header encoder.

func (*HeaderEncoder) Close

func (e *HeaderEncoder) Close()

Close releases internal resources back to the pool.

func (*HeaderEncoder) Encode

func (e *HeaderEncoder) Encode(headers [][2]string) ([]byte, error)

Encode encodes headers to HPACK format. Returns a copy safe for concurrent use.

func (*HeaderEncoder) EncodeBorrow

func (e *HeaderEncoder) EncodeBorrow(headers [][2]string) ([]byte, error)

EncodeBorrow encodes headers and returns a byte slice backed by the encoder's internal buffer. The returned slice is only valid until the next call to Encode/EncodeBorrow or Close.

type Parser

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

Parser handles HTTP/2 frame parsing.

func NewParser

func NewParser() *Parser

NewParser creates a new frame parser.

func (*Parser) InitReader

func (p *Parser) InitReader(r io.Reader)

InitReader binds the parser to a persistent reader. This allows the framer to preserve CONTINUATION expectations across frames and read progressively as more data arrives.

func (*Parser) Parse

func (p *Parser) Parse(r io.Reader) (*Frame, error)

Parse reads and parses an HTTP/2 frame from the reader.

func (*Parser) ReadNextFrame

func (p *Parser) ReadNextFrame() (http2.Frame, error)

ReadNextFrame reads the next frame using the bound reader.

type RawFrame added in v1.1.0

type RawFrame struct {
	Type     byte
	Flags    byte
	StreamID uint32
	Length   uint32
	Payload  []byte
}

RawFrame is a zero-copy frame representation. Payload is a slice into the caller's buffer and is only valid until the next buffer modification.

func ReadRawFrame added in v1.1.0

func ReadRawFrame(buf []byte) (RawFrame, int, error)

ReadRawFrame parses a raw frame from buf without allocating. Returns the frame and the total number of bytes consumed (9 + payload length). Returns io.ErrUnexpectedEOF if buf doesn't contain a complete frame.

func (RawFrame) HasEndHeaders added in v1.1.0

func (f RawFrame) HasEndHeaders() bool

HasEndHeaders reports whether the END_HEADERS flag (0x04) is set.

func (RawFrame) HasEndStream added in v1.1.0

func (f RawFrame) HasEndStream() bool

HasEndStream reports whether the END_STREAM flag (0x01) is set.

func (RawFrame) HasPadded added in v1.1.0

func (f RawFrame) HasPadded() bool

HasPadded reports whether the PADDED flag (0x08) is set.

func (RawFrame) HasPriority added in v1.1.0

func (f RawFrame) HasPriority() bool

HasPriority reports whether the PRIORITY flag (0x20) is set (HEADERS frames).

type Type

type Type uint8

Type represents HTTP/2 frame types.

const (
	FrameData         Type = 0x0
	FrameHeaders      Type = 0x1
	FramePriority     Type = 0x2
	FrameRSTStream    Type = 0x3
	FrameSettings     Type = 0x4
	FramePushPromise  Type = 0x5
	FramePing         Type = 0x6
	FrameGoAway       Type = 0x7
	FrameWindowUpdate Type = 0x8
	FrameContinuation Type = 0x9
)

HTTP/2 frame type constants as defined in RFC 7540 Section 6.

func (Type) String

func (t Type) String() string

type Writer

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

Writer handles HTTP/2 frame writing with mutex protection.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new frame writer.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the underlying writer if it implements Close.

func (*Writer) CloseImmediate

func (w *Writer) CloseImmediate() error

CloseImmediate immediately closes the underlying writer.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush flushes any buffered data.

func (*Writer) WriteData

func (w *Writer) WriteData(streamID uint32, endStream bool, data []byte) error

WriteData writes a DATA frame.

func (*Writer) WriteFrame

func (w *Writer) WriteFrame(f *Frame) error

WriteFrame writes a generic frame.

func (*Writer) WriteGoAway

func (w *Writer) WriteGoAway(lastStreamID uint32, code http2.ErrCode, debugData []byte) error

WriteGoAway writes a GOAWAY frame.

func (*Writer) WriteHeaders

func (w *Writer) WriteHeaders(streamID uint32, endStream bool, headerBlock []byte, maxFrameSize uint32) error

WriteHeaders writes HEADERS (and CONTINUATION) frames, fragmenting by maxFrameSize.

func (*Writer) WritePing

func (w *Writer) WritePing(ack bool, data [8]byte) error

WritePing writes a PING frame.

func (*Writer) WritePushPromise

func (w *Writer) WritePushPromise(streamID uint32, promiseID uint32, endHeaders bool, headerBlock []byte) error

WritePushPromise writes a PUSH_PROMISE frame.

func (*Writer) WriteRSTStream

func (w *Writer) WriteRSTStream(streamID uint32, code http2.ErrCode) error

WriteRSTStream writes a RST_STREAM frame.

func (*Writer) WriteSettings

func (w *Writer) WriteSettings(settings ...http2.Setting) error

WriteSettings writes a SETTINGS frame.

func (*Writer) WriteSettingsAck

func (w *Writer) WriteSettingsAck() error

WriteSettingsAck writes a SETTINGS acknowledgment frame.

func (*Writer) WriteWindowUpdate

func (w *Writer) WriteWindowUpdate(streamID uint32, increment uint32) error

WriteWindowUpdate writes a WINDOW_UPDATE frame.

Jump to

Keyboard shortcuts

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