{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package middleware\n\nPackage middleware provides HTTP middleware functionalities such as rate limiting and IP blacklisting.\n\n## Constants\n\n```go\nconst (\n\t// DefaultMaxRequestSize is the default maximum request body size (10MB)\n\tDefaultMaxRequestSize = 10 * 1024 * 1024 // 10MB\n\n\t// MaxRequestSizeHeader is the header name for max request size\n\tMaxRequestSizeHeader = \"X-Max-Request-Size\"\n)\n```\n\n```go\nconst (\n\tSize1MB   = 1 * 1024 * 1024\n\tSize5MB   = 5 * 1024 * 1024\n\tSize10MB  = 10 * 1024 * 1024\n\tSize50MB  = 50 * 1024 * 1024\n\tSize100MB = 100 * 1024 * 1024\n)\n```\nCommon size limits\n\n```go\nconst DefaultClientIDHeader = \"X-Client-Id\"\n```\nDefaultClientIDHeader is the header a client (one per browser tab) sends to identify itself to the ClientQueue.\n\n## Functions\n\n```go\nfunc Chain(mws ...func(http.Handler) http.Handler) func(http.Handler) http.Handler\n```\nChain composes middlewares into one, for the single middleware slot of restheadspec.SetupMuxRoutes and friends. The first argument is the outermost: Chain(auth, q.Middleware) authenticates before a request may occupy a queue slot, so unauthenticated traffic cannot fill queues.\n\n```go\nfunc PanicRecovery(next http.Handler) http.Handler\n```\nPanicRecovery is a middleware that recovers from panics, logs the error, sends it to an error tracker, records a metric, and returns a 500 Internal Server Error.\n\n```go\nfunc SanitizeEmail(email string) string\n```\nSanitizeEmail performs basic email sanitization\n\n```go\nfunc SanitizeFilename(filename string) string\n```\nSanitizeFilename sanitizes a filename\n\n```go\nfunc SanitizeURL(url string) string\n```\nSanitizeURL performs basic URL sanitization\n\n## Types\n\n```go\ntype BlacklistConfig struct {\n\t// UseProxy indicates whether to extract IP from X-Forwarded-For/X-Real-IP headers\n\tUseProxy bool\n}\n```\nBlacklistConfig configures the IP blacklist\n\n```go\ntype ClientQueue struct {\n\t// contains filtered or unexported fields\n}\n```\nClientQueue limits how many requests each client runs concurrently and queues the rest first-in-first-out, smoothing bursts such as a page load that fires many requests at once. A client is identified by, in order: the client id header, the Authorization header (session), then the IP.\n\n```go\nfunc NewClientQueue(cfg ClientQueueConfig) *ClientQueue\n```\nNewClientQueue creates a ClientQueue and starts its idle-client cleanup. Call Close to stop it.\n\n```go\nfunc (q *ClientQueue) Close()\n```\nClose stops the cleanup goroutine. Requests in flight are unaffected.\n\n```go\nfunc (q *ClientQueue) Middleware(next http.Handler) http.Handler\n```\nMiddleware queues requests per client. CORS preflights and connection upgrades (websockets) bypass the queue, since they are short or long-lived respectively and should not hold or wait for a slot.\n\n```go\ntype ClientQueueConfig struct {\n\t// MaxConcurrent is how many requests one client may have running at once.\n\t// Default 4.\n\tMaxConcurrent int\n\t// MaxQueue is how many further requests one client may have waiting.\n\t// Requests beyond it are rejected with 429. Default 50.\n\tMaxQueue int\n\t// MaxWait is the longest a request waits for a slot before it is rejected\n\t// with 503. Default 30s.\n\tMaxWait time.Duration\n\t// IdleTimeout is how long a client with no requests is remembered.\n\t// Default 5m.\n\tIdleTimeout time.Duration\n\t// HeaderName is the client id header. Default X-Client-Id.\n\tHeaderName string\n}\n```\nClientQueueConfig configures a ClientQueue. Zero values use the defaults.\n\n```go\ntype IPBlacklist struct {\n\t// contains filtered or unexported fields\n}\n```\nIPBlacklist provides IP blocking functionality\n\n```go\nfunc NewIPBlacklist(config BlacklistConfig) *IPBlacklist\n```\nNewIPBlacklist creates a new IP blacklist\n\n```go\nfunc (bl *IPBlacklist) BlockCIDR(cidr string, reason string) error\n```\nBlockCIDR blocks an IP range using CIDR notation\n\n```go\nfunc (bl *IPBlacklist) BlockIP(ip string, reason string) error\n```\nBlockIP blocks a single IP address\n\n```go\nfunc (bl *IPBlacklist) GetBlacklist() (ips []string, cidrs []string)\n```\nGetBlacklist returns all blacklisted IPs and CIDRs\n\n```go\nfunc (bl *IPBlacklist) IsBlocked(ip string) (blacklist bool, reason string)\n```\nIsBlocked checks if an IP is blacklisted\n\n```go\nfunc (bl *IPBlacklist) Middleware(next http.Handler) http.Handler\n```\nMiddleware returns an HTTP middleware that blocks blacklisted IPs\n\n```go\nfunc (bl *IPBlacklist) StatsHandler() http.Handler\n```\nStatsHandler returns an HTTP handler that shows blacklist statistics\n\n```go\nfunc (bl *IPBlacklist) UnblockCIDR(cidr string)\n```\nUnblockCIDR removes a CIDR range from the blacklist\n\n```go\nfunc (bl *IPBlacklist) UnblockIP(ip string)\n```\nUnblockIP removes an IP from the blacklist\n\n```go\ntype RateLimitInfo struct {\n\tIP              string  `json:\"ip\"`\n\tTokensRemaining float64 `json:\"tokens_remaining\"`\n\tLimit           float64 `json:\"limit\"`\n\tBurst           int     `json:\"burst\"`\n}\n```\nRateLimitInfo contains information about a specific IP's rate limit status\n\n```go\ntype RateLimiter struct {\n\t// contains filtered or unexported fields\n}\n```\nRateLimiter provides rate limiting functionality\n\n```go\nfunc NewRateLimiter(rps float64, burst int) *RateLimiter\n```\nNewRateLimiter creates a new rate limiter rps is requests per second, burst is the maximum burst size\n\n```go\nfunc (rl *RateLimiter) GetAllRateLimitInfo() []*RateLimitInfo\n```\nGetAllRateLimitInfo returns rate limit information for all tracked IPs\n\n```go\nfunc (rl *RateLimiter) GetRateLimitInfo(ip string) *RateLimitInfo\n```\nGetRateLimitInfo returns rate limit information for a specific IP\n\n```go\nfunc (rl *RateLimiter) GetTrackedIPs() []string\n```\nGetTrackedIPs returns all IPs currently being tracked by the rate limiter\n\n```go\nfunc (rl *RateLimiter) Middleware(next http.Handler) http.Handler\n```\nMiddleware returns an HTTP middleware that applies rate limiting Automatically handles X-Forwarded-For headers when behind a proxy\n\n```go\nfunc (rl *RateLimiter) MiddlewareWithKeyFunc(keyFunc func(*http.Request) string) func(http.Handler) http.Handler\n```\nMiddlewareWithKeyFunc returns an HTTP middleware with a custom key extraction function\n\n```go\nfunc (rl *RateLimiter) StatsHandler() http.Handler\n```\nStatsHandler returns an HTTP handler that exposes rate limit statistics Example: GET /rate-limit-stats\n\n```go\ntype RequestSizeLimiter struct {\n\t// contains filtered or unexported fields\n}\n```\nRequestSizeLimiter limits the size of request bodies\n\n```go\nfunc NewRequestSizeLimiter(maxSize int64) *RequestSizeLimiter\n```\nNewRequestSizeLimiter creates a new request size limiter maxSize is in bytes. If 0, uses DefaultMaxRequestSize (10MB)\n\n```go\nfunc (rsl *RequestSizeLimiter) Middleware(next http.Handler) http.Handler\n```\nMiddleware returns an HTTP middleware that enforces request size limits\n\n```go\nfunc (rsl *RequestSizeLimiter) MiddlewareWithCustomSize(sizeFunc func(*http.Request) int64) func(http.Handler) http.Handler\n```\nMiddlewareWithCustomSize returns middleware with a custom size limit function This allows different size limits based on the request\n\n```go\ntype Sanitizer struct {\n\t// StripHTML removes HTML tags from input\n\tStripHTML bool\n\n\t// EscapeHTML escapes HTML entities\n\tEscapeHTML bool\n\n\t// RemoveNullBytes removes null bytes from input\n\tRemoveNullBytes bool\n\n\t// RemoveControlChars removes control characters (except newline, carriage return, tab)\n\tRemoveControlChars bool\n\n\t// MaxStringLength limits individual string field length (0 = no limit)\n\tMaxStringLength int\n\n\t// BlockPatterns are regex patterns to block (e.g., script tags, SQL keywords)\n\tBlockPatterns []*regexp.Regexp\n\n\t// Custom sanitization function\n\tCustomSanitizer func(string) string\n}\n```\nSanitizer provides input sanitization beyond SQL injection protection\n\n```go\nfunc DefaultSanitizer() *Sanitizer\n```\nDefaultSanitizer returns a sanitizer with secure defaults\n\n```go\nfunc StrictSanitizer() *Sanitizer\n```\nStrictSanitizer returns a sanitizer with very strict rules\n\n```go\nfunc (s *Sanitizer) Middleware(next http.Handler) http.Handler\n```\nMiddleware returns an HTTP middleware that sanitizes request headers and query params Note: Body sanitization should be done at the application level after parsing\n\n```go\nfunc (s *Sanitizer) Sanitize(value string) string\n```\nSanitize sanitizes a string value\n\n```go\nfunc (s *Sanitizer) SanitizeMap(data map[string]interface{}) map[string]interface{}\n```\nSanitizeMap sanitizes all string values in a map\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/middleware","name":"middleware","synopsis":"Package middleware provides HTTP middleware functionalities such as rate limiting and IP blacklisting.","isRedistributable":true}
