Documentation
¶
Overview ¶
Package httputil provides HTTP-related validation utilities and constants.
Index ¶
- Constants
- Variables
- func IsExtensionKey(key string) bool
- func IsNumericStatusCode(code string) bool
- func IsStandardStatusCode(code string) bool
- func IsStatusCode(code string) bool
- func IsSuccessStatusCode(code string) bool
- func IsValidMediaType(mediaType string) bool
- func IsWildcardStatusCode(code string) bool
- func MediaTypeRank(mediaType string) int
- func PreferredMediaType(a, b string) string
Constants ¶
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
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
const ( // MediaTypeRankJSON is application/json itself. MediaTypeRankJSON = iota // MediaTypeRankJSONSuffix is a JSON structured syntax suffix, such as // application/problem+json or application/ld+json. MediaTypeRankJSONSuffix // MediaTypeRankOther is everything else. MediaTypeRankOther )
Media type ranks, lowest first, for a target that admits only one media type where the source offered several.
const ExtensionPrefix = "x-"
ExtensionPrefix marks a specification extension field.
const MediaTypeJSON = "application/json"
MediaTypeJSON is the media type a Schema Object describes directly, and the default this codebase falls back to.
const ResponsesKeyDefault = "default"
ResponsesKeyDefault is the Responses Object key holding the default response.
Variables ¶
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
IsExtensionKey reports whether key names a specification extension.
func IsNumericStatusCode ¶ added in v1.60.0
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 ¶
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
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
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 ¶
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
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.
func MediaTypeRank ¶ added in v1.64.0
MediaTypeRank orders media types by how faithfully one Schema Object describes them. JSON wins because a Schema Object describes JSON, and because every other part of this codebase already defaults to it.
Parameters are ignored, so application/json; charset=utf-8 ranks as JSON. A media type that does not parse is ranked last rather than rejected, since the caller is choosing between what a document actually offers, and a name ending in +json is not a JSON media type if it is not a media type at all.
func PreferredMediaType ¶ added in v1.64.0
PreferredMediaType reports which of two media types a single-schema target should keep. Rank decides it, and the name breaks a tie so the choice does not depend on map iteration order.
Types ¶
This section is empty.