httputil

package
v1.60.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package httputil provides HTTP-related validation utilities and constants.

Index

Constants

View Source
const (
	StatusCodeLength = 3   // Standard length of HTTP status codes (e.g., "200", "404")
	MinStatusCode    = 100 // Minimum valid HTTP status code
	MaxStatusCode    = 599 // Maximum valid HTTP status code
	WildcardChar     = 'X' // Wildcard character used in status code patterns (e.g., "2XX")
)

HTTP Status Code Constants

View Source
const (
	MethodGet     = "get"
	MethodPut     = "put"
	MethodPost    = "post"
	MethodDelete  = "delete"
	MethodOptions = "options"
	MethodHead    = "head"
	MethodPatch   = "patch"
	MethodTrace   = "trace"   // OAS 3.0+ only
	MethodConnect = "connect" // Standard HTTP method, rarely used in APIs
	MethodQuery   = "query"   // OAS 3.2+ only
)

HTTP Method Constants

View Source
const ExtensionPrefix = "x-"

ExtensionPrefix marks a specification extension field.

View Source
const ResponsesKeyDefault = "default"

ResponsesKeyDefault is the Responses Object key holding the default response.

Variables

View Source
var StandardHTTPStatusCodes = map[string]bool{

	"100": true, "101": true, "102": true, "103": true,

	"200": true, "201": true, "202": true, "203": true, "204": true, "205": true,
	"206": true, "207": true, "208": true, "226": true,

	"300": true, "301": true, "302": true, "303": true, "304": true, "305": true,
	"307": true, "308": true,

	"400": true, "401": true, "402": true, "403": true, "404": true, "405": true,
	"406": true, "407": true, "408": true, "409": true, "410": true, "411": true,
	"412": true, "413": true, "414": true, "415": true, "416": true, "417": true,
	"418": true, "421": true, "422": true, "423": true, "424": true, "425": true,
	"426": true, "428": true, "429": true, "431": true, "451": true,

	"500": true, "501": true, "502": true, "503": true, "504": true, "505": true,
	"506": true, "507": true, "508": true, "510": true, "511": true,
}

StandardHTTPStatusCodes contains RFC 9110 officially defined HTTP status codes. These are used in strict mode validation to warn about non-standard codes.

Functions

func IsExtensionKey added in v1.60.0

func IsExtensionKey(key string) bool

IsExtensionKey reports whether key names a specification extension.

func IsNumericStatusCode added in v1.60.0

func IsNumericStatusCode(code string) bool

IsNumericStatusCode reports whether code is a numeric HTTP status code from MinStatusCode to MaxStatusCode. Every OAS version defines these, unlike the wildcard ranges IsWildcardStatusCode recognizes.

func IsStandardStatusCode

func IsStandardStatusCode(code string) bool

IsStandardStatusCode checks if a status code is a well-defined standard HTTP code. Returns true only for codes in StandardHTTPStatusCodes map.

func IsStatusCode added in v1.60.0

func IsStatusCode(code string) bool

IsStatusCode reports whether code is an HTTP status code or a wildcard range:

  • Wildcard patterns: 1XX, 2XX, 3XX, 4XX, 5XX
  • Numeric codes: 100-599

It is deliberately narrow, which is what a caller ranging Responses.Codes needs. "default" and specification extensions are legal Responses Object keys but are not status codes, so both return false here.

It is also version-blind, and the two forms it accepts are not defined by the same OAS versions. A caller that knows the document version wants IsNumericStatusCode and IsWildcardStatusCode instead.

func IsSuccessStatusCode added in v1.60.0

func IsSuccessStatusCode(code string) bool

IsSuccessStatusCode reports whether code denotes a successful response: a numeric 2xx code, or the 2XX wildcard range.

Only a status code can be a successful one, so anything IsStatusCode rejects is rejected here too. That matters for a caller ranging Responses.Codes, which may hold a key no decoder validated.

func IsValidMediaType

func IsValidMediaType(mediaType string) bool

IsValidMediaType validates a media type string according to RFC 2045/2046. Handles wildcards (*/* and type/*) and prevents invalid combinations (*/subtype).

func IsWildcardStatusCode added in v1.60.0

func IsWildcardStatusCode(code string) bool

IsWildcardStatusCode reports whether code is a wildcard response range: 1XX, 2XX, 3XX, 4XX or 5XX.

OAS 3.0 introduced these. The OAS 2.0 Responses Object states only that "any HTTP status code can be used as the property name (one property per HTTP status code)", so a 2.0 document may not use one.

https://spec.openapis.org/oas/v2.0.html#responses-object

Types

This section is empty.

Jump to

Keyboard shortcuts

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