-
Notifications
You must be signed in to change notification settings - Fork 56
/
handler.go
206 lines (170 loc) · 3.58 KB
/
handler.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
package serverHandler
import (
"../param"
"../serverErrHandler"
"../serverLog"
"../tpl"
"../user"
"net/http"
"regexp"
"strings"
)
type handler struct {
root string
emptyRoot bool
globalHsts bool
globalHttps bool
httpsPort string // with prefix ":"
defaultSort string
urlPrefix string
dirIndexes []string
aliases aliases
globalHeaders [][2]string
globalUpload bool
uploadUrls []string
uploadDirs []string
globalMkdir bool
mkdirUrls []string
mkdirDirs []string
globalDelete bool
deleteUrls []string
deleteDirs []string
globalArchive bool
archiveUrls []string
archiveDirs []string
globalCors bool
corsUrls []string
corsDirs []string
globalAuth bool
authUrls []string
authDirs []string
users user.List
shows *regexp.Regexp
showDirs *regexp.Regexp
showFiles *regexp.Regexp
hides *regexp.Regexp
hideDirs *regexp.Regexp
hideFiles *regexp.Regexp
theme tpl.Theme
logger *serverLog.Logger
errHandler *serverErrHandler.ErrHandler
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
go h.logRequest(r)
// hsts redirect
if h.globalHsts && h.hsts(w, r) {
return
}
// https redirect
if h.globalHttps && h.https(w, r) {
return
}
// asset
const assetPrefix = "asset="
if strings.HasPrefix(r.URL.RawQuery, assetPrefix) {
assetPath := r.URL.RawQuery[len(assetPrefix):]
h.asset(w, r, assetPath)
return
}
// data
data := h.getResponseData(r)
if len(data.errors) > 0 {
go h.logger.LogErrors(data.errors...)
}
file := data.File
if file != nil {
defer file.Close()
}
if data.NeedAuth && !h.auth(w, r, data) {
return
}
h.header(w)
if data.CanCors {
h.cors(w)
}
if data.IsMutate {
h.mutate(w, r, data)
return
}
// regular flows
if len(r.URL.RawQuery) >= 3 {
switch r.URL.RawQuery[:3] {
case "tar":
h.tar(w, r, data)
return
case "tgz":
h.tgz(w, r, data)
return
case "zip":
h.zip(w, r, data)
return
}
}
item := data.Item
if data.WantJson {
h.json(w, r, data)
} else if file != nil && item != nil && !item.IsDir() {
h.content(w, r, data)
} else {
h.page(w, r, data)
}
}
func newHandler(
p *param.Param,
root string,
urlPrefix string,
allAliases aliases,
users user.List,
theme tpl.Theme,
logger *serverLog.Logger,
errHandler *serverErrHandler.ErrHandler,
) http.Handler {
emptyRoot := p.EmptyRoot && urlPrefix == "/"
aliases := aliases{}
for _, alias := range allAliases {
if alias.isSuccessorOf(urlPrefix) {
aliases = append(aliases, alias)
}
}
h := &handler{
root: root,
emptyRoot: emptyRoot,
globalHsts: p.GlobalHsts,
globalHttps: p.GlobalHttps,
httpsPort: p.HttpsPort,
defaultSort: p.DefaultSort,
urlPrefix: urlPrefix,
aliases: aliases,
dirIndexes: p.DirIndexes,
globalHeaders: p.GlobalHeaders,
globalUpload: p.GlobalUpload,
uploadUrls: p.UploadUrls,
uploadDirs: p.UploadDirs,
globalMkdir: p.GlobalMkdir,
mkdirUrls: p.MkdirUrls,
mkdirDirs: p.MkdirDirs,
globalDelete: p.GlobalDelete,
deleteUrls: p.DeleteUrls,
deleteDirs: p.DeleteDirs,
globalArchive: p.GlobalArchive,
archiveUrls: p.ArchiveUrls,
archiveDirs: p.ArchiveDirs,
globalCors: p.GlobalCors,
corsUrls: p.CorsUrls,
corsDirs: p.CorsDirs,
globalAuth: p.GlobalAuth,
authUrls: p.AuthUrls,
authDirs: p.AuthDirs,
users: users,
shows: p.Shows,
showDirs: p.ShowDirs,
showFiles: p.ShowFiles,
hides: p.Hides,
hideDirs: p.HideDirs,
hideFiles: p.HideFiles,
theme: theme,
logger: logger,
errHandler: errHandler,
}
return h
}