From c7dd0366a5297f430ad6752eb212bdcd01ebe472 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 23 Sep 2024 08:15:31 +0200 Subject: [PATCH 1/4] feat: add http --- http/authprefix.go | 25 ++++++++ http/encoding.go | 15 +++++ http/header.go | 145 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 http/authprefix.go create mode 100644 http/encoding.go create mode 100644 http/header.go diff --git a/http/authprefix.go b/http/authprefix.go new file mode 100644 index 0000000..0a35208 --- /dev/null +++ b/http/authprefix.go @@ -0,0 +1,25 @@ +package http + +type AuthPrefix string + +const ( + AuthPrefixBasic AuthPrefix = "Basic" // Basic Authentication + AuthPrefixBearer AuthPrefix = "Bearer" // Bearer Token Authentication (commonly used with OAuth 2.0 and JWT) + AuthPrefixDigest AuthPrefix = "Digest" // Digest Authentication + AuthPrefixHOBA AuthPrefix = "HOBA" // HTTP Origin-Bound Authentication + AuthPrefixMutual AuthPrefix = "Mutual" // Mutual Authentication (used with TLS) + AuthPrefixAWS4HMAC AuthPrefix = "AWS4-HMAC-SHA256" // AWS Signature Version 4 + AuthPrefixNTLM AuthPrefix = "NTLM" // NT LAN Manager (Microsoft's proprietary authentication protocol) + AuthPrefixNegotiate AuthPrefix = "Negotiate" // SPNEGO-based Kerberos and NTLM Authentication + AuthPrefixOAuth AuthPrefix = "OAuth" // OAuth 1.0 Authentication + AuthPrefixSCRAMSHA256 AuthPrefix = "SCRAM-SHA-256" // Salted Challenge Response Authentication Mechanism (SCRAM) + AuthPrefixApiKey AuthPrefix = "ApiKey" // Custom API key-based authentication +) + +func (p AuthPrefix) String() string { + return string(p) +} + +func (p AuthPrefix) Set(value string) string { + return p.String() + " " + value +} diff --git a/http/encoding.go b/http/encoding.go new file mode 100644 index 0000000..6494ef1 --- /dev/null +++ b/http/encoding.go @@ -0,0 +1,15 @@ +package http + +type Encoding string + +const ( + EncodingGzip Encoding = "gzip" // Gzip compression + EncodingDeflate Encoding = "deflate" // Deflate compression + EncodingBr Encoding = "br" // Brotli compression + EncodingIdentity Encoding = "identity" // No transformation or compression + EncodingCompress Encoding = "compress" // Deprecated Unix 'compress' algorithm +) + +func (e Encoding) String() string { + return string(e) +} diff --git a/http/header.go b/http/header.go new file mode 100644 index 0000000..f00a32f --- /dev/null +++ b/http/header.go @@ -0,0 +1,145 @@ +package http + +import ( + "net/http" +) + +type Header string + +const ( + HeaderAIM Header = "A-IM" + HeaderAccept Header = "Accept" + HeaderAcceptCharset Header = "Accept-Charset" + HeaderAcceptDatetime Header = "Accept-Datetime" + HeaderAcceptEncoding Header = "Accept-Encoding" + HeaderAcceptLanguage Header = "Accept-Language" + HeaderAcceptPatch Header = "Accept-Patch" + HeaderAcceptRanges Header = "Accept-Ranges" + HeaderAccessControlAllowCredentials Header = "Access-Control-Allow-Credentials" + HeaderAccessControlAllowHeaders Header = "Access-Control-Allow-Headers" + HeaderAccessControlAllowMethods Header = "Access-Control-Allow-Methods" + HeaderAccessControlAllowOrigin Header = "Access-Control-Allow-Origin" + HeaderAccessControlExposeHeaders Header = "Access-Control-Expose-Headers" + HeaderAccessControlMaxAge Header = "Access-Control-Max-Age" + HeaderAccessControlRequestHeaders Header = "Access-Control-Request-Headers" + HeaderAccessControlRequestMethod Header = "Access-Control-Request-Method" + HeaderAge Header = "Age" + HeaderAllow Header = "Allow" + HeaderAltSvc Header = "Alt-Svc" + HeaderAuthorization Header = "Authorization" + HeaderBaggage Header = "Baggage" // W3C Baggage header for passing contextual information + HeaderCFAuthorization Header = "CF-Authorization" // Used for Cloudflare Access (for securing endpoints) + HeaderCFBypass Header = "CF-Bypass" // Bypass rules for Cloudflare Access or other services + HeaderCFCacheStatus Header = "CF-Cache-Status" // Cache status (HIT, MISS, EXPIRED, etc.) + HeaderCFConnectingIP Header = "CF-Connecting-IP" // Original client IP address as seen by Cloudflare + HeaderCFIPCountry Header = "CF-IPCountry" // Country of the requester's IP address + HeaderCFRailgun Header = "CF-Railgun" // Railgun performance information (used in Cloudflare Railgun optimization) + HeaderCFRay Header = "CF-RAY" // Unique request ID (used for tracing through Cloudflare's network) + HeaderCFRequestID Header = "CF-Request-ID" // Unique ID for the request for tracking purposes + HeaderCFRocketLoader Header = "CF-Rocket-Loader" // Related to Cloudflare's Rocket Loader optimization + HeaderCFVisitor Header = "CF-Visitor" // Information about the protocol (HTTP/HTTPS) used by the visitor + HeaderCFWorker Header = "CF-Worker" // Header set by Cloudflare Workers + HeaderCacheControl Header = "Cache-Control" + HeaderConnection Header = "Connection" + HeaderContentDisposition Header = "Content-Disposition" + HeaderContentEncoding Header = "Content-Encoding" + HeaderContentLanguage Header = "Content-Language" + HeaderContentLength Header = "Content-Length" + HeaderContentLocation Header = "Content-Location" + HeaderContentMD5 Header = "Content-MD5" + HeaderContentRange Header = "Content-Range" + HeaderContentSecurityPolicy Header = "Content-Security-Policy" + HeaderContentSecurityPolicyReportOnly Header = "Content-Security-Policy-Report-Only" + HeaderContentType Header = "Content-Type" + HeaderCookie Header = "Cookie" + HeaderDAV Header = "DAV" + HeaderDate Header = "Date" + HeaderDepth Header = "Depth" + HeaderDestination Header = "Destination" + HeaderETag Header = "ETag" + HeaderExpect Header = "Expect" + HeaderExpires Header = "Expires" + HeaderForwarded Header = "Forwarded" + HeaderFrom Header = "From" + HeaderHost Header = "Host" + HeaderIf Header = "If" + HeaderIfMatch Header = "If-Match" + HeaderIfModifiedSince Header = "If-Modified-Since" + HeaderIfNoneMatch Header = "If-None-Match" + HeaderIfRange Header = "If-Range" + HeaderIfUnmodifiedSince Header = "If-Unmodified-Since" + HeaderLastModified Header = "Last-Modified" + HeaderLink Header = "Link" + HeaderLocation Header = "Location" + HeaderLockToken Header = "Lock-Token" + HeaderMaxForwards Header = "Max-Forwards" + HeaderOrigin Header = "Origin" + HeaderP3P Header = "P3P" + HeaderPragma Header = "Pragma" + HeaderProxyAuthenticate Header = "Proxy-Authenticate" + HeaderProxyAuthorization Header = "Proxy-Authorization" + HeaderPublicKeyPins Header = "Public-Key-Pins" + HeaderRange Header = "Range" + HeaderReferer Header = "Referer" + HeaderReferrerPolicy Header = "Referrer-Policy" + HeaderRetryAfter Header = "Retry-After" + HeaderServer Header = "Server" + HeaderSetCookie Header = "Set-Cookie" + HeaderStrictTransportSecurity Header = "Strict-Transport-Security" + HeaderTE Header = "TE" + HeaderTimeout Header = "Timeout" + HeaderTk Header = "Tk" + HeaderTraceParent Header = "traceparent" // W3C Trace Context header for trace propagation + HeaderTraceState Header = "tracestate" // W3C Trace Context header for vendor-specific trace information + HeaderTrailer Header = "Trailer" + HeaderTransferEncoding Header = "Transfer-Encoding" + HeaderUpgrade Header = "Upgrade" + HeaderUpgradeInsecureRequests Header = "Upgrade-Insecure-Requests" + HeaderUserAgent Header = "User-Agent" + HeaderVary Header = "Vary" + HeaderVia Header = "Via" + HeaderWWWAuthenticate Header = "WWW-Authenticate" + HeaderWarning Header = "Warning" + HeaderXAccelRedirect Header = "X-Accel-Redirect" // Internal redirect by reverse proxy (e.g., Nginx) + HeaderXAmznTraceID Header = "X-Amzn-Trace-Id" // AWS X-Ray trace header + HeaderXB3Flags Header = "X-B3-Flags" // B3 Debug flag + HeaderXB3ParentSpanID Header = "X-B3-ParentSpanId" // B3 Parent Span ID used for distributed tracing + HeaderXB3Sampled Header = "X-B3-Sampled" // B3 Sampling flag (whether or not the request is sampled) + HeaderXB3SpanID Header = "X-B3-SpanId" // B3 Span ID used for distributed tracing + HeaderXB3TraceID Header = "X-B3-TraceId" // B3 Trace ID used for distributed tracing + HeaderXCSRFToken Header = "X-CSRF-Token" + HeaderXCache Header = "X-Cache" // Cache status of the request (e.g., HIT or MISS) + HeaderXCloudTraceContext Header = "X-Cloud-Trace-Context" // Google Cloud Trace context header + HeaderXContentDuration Header = "X-Content-Duration" // Duration of the content (e.g., media file) + HeaderXContentTypeOptions Header = "X-Content-Type-Options" + HeaderXCorrelationID Header = "X-Correlation-ID" // Correlation ID for tracing requests + HeaderXDNSPrefetchControl Header = "X-DNS-Prefetch-Control" // + HeaderXDownloadOptions Header = "X-Download-Options" // Prevents file download behavior in IE + HeaderXForwardedFor Header = "X-Forwarded-For" // Original client IP behind proxy + HeaderXForwardedHost Header = "X-Forwarded-Host" // Original host requested by the client + HeaderXForwardedProto Header = "X-Forwarded-Proto" // Original protocol (HTTP or HTTPS) used by the client + HeaderXFrameOptions Header = "X-Frame-Options" + HeaderXOTSpanContext Header = "X-OT-Span-Context" // OpenTracing Span Context (legacy, pre-OpenTelemetry) + HeaderXPermittedCrossDomainPolicies Header = "X-Permitted-Cross-Domain-Policies" // Adobe Flash Player policy control + HeaderXPoweredBy Header = "X-Powered-By" // + HeaderXRateLimitLimit Header = "X-RateLimit-Limit" // Limit for the number of requests in a given time + HeaderXRateLimitRemaining Header = "X-RateLimit-Remaining" // Number of remaining requests in the current rate limit window + HeaderXRateLimitReset Header = "X-RateLimit-Reset" // Time at which the rate limit resets + HeaderXRealIP Header = "X-Real-IP" // The original client IP address + HeaderXRequestID Header = "X-Request-ID" // Unique request ID for tracing + HeaderXRobotsTag Header = "X-Robots-Tag" // Search engine control + HeaderXUACompatible Header = "X-UA-Compatible" // Compatibility mode for Internet Explorer + HeaderXXSSProtection Header = "X-XSS-Protection" +) + +func (h Header) String() string { + return string(h) +} + +func (h Header) Add(header http.Header, value string) { + header.Add(h.String(), value) +} + +func (h Header) Get(header http.Header) string { + return header.Get(h.String()) +} From 51b075f1efd58a31fe367845a7d3dca0fa4c5ec7 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 23 Sep 2024 08:15:46 +0200 Subject: [PATCH 2/4] chore: update golangci --- .golangci.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b10875e..0436b5e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -84,14 +84,13 @@ linters: - bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false] - containedctx # containedctx is a linter that detects struct contained context.Context field [fast: false, auto-fix: false] - contextcheck # check whether the function uses a non-inherited context [fast: false, auto-fix: false] - #- copyloopvar # (go >= 1.22) copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false] + - copyloopvar # (go >= 1.22) copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false] - decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false] - durationcheck # check for two durations multiplied together [fast: false, auto-fix: false] - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omitted. [fast: false, auto-fix: false] - errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false] - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false] - exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false] - - exportloopref # checks for pointers to enclosing loop variables [fast: false, auto-fix: false] - forbidigo # Forbids identifiers [fast: false, auto-fix: false] - forcetypeassert # finds forced type assertions [fast: true, auto-fix: false] - gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false] @@ -109,7 +108,7 @@ linters: - grouper # Analyze expression groups. [fast: true, auto-fix: false] - importas # Enforces consistent import aliases [fast: false, auto-fix: false] - inamedparam # reports interfaces with unnamed method parameters [fast: true, auto-fix: false] - #- intrange # (go >= 1.22) intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false] + - intrange # (go >= 1.22) intrange is a linter to find places where for loops could make use of an integer range. [fast: true, auto-fix: false] - loggercheck # (logrlint) Checks key value pairs for common logger libraries (kitlog,klog,logr,zap). [fast: false, auto-fix: false] - makezero # Finds slice declarations with non-zero initial length [fast: false, auto-fix: false] - misspell # Finds commonly misspelled English words [fast: true, auto-fix: true] From e06afd47794c5018e58b02634b8c43044f6cb36a Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 23 Sep 2024 08:23:43 +0200 Subject: [PATCH 3/4] fix: lint issue --- http/authprefix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http/authprefix.go b/http/authprefix.go index 0a35208..34b0219 100644 --- a/http/authprefix.go +++ b/http/authprefix.go @@ -13,7 +13,7 @@ const ( AuthPrefixNegotiate AuthPrefix = "Negotiate" // SPNEGO-based Kerberos and NTLM Authentication AuthPrefixOAuth AuthPrefix = "OAuth" // OAuth 1.0 Authentication AuthPrefixSCRAMSHA256 AuthPrefix = "SCRAM-SHA-256" // Salted Challenge Response Authentication Mechanism (SCRAM) - AuthPrefixApiKey AuthPrefix = "ApiKey" // Custom API key-based authentication + AuthPrefixAPIKey AuthPrefix = "ApiKey" // Custom API key-based authentication ) func (p AuthPrefix) String() string { From 7745b1e8ce7c5263c271e9b64471933782a45e45 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 23 Sep 2024 08:23:51 +0200 Subject: [PATCH 4/4] feat: go 1.22 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 536059d..9ef3955 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/foomo/gostandards -go 1.21 +go 1.22 require github.com/stretchr/testify v1.9.0