-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandling.go
74 lines (58 loc) · 1.32 KB
/
handling.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package aspen
import (
"fmt"
"net/http"
"regexp"
)
const (
internalAcceptHeader = "X-AspenGo-Accept"
pathTransHeader = "X-HTTP-Path-Translated"
)
var (
vPathPart = regexp.MustCompile("%([a-zA-Z_][-a-zA-Z0-9_]*)")
vPathPartRep = "(?P<$1>[a-zA-Z_][-a-zA-Z0-9_]*)"
nonAlNumDash = regexp.MustCompile("[^-a-zA-Z0-9]")
)
type handlerFuncRegistration struct {
RequestPath string
HandlerFunc http.HandlerFunc
Negotiated bool
Virtual bool
Regexp bool
w *Website
}
func serve404(w http.ResponseWriter, req *http.Request) {
charset := req.Header.Get("X-AspenGo-CharsetDynamic")
if len(charset) == 0 {
charset = "utf-8"
}
w.Header().Set("Content-Type", fmt.Sprintf("text/html; charset=%v", charset))
w.WriteHeader(http.StatusNotFound)
w.Write(http404Response)
}
// ripped right out of net/http/server.go, matches paths to longest similar
// path, which isn't exactly what we want.
func stdPathMatch(pattern, p string) bool {
n := len(pattern)
if n == 0 {
return false
}
if pattern[n-1] != '/' {
return pattern == p
}
return len(p) >= n && p[0:n] == pattern
}
func pathMatch(pattern, p string) bool {
n := len(pattern)
pathLen := len(p)
if n == 0 {
return false
}
if pattern[n-1] == '/' {
pattern = pattern[:n-1]
}
if p[pathLen-1] == '/' {
p = p[:pathLen-1]
}
return p == pattern
}