/
var.go
50 lines (44 loc) · 1.1 KB
/
var.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
41
42
43
44
45
46
47
48
49
50
package darwini
import (
"net/http"
)
// Var multiplexes dynamically based on the next path segment. For Var
// at /path, /path will be forbidden, /path/ is served by Index or
// forbidden if nil, /path/seg and /path/seg/anything are served by
// the handler Child returns for seg, or not found if Child is nil or
// returns nil.
type Var struct {
Index http.Handler
Child func(seg string) http.Handler
}
var _ http.Handler = Var{}
func (v Var) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "" {
// Var does not serve "itself", ever. Use a wrapper Dir if you
// want that.
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
seg, rest := segment(req.URL.Path)
req.URL.Path = rest
if seg == "" {
if v.Index == nil {
// No index resource.
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
v.Index.ServeHTTP(w, req)
return
}
if v.Child == nil {
// No child resource.
http.Error(w, "Not found", http.StatusNotFound)
return
}
h := v.Child(seg)
if h == nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
h.ServeHTTP(w, req)
}