~samwhited/mux

ref: v0.0.4 mux/norm.go -rw-r--r-- 2.4 KiB
fee92349Sam Whited mux: add changelog and release v0.0.4 7 months ago
                                                                                
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package mux

import (
	"context"
	"errors"
	"net/http"
	"strings"
)

var (
	errNoRoute = errors.New("mux: no route was found in the context")
	errNoParam = errors.New("mux: context was missing an expected parameter")
)

// WithParam returns a shallow copy of r with a new context that shadows the
// given route parameter.
// If the parameter does not exist, the original request is returned unaltered.
//
// Because WithParam is used to normalize request parameters after the route
// has already been resolved, all replaced parameters are of type string.
func WithParam(r *http.Request, name, val string) *http.Request {
	pinfo := Param(r, name)
	if pinfo.Value == nil {
		return r
	}

	pinfo.Value = val
	pinfo.Raw = val
	pinfo.Type = typString
	return r.WithContext(context.WithValue(r.Context(), ctxParam(name), pinfo))
}

// Path returns the request path by applying the route parameters found in the
// context to the route used to match the given request.
// This value may be different from r.URL.Path if some form of normalization has
// been applied to a route parameter, in which case the user may choose to issue
// a redirect to the canonical path.
func Path(r *http.Request) (string, error) {
	route := r.Context().Value(ctxRoute{}).(string)
	if route == "" {
		return "", errNoRoute
	}
	hasTrailingSlash := strings.HasSuffix(route, "/")
	oldPath := strings.TrimPrefix(r.URL.Path, "/")

	var canonicalPath strings.Builder
	// Give us a comfortable capacity so that we have to resize the buffer less
	// often.
	canonicalPath.Grow(len(route))

	for {
		var component, pathComponent string
		pathComponent, oldPath = nextPart(oldPath)

		component, route = nextPart(route)
		if component == "" {
			// Add back any trailing slash consumed by nextPart.
			if hasTrailingSlash {
				err := canonicalPath.WriteByte('/')
				if err != nil {
					return "", err
				}
			}
			break
		}
		err := canonicalPath.WriteByte('/')
		if err != nil {
			return "", err
		}
		name, typ := parseParam(component)
		switch {
		case typ == typStatic:
			_, err = canonicalPath.WriteString(name)
			if err != nil {
				return "", err
			}
		case name == "":
			_, err = canonicalPath.WriteString(pathComponent)
			if err != nil {
				return "", err
			}
		default:
			pinfo := Param(r, name)
			if pinfo.Value == nil {
				return "", errNoParam
			}
			_, err = canonicalPath.WriteString(pinfo.Raw)
			if err != nil {
				return "", err
			}
		}
	}

	return canonicalPath.String(), nil
}