cachecontrol

package
v0.0.0-...-59b23c3 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cachecontrol implements tokenization and parsing of Cache-Control header directives based on a relaxed version of RFC 9111, similar to the implementations in major web browser engines (Chromium, Firefox, Safari).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(s string) iter.Seq[Directive]

Parse parses the given Cache-Control value and returns a sequence of directives.

The parsing uses Tokenize to extract tokens from the given string and tries its best to form directives even from non RFC9111 compliant inputs.

Notably a directive like `directive1=value "with" space,directive2` will _correctly_ parse the first directive with the value `value "with" space`.

func Tokenize

func Tokenize(s string) iter.Seq[Token]

Tokenize takes a string of Cache-Control directives and returns a sequence of tokens.

For quoted strings, it will return a token containing the unquoted, unescaped text.

For quoted strings without ending quoted, it will read until the next comma or the end of the string.

Types

type Directive

type Directive struct {
	// Name of the directive. May be empty if HasValue is true.
	Name string

	// Value of the directive, if any. May be empty. Check HasValue to differentiate between an empty and no value.
	Value string

	// HasValue is true if Value is set.
	HasValue bool
}

Directive represents a single Cache-Control directive with optional value.

type Token

type Token struct {
	// Type is the type of the token.
	Type TokenType

	// Start is the byte offset in the input string where the token starts.
	Start int

	// End is the byte offset in the input string where the token ends.
	End int

	// Text is the text of the token.
	//
	// For tokens of type [TokenTypeText], if the parsed text was a quoted string, this contains the unescaped string
	// without quotes.
	//
	// For [TokenTypeComma] and [TokenTypeEquals] this contains a single "," or "=".
	//
	// For [TokenTypeSpace] this contains the spaces, including control characters.
	Text string
}

Token represents a parsed token from a string of Cache-Control directives.

type TokenType

type TokenType uint8

TokenType is an enum of token types as understood by Tokenize.

const (
	// TokenTypeInvalid is the zero value of [TokenType] and is not a valid value.
	// [Tokenize] will never return a token of this type.
	TokenTypeInvalid TokenType = iota

	// TokenTypeComma represents a single comma.
	TokenTypeComma

	// TokenTypeEquals represents a single equals sign.
	TokenTypeEquals

	// TokenTypeSpace represents one or more spaces or control characters.
	TokenTypeSpace

	// TokenTypeText represents a text value. This can be either a quoted-string or an unquoted value.
	TokenTypeText
)

func (TokenType) String

func (t TokenType) String() string

String implements the fmt.Stringer interface.

Jump to

Keyboard shortcuts

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