Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrPatternParsing = results.Define("ERRBHUP001", "unable to parse pattern") //nolint:gochecknoglobals ErrInvalidWildcard = results.Define("ERRBHUP002", "invalid wildcard") //nolint:gochecknoglobals ErrInvalidMethod = results.Define("ERRBHUP003", "invalid method") //nolint:gochecknoglobals )
Functions ¶
func CommonPath ¶
commonPath returns a path that both p1 and p2 match. It assumes there is such a path.
func DifferencePath ¶
DifferencePath returns a path that p1 matches and p2 doesn't. It assumes there is such a path.
func IsValidMethod ¶
Types ¶
type Pattern ¶
type Pattern struct { Str string // original string Method string Host string Path string Loc string // source location of registering call, for helpful messages // The representation of a path differs from the surface syntax, which // simplifies most algorithms. // // Paths ending in '/' are represented with an anonymous "..." wildcard. // For example, the path "a/" is represented as a literal segment "a" followed // by a segment with multi==true. // // Paths ending in "{$}" are represented with the literal segment "/". // For example, the path "a/{$}" is represented as a literal segment "a" followed // by a literal segment "/". Segments []Segment }
A pattern is something that can be matched against an HTTP request. It has an optional method, an optional host, and a path.
func ParsePattern ¶
parsePattern parses a string into a Pattern. The string's syntax is
[METHOD] [HOST]/[PATH]
where:
- METHOD is an HTTP method
- HOST is a hostname
- PATH consists of slash-separated segments, where each segment is either a literal or a wildcard of the form "{name}", "{name...}", or "{$}".
METHOD, HOST and PATH are all optional; that is, the string can be "/". If METHOD is present, it must be followed by a single space. Wildcard names must be valid Go identifiers. The "{$}" and "{name...}" wildcard must occur at the end of PATH. PATH may end with a '/'. Wildcard names in a path must be distinct.
type Segment ¶
type Segment struct { Str string // literal or wildcard name or "/" for "/{$}". Wild bool Multi bool // "..." wildcard }
A segment is a pattern piece that matches one or more path segments, or a trailing slash.
If wild is false, it matches a literal segment, or, if s == "/", a trailing slash. Examples:
"a" => segment{s: "a"} "/{$}" => segment{s: "/"}
If wild is true and multi is false, it matches a single path segment. Example:
"{x}" => segment{s: "x", wild: true}
If both wild and multi are true, it matches all remaining path segments. Example:
"{rest...}" => segment{s: "rest", wild: true, multi: true}