rawhttp

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecompressResponse

func DecompressResponse(reader io.Reader, contentEncoding string) (io.Reader, error)

func DumpRequest

func DumpRequest(req *http.Request, normalizeHTTP bool) string

DumpRequest dumps an HTTP request to a string format. It normalizes HTTP proxy requests to use path-only format (like HTTPS requests). IMPORTANT: This function restores the request body so it can still be forwarded.

func DumpResponse

func DumpResponse(resp *http.Response) string

DumpResponse dumps an HTTP response to a string format. It uses "magic" detection to automatically decompress content regardless of headers, trying multiple compression formats (gzip, brotli, zlib) to see if any work. IMPORTANT: This function restores the response body so it can still be sent to the client.

func ExtractHost

func ExtractHost(rawRequest []byte) string

ExtractHost attempts to extract the host from a raw HTTP request. This is a minimal parser that only tries to find the Host header. It returns an empty string if no host is found. This is optional and only used for convenience - the raw bytes are preserved as-is.

func GetHeaderValue

func GetHeaderValue(headers [][]string, key string) (string, bool)

GetHeaderValue returns the first header value matching the key (case-insensitive) Returns raw key and value without trimming

func GetHeaders

func GetHeaders(h http.Header) map[string]string

func MagicDecompress

func MagicDecompress(reader io.Reader) (io.Reader, error)

MagicDecompress attempts to decompress content using multiple compression formats regardless of headers. It tries each format and returns the first successful one.

func ReadFromFile

func ReadFromFile(filepath string) ([]byte, error)

ReadFromFile reads a raw HTTP request from a file. It preserves all formatting, including malformed headers, whitespace, and line endings. Only minimal parsing is performed to extract the target host if needed.

func ReadFromReader

func ReadFromReader(reader io.Reader) ([]byte, error)

ReadFromReader reads a raw HTTP request from an io.Reader. It preserves all formatting, including malformed headers, whitespace, and line endings.

func ReadFromString

func ReadFromString(rawRequest string) []byte

ReadFromString reads a raw HTTP request from a string. It preserves all formatting, including malformed headers, whitespace, and line endings.

func UnparseRequest

func UnparseRequest(pr ParsedRequest) []byte

UnparseRequest converts a ParsedRequest back into raw HTTP request bytes. It uses the LineBreak field to determine line endings.

func UnparseResponse

func UnparseResponse(pr ParsedResponse) []byte

UnparseResponse converts a ParsedResponse back into raw HTTP response bytes. It uses the LineBreak field to determine line endings.

Types

type Client

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

Client is a raw HTTP client that sends requests with minimal validation

func DefaultClient

func DefaultClient() *Client

DefaultClient returns a client with sensible defaults Uses browser TLS fingerprint to bypass Cloudflare

func NewClient

func NewClient(config Config) *Client

NewClient creates a new raw HTTP client with the given configuration Always uses browser TLS fingerprint to bypass Cloudflare and other CDN bot detection

func (*Client) Send

func (c *Client) Send(req Request) (*Response, error)

Send sends a raw HTTP request and returns the raw response. This function performs minimal validation - only what's necessary for TCP/TLS connection. All malformed headers, formatting issues, and protocol violations are preserved and sent as-is. If UseHTTP2 is true, the request will be sent using HTTP/2 protocol.

func (*Client) SendBytes

func (c *Client) SendBytes(rawRequest []byte, host string, port string, useTLS bool) (*Response, error)

SendBytes is a convenience method that sends raw HTTP request bytes

func (*Client) SendFile

func (c *Client) SendFile(filepath string, host string, port string, useTLS bool) (*Response, error)

SendFile is a convenience method that reads a request from a file and sends it

func (*Client) SendHTTP2

func (c *Client) SendHTTP2(req Request) (*Response, error)

SendHTTP2 sends a raw HTTP request using HTTP/2 protocol

func (*Client) SendString

func (c *Client) SendString(rawRequest string, host string, port string, useTLS bool) (*Response, error)

SendString is a convenience method that sends a raw HTTP request from a string

type Config

type Config struct {
	// Timeout for connection and read operations
	Timeout time.Duration

	// InsecureSkipVerify skips TLS certificate verification
	InsecureSkipVerify bool

	// TLSMinVersion sets the minimum TLS version (default: TLS 1.0 for compatibility)
	TLSMinVersion uint16

	// UseBrowserFingerprint enables uTLS to mimic browser TLS fingerprint
	// This helps bypass Cloudflare and other CDN bot detection that use JA3/JA4 fingerprinting
	UseBrowserFingerprint bool

	// BrowserFingerprint specifies which browser to mimic (default: Chrome)
	// Options: "chrome", "firefox", "safari", "edge", "random"
	BrowserFingerprint string
}

Config holds configuration for the raw HTTP client

type ParsedRequest

type ParsedRequest struct {
	Method        string
	URL           string
	HTTPVersion   string
	Headers       [][]string // Array of [key, value] pairs
	Body          string
	LineBreak     string
	BodySeparator string // The body separator found in the original (e.g., "\r\n\r\n", "\n\n", or empty if none)
}

ParsedRequest is a minimal parsed shape for a raw HTTP request

func ParseRequest

func ParseRequest(raw []byte) ParsedRequest

ParseRequest performs a tolerant, minimal parse of a raw HTTP request. It extracts method, URL, HTTP version, headers (as array of [key, value] pairs), and body.

type ParsedResponse

type ParsedResponse struct {
	Version       string
	Status        int
	StatusFull    string
	Headers       [][]string // Array of [key, value] pairs
	Body          string
	LineBreak     string
	BodySeparator string // The body separator found in the original (e.g., "\r\n\r\n", "\n\n", or empty if none)
}

ParsedResponse is a minimal parsed shape for a raw HTTP response

func ParseResponse

func ParseResponse(raw []byte) ParsedResponse

ParseResponse performs a tolerant, minimal parse of a raw HTTP response. It extracts version, numeric status, full status line, headers (as array of [key, value] pairs), and body.

type Request

type Request struct {
	// RawBytes contains the complete raw HTTP request as bytes
	// This preserves all malformations, whitespace, and formatting
	RawBytes []byte

	// Host is the target hostname (e.g., "example.com")
	Host string

	// Port is the target port (e.g., "80", "443")
	// If empty, defaults to 80 for HTTP and 443 for HTTPS
	Port string

	// UseTLS determines whether to use TLS/HTTPS
	UseTLS bool

	// UseHTTP2 determines whether to use HTTP/2 protocol
	// If true, the request will be sent using HTTP/2
	// Note: HTTP/2 requires TLS (UseTLS must also be true)
	UseHTTP2 bool

	// Timeout specifies the connection and read timeout
	Timeout time.Duration
}

Request represents a raw HTTP request to be sent

type Response

type Response struct {
	// RawBytes contains the complete raw HTTP response as bytes
	RawBytes []byte

	// StatusCode is the HTTP status code (if parseable)
	StatusCode int

	// Status is the status line (if parseable)
	Status string

	// ResponseTime is the time taken to receive the response headers (not including body)
	ResponseTime time.Duration
}

Response represents the raw HTTP response received

Jump to

Keyboard shortcuts

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