httputil

package
v1.70.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httputil provides HTTP-related validation utilities and constants.

Index

Constants

View Source
const (
	// MediaTypeMatchAny is the range `*/*`.
	MediaTypeMatchAny = iota + 1
	// MediaTypeMatchType is a range such as `text/*`.
	MediaTypeMatchType
	// MediaTypeMatchExact names the media type itself.
	MediaTypeMatchExact
)

Specificity of a content key against a media type, lowest first. The specification says that for a request matching several keys "only the most specific key is applicable".

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 (
	// 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.

View Source
const ExtensionPrefix = "x-"

ExtensionPrefix marks a specification extension field.

View Source
const MediaTypeJSON = "application/json"

MediaTypeJSON is the media type a Schema Object describes directly, and the default this codebase falls back to.

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 EqualHeaderName added in v1.70.0

func EqualHeaderName(a, b string) bool

EqualHeaderName reports whether two header names are the same header. It is the equality of HeaderKey, so it agrees with a map keyed by it.

func EqualScheme added in v1.70.0

func EqualScheme(a, b string) bool

EqualScheme reports whether two HTTP authentication scheme names are the same scheme.

func HeaderKey added in v1.70.0

func HeaderKey(name string) string

HeaderKey returns the form under which a header name is compared and keyed: net/http's canonical form, which is the one a request's header map uses. A name that is not an HTTP token is returned as written.

func IsExtensionKey added in v1.60.0

func IsExtensionKey(key string) bool

IsExtensionKey reports whether key names a specification extension.

func IsIgnoredHeaderField added in v1.70.0

func IsIgnoredHeaderField(name string) bool

IsIgnoredHeaderField reports whether a response header, or an encoding header, of this name is one the specification says SHALL be ignored. Only Content-Type is: the other two names IsIgnoredHeaderParam lists are ordinary response headers.

func IsIgnoredHeaderParam added in v1.70.0

func IsIgnoredHeaderParam(name string) bool

IsIgnoredHeaderParam reports whether a header parameter of this name is one the specification says SHALL be ignored: Accept, Content-Type and Authorization.

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 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

func MatchMediaType added in v1.70.0

func MatchMediaType(pattern, mediaType string) bool

MatchMediaType reports whether a media type, or a media type range such as `text/*` or `*/*`, covers a media type. Parameters are ignored on both sides.

func MediaTypeKey added in v1.70.0

func MediaTypeKey(mediaType string) string

MediaTypeKey returns the form under which a media type is compared and keyed: type, subtype and parameter names in lower case, parameter values as written. Text that is not a media type, which includes the ranges `*/*` and `text/*` only where mime cannot parse them, is lower-cased whole.

func MediaTypeMatchRank added in v1.70.0

func MediaTypeMatchRank(pattern, mediaType string) (int, bool)

MediaTypeMatchRank reports how specifically a content key covers a media type, as one of the MediaTypeMatch constants, or false when it does not cover it. Parameters are ignored on both sides, so `application/*; charset=utf-8` is the range `application/*`.

func MediaTypeRank added in v1.64.0

func MediaTypeRank(mediaType string) int

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

func PreferredMediaType(a, b string) string

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.

Jump to

Keyboard shortcuts

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