Documentation
¶
Index ¶
- Constants
- Variables
- func CondenseResponseNot2xxToError(resp *http.Response, err error, msg string) error
- func CreateProxyRequest(m map[string]string, r *http.Request) (*httputil.ProxyRequest, error)
- func Delete(client *http.Client, url string) (*http.Response, error)
- func DoJSON(client *http.Client, httpMethod, reqURL string, headers map[string][]string, ...) ([]byte, *http.Response, error)
- func DoJSONSimple(client *http.Client, httpMethod, requrl string, headers map[string][]string, ...) (*http.Response, error)
- func DoRequestRateLimited(client *http.Client, req *http.Request, useXrlHyphen bool, ...) (*http.Response, error)
- func GetJSONSimple(requrl string, header http.Header, data any) (*http.Response, error)
- func GetReqQueryParam(req *http.Request, paramName string) string
- func GetReqQueryParamSplit(req *http.Request, paramName, sep string) []string
- func GetRequestRateLimited(client *http.Client, reqURL string, useXrlHyphen bool, fn FnLogRateLimitInfo) (*http.Response, error)
- func GetResponseAndBytes(url string) (*http.Response, []byte, error)
- func GetURLOrReadFile(input string) ([]byte, error)
- func GetWriteFile(client *http.Client, url, filename string) (*http.Response, error)
- func GetWriteFileSimple(url string, filename string, perm os.FileMode) ([]byte, error)
- func HeaderContentTypeContains(h http.Header, ct string) bool
- func HeaderMerge(headers ...http.Header) http.Header
- func HeaderString(h http.Header) (string, error)
- func LimitRequestBody(w http.ResponseWriter, r *http.Request, maxBytes int64)
- func LimitRequestBodyDefault(w http.ResponseWriter, r *http.Request)
- func LimitRequestBodyLarge(w http.ResponseWriter, r *http.Request)
- func LimitRequestBodyMultipart(w http.ResponseWriter, r *http.Request)
- func LimitRequestBodySmall(w http.ResponseWriter, r *http.Request)
- func ListenAndServeTimeouts(addr string, handler http.Handler, timeout time.Duration) error
- func Log(handler http.Handler) http.Handler
- func LogRequestRateLimited(rlstat RateLimitInfo)
- func MergeHeader(base, more http.Header, overwrite bool) http.Header
- func Methods() []string
- func MethodsMap() map[string]int
- func NewHTTPClient() *http.Client
- func NewHeadersMSS(headersMap map[string]string) http.Header
- func NewServerTimeouts(addr string, handler http.Handler, timeout time.Duration) *http.Server
- func ParseFormLimited(w http.ResponseWriter, r *http.Request, maxBytes int64) error
- func ParseFormLimitedDefault(w http.ResponseWriter, r *http.Request) error
- func ParseHTTPMethodString(method string) (string, error)
- func ParseHeader(s string) http.Header
- func ParseMultipartFormDataBoundaryFromHeader(contentType string) string
- func ParseRequestMethodPath(r *http.Request) string
- func PrintRequestOut(req *http.Request, includeBody bool) error
- func PrintResponse(resp *http.Response, includeBody bool) error
- func ProxyResponse(w http.ResponseWriter, resp *http.Response) ([]byte, error)
- func RequestURLParam(r *http.Request, key string) string
- func ResponseBodyJSONMapIndent(res *http.Response, prefix string, indent string) ([]byte, error)
- func ResponseBodyMore(r *http.Response, jsonPrefix, jsonIndent string) ([]byte, error)
- func ResponseIsContentType(ct string, r *http.Response) bool
- func ResponseWriterWriteJSON(w http.ResponseWriter, statusCode int, body any, prefix, indent string) error
- func SendWWWFormURLEncodedSimple(method, urlStr string, data url.Values) (*http.Response, error)
- func TransportTLSVersions(tr *http.Transport) (tlsutil.TLSVersion, tlsutil.TLSVersion, error)
- type ClientMore
- type Cookies
- type Endpoint
- type FnLogRateLimitInfo
- type HTTPError
- type HTTPMethod
- type RateLimitInfo
- type ResponseInfo
- type TransportRequestModifier
Constants ¶
const ( // DefaultMaxBodySize is the default maximum body size (1MB). // Suitable for most form submissions and JSON payloads. DefaultMaxBodySize int64 = 1 << 20 // 1MB // SmallMaxBodySize is for endpoints expecting small payloads (64KB). // Suitable for login forms, simple API calls. SmallMaxBodySize int64 = 64 << 10 // 64KB // LargeMaxBodySize is for endpoints expecting larger payloads (10MB). // Suitable for file uploads with known size limits. LargeMaxBodySize int64 = 10 << 20 // 10MB // MultipartMaxBodySize is for multipart form uploads (32MB). // Matches http.defaultMaxMemory. MultipartMaxBodySize int64 = 32 << 20 // 32MB )
Body size constants for common use cases.
const ( DialTimeout int = 5 TLSTimeout int = 5 HTTPTimeout int = 10 )
const ( HeaderAccept = "Accept" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderAuthorization = "Authorization" HeaderCacheControl = "Cache-Control" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentID = "Content-ID" HeaderContentLanguage = "Content-Language" HeaderContentLength = "Content-Length" HeaderContentMD5 = "Content-MD5" HeaderContentTransferEncoding = "Content-Transfer-Encoding" HeaderContentType = "Content-Type" ContentTypeAppJSON = "application/json" ContentTypeAppJSONUtf8 = "application/json; charset=utf-8" ContentTypeAppOctetStream = "application/octet-stream" ContentTypeAppFormURLEncoded = "application/x-www-form-urlencoded" ContentTypeAppFormURLEncodedUtf8 = "application/x-www-form-urlencoded; charset=utf-8" ContentTypeAppXML = "application/xml" ContentTypeAppXMLUtf8 = "application/xml; charset=utf-8" ContentTypeImageGIF = "image/gif" ContentTypeImageJPEG = "image/jpeg" ContentTypeImagePNG = "image/png" ContentTypeImageSVG = "image/svg+xml" ContentTypeImageWebP = "image/webp" ContentTypeMultipartAlternative = "multipart/alternative" ContentTypeMultipartFormData = "multipart/form-data" ContentTypeMultipartMixed = "multipart/mixed" ContentTypeTextCalendarUtf8Request = "text/calendar; charset=utf-8; method=REQUEST" ContentTypeTextHTML = "text/html" ContentTypeTextHTMLUtf8 = "text/html; charset=utf-8" ContentTypeTextMarkdown = "text/markdown" ContentTypeTextPlain = "text/plain" ContentTypeTextPlainUsASCII = "text/plain; charset=us-ascii" ContentTypeTextPlainUtf8 = "text/plain; charset=utf-8" ContentTypeTextXMLUtf8 = "text/xml; charset=utf-8" DispositionTypeAttachment = "attachment" DispositionTypeFormData = "form-data" DispositionTypeInline = "inline" HeaderDate = "Date" HeaderExpect = "Expect" Expect100Continue = "100-continue" HeaderIfMatch = "If-Match" HeaderIfModifiedSince = "If-Modified-Since" HeaderIfNoneMatch = "If-None-Match" HeaderIfUnmodifiedSince = "If-Unmodified-Since" HeaderLocation = "Location" HeaderPrefer = "Prefer" // IETF RFC-7240 PreferHandlingStrict = "handling=strict" PreferRespondAsync = "respond-async" PreferReturnMinimal = "return=minimal" PreferReturnRepresentation = "return=representation" HeaderRange = "Range" HeaderUserAgent = "User-Agent" HeaderWWWAuthenticate = "WWW-Authenticate" WWWAuthenticateBasicRestricted = "Basic realm=Restricted" HeaderXContentTypeOptions = "X-Content-Type-Options" SchemeHTTPS = "https" HeaderNgrokSkipBrowserWarning = "ngrok-skip-browser-warning" // header needs to be present. value can be anything. See more at: https://stackoverflow.com/questions/73017353/how-to-bypass-ngrok-browser-warning HeaderNgrokSkipBrowserWarningValue = "skip-browser-warning" )
Constants ensuring that header names are correctly spelled and consistently cased.
const ( MethodConnect HTTPMethod = http.MethodConnect MethodDelete = http.MethodDelete MethodGet = http.MethodGet MethodHead = http.MethodHead MethodOptions = http.MethodOptions MethodPatch = http.MethodPatch MethodPost = http.MethodPost MethodPut = http.MethodPut MethodTrace = http.MethodTrace )
Variables ¶
var (
ErrHTTPResponseCannotBeNil = errors.New("'*http.Response' cannot be nil. Expected a non-nil value")
)
var ErrStatus404 = errors.New("status " + strconv.Itoa(http.StatusNotFound) + " " + http.StatusText(http.StatusNotFound))
Functions ¶
func CreateProxyRequest ¶ added in v0.63.11
CreateProxyRequest creates a proxy request given a mapping "POST /path" => "POST https://newurl"
func DoJSONSimple ¶
func DoRequestRateLimited ¶
func DoRequestRateLimited(client *http.Client, req *http.Request, useXrlHyphen bool, fnLog FnLogRateLimitInfo) (*http.Response, error)
DoRequestRateLimited will pause a request for the time specified in the HTTP response headers.
func GetJSONSimple ¶
func GetReqQueryParamSplit ¶
func GetRequestRateLimited ¶
func GetResponseAndBytes ¶
GetResponseAndBytes retreives a URL and returns the response body as a byte array in addition to the *http.Response.
func GetURLOrReadFile ¶
GetURLOrReadFile takes a string and will either call HTTP GET if the string begins with `http` or `https` URI scheme or read a file if it does not.
func GetWriteFile ¶
GetWriteFile gets the conents of a URL and stores the body in the desired filename location.
func GetWriteFileSimple ¶
GetWriteFile performs a HTTP GET request and saves the response body to the file path specified. It reeads the entire file into memory which is not ideal for large files.
func HeaderContentTypeContains ¶ added in v0.68.0
func HeaderMerge ¶
HeaderMerge combines data from multiple `http.Header` structs.
func HeaderString ¶
HeaderString converts a `http.Header` to a string.
func LimitRequestBody ¶ added in v0.73.4
func LimitRequestBody(w http.ResponseWriter, r *http.Request, maxBytes int64)
LimitRequestBody wraps r.Body with http.MaxBytesReader to prevent memory exhaustion attacks. Call this before ParseForm, ParseMultipartForm, or any operation that reads the request body.
This function addresses gosec rule G120: "Parsing form data without limiting request body size can allow memory exhaustion"
Example:
func handler(w http.ResponseWriter, r *http.Request) {
httputilmore.LimitRequestBody(w, r, httputilmore.DefaultMaxBodySize)
if err := r.ParseForm(); err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
// ... handle form data
}
Note: This modifies r.Body in place. The original body is replaced with a limited reader that returns an error if the limit is exceeded.
func LimitRequestBodyDefault ¶ added in v0.73.4
func LimitRequestBodyDefault(w http.ResponseWriter, r *http.Request)
LimitRequestBodyDefault calls LimitRequestBody with DefaultMaxBodySize (1MB).
This is a convenience function for the common case where 1MB is sufficient.
func LimitRequestBodyLarge ¶ added in v0.73.4
func LimitRequestBodyLarge(w http.ResponseWriter, r *http.Request)
LimitRequestBodyLarge calls LimitRequestBody with LargeMaxBodySize (10MB).
Use this for endpoints that accept file uploads or larger payloads.
func LimitRequestBodyMultipart ¶ added in v0.73.4
func LimitRequestBodyMultipart(w http.ResponseWriter, r *http.Request)
LimitRequestBodyMultipart calls LimitRequestBody with MultipartMaxBodySize (32MB).
Use this for multipart form uploads. This matches the default memory limit used by http.Request.ParseMultipartForm when called with 0.
func LimitRequestBodySmall ¶ added in v0.73.4
func LimitRequestBodySmall(w http.ResponseWriter, r *http.Request)
LimitRequestBodySmall calls LimitRequestBody with SmallMaxBodySize (64KB).
Use this for endpoints that expect small payloads like login forms.
func ListenAndServeTimeouts ¶ added in v0.64.9
func Log ¶
Log is a custom Http handler that will log all requests. It can be called using http.ListenAndServe(":8080", Log(http.DefaultServeMux)) From: https://groups.google.com/forum/#!topic/golang-nuts/s7Xk1q0LSU0
func LogRequestRateLimited ¶
func LogRequestRateLimited(rlstat RateLimitInfo)
func MergeHeader ¶
MergeHeader merges two http.Header adding the values of the second to the first.
func MethodsMap ¶
func NewHTTPClient ¶
NewHTTPClient is a HTTP client provides default and updatable timeouts. The default Go HTTP client never times out. More at: https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779#.ymd655pgz
func NewHeadersMSS ¶
NewHeadersMSS returns a `http.Header` struct give a `map[string]string`
func NewServerTimeouts ¶ added in v0.59.0
NewServerTimeouts returns a `*http.Server` with all timeouts set to a single value provided.
func ParseFormLimited ¶ added in v0.73.4
ParseFormLimited is a convenience function that limits the body size and parses the form in one call. It returns any error from ParseForm.
Example:
func handler(w http.ResponseWriter, r *http.Request) {
if err := httputilmore.ParseFormLimited(w, r, httputilmore.DefaultMaxBodySize); err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
// ... handle form data
}
func ParseFormLimitedDefault ¶ added in v0.73.4
func ParseFormLimitedDefault(w http.ResponseWriter, r *http.Request) error
ParseFormLimitedDefault calls ParseFormLimited with DefaultMaxBodySize.
func ParseHTTPMethodString ¶
ParseHTTPMethodString returns a HTTPMethod as a string for a string.
func ParseHeader ¶
ParseHeader converts a raw strign to a header struct.
func ParseRequestMethodPath ¶ added in v0.63.11
func PrintRequestOut ¶
PrintRequestOut prints a http.Request using `httputil.DumpRequestOut`.
func PrintResponse ¶
PrintResponse prints a http.Response using `httputil.DumpResponse`.
func ProxyResponse ¶
ProxyResponse copies the information from a `*http.Response` to a `http.ResponseWriter`.
func RequestURLParam ¶ added in v0.51.0
RequestURLParam returns the result of `url.Values.Get()` given an `*http.Response`. An empty string is returned if `*http.Request` or `*url.URL` are `nil`.
func ResponseBodyJSONMapIndent ¶
ResponseBodyJSONMapIndent returns the body as a generic JSON dictionary
func ResponseBodyMore ¶ added in v0.68.0
func ResponseIsContentType ¶ added in v0.64.0
func ResponseWriterWriteJSON ¶
func TransportTLSVersions ¶ added in v0.64.1
func TransportTLSVersions(tr *http.Transport) (tlsutil.TLSVersion, tlsutil.TLSVersion, error)
Types ¶
type ClientMore ¶
func (*ClientMore) PostToJSON ¶
type Endpoint ¶ added in v0.63.11
type Endpoint struct {
Method HTTPMethod
URL *url.URL
}
func ParseEndpoint ¶ added in v0.63.11
ParseEndpoint returns an `Endpoint` upon parsing a string like "POST https://example.com". If no method is provided, `GET` is returned. If the string has more than two fields, the lsat field is ignored.
func ParseRequestEndpoint ¶ added in v0.63.11
type FnLogRateLimitInfo ¶
type FnLogRateLimitInfo func(RateLimitInfo)
type HTTPError ¶
type HTTPMethod ¶
type HTTPMethod string
func ParseHTTPMethod ¶
func ParseHTTPMethod(method string) (HTTPMethod, error)
ParseHTTPMethod returns a HTTPMethod type for a string.
type RateLimitInfo ¶
type RateLimitInfo struct {
StatusCode int
RetryAfter int
XRateLimitLimit int
XRateLimitRemaining int
XRateLimitReset int
XRateLimitWindow int
}
RateLimitInfo is a structure for holding parsed rate limit info. It uses headers from the GitHub, RingCentral and Twitter APIs.
func NewResponseRateLimitInfo ¶
func NewResponseRateLimitInfo(resp *http.Response, useXrlHyphen bool) RateLimitInfo
NewResponseRateLimitInfo returns a RateLimitInfo from a http.Response.
type ResponseInfo ¶
type ResponseInfo struct {
Name string `json:"name,omitempty"` // to distinguish from other requests
Method string `json:"method,omitempty"`
URL string `json:"url,omitempty"`
StatusCode int `json:"statusCode,omitempty"`
Time time.Time `json:"time,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
}
ResponseInfo is a generic struct to handle response info.
func (*ResponseInfo) ToJSON ¶
func (resIn *ResponseInfo) ToJSON() []byte
ToJSON returns ResponseInfo as a JSON byte array, embedding json.Marshal errors if encountered.
type TransportRequestModifier ¶
type TransportRequestModifier struct {
Transport http.RoundTripper
Header http.Header
Query url.Values
Override bool
}
TransportRequestModifier implements http.RoundTripper. When set as Transport of http.Client, it adds HTTP headers and or query string parameters to requests. No field is mandatory. Can be implemented with http.Client as: client.Transport = httputilmore.TransportRequestModifier{ Transport:client.Transport, Header:myHeader}