uris

package
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 CleanPath

func CleanPath(p string) string

CleanPath returns the canonical path for p, eliminating . and .. elements.

func CommonPath

func CommonPath(p1, p2 *Pattern) string

commonPath returns a path that both p1 and p2 match. It assumes there is such a path.

func DifferencePath

func DifferencePath(p1, p2 *Pattern) string

DifferencePath returns a path that p1 matches and p2 doesn't. It assumes there is such a path.

func IsValidMethod

func IsValidMethod(method string) bool

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

func ParsePattern(s string) (*Pattern, error)

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.

func (*Pattern) String

func (p *Pattern) String() string

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}

Jump to

Keyboard shortcuts

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