or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bpf.mdcontext-ctxhttp.mdcontext.mddict.mddns-dnsmessage.mdhtml-atom.mdhtml-charset.mdhtml.mdhttp-httpguts.mdhttp-httpproxy.mdhttp2-h2c.mdhttp2-hpack.mdhttp2.mdicmp.mdidna.mdindex.mdipv4.mdipv6.mdnettest.mdnetutil.mdproxy.mdpublicsuffix.mdquic-qlog.mdquic.mdtrace.mdwebdav.mdwebsocket.mdxsrftoken.md
tile.json

http-httpguts.mddocs/

HTTP Internals

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

import "golang.org/x/net/http/httpguts"

Functions

// 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) bool

Function Details

HeaderValuesContainsToken

HeaderValuesContainsToken reports whether any string in values contains the provided token, ASCII case-insensitively.

ValidHeaderFieldName

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:

  • header-field = field-name ":" OWS field-value OWS
  • field-name = token
  • token = 1*tchar
  • tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA

ValidHeaderFieldValue

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

ValidHostHeader reports whether h is a valid host header.

ValidTrailerHeader

ValidTrailerHeader reports whether name is a valid header field name to appear in trailers. See RFC 7230, Section 4.1.2.

PunycodeHostPort

PunycodeHostPort returns the IDNA Punycode version of the provided "host" or "host:port" string.

Usage Examples

Validating HTTP Headers

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
}

Checking for Header Tokens

func hasUpgrade(headerValues []string) bool {
    return httpguts.HeaderValuesContainsToken(headerValues, "upgrade")
}

// Usage:
// connectionValues := []string{"upgrade", "keep-alive"}
// if hasUpgrade(connectionValues) {
//     // Handle upgrade
// }

Converting Host Names to Punycode

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"

Validating Trailer Headers

func canUseAsTrailer(headerName string) bool {
    return httpguts.ValidTrailerHeader(headerName)
}