Supplementary Go networking libraries providing additional networking protocols and utilities
Package httpguts provides functions implementing various details of the HTTP specification.
This package is shared by the standard library (which vendors it) and x/net/http2. It comes with no API stability promise.
import "golang.org/x/net/http/httpguts"// HeaderValuesContainsToken reports whether any string in values contains the provided token
func HeaderValuesContainsToken(values []string, token string) bool
// IsTokenRune reports whether r is a valid token rune
func IsTokenRune(r rune) bool
// PunycodeHostPort returns the IDNA Punycode version of the provided "host" or "host:port" string
func PunycodeHostPort(v string) (string, error)
// ValidHeaderFieldName reports whether v is a valid HTTP/1.x header name
func ValidHeaderFieldName(v string) bool
// ValidHeaderFieldValue reports whether v is a valid "field-value"
func ValidHeaderFieldValue(v string) bool
// ValidHostHeader reports whether h is a valid host header
func ValidHostHeader(h string) bool
// ValidTrailerHeader reports whether name is a valid header field name to appear in trailers
func ValidTrailerHeader(name string) boolHeaderValuesContainsToken reports whether any string in values contains the provided token, ASCII case-insensitively.
ValidHeaderFieldName reports whether v is a valid HTTP/1.x header name. HTTP/2 imposes the additional restriction that uppercase ASCII letters are not allowed.
RFC 7230 defines:
ValidHeaderFieldValue reports whether v is a valid "field-value" according to RFC 2616 and RFC 7230. This function validates that header field values contain only permitted characters.
ValidHostHeader reports whether h is a valid host header.
ValidTrailerHeader reports whether name is a valid header field name to appear in trailers. See RFC 7230, Section 4.1.2.
PunycodeHostPort returns the IDNA Punycode version of the provided "host" or "host:port" string.
import (
"fmt"
"golang.org/x/net/http/httpguts"
)
func validateHeaders(name, value string) error {
if !httpguts.ValidHeaderFieldName(name) {
return fmt.Errorf("invalid header name: %s", name)
}
if !httpguts.ValidHeaderFieldValue(value) {
return fmt.Errorf("invalid header value: %s", value)
}
return nil
}func hasUpgrade(headerValues []string) bool {
return httpguts.HeaderValuesContainsToken(headerValues, "upgrade")
}
// Usage:
// connectionValues := []string{"upgrade", "keep-alive"}
// if hasUpgrade(connectionValues) {
// // Handle upgrade
// }func convertToPunycode(host string) (string, error) {
punyHost, err := httpguts.PunycodeHostPort(host)
if err != nil {
return "", err
}
return punyHost, nil
}
// Usage:
// punycode, err := convertToPunycode("münchen.de:8080")
// Result: "xn--mnchen-3ya.de:8080"func canUseAsTrailer(headerName string) bool {
return httpguts.ValidTrailerHeader(headerName)
}Install with Tessl CLI
npx tessl i tessl/golang-golang-org-x--netdocs