-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
167 lines (140 loc) · 2.93 KB
/
main.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
package serverHandler
import (
"../param"
"../serverErrHandler"
"../serverLog"
"../user"
"html/template"
"net/http"
"regexp"
)
type handler struct {
root string
emptyRoot bool
urlPrefix string
aliases aliases
globalUpload bool
uploadUrls []string
uploadDirs []string
globalArchive bool
archiveUrls []string
archiveDirs []string
globalCors bool
corsUrls []string
corsDirs []string
globalAuth bool
authUrls []string
authDirs []string
users user.Users
shows *regexp.Regexp
showDirs *regexp.Regexp
showFiles *regexp.Regexp
hides *regexp.Regexp
hideDirs *regexp.Regexp
hideFiles *regexp.Regexp
template *template.Template
logger *serverLog.Logger
errHandler *serverErrHandler.ErrHandler
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
go h.logRequest(r)
// assert
if len(r.URL.RawQuery) > 0 {
queryValues := r.URL.Query()
assertPath := queryValues.Get("assert")
if len(assertPath) > 0 {
h.assert(w, r, assertPath)
return
}
}
// data
data := h.getResponseData(r)
if len(data.errors) > 0 {
go func() {
for _, err := range data.errors {
h.errHandler.LogError(err)
}
}()
}
file := data.File
if file != nil {
defer file.Close()
}
if data.NeedAuth && !h.auth(w, r) {
return
}
if data.CanCors {
h.cors(w, r)
}
if data.CanUpload && r.Method == http.MethodPost {
h.saveUploadFiles(data.handlerReqPath, r)
http.Redirect(w, r, r.RequestURI, http.StatusFound)
return
}
// regular flows
if data.CanArchive && len(r.URL.RawQuery) > 0 {
switch r.URL.RawQuery {
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 r.URL.RawQuery == "json" {
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(
root string,
emptyRoot bool,
urlPrefix string,
p *param.Param,
users user.Users,
template *template.Template,
logger *serverLog.Logger,
errHandler *serverErrHandler.ErrHandler,
) *handler {
aliases := aliases{}
for urlPath, fsPath := range p.Aliases {
aliases = append(aliases, &alias{urlPath, fsPath})
}
h := &handler{
root: root,
emptyRoot: emptyRoot,
urlPrefix: urlPrefix,
aliases: aliases,
globalUpload: p.GlobalUpload,
uploadUrls: p.UploadUrls,
uploadDirs: p.UploadDirs,
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,
template: template,
logger: logger,
errHandler: errHandler,
}
return h
}