fasthttp

package
v0.63.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package fasthttp contains fasthttp specific utilities for handling compression, authorization, and more

Index

Examples

Constants

View Source
const UserAgent = "Mozilla/5.0 (compatible; netutils/1.0; +https://github.com/xplorfin/netutils)"

UserAgent contains the netutils user agent

Variables

View Source
var (
	// AcceptEncoding is a byte encoded header for accepting encoding
	// used to speed up fasthttp requests. Defined here: https://git.io/JYTc2
	AcceptEncoding = []byte(fasthttp.HeaderAcceptEncoding)
	// Gzip is the gzip header defined here https://git.io/JYTcg
	Gzip = []byte("gzip")
	// Authorization contains the authorization header
	Authorization = []byte(fasthttp.HeaderAuthorization)
	// ContentEncoding is the
	ContentEncoding = []byte(fasthttp.HeaderContentEncoding)
	ContentType     = []byte(fasthttp.HeaderContentType)
	// Brotli is the long brotli header
	Brotli = []byte("brotli")
	// BrotliShort is the abbreviated brotli header
	BrotliShort = []byte("br")
	// JSONEncoding is the header for when data is json encoded
	JSONEncoding = []byte("application/json")
	// GzipBrotli is the header for data encoded with both gzip and brotli
	GzipBrotli = []byte("gzip, brotli")
)

Byte encoded headers for convience

View Source
var UserAgentBytes = []byte(UserAgent)

UserAgentBytes contains the raw user agent encoded as a byte slice

Functions

func GetRawURL

func GetRawURL(uri []byte) ([]byte, error)

GetRawURL gets a rawurl using fasthttp

Example

gets a byte slice url using fasthttp

// fetch example.org
res, err := fasthttpHelper.GetRawURL([]byte("https://api.entropy.rocks"))
if err != nil {
	panic(err)
}
// print the response body
fmt.Println(string(res))
Output:

func GetURL

func GetURL(uri string) ([]byte, error)

GetURL gets a string url and unzips body. This is mostly a utility function

Example

gets a byte slice url using fasthttp

// fetch example.org
res, err := fasthttpHelper.GetURL("https://api.entropy.rocks")
if err != nil {
	panic(err)
}
// print the response body
fmt.Println(string(res))
Output:

func IsJSON

func IsJSON(resp *fasthttp.Response) bool

IsJSON determines whether a fasthttp response is brotli or json

func UnzipBody

func UnzipBody(resp *fasthttp.Response) []byte

UnzipBody unzips a fasthttp body that uses brotli, gzip, or borth

Types

type FastClient

type FastClient struct {
	//modify a fast http request before it's send
	ModifyRequest ModifyRequestFunc
	// process a response before it's received
	ProcessResponse ProcessResponseFunc
	// user agent to use
	UserAgent string
}

FastClient represents a client used for requests

func NewFastClient

func NewFastClient() FastClient

NewFastClient creates a fast client with a user agent this can be changed if needed in ModifyRequest

Example
// create a new fast client
fastClient := fasthttpHelper.NewFastClient()
fastClient.ModifyRequest = func(request *fasthttp.Request) {
	// use a custom user agent
	request.Header.SetUserAgent(browser.Android())
}
// since responses are dropped after use in fast http this processes them in method
fastClient.ProcessResponse = func(response *fasthttp.Response) {
	// print the headers
	fmt.Println(string(response.Header.Header()))
}
resp, err := fastClient.Request("https://api.entropy.rocks/")
if err != nil {
	panic(err)
}

// get the response after it's been unzipped
fmt.Println(string(resp))
Output:

func (FastClient) Request

func (h FastClient) Request(url string) ([]byte, error)

Request requests a url

type ModifyRequestFunc

type ModifyRequestFunc func(request *fasthttp.Request)

ModifyRequestFunc represents a function that allows a request to be modified before it is set

type ProcessResponseFunc

type ProcessResponseFunc func(response *fasthttp.Response)

ProcessResponseFunc represents a function that allows a response to be modified before it is set

Jump to

Keyboard shortcuts

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