-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
73 lines (59 loc) · 1.36 KB
/
http.go
File metadata and controls
73 lines (59 loc) · 1.36 KB
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
package pages
import (
"net/http"
"strings"
"github.com/gowool/keratin"
)
const (
HeaderXPageDecorable = "X-Page-Decorable"
HeaderXPageNotDecorable = "X-Page-Not-Decorable"
)
type PatternArgsFunc func(*http.Request) []any
func PatternArgs() PatternArgsFunc {
return func(r *http.Request) (args []any) {
pattern := keratin.Pattern(r)
n := strings.Count(pattern, "{")
if n == 0 {
return
}
args = make([]any, 0, n*2)
var key strings.Builder
for _, c := range pattern {
switch c {
case '{':
key.Reset()
key.WriteRune(c)
case '.':
continue
case '}':
if key.Len() == 0 {
panic("invalid dynamic page pattern")
}
key.WriteRune(c)
param := key.String()
args = append(args, param, r.PathValue(param[1:len(param)-1]))
default:
if key.Len() > 0 {
key.WriteRune(c)
}
}
}
return
}
}
func IsDecorable(w http.ResponseWriter, r *http.Request) bool {
contentType := w.Header().Get(keratin.HeaderContentType)
if contentType != "" && !strings.HasPrefix(contentType, keratin.MIMETextHTML) {
return false
}
if w.Header().Get(HeaderXPageNotDecorable) == "1" {
return false
}
if w.Header().Get(HeaderXPageDecorable) == "1" {
return true
}
if r.Header.Get(keratin.HeaderXRequestedWith) == keratin.XMLHTTPRequest {
return false
}
return keratin.ResponseStatusCode(w) == http.StatusOK
}