SplitPathSignature splits signature and path components from the request URI
(r *http.Request)
| 12 | |
| 13 | // SplitPathSignature splits signature and path components from the request URI |
| 14 | func SplitPathSignature(r *http.Request) (string, string, error) { |
| 15 | uri := r.RequestURI |
| 16 | |
| 17 | // cut query params |
| 18 | uri, _, _ = strings.Cut(uri, "?") |
| 19 | |
| 20 | // Cut path prefix. |
| 21 | // r.Pattern is set by the router and contains both global and route-specific prefixes combined. |
| 22 | if len(r.Pattern) > 0 { |
| 23 | uri = strings.TrimPrefix(uri, r.Pattern) |
| 24 | } |
| 25 | |
| 26 | // cut leading slash |
| 27 | uri = strings.TrimPrefix(uri, "/") |
| 28 | |
| 29 | signature, path, _ := strings.Cut(uri, "/") |
| 30 | if len(signature) == 0 || len(path) == 0 { |
| 31 | return "", "", NewInvalidPathError(r.Context(), path) |
| 32 | } |
| 33 | |
| 34 | // restore broken slashes in the path |
| 35 | path = RedenormalizePath(path) |
| 36 | |
| 37 | return path, signature, nil |
| 38 | } |
| 39 | |
| 40 | // RedenormalizePath undoes path normalization done by some browsers and revers proxies. |
| 41 | // Also trims leading slash if it is present. |