-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatic.go
251 lines (197 loc) · 5.19 KB
/
static.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package aspen
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"path"
"strings"
)
type websiteStaticHandler struct {
w *Website
nh pipelineHandler
}
type directoryListing struct {
RequestPath string
FullPath string
Entries []*directoryListingEntry
}
type directoryListingEntry struct {
RequestPath string
LinkName string
FileInfo os.FileInfo
}
type serveDirError struct {
Path string
}
func (me *websiteStaticHandler) NextHandler() pipelineHandler {
return me.nh
}
func (me *websiteStaticHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
debugf("Handling static request for %q", req.URL.Path)
fullPath := path.Join(me.w.WwwRoot, strings.TrimLeft(req.URL.Path, "/"))
req.Header.Set(pathTransHeader, fullPath)
err := me.serveStatic(w, req)
if err == nil {
return
}
debugf("Serving static failed, so checking if directory listing is appropriate")
if _, ok := err.(*serveDirError); ok && me.w.ListDirs {
err = me.serveDirListing(w, req)
if err == nil {
return
}
}
if strings.HasSuffix(req.URL.Path, "/favicon.ico") {
debugf("Serving canned favicon response for %q", req.URL.Path)
w.Header().Set("Content-Type", "image/x-icon")
w.WriteHeader(http.StatusOK)
w.Write(faviconIco)
return
}
debugf("Falling through to 404 for %q!", req.URL.Path)
serve404(w, req)
}
func (me *websiteStaticHandler) String() string {
return fmt.Sprintf("*websiteStaticHandler{w.WwwRoot: %q}", me.w.WwwRoot)
}
func (me *websiteStaticHandler) serveStatic(w http.ResponseWriter, req *http.Request) error {
fullPath, err := me.findStaticPath(req)
if err != nil {
return err
}
debugf("Found static path %q from root %q and request path %q",
fullPath, me.w.WwwRoot, req.URL.Path)
fi, err := os.Stat(fullPath)
if err != nil {
return err
}
if fi.IsDir() {
return &serveDirError{Path: fullPath}
}
ctype := mime.TypeByExtension(path.Ext(fullPath))
if strings.HasPrefix(ctype, "text/") && !strings.Contains(ctype, "charset=") {
ctype = fmt.Sprintf("%v; charset=utf-8", ctype)
}
outf, err := os.Open(fullPath)
if err != nil {
debugf("Could not open %q", fullPath)
return err
}
defer outf.Close()
w.Header().Set("Content-Length", fmt.Sprintf("%v", fi.Size()))
w.Header().Set("Content-Type", ctype)
w.WriteHeader(http.StatusOK)
io.Copy(w, outf)
return nil
}
func (me *websiteStaticHandler) serveDirListing(w http.ResponseWriter,
req *http.Request) error {
debugf("Serving directory listing for %q", req.URL.Path)
fullPath := req.Header.Get(pathTransHeader)
if len(fullPath) == 0 {
fullPath = path.Join(me.w.WwwRoot, strings.TrimLeft(req.URL.Path, "/"))
}
fi, err := os.Stat(fullPath)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("%q is not a directory!", fullPath)
}
dirListing, err := newDirListing(req.URL.Path, fullPath)
if err != nil {
return err
}
htmlListing, err := dirListing.Html()
if err != nil {
return err
}
w.Header().Set("Content-Length", fmt.Sprintf("%v", len(htmlListing)))
w.Header().Set("Content-Type",
fmt.Sprintf("text/html; charset=%v", me.w.CharsetDynamic))
w.WriteHeader(http.StatusOK)
w.Write(htmlListing)
return nil
}
func (me *websiteStaticHandler) findStaticPath(req *http.Request) (string, error) {
fullPath := req.Header.Get(pathTransHeader)
if len(fullPath) == 0 {
fullPath = path.Join(me.w.WwwRoot, strings.TrimLeft(req.URL.Path, "/"))
}
fi, err := os.Stat(fullPath)
if _, ok := err.(*os.PathError); ok {
debugf("Failed to stat %q: %v", fullPath, err)
return "", err
}
if fi.IsDir() {
debugf("%q is a directory", fullPath)
debugf("Looking for candidate index files. Configured indices = %+v",
me.w.Indices)
for _, idx := range me.w.Indices {
if len(idx) == 0 {
continue
}
tryFullPath := path.Join(fullPath, idx)
debugf("Checking for candidate index file at %q", tryFullPath)
fi, err := os.Stat(tryFullPath)
if err != nil || fi.IsDir() {
continue
}
debugf("Found candidate index file at %q", tryFullPath)
return tryFullPath, nil
}
}
return fullPath, nil
}
func newDirListing(requestPath, dirPath string) (*directoryListing, error) {
entries, err := ioutil.ReadDir(dirPath)
if err != nil {
return nil, err
}
dlEntries := []*directoryListingEntry{}
for _, ent := range entries {
reqPath := path.Join(requestPath, ent.Name())
linkName := ent.Name()
if ent.IsDir() {
reqPath = reqPath + "/"
linkName = linkName + "/"
}
dlEnt := &directoryListingEntry{
RequestPath: reqPath,
LinkName: linkName,
FileInfo: ent,
}
dlEntries = append(dlEntries, dlEnt)
}
dl := &directoryListing{
RequestPath: requestPath,
FullPath: dirPath,
Entries: dlEntries,
}
return dl, nil
}
func (me *directoryListing) Html() ([]byte, error) {
var buf bytes.Buffer
err := directoryListingTmpl.Execute(&buf, me)
if err != nil {
return []byte(""), err
}
return buf.Bytes(), nil
}
func (me *directoryListing) WebParentDir() string {
if me.RequestPath == "/" {
return "/"
}
parDir := path.Dir(strings.TrimRight(me.RequestPath, "/"))
if parDir == "/" {
return "/"
}
return parDir + "/"
}
func (me *serveDirError) Error() string {
return fmt.Sprintf("Directory %q cannot be served!", me.Path)
}