httputils

package
v0.0.0-...-642df0c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Index

Constants

View Source
const (
	VERSION string = "r.20240114.2007"

	DEBUG       bool = false
	DEBUG_CACHE bool = false

	//--
	DEFAULT_CLIENT_UA string = "NetSurf/3.10"
	//--
	HTTP_CLI_DEF_BODY_READ_SIZE uint64 = smart.SIZE_BYTES_16M      //  16MB
	HTTP_CLI_MAX_BODY_READ_SIZE uint64 = smart.SIZE_BYTES_16M * 32 // 512MB
	//--
	HTTP_CLI_MAX_POST_VAL_SIZE  uint64 = smart.SIZE_BYTES_16M      //  16MB
	HTTP_CLI_MAX_POST_FILE_SIZE uint64 = smart.SIZE_BYTES_16M * 16 // 256MB
	HTTP_CLI_MAX_POST_DATA_SIZE uint64 = smart.SIZE_BYTES_16M * 24 // 384MB
	//--
	HTTP_CLI_MAX_REDIRECTS uint8 = 25
	//--
	HTTP_CLI_MIN_TIMEOUT uint32 = 5     // 5 seconds
	HTTP_CLI_DEF_TIMEOUT uint32 = 720   // 12 minutes
	HTTP_CLI_MAX_TIMEOUT uint32 = 86400 // 24 hours
	//--
	HTTP_CLI_TLS_TIMEOUT uint32 = 15 // 15 seconds (default is 10 seconds, as defined in the net library)
	//--
	HTTP_MAX_SIZE_SAFE_COOKIE uint16 = 4096 // max safe raw size is 4096, which includes also the variable name
	HTTP_MAX_SIZE_SAFE_URL    uint16 = 4096

	//--
	DEFAULT_REALM string = "SmartGO Web Server" // must be min 7 chars
	//--
	HTTP_SRV_IDLE_TIMEOUT uint32 = 60 // standard, as Apache
	//--
	DISP_TYPE_INLINE     string = "inline"
	DISP_TYPE_ATTACHMENT string = "attachment"
	MIME_TYPE_DEFAULT    string = "application/octet-stream"
	//--
	ICACHEM_CLEANUP_INTERVAL uint32 = 5     // 5 seconds
	ICACHEM_EXPIRATION       uint32 = 300   // 300 seconds = 5 mins ; cache unsuccessful logins for 5 mins
	ICACHEM_FAILS_NUM        uint32 = 8 * 2 // max fail attempts before lock 5 mins (300 sec) ; use x2 because most of the time in browsers each unauth request will generate double requests
	//--
	CACHE_CONTROL_NOCACHE string = "no-cache"
	CACHE_CONTROL_PRIVATE string = "private"
	CACHE_CONTROL_PUBLIC  string = "public"
	CACHE_CONTROL_DEFAULT string = "default"
	//--
	MAX_SIZE_ETAG int64 = 1048576 // 1MB 1048576 bytes ; 100% of assets match this criteria ; for the rest (ex: public files), Weak ETag is not worth ...
	//--
	REGEX_SAFE_HTTP_FORM_VAR_NAME string = `^[a-zA-Z0-9_\-]+$`

	//--
	HTTP_STATUS_200 string = "200 OK"
	HTTP_STATUS_202 string = "202 Accepted"
	HTTP_STATUS_203 string = "203 Non-Authoritative Information"
	HTTP_STATUS_208 string = "208 Already Reported"
	//--
	HTTP_STATUS_301 string = "301 Moved Permanently"
	HTTP_STATUS_302 string = "302 Found" // "302 Moved Temporarily"
	//--
	HTTP_STATUS_400 string = "400 Bad Request"
	HTTP_STATUS_401 string = "401 Unauthorized"
	HTTP_STATUS_403 string = "403 Forbidden"
	HTTP_STATUS_404 string = "404 Not Found"
	HTTP_STATUS_405 string = "405 Method Not Allowed"
	HTTP_STATUS_410 string = "410 Gone"
	HTTP_STATUS_429 string = "429 Too Many Requests"
	//--
	HTTP_STATUS_500 string = "500 Internal Server Error"
	HTTP_STATUS_501 string = "501 Not Implemented"
	HTTP_STATUS_502 string = "502 Bad Gateway"
	HTTP_STATUS_503 string = "503 Service Unavailable"
	HTTP_STATUS_504 string = "504 Gateway Timeout"

	//-- {{{SYNC-GO-HTTP-LOW-CASE-HEADERS}}}
	HTTP_HEADER_CONTENT_TYPE string = "content-type"
	HTTP_HEADER_CONTENT_DISP string = "content-disposition"
	HTTP_HEADER_CONTENT_LEN  string = "content-length"
	//--
	HTTP_HEADER_CACHE_CTRL string = "cache-control"
	HTTP_HEADER_CACHE_PGMA string = "pragma"
	HTTP_HEADER_CACHE_EXPS string = "expires"
	HTTP_HEADER_CACHE_LMOD string = "last-modified"
	//--
	HTTP_HEADER_RETRY_AFTER string = "retry-after"
	HTTP_HEADER_ALLOW       string = "allow"
	//--
	HTTP_HEADER_ETAG_SUM  string = "etag"
	HTTP_HEADER_ETAG_IFNM string = "if-none-match"
	//--
	HTTP_HEADER_SERVER_DATE    string = "date"
	HTTP_HEADER_SERVER_SIGN    string = "server"
	HTTP_HEADER_SERVER_POWERED string = "x-powered-by"
	//--
	HTTP_HEADER_REDIRECT_LOCATION string = "location"
	//--
	HTTP_HEADER_AUTH_AUTHORIZATION    string = "authorization"
	HTTP_HEADER_AUTH_AUTHENTICATE     string = "www-authenticate"
	HTTP_HEADER_VALUE_AUTH_TYPE_BASIC string = "Basic" // keep camel case
	//--
	HTTP_HEADER_DAV_DESTINATION string = "destination"
	HTTP_HEADER_DAV_OVERWRITE   string = "overwrite"
	//--
	HTTP_HEADER_USER_AGENT string = "user-agent"
)

Variables

This section is empty.

Functions

func HttpBasicAuthCheck

func HttpBasicAuthCheck(w http.ResponseWriter, r *http.Request, authRealm string, authUsername string, authPassword string, allowedIPs string, customAuthCheck HttpAuthCheckFunc, outputHtml bool) (error, smart.AuthDataStruct)

if returns a non empty string there is an error ; if error it already outputs the 401 headers and content so there is nothing more to do ... it handles 401 or 403 access by IP list

func HttpClientAuthBasicHeader

func HttpClientAuthBasicHeader(authUsername string, authPassword string) http.Header

func HttpHeaderAuthBasic

func HttpHeaderAuthBasic(w http.ResponseWriter, authRealm string)

func HttpHeadersCacheControl

func HttpHeadersCacheControl(w http.ResponseWriter, r *http.Request, expiration int, modified string, control string) (isCachedContent bool)

func HttpMuxServer

func HttpMuxServer(srvAddr string, timeoutSec uint32, forceHttpV1 bool, disableGeneralOptionsHandler bool, description string) (*http.ServeMux, http.Server)

func HttpRequestGetCookie

func HttpRequestGetCookie(r *http.Request, name string) string

func HttpRequestGetCookies

func HttpRequestGetCookies(r *http.Request) map[string]string

func HttpRequestGetHeaderArr

func HttpRequestGetHeaderArr(r *http.Request, hdrKey string) []string

func HttpRequestGetHeaderStr

func HttpRequestGetHeaderStr(r *http.Request, hdrKey string) string

func HttpRequestGetHeaders

func HttpRequestGetHeaders(r *http.Request) map[string][]string

func HttpRequestSetCookie

func HttpRequestSetCookie(w http.ResponseWriter, r *http.Request, name string, value string, expires int64, path string, domain string, samesite CookieSameSiteType, secure bool, httpOnly bool) error

func HttpRequestSetCookieWithDefaults

func HttpRequestSetCookieWithDefaults(w http.ResponseWriter, r *http.Request, name string, value string, expires int64) error

func HttpServerIsIpRateLimited

func HttpServerIsIpRateLimited(r *http.Request, theLimit int, theBurst int) bool

func HttpStatus200

func HttpStatus200(w http.ResponseWriter, r *http.Request, content string, contentFnameOrPath string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string)

@params description: see httpStatusOKX()

func HttpStatus202

func HttpStatus202(w http.ResponseWriter, r *http.Request, content string, contentFnameOrPath string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string)

@params description: see httpStatusOKX()

func HttpStatus203

func HttpStatus203(w http.ResponseWriter, r *http.Request, content string, contentFnameOrPath string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string)

@params description: see httpStatusOKX()

func HttpStatus208

func HttpStatus208(w http.ResponseWriter, r *http.Request, content string, contentFnameOrPath string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string)

@params description: see httpStatusOKX()

func HttpStatus301

func HttpStatus301(w http.ResponseWriter, r *http.Request, redirectUrl string, outputHtml bool)

func HttpStatus302

func HttpStatus302(w http.ResponseWriter, r *http.Request, redirectUrl string, outputHtml bool)

func HttpStatus400

func HttpStatus400(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus401

func HttpStatus401(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus403

func HttpStatus403(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus404

func HttpStatus404(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus405

func HttpStatus405(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus410

func HttpStatus410(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus429

func HttpStatus429(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus500

func HttpStatus500(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus501

func HttpStatus501(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus502

func HttpStatus502(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus503

func HttpStatus503(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func HttpStatus504

func HttpStatus504(w http.ResponseWriter, r *http.Request, messageText string, outputHtml bool)

func MimeDispositionConformParam

func MimeDispositionConformParam(mimeDisposition string) string

func MimeDispositionEval

func MimeDispositionEval(fpath string) (mimType string, mimUseCharset bool, mimDisposition string)

func TLSProtoHttpV1Server

func TLSProtoHttpV1Server() map[string]func(*http.Server, *tls.Conn, http.Handler)

func TlsConfigClient

func TlsConfigClient(insecureSkipVerify bool, serverPEM string) tls.Config

func TlsConfigServer

func TlsConfigServer() tls.Config

Types

type CookieSameSiteType

type CookieSameSiteType uint8
const (
	CookieSameSiteDefault CookieSameSiteType = iota
	CookieSameSiteEmpty
	CookieSameSiteNone
	CookieSameSiteLax
	CookieSameSiteStrict
)

type HttpAuthCheckFunc

type HttpAuthCheckFunc func(string, uint8, string, map[string]string, string, string, string) (bool, smart.AuthDataStruct) // (authRealm, authMode, realClientIp, arrCookies, user, pass, token) : true if OK / false if NOT

modes: 1 = Basic Auth ; 2 = Bearer ; 3 = Cookie ; 4..255 unused at the moment ...

type HttpClientRequest

type HttpClientRequest struct {
	Errors                string            `json:"errors"`
	UserAgent             string            `json:"userAgent"`
	ConnTimeout           uint32            `json:"connTimeout,string"`
	MaxDownloadSize       uint64            `json:"maxDownloadSize,string"`
	HttpMethod            string            `json:"httpMethod"`
	AuthUserName          string            `json:"authUserName"`
	Uri                   string            `json:"uri"`
	RedirectLocation      string            `json:"redirectLocation"`
	MaxRedirects          uint8             `json:"maxRedirects,string"`
	NumRedirects          uint8             `json:"numRedirects,string"`
	RedirUris             []string          `json:"redirUris"`
	LastUri               string            `json:"lastUri"`
	UploadLocalFile       string            `json:"uploadLocalFile"`       // PUT
	UploadFileName        string            `json:"uploadFileName"`        // PUT
	UploadDataSize        int64             `json:"uploadDataSize,string"` // POST | PUT
	HttpStatus            int               `json:"httpStatus,string"`
	HeadData              string            `json:"headData"`
	HeadDataSize          uint64            `json:"headDataSize,string"`
	Cookies               map[string]string `json:"cookies"`
	AllowMethods          string            `json:"allowMethods"` // OPTIONS
	LastModified          string            `json:"lastModified"`
	Expires               string            `json:"expires"`
	RetryAfter            string            `json:"retryAfter"`
	MimeType              string            `json:"mimeType"`
	MimeCharSet           string            `json:"mimeCharSet"`
	MimeDisp              string            `json:"mimeDisp"`
	MimeFileName          string            `json:"mimeFileName"`
	ContentLength         int64             `json:"contentLength,string"`
	BodyData              string            `json:"bodyData"`
	BodyDataSize          uint64            `json:"bodyDataSize,string"` // unencoded
	BodyDataEncoding      string            `json:"bodyDataEncoding"`    // plain | base64 | log
	DownloadLocalDir      string            `json:"downloadLocalDir"`
	DownloadLocalFileName string            `json:"downloadLocalFileName"`
}

func HttpClientDoRequestCOPY

func HttpClientDoRequestCOPY(destinationUri string, overwrite bool, uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestDELETE

func HttpClientDoRequestDELETE(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestDownloadFile

func HttpClientDoRequestDownloadFile(downloadLocalDirPath string, method string, uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, reqArr map[string][]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

IMPORTANT: will not rewrite the download file if exists ... must be previous deleted ! can handle: GET or POST

func HttpClientDoRequestGET

func HttpClientDoRequestGET(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxBytesRead uint64, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestHEAD

func HttpClientDoRequestHEAD(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestMKCOL

func HttpClientDoRequestMKCOL(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestMOVE

func HttpClientDoRequestMOVE(destinationUri string, overwrite bool, uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestOPTIONS

func HttpClientDoRequestOPTIONS(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestPATCH

func HttpClientDoRequestPATCH(patchData string, uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestPOST

func HttpClientDoRequestPOST(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, reqArr map[string][]string, timeoutSec uint32, maxBytesRead uint64, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestPROPFIND

func HttpClientDoRequestPROPFIND(uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestPUT

func HttpClientDoRequestPUT(putData string, uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

func HttpClientDoRequestPUTFile

func HttpClientDoRequestPUTFile(upldLocalFilePath string, uri string, tlsServerPEM string, tlsInsecureSkipVerify bool, ckyArr map[string]string, timeoutSec uint32, maxRedirects uint8, authUsername string, authPassword string) HttpClientRequest

Jump to

Keyboard shortcuts

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