httputil

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 21 Imported by: 60

README

httputil

The package contains various helpers related to http protocol

Documentation

Index

Constants

View Source
const (
	// DefaultBufferSize is the default size of bytes buffer used for response
	// body storage.
	//
	// Use [SetBufferSize] to adjust the size.
	DefaultBufferSize = int64(10000)

	// DefaultMaxBodySize is the default maximum size of HTTP response body to
	// read.
	//
	// Responses larger than this will be truncated.
	DefaultMaxBodySize = 8 * 1024 * 1024 // 8 MB

	// DefaultMaxLargeBuffers is the maximum number of buffers at [maxBodyRead]
	// size that will be kept in the pool.
	//
	// This prevents pool pollution from accumulating many large buffers while
	// still allowing buffer reuse during burst workloads (e.g., nuclei scans
	// with compression bombs). Excess large buffers are discarded and handled
	// by GC.
	//
	// Default of 20 balances memory usage (~160MB max for large buffers) with
	// performance for typical concurrent workloads.
	//
	// Tuning:
	// - Increase for higher concurrency workloads (e.g., 50+ concurrent reqs)
	// - Decrease for memory-constrained environments (min. 10 recommended)
	//
	// Use [SetMaxLargeBuffers] to adjust the size.
	DefaultMaxLargeBuffers = 20
)
View Source
const (
	// DefaultChunkSize defines the default chunk size for reading response
	// bodies.
	//
	// Use [SetChunkSize] to adjust the size.
	DefaultChunkSize = 32 * 1024 // 32KB
)

Variables

View Source
var (
	// DefaultBytesBufferAlloc is the default size of bytes buffer used for
	// response body storage.
	//
	// Deprecated: Use [DefaultBufferSize] instead.
	DefaultBytesBufferAlloc = int64(10000)
)
View Source
var (
	// MaxBodyRead is the maximum size of HTTP response body to read.
	//
	// Responses larger than this will be truncated.
	//
	// Deprecated: Use [DefaultMaxBodySize] instead.
	MaxBodyRead = 4 * 1024 * 1024 // 4 MB
)

Functions

func AllHTTPMethods

func AllHTTPMethods() []string

AllHTTPMethods contains all available HTTP methods

func ChangePoolSize added in v0.0.84

func ChangePoolSize(x int64) error

func DrainResponseBody

func DrainResponseBody(resp *http.Response)

DrainResponseBody drains the response body and closes it.

This reads and discards up to MaxBodyRead bytes to check for any remaining data, then closes the connection. This prevents connection reuse for responses that exceed the expected size (potential DoS).

func DumpRequest

func DumpRequest(req *http.Request) (string, error)

DumpRequest to string

func DumpResponseHeadersAndRaw

func DumpResponseHeadersAndRaw(resp *http.Response) (headers, fullresp []byte, err error)

DumpResponseHeadersAndRaw returns http headers and response as strings

func DumpResponseIntoBuffer added in v0.0.80

func DumpResponseIntoBuffer(resp *http.Response, body bool, buff *bytes.Buffer) (err error)

DumpResponseIntoBuffer dumps a http response without allocating a new buffer for the response body.

func GetPoolSize added in v0.0.84

func GetPoolSize() int64

func SetBufferSize added in v0.7.0

func SetBufferSize(size int64)

SetBufferSize sets the size of bytes buffer used for response body storage.

Changing the size will reset the buffer pool.

If size is less than 1000, it will be set to 1000.

func SetChunkSize added in v0.7.2

func SetChunkSize(size int)

SetChunkSize sets the chunk size for reading response bodies.

If size is less than or equal to zero, it resets to the default chunk size.

func SetMaxLargeBuffers added in v0.7.0

func SetMaxLargeBuffers(max int)

SetMaxLargeBuffers adjusts the maximum number of large buffers that can be pooled.

This should be called before making HTTP requests. Changing the limit will drain existing pooled buffers to ensure clean state.

If max is less than DefaultMaxLargeBuffers, it will be set to DefaultMaxLargeBuffers.

Types

type ChainItem

type ChainItem struct {
	Request    []byte
	Response   []byte
	StatusCode int
	Location   string
	RequestURL string
}

ChainItem request=>response Deprecated: use ResponseChain instead which is more efficient and lazy

func GetChain

func GetChain(r *http.Response) (chain []ChainItem, err error)

GetChain if redirects Deprecated: use ResponseChain instead which is more efficient and lazy

type CookieJar added in v0.6.0

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

CookieJar is a thread-safe wrapper around http.CookieJar

func NewCookieJar added in v0.6.0

func NewCookieJar(opts ...Option) (*CookieJar, error)

New creates a new thread-safe cookie jar with the given options If no jar is provided, creates a simple in-memory cookie jar

func (*CookieJar) Cookies added in v0.6.0

func (cj *CookieJar) Cookies(u *url.URL) []*http.Cookie

Cookies implements http.CookieJar.Cookies

func (*CookieJar) SetCookies added in v0.6.0

func (cj *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)

SetCookies implements http.CookieJar.SetCookies

type Option added in v0.6.0

type Option func(*CookieJar)

Option represents a configuration option for the cookie jar

func WithCookieJar added in v0.6.0

func WithCookieJar(jar http.CookieJar) Option

WithCookieJar sets an existing cookie jar to wrap

type ResponseChain added in v0.0.80

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

ResponseChain is a response chain for a http request on every call to previous it returns the previous response if it was redirected.

func NewResponseChain added in v0.0.80

func NewResponseChain(resp *http.Response, maxBody int64) *ResponseChain

NewResponseChain creates a new response chain for a http request with a maximum body size.

If maxBody is less than or equal to zero, it defaults to DefaultMaxBodySize.

func (*ResponseChain) Body added in v0.0.80

func (r *ResponseChain) Body() *bytes.Buffer

Body returns the current response body buffer in the chain.

Warning: The returned buffer is pooled and must not be modified or retained. Prefer BodyBytes() or BodyString() for safe read-only access.

func (*ResponseChain) BodyBytes added in v0.7.0

func (r *ResponseChain) BodyBytes() []byte

BodyBytes returns the current response body as byte slice in the chain.

The returned slice is valid only until Close() is called.

func (*ResponseChain) BodyString added in v0.7.0

func (r *ResponseChain) BodyString() string

BodyString returns the current response body as string in the chain.

The returned string is a copy and remains valid even after Close() is called.

func (*ResponseChain) Close added in v0.0.80

func (r *ResponseChain) Close()

Close the response chain and releases the buffers.

func (*ResponseChain) Fill added in v0.0.80

func (r *ResponseChain) Fill() error

Fill buffers

func (*ResponseChain) FullResponse added in v0.0.80

func (r *ResponseChain) FullResponse() *bytes.Buffer

FullResponse returns a new buffer containing headers+body.

Warning: The caller is responsible for managing the returned buffer's lifecycle. The buffer should be returned to the pool using putBuffer() or allowed to be garbage collected. Prefer FullResponseBytes() or FullResponseString() for safe read-only access.

func (*ResponseChain) FullResponseBytes added in v0.7.0

func (r *ResponseChain) FullResponseBytes() []byte

FullResponseBytes returns the current response (headers+body) as byte slice.

The returned slice is a copy and remains valid even after Close() is called.

func (*ResponseChain) FullResponseString added in v0.7.0

func (r *ResponseChain) FullResponseString() string

FullResponseString returns the current response as string in the chain.

The returned string is a copy and remains valid even after Close() is called.

func (*ResponseChain) Has added in v0.0.80

func (r *ResponseChain) Has() bool

Has returns true if the response chain has a response

func (*ResponseChain) Headers added in v0.0.80

func (r *ResponseChain) Headers() *bytes.Buffer

Headers returns the current response headers buffer in the chain.

Warning: The returned buffer is pooled and must not be modified or retained. Prefer HeadersBytes() or HeadersString() for safe read-only access.

func (*ResponseChain) HeadersBytes added in v0.7.0

func (r *ResponseChain) HeadersBytes() []byte

HeadersBytes returns the current response headers as byte slice in the chain.

The returned slice is valid only until Close() is called.

func (*ResponseChain) HeadersString added in v0.7.0

func (r *ResponseChain) HeadersString() string

HeadersString returns the current response headers as string in the chain.

The returned string is a copy and remains valid even after Close() is called.

func (*ResponseChain) Previous added in v0.0.80

func (r *ResponseChain) Previous() bool

previous updates response pointer to previous response if it was redirected and returns true else false

func (*ResponseChain) Request added in v0.0.80

func (r *ResponseChain) Request() *http.Request

Request is request of current response

func (*ResponseChain) Response added in v0.0.80

func (r *ResponseChain) Response() *http.Response

Response is response of current response

Jump to

Keyboard shortcuts

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