summaryrefslogtreecommitdiff
path: root/pathrouter/context.go
blob: 7408960eb9058a627a2bdcb164fa6cbb981382dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package pathrouter

import (
	"context"
	"maps"
	"net/http"
)

type routingContextKeyType struct{}

var routingContextKey = any(routingContextKeyType{})

// routingContext contains information about an HTTP request that has been processed by matchers from this package.
// It is stored in `r.Body` of the HTTP request by wrapping the original response body's behavior.
type routingContext struct {
	vars map[string]string
}

func injectContext(r *http.Request) (*http.Request, routingContext) {
	// NOTE: Using context.WithValue() and thus extending the chain within r.Context by another element
	//       incurs a performance penalty whenever `r.Context` is used, but it's the option that sucks the least.
	//       We initially had `type HandlerFunc = func(http.ResponseWriter, *http.Request, pathrouter.Context)`
	//       to avoid this performance penalty, but having another thing that's called "Context" is confusing.
	//       The final API implementation accepts a slight performance decrease to prioritize developer ergonomics.
	//
	//       The only other option that does not add an extra argument to the HandlerFunc signature is to stash
	//       `type routingContext` within `r.Body`, but that would incur the performance penalty on every use of
	//       `r.Body` instead of `r.Context()`, which is usually much worse.

	rc := routingContext{
		vars: make(map[string]string),
	}
	ctx := context.WithValue(r.Context(), routingContextKey, rc)
	return r.WithContext(ctx), rc
}

// VariableValue returns the path element(s) collected by a [Variable] or [CatchAllVariable] matcher of the same name,
// with any URL escaping removed.
// If no matcher with this name appeared in the path leading up to the handler, the empty string is returned.
//
// Panics if r is not a request that was routed by a [Matcher] from this package.
func VariableValue(r *http.Request, name string) string {
	rc, ok := r.Context().Value(routingContextKey).(routingContext)
	if !ok {
		panic("request was not routed by gg/pathrouter")
	}
	return rc.vars[name]
}

// VariableValues returns all path element(s) collected by [Variable] or [CatchAllVariable] matchers, keyed on the name of the matcher.
// The result is the set of all possible values that [VariableValue] can return.
//
// Panics if r is not a request that was routed by a [Matcher] from this package.
func VariableValues(r *http.Request) map[string]string {
	rc, ok := r.Context().Value(routingContextKey).(routingContext)
	if !ok {
		panic("request was not routed by gg/pathrouter")
	}
	return maps.Clone(rc.vars)
}