muxpatterns

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: BSD-3-Clause Imports: 16 Imported by: 9

README

muxpatterns

This repo holds a sample implementation of the enhanced http.ServeMux routing patterns that are being discussed at https://github.com/golang/go/discussions/60227.

The ServeMux type should behave like http.ServeMux with the additional features described in the discussion top post.

The Pattern type is used internally. It is exported here for experimentation.

The DescribeRelationship function will not be part of the proposed API, but it can help in understanding how patterns are related.

Documentation

Overview

Package muxpatterns is a sample implementation of enhanced http.ServeMux routing patterns. See https://github.com/golang/go/discussions/60227.

The API in this package is for experimentation only. It is likely that none of it will be in the proposal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DescribeRelationship added in v0.1.2

func DescribeRelationship(pat1, pat2 string) string

DescribeRelationship returns a string that describes how pat1 and pat2 are related.

func PathValue added in v0.3.0

func PathValue(r *http.Request, name string) string

PathValue returns the value for the named path wildcard in the pattern that matched the request. If there is no matched wildcard with the name, PathValue returns the empty string.

In the actual implementation, this will be a method on Request.

Types

type Pattern

type Pattern struct {
	// contains filtered or unexported fields
}

A Pattern is something that can be matched against an HTTP request.

func Parse

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

Parse parses a string into a Pattern. The string's syntax is

[METHOD] [HOST]/[PATH]

where:

  • METHOD is the uppercase name of 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) ConflictsWith

func (p1 *Pattern) ConflictsWith(p2 *Pattern) bool

ConflictsWith reports whether p1 conflicts with p2, that is, whether there is a request that both match but where neither is higher precedence than the other.

func (*Pattern) HigherPrecedence

func (p1 *Pattern) HigherPrecedence(p2 *Pattern) bool

HigherPrecedence reports whether p1 has higher precedence than p2. If p1 and p2 both match a request, then p1 will be chosen.

Precedence is defined by these rules:

  1. Patterns with a host win over patterns without a host.
  2. Patterns whose method and path is more specific win. One pattern is more specific than another if the second matches all the (method, path) pairs of the first and more.

func (*Pattern) Method

func (p *Pattern) Method() string

func (*Pattern) String

func (p *Pattern) String() string

type ServeMux added in v0.2.0

type ServeMux struct {
	// contains filtered or unexported fields
}

ServeMux is an HTTP request multiplexer. It behaves like net/http.ServeMux, but using the enhanced patterns of this package.

func NewServeMux added in v0.2.0

func NewServeMux() *ServeMux

func (*ServeMux) Handle added in v0.2.0

func (mux *ServeMux) Handle(pattern string, handler http.Handler)

func (*ServeMux) HandleFunc added in v0.2.0

func (mux *ServeMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

func (*ServeMux) Handler added in v0.2.0

func (mux *ServeMux) Handler(r *http.Request) (h http.Handler, pattern string)

func (*ServeMux) PathValue added in v0.2.0

func (mux *ServeMux) PathValue(r *http.Request, name string) string

PathValue calls the top-level PathValue function. deprecated: use PathValue.

func (*ServeMux) ServeHTTP added in v0.2.0

func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ServeMux) SetPathValue added in v0.3.0

func (mux *ServeMux) SetPathValue(r *http.Request, name, value string)

SetPathValue sets the value for path element name in r.

This is a method on ServeMux only for demo purposes. In the actual implementation, it will be a method on Request.

Jump to

Keyboard shortcuts

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