/
mounts.go
40 lines (36 loc) · 1018 Bytes
/
mounts.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
37
38
39
40
package httptools
import (
"net/http"
"net/url"
"strings"
)
// Mounts is a list of handlers which are mounted at the given path.
// Mounting is a simple path prefix-based routing. The prefix will be
// stripped from the request before being passed to the associated handler.
// The original request path will available in the VarsResponseWriter
// under "OrigPath".
type Mounts map[string]http.Handler
func (m Mounts) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vrw, ok := w.(VarsResponseWriter)
if !ok {
vrw = newOurResponseWriter(w)
}
for path, h := range m {
path = strings.TrimRight(path, "/")
if strings.HasPrefix(r.URL.Path, path) {
// FIXME: This is a shallow copy
nr := *r
nr.URL = &url.URL{}
*nr.URL = *r.URL
nr.URL.Path = strings.TrimPrefix(nr.URL.Path, path)
if _, ok := vrw.Vars()["OrigPath"]; !ok {
vrw.Vars()["OrigPath"] = r.URL.Path
}
if h != nil {
h.ServeHTTP(vrw, &nr)
}
return
}
}
http.Error(vrw, "Not found", http.StatusNotFound)
}