/
http.go
222 lines (190 loc) · 5.7 KB
/
http.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
package web
import (
"encoding/json"
"github.com/browsefile/backend/src/cnst"
fb "github.com/browsefile/backend/src/lib"
"github.com/browsefile/backend/src/lib/utils"
"html/template"
"log"
"net/http"
"strings"
)
// Handler returns a function compatible with web.HandleFunc.
func Handler(m *fb.FileBrowser) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := &fb.Context{
FileBrowser: m,
User: nil,
File: nil,
Params: new(fb.Params),
}
c.REQ = r
c.RESP = w
c.Method = c.REQ.Method
code, err := serve(c)
if err != nil {
txt := http.StatusText(code)
if len(c.Params.PreviewType) == 0 {
log.Printf("%v: %v %v\n", r.URL.Path, code, txt)
log.Println(err)
} else {
log.Println(err)
err = nil
}
}
if !c.Rendered && c.Router > 0 && c.Router != cnst.R_DOWNLOAD && c.Router != cnst.R_PLAYLIST {
w.WriteHeader(code)
}
})
}
// serve is the main entry point of this HTML application.
func serve(c *fb.Context) (int, error) {
// Checks if this request is made to the static assets folder. If so, and
// if it is a GET request, returns with the asset. Otherwise, returns
// a status not implemented.
if strings.HasPrefix(c.REQ.URL.Path, "/static") {
if c.Method != http.MethodGet {
return http.StatusNotImplemented, nil
}
return staticHandler(c)
}
if strings.HasPrefix(c.REQ.URL.Path, cnst.WEB_DAV_URL) {
ServeDav(c, c.RESP, c.REQ)
return http.StatusOK, nil
}
// Checks if this request is made to the API and directs to the
// API handler if so.
if strings.HasPrefix(c.REQ.URL.Path, "/api") {
return apiHandler(c)
}
// Any other request should show the index.html file.
c.RESP.Header().Set("x-content-type-options", "nosniff")
c.RESP.Header().Set("x-xss-protection", "1; mode=block")
return renderFile(c, "index.html")
}
// staticHandler handles the static assets path.
func staticHandler(c *fb.Context) (code int, err error) {
if c.REQ.URL.Path != "/static/manifest.json" {
c.REQ.URL.Path = strings.TrimPrefix(c.REQ.URL.Path, "/static")
http.FileServer(c.Assets.HTTPBox()).ServeHTTP(c.RESP, c.REQ)
return 0, nil
}
return renderFile(c, "manifest.json")
}
// apiHandler is the main entry point for the /api endpoint.
func apiHandler(c *fb.Context) (code int, err error) {
c.REQ.URL.Path = strings.TrimPrefix(c.REQ.URL.Path, "/api")
if c.REQ.URL.Path == "/auth/get" {
return authHandler(c)
}
if c.REQ.URL.Path == "/auth/renew" {
return renewAuthHandler(c)
}
valid, _ := validateAuth(c)
if !valid {
return http.StatusForbidden, nil
}
isShares := ProcessParams(c)
//allow only GET requests, for external share
if valid && c.User.IsGuest() && (!isShares ||
c.Method != http.MethodGet ||
c.Router == cnst.R_USERS ||
c.Router == cnst.R_SETTINGS) {
return http.StatusForbidden, nil
}
if c.Checksum != "" {
err := c.File.Checksum(c.Checksum)
if err == cnst.ErrInvalidOption {
return http.StatusBadRequest, nil
} else if err != nil {
return http.StatusInternalServerError, err
}
// do not waste bandwidth if we just want the checksum
c.File.Content = ""
return renderJSON(c, c.File)
}
switch c.Router {
case cnst.R_DOWNLOAD:
code, err = downloadHandler(c)
case cnst.R_RESOURCE:
code, err = resourceHandler(c)
case cnst.R_USERS:
code, err = usersHandler(c)
case cnst.R_SETTINGS:
code, err = settingsHandler(c)
case cnst.R_SHARES:
code, err = shareHandler(c)
case cnst.R_SEARCH:
code, err = searchHandler(c)
case cnst.R_PLAYLIST:
code, err = makePlaylist(c)
default:
code = http.StatusNotFound
}
if (c.Router == cnst.R_SETTINGS ||
c.Router == cnst.R_USERS ||
c.Router == cnst.R_RESOURCE ||
c.Router == cnst.R_SHARES) &&
c.Method == http.MethodPatch ||
c.Method == http.MethodPut ||
c.Method == http.MethodPost ||
c.Method == http.MethodDelete {
if c.Config != nil && err == nil {
c.Config.WriteConfig()
}
}
return code, err
}
// renderFile renders a file using a template with some needed variables.
func renderFile(c *fb.Context, file string) (int, error) {
contentType := utils.GetMimeType(file)
if len(contentType) == 0 {
contentType = cnst.TEXT
}
c.Query = c.REQ.URL.Query()
isExternal := len(c.Query.Get(cnst.P_ROOTHASH)) > 0
c.RESP.Header().Set("Content-Type", contentType+"; charset=utf-8")
cfgM := c.GetAuthConfig()
data := map[string]interface{}{
"Name": "Browsefile",
"DisableExternal": false,
"Version": cnst.Version,
"isExternal": isExternal,
"StaticURL": "/static",
"Signup": false,
"NoAuth": strings.ToLower(cfgM.AuthMethod) == "noauth" || strings.ToLower(cfgM.AuthMethod) == "ip",
"ReCaptcha": c.ReCaptcha.Key != "" && c.ReCaptcha.Secret != "",
"ReCaptchaHost": c.ReCaptcha.Host,
"ReCaptchaKey": c.ReCaptcha.Key,
}
if isExternal {
data["StaticURL"] = c.Config.ExternalShareHost + "/static"
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return http.StatusInternalServerError, err
}
data["Json"] = template.JS(string(b))
index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(c.Assets.MustString(file)))
err = index.Execute(c.RESP, data)
if err != nil {
return http.StatusInternalServerError, err
}
if err != nil {
return http.StatusInternalServerError, err
}
return 0, nil
}
// renderJSON prints the JSON version of data to the browser.
func renderJSON(c *fb.Context, data interface{}) (int, error) {
marsh, err := json.Marshal(data)
if err != nil {
return http.StatusInternalServerError, err
}
c.Rendered = true
c.RESP.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err := c.RESP.Write(marsh); err != nil {
return http.StatusInternalServerError, err
}
return 0, nil
}