-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_route.go
36 lines (32 loc) · 954 Bytes
/
http_route.go
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
package golib
import (
"net/http"
"regexp"
"github.com/gorilla/mux"
)
// Subroute returns the subroute form a Gorilla Mux Subrouter. It uses the
// escaped form of the path, so a path like /files/he%2Fnk/more will keep the
// %2F.
func Subroute(req *http.Request) string {
// Pln("route: %s", Route(req))
// Pln("rout2: %s", url.PathEscape(Route(req)))
// Pln("esc : %s", req.URL.EscapedPath())
return req.URL.EscapedPath()[len(Route(req)):]
}
// Route returns the route from a Gorilla Mux. It uses the escaped
// form of the path.
func Route(req *http.Request) string {
route := mux.CurrentRoute(req)
if route == nil {
return req.URL.Path
}
routeRegex, err := route.GetPathRegexp()
if err != nil {
panic(err)
}
reg := regexp.MustCompile(routeRegex)
escPath := req.URL.EscapedPath()
match := reg.FindString(escPath)
// Pln("Route: %s\nMatch: %v\ncut: %q", routeRegex, match, escPath[:len(match)])
return escPath[:len(match)]
}