-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttp.go
310 lines (273 loc) · 7.16 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"html/template"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/ncruces/jason"
"github.com/ncruces/rethinkraw/internal/config"
)
var templates *template.Template
func setupHTTP() *http.Server {
mux := http.NewServeMux()
mux.Handle("/gallery/", http.StripPrefix("/gallery", httpHandler(galleryHandler)))
mux.Handle("/photo/", http.StripPrefix("/photo", httpHandler(photoHandler)))
mux.Handle("/batch/", http.StripPrefix("/batch", httpHandler(batchHandler)))
mux.Handle("/thumb/", http.StripPrefix("/thumb", httpHandler(thumbHandler)))
mux.Handle("/dialog", httpHandler(dialogHandler))
mux.Handle("/upload", httpHandler(uploadHandler))
mux.Handle("/", assetHandler)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !isLocalhost(r) {
if !config.ServerMode || !matchHostServerName(r) {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
if url := canUseTLS(r); url != "" {
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
return
}
if _, pwd, _ := r.BasicAuth(); pwd != serverAuth {
w.Header().Set("WWW-Authenticate", `Basic charset="UTF-8"`)
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if r.URL.Path == "/" {
http.Redirect(w, r, "/gallery", http.StatusTemporaryRedirect)
return
}
}
mux.ServeHTTP(w, r)
})
templates = assetTemplates()
server := &http.Server{
ReadHeaderTimeout: time.Second,
IdleTimeout: time.Minute,
Handler: handler,
}
return server
}
// httpResult helps httpHandler short circuit a result
type httpResult struct {
Status int
Message string
Location string
Error error
}
func (r *httpResult) Done() bool { return r.Status != 0 || r.Location != "" || r.Error != nil }
// httpHandler is an [http.Handler] that returns an httpResult
type httpHandler func(w http.ResponseWriter, r *http.Request) httpResult
func (h httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch res := h(w, r); {
case res.Location != "":
if res.Status == 0 {
res.Status = http.StatusTemporaryRedirect
}
http.Redirect(w, r, res.Location, res.Status)
case res.Status >= 400:
sendError(w, r, res.Status, res.Message)
case res.Status != 0:
w.WriteHeader(res.Status)
case res.Error != nil:
status, message := errorStatus(res.Error)
sendError(w, r, status, message)
log.Print(message)
}
}
func errorStatus(err error) (status int, message string) {
switch {
case errors.Is(err, fs.ErrNotExist):
status = http.StatusNotFound
case errors.Is(err, fs.ErrPermission):
status = http.StatusForbidden
default:
status = http.StatusInternalServerError
}
var buf strings.Builder
buf.WriteString(strings.TrimSpace(err.Error()))
var eerr *exec.ExitError
if errors.As(err, &eerr) {
if msg := bytes.TrimSpace(eerr.Stderr); len(msg) > 0 {
buf.WriteByte('\n')
buf.Write(msg)
}
}
return status, buf.String()
}
func sendError(w http.ResponseWriter, r *http.Request, status int, message string) {
h := w.Header()
for n := range h {
delete(h, n)
}
h.Set("X-Content-Type-Options", "nosniff")
if strings.HasPrefix(r.Header.Get("Accept"), "text/html") {
h.Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
templates.ExecuteTemplate(w, "error.gohtml", jason.Object{
"Status": http.StatusText(status),
"Message": message,
})
} else {
h.Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(message)
}
}
func sendCached(w http.ResponseWriter, r *http.Request, path string) httpResult {
if fi, err := os.Stat(path); err != nil {
return httpResult{Error: err}
} else {
headers := w.Header()
headers.Set("Last-Modified", fi.ModTime().UTC().Format(http.TimeFormat))
if ims := r.Header.Get("If-Modified-Since"); ims != "" {
if t, err := http.ParseTime(ims); err == nil {
if fi.ModTime().Before(t.Add(time.Second)) {
for k := range headers {
switch k {
case "Cache-Control", "Last-Modified":
// keep
default:
delete(headers, k)
}
}
return httpResult{Status: http.StatusNotModified}
}
}
}
}
return httpResult{}
}
func sendAllowed(w http.ResponseWriter, r *http.Request, allowed ...string) httpResult {
for _, method := range allowed {
if method == r.Method {
return httpResult{}
}
}
w.Header().Set("Allow", strings.Join(allowed, ", "))
return httpResult{Status: http.StatusMethodNotAllowed}
}
func isLocalhost(r *http.Request) bool {
return r.TLS == nil && strings.TrimSuffix(r.Host, serverPort) == "localhost"
}
func getPathPrefix(r *http.Request) string {
if !isLocalhost(r) {
return serverPrefix
}
return ""
}
func toUsrPath(path, prefix string) string {
path = filepath.Clean(path)
if prefix != "" {
if len(path) == len(prefix) || !strings.HasPrefix(path, prefix) {
return "/"
}
path = path[len(prefix):]
return filepath.ToSlash(path)
}
return path
}
func toURLPath(path, prefix string) string {
path = filepath.Clean(path)
if !strings.HasPrefix(path, prefix) {
return ""
}
path = path[len(prefix):]
if filepath.Separator == '\\' && strings.HasPrefix(path, `\\`) {
return `\\` + filepath.ToSlash(path[2:])
}
return strings.TrimPrefix(filepath.ToSlash(path), "/")
}
func fromURLPath(path, prefix string) string {
if filepath.Separator != '/' {
path = filepath.FromSlash(strings.TrimPrefix(path, "/"))
}
return filepath.Join(prefix, path)
}
func matchHostServerName(r *http.Request) bool {
if r.TLS == nil {
return true
}
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
return r.TLS.ServerName == host
}
func canUseTLS(r *http.Request) (url string) {
if r.TLS != nil {
return ""
}
host := r.Host
name, port, err := net.SplitHostPort(host)
if err != nil {
return ""
}
if app := getAppDomain(name); app != "" {
host = net.JoinHostPort(app, port)
name = app
}
chi := tls.ClientHelloInfo{ServerName: name, SupportedVersions: []uint16{
tls.VersionTLS13,
tls.VersionTLS12,
}}
config := &serverConfig
if config.GetConfigForClient != nil {
cfg, err := config.GetConfigForClient(&chi)
if err != nil {
return ""
}
if cfg != nil {
config = cfg
}
}
if config.GetCertificate != nil {
cert, err := config.GetCertificate(&chi)
if err != nil {
return ""
}
if cert != nil {
u := r.URL
u.Host = host
u.Scheme = "https"
return u.String()
}
}
for _, cert := range config.Certificates {
if chi.SupportsCertificate(&cert) == nil {
u := r.URL
u.Host = host
u.Scheme = "https"
return u.String()
}
}
return ""
}
func getAppDomain(name string) string {
if ip := net.ParseIP(name); ip != nil {
name = ip.String()
if ip4 := ip.To4(); len(ip4) == net.IPv4len {
name = strings.ReplaceAll(name, ".", "-")
return name + ".app.rethinkraw.com"
} else if len(ip) == net.IPv6len {
if strings.HasPrefix(name, "::") {
name = "0" + name
}
if strings.HasSuffix(name, "::") {
name = name + "0"
}
name = strings.ReplaceAll(name, ":", "-")
return name + ".app.rethinkraw.com"
}
}
return ""
}