-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
146 lines (123 loc) · 3.04 KB
/
serve.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package static
import (
"fmt"
"io"
"log"
"mime"
"net/http"
"path/filepath"
"regexp"
"strings"
)
var (
reHash = regexp.MustCompile(`([a-z0-9A-Z]{6,24})`)
)
func (c *ServeFs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var endsWithSlash bool
p := c.AddPrefix + r.URL.Path
if strings.HasSuffix(p, "/index.html") {
// don't support direct loading
w.WriteHeader(http.StatusNotFound)
return
}
head := w.Header()
if strings.HasSuffix(p, "/") {
p += "index.html"
endsWithSlash = true
}
p = strings.TrimPrefix(p, "/")
info, reader := c.Content.Get(p)
if info == nil && !endsWithSlash {
checkP := strings.TrimPrefix(p+"/index.html", "/")
indexExists := c.Content.Exists(checkP)
if indexExists {
// we know that the next call will add "index.html" to this
head.Set("Location", p+"/")
w.WriteHeader(http.StatusSeeOther)
return
}
}
serve404 := false
if info == nil {
if !endsWithSlash {
// not inferred as a HTML page
w.WriteHeader(http.StatusNotFound)
return
}
serve404 = true
if c.HtmlNotFoundPath != "" {
info, reader = c.Content.Get(c.HtmlNotFoundPath)
}
}
// copy headers (do direct, already canonicalized)
for h, already := range info.Header {
head[h] = append(head[h], already...)
}
// frames (default deny)
if !c.AllowFrame {
head.Set("X-Frame-Options", "deny")
}
// include Content-Type
ct := info.ContentType
if ct == "" {
ct = mime.TypeByExtension(filepath.Ext(p))
}
if ct != "" {
head.Set("Content-Type", ct)
}
isHtml := ct == "text/html" || strings.HasPrefix(ct, "text/html;")
// include Etag if we have one
cacheForever := false
if info.Hash != "" && !serve404 {
head.Set("ETag", info.Hash)
if !info.ContentHash {
cacheForever = true
}
}
// if we have a query-string that looks like a hash only (i.e., no & etc) then cache forever
if !serve404 && !cacheForever && r.URL.RawQuery != "" && reHash.MatchString(r.URL.RawQuery) {
cacheForever = true
}
if cacheForever {
head.Set("Cache-Control", "public, max-age=7776000, immutable")
}
isNoneMatch := !serve404 && info.Hash != "" && r.Header.Get("If-None-Match") == info.Hash
if c.UpdateHeader != nil {
c.UpdateHeader(head, ServeInfo{
FileInfo: *info,
Is404: serve404,
IsHtml: isHtml,
IsHead: r.Method == http.MethodHead,
IfNoneMatch: isNoneMatch,
CacheForever: cacheForever,
})
}
// check etag
if isNoneMatch {
w.WriteHeader(http.StatusNotModified)
return
}
// if 404 (html or not) explicitly write header
if serve404 {
w.WriteHeader(http.StatusNotFound)
}
if r.Method == "HEAD" || reader == nil {
return // don't serve bytes
}
_, err := io.Copy(w, reader)
if err != nil {
log.Printf("couldn't write bytes: p=%v %v", p, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// add etag to html entrypoints
if info.ContentHash && info.Hash != "" {
if isHtml {
fmt.Fprintf(w, "<!--:%s:-->\n", info.Hash)
}
}
}