http1

package
v0.0.0-...-44a8419 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const MaxHeaderSize = 32 << 10 // 32 KiB

MaxHeaderSize is the maximum size of a header in bytes

Variables

View Source
var (
	ErrIncompleteHeader = errors.New("incomplete header")
)

Functions

func RunParserTest

func RunParserTest(t *testing.T, tc ParserTestCase)

RunParserTest runs a single parser test case

func RunParserTestTable

func RunParserTestTable(t *testing.T, cases []ParserTestCase)

RunParserTestTable runs a table of parser test cases

Types

type BodyCallback

type BodyCallback func([]byte, bool)

BodyCallback is a function type for handling body data

type Callbacks

type Callbacks interface {
	OnRequest(*Request, bool)   // request, noBody
	OnRequestBody([]byte, bool) // chunk data, isComplete
	OnInterimResponse(*Response)
	OnResponse(*Response, bool)  // response, noBody
	OnResponseBody([]byte, bool) // chunk data, isComplete
	OnError(error)
	OnDone() // called when transaction is complete
}

Callbacks interface for parsed HTTP messages

type ChunkedDataSender

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

ChunkedDataSender helps test partial data transmission scenarios

func NewChunkedDataSender

func NewChunkedDataSender(t *testing.T, parser *Parser) *ChunkedDataSender

NewChunkedDataSender creates a helper for sending data in chunks

func (*ChunkedDataSender) SendByLines

func (c *ChunkedDataSender) SendByLines(phase Phase, data []byte, delay time.Duration) error

SendByLines sends data line by line with optional delay

func (*ChunkedDataSender) SendInChunks

func (c *ChunkedDataSender) SendInChunks(phase Phase, data []byte, chunkSize int, delay time.Duration) error

SendInChunks sends data in specified chunk sizes with optional delay

type ErrorCallback

type ErrorCallback func(error)

ErrorCallback is a function type for reporting errors

type Event

type Event struct {
	Type     EventType
	Request  *Request
	Response *Response
	Body     []byte
	Complete bool
	NoBody   bool
	Error    error
}

Event represents a callback event for testing

type EventCollector

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

EventCollector helps collect and analyze events

func NewEventCollector

func NewEventCollector(t *testing.T, recorder *TestCallbackRecorder) *EventCollector

NewEventCollector creates a new event collector

func (*EventCollector) AssertEventSequence

func (c *EventCollector) AssertEventSequence(expected []EventType)

AssertEventSequence verifies events occurred in expected order

func (*EventCollector) CollectFor

func (c *EventCollector) CollectFor(duration time.Duration)

CollectFor collects events for a specified duration

func (*EventCollector) GetEventsByType

func (c *EventCollector) GetEventsByType(eventType EventType) []Event

GetEventsByType returns all events of a specific type

func (*EventCollector) WaitForEventType

func (c *EventCollector) WaitForEventType(eventType EventType, timeout time.Duration) ([]Event, bool)

WaitForEventType waits for a specific event type and returns all collected events

type EventType

type EventType int

Event types for testing

const (
	EventRequest EventType = iota
	EventRequestBody
	EventInterimResponse
	EventResponse
	EventResponseBody
	EventError
	EventDone
)

func (EventType) String

func (e EventType) String() string

type HTTPStream

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

HTTPStream manages the read/write & open/close events for an http req/res connection stream based on socket events.

func NewHTTPStream

func NewHTTPStream(ctx context.Context, domain string, logger *zap.Logger, conn *connection.Connection, opts ...HTTPStreamOpt) *HTTPStream

func (*HTTPStream) Close

func (t *HTTPStream) Close()

func (*HTTPStream) Closed

func (t *HTTPStream) Closed() bool

func (*HTTPStream) Process

func (t *HTTPStream) Process(event *connection.DataEvent) error

type HTTPStreamOpt

type HTTPStreamOpt func(*HTTPStream)

func SetPluginManager

func SetPluginManager(manager *plugins.Manager) HTTPStreamOpt

type Parser

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

Parser handles a single HTTP/1.1 transaction

func NewParser

func NewParser(ctx context.Context, callbacks Callbacks) *Parser

NewParser creates a new HTTP/1.1 parser for a single transaction

func (*Parser) Close

func (p *Parser) Close() error

Close cleans up resources and stops processor goroutines

func (*Parser) IsComplete

func (p *Parser) IsComplete() bool

IsComplete returns true if the transaction is complete

func (*Parser) ProcessEvent

func (p *Parser) ProcessEvent(phase Phase, data []byte) error

ProcessEvent handles incoming data events by writing them to the appropriate pipe

func (*Parser) WaitForCompletion

func (p *Parser) WaitForCompletion() error

WaitForCompletion blocks until the transaction is complete or context is cancelled

type ParserTestCase

type ParserTestCase struct {
	Name           string
	RequestData    []byte
	ResponseData   []byte
	ExpectedEvents []EventType
	Validate       func(t *testing.T, state TestState)
	CloseAfterData bool // Whether to close parser after sending data
}

ParserTestCase defines a test case for parser testing

type Phase

type Phase int

Phase indicates whether data is from request or response

const (
	PhaseRequest Phase = iota
	PhaseResponse
)

func (Phase) String

func (p Phase) String() string

type Request

type Request struct {
	Method     string
	RequestURI string
	Proto      string
	Host       string
	URL        *url.URL
	Headers    http.Header
}

Request represents a parsed HTTP request

func ReadRequest

func ReadRequest(r io.Reader) (*Request, error)

type Response

type Response struct {
	StatusCode int
	Status     string
	Proto      string
	Headers    http.Header
	IsInterim  bool // true for 1xx responses
}

Response represents a parsed HTTP response

func ReadResponse

func ReadResponse(r io.Reader) (*Response, error)

type Session

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

func NewSession

func NewSession(ctx context.Context, logger *zap.Logger, domain string, conn *connection.Connection, pluginManager *plugins.Manager) *Session

func (*Session) Close

func (s *Session) Close() error

func (*Session) Closed

func (s *Session) Closed() bool

func (*Session) OnDone

func (s *Session) OnDone()

OnDone handles transaction completion

func (*Session) OnError

func (s *Session) OnError(err error)

OnError handles HTTP parsing error callbacks

func (*Session) OnInterimResponse

func (s *Session) OnInterimResponse(resp *Response)

OnInterimResponse handles HTTP interim response callbacks (1xx responses)

func (*Session) OnRequest

func (s *Session) OnRequest(req *Request, noBody bool)

OnRequest handles HTTP request callbacks

func (*Session) OnRequestBody

func (s *Session) OnRequestBody(chunk []byte, isComplete bool)

OnRequestBody handles HTTP request body chunks

func (*Session) OnResponse

func (s *Session) OnResponse(resp *Response, noBody bool)

OnResponse handles HTTP response callbacks

func (*Session) OnResponseBody

func (s *Session) OnResponseBody(chunk []byte, isComplete bool)

OnResponseBody handles HTTP response body chunks

func (*Session) Write

func (s *Session) Write(phase Phase, data []byte) error

type TestCallbackOption

type TestCallbackOption func(*TestCallbackRecorder)

TestCallbackOption configures the TestCallbackRecorder

func WithBufferSize

func WithBufferSize(size int) TestCallbackOption

WithBufferSize sets the event channel buffer size

func WithTimeout

func WithTimeout(d time.Duration) TestCallbackOption

WithTimeout sets the default timeout for waiting operations

type TestCallbackRecorder

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

TestCallbackRecorder captures parser events with channel-based synchronization

func NewTestCallbackRecorder

func NewTestCallbackRecorder(opts ...TestCallbackOption) *TestCallbackRecorder

NewTestCallbackRecorder creates a new test callback recorder

func (*TestCallbackRecorder) CollectEvents

func (r *TestCallbackRecorder) CollectEvents(stopOnDone bool) []Event

CollectEvents collects all events until timeout or done

func (*TestCallbackRecorder) GetState

func (r *TestCallbackRecorder) GetState() TestState

GetState returns the current accumulated state (thread-safe)

func (*TestCallbackRecorder) OnDone

func (r *TestCallbackRecorder) OnDone()

OnDone implements Callbacks

func (*TestCallbackRecorder) OnError

func (r *TestCallbackRecorder) OnError(err error)

OnError implements Callbacks

func (*TestCallbackRecorder) OnInterimResponse

func (r *TestCallbackRecorder) OnInterimResponse(resp *Response)

OnInterimResponse implements Callbacks

func (*TestCallbackRecorder) OnRequest

func (r *TestCallbackRecorder) OnRequest(req *Request, noBody bool)

OnRequest implements Callbacks

func (*TestCallbackRecorder) OnRequestBody

func (r *TestCallbackRecorder) OnRequestBody(data []byte, complete bool)

OnRequestBody implements Callbacks

func (*TestCallbackRecorder) OnResponse

func (r *TestCallbackRecorder) OnResponse(resp *Response, noBody bool)

OnResponse implements Callbacks

func (*TestCallbackRecorder) OnResponseBody

func (r *TestCallbackRecorder) OnResponseBody(data []byte, complete bool)

OnResponseBody implements Callbacks

func (*TestCallbackRecorder) WaitForCompletion

func (r *TestCallbackRecorder) WaitForCompletion() error

WaitForCompletion waits for the OnDone callback or timeout

func (*TestCallbackRecorder) WaitForEvent

func (r *TestCallbackRecorder) WaitForEvent(eventType EventType) (*Event, error)

WaitForEvent waits for a specific event type with timeout

func (*TestCallbackRecorder) WaitForEvents

func (r *TestCallbackRecorder) WaitForEvents(eventTypes ...EventType) ([]*Event, error)

WaitForEvents waits for multiple events in order

func (*TestCallbackRecorder) WaitForEventsFlexible

func (r *TestCallbackRecorder) WaitForEventsFlexible(timeout time.Duration, eventTypes ...EventType) ([]*Event, error)

WaitForEventsFlexible waits for multiple events but allows them to arrive in any order

type TestState

type TestState struct {
	Request          *Request
	RequestBody      []byte
	RequestComplete  bool
	InterimResponses []*Response
	Response         *Response
	ResponseBody     []byte
	ResponseComplete bool
	Errors           []error
	DoneCalled       bool
}

TestState represents the accumulated parser state

type TransactionState

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

TransactionState coordinates between request and response processors

Jump to

Keyboard shortcuts

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