-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandlers.go
524 lines (472 loc) · 21.8 KB
/
handlers.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package main
import (
"bytes"
"compress/gzip"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"time"
)
func handleRoot(mux *http.ServeMux) {
handler := func(w http.ResponseWriter, r *http.Request) {
addHeaders(w, r)
if validRequest(r) {
handleRequests(w, r)
} else {
http.Error(w, errServerError, http.StatusInternalServerError)
}
}
mux.HandleFunc("/", handler)
}
// handleRequests will handle all web requests and direct the right action to the right linkLen
func handleRequests(w http.ResponseWriter, r *http.Request) {
if r == nil {
logErrors(w, r, errServerError, http.StatusInternalServerError, "invalid request")
return
}
// browsers should send a path that begins with a /
if r.URL.Path[0] != '/' {
logErrors(w, r, errServerError, http.StatusInternalServerError, "")
return
}
if r.Method == http.MethodGet {
handleGET(w, r)
return
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
// If the user tries to submit data via POST
if r.Method == http.MethodPost {
err := r.ParseMultipartForm(config.MaxFileSize)
if err != nil {
logErrors(w, r, errServerError, http.StatusInternalServerError, "Error: "+url.QueryEscape(err.Error()))
return
}
// Get length of key to be used
length := r.Form.Get("len")
var currentLinkLen *LinkLen
switch length {
case "1":
currentLinkLen = &domainLinkLens[r.Host].LinkLen1
case "2":
currentLinkLen = &domainLinkLens[r.Host].LinkLen2
case "3":
currentLinkLen = &domainLinkLens[r.Host].LinkLen3
case "custom":
currentLinkLen = &domainLinkLens[r.Host].LinkCustom
default:
logErrors(w, r, errServerError, http.StatusInternalServerError, "Error: Invalid len argument.")
return
}
// Get how many times the link can be used before becoming invalid, -1 represents no limit
xTimes, err := strconv.Atoi(r.Form.Get("xTimes"))
if err != nil || xTimes < 1 {
xTimes = -1
} else if xTimes > config.LinkAccessMaxNr {
xTimes = config.LinkAccessMaxNr
}
// Check if request is a custom key request and report error if it is invalid
customKey := ""
if length == "custom" {
customKey = r.Form.Get("custom")
if !validate(customKey) || len(customKey) < 4 || len(customKey) > maxKeyLen {
logErrors(w, r, errInvalidCustomKey, http.StatusInternalServerError, "")
return
}
if _, used := domainLinkLens[r.Host].LinkCustom.LinkMap[customKey]; used {
http.Error(w, errInvalidKeyUsed, http.StatusInternalServerError)
return
}
}
// Handle different request types
requestType := r.Form.Get("requestType")
switch requestType {
case "url":
formURL := r.Form.Get("url")
valid := validURL(formURL)
if !valid {
logErrors(w, r, "Invalid url, only \"http://\" and \"https://\" url schemes are allowed.", http.StatusInternalServerError, "")
return
}
currentLinkLen.Mutex.RLock()
currentLinkLenTimeout := currentLinkLen.Timeout
currentLinkLen.Mutex.RUnlock()
isCompressed := false
showLnk := &Link{Key: customKey, LinkType: "url", Data: formURL, IsCompressed: isCompressed, Times: xTimes, Timeout: time.Now().Add(currentLinkLenTimeout)}
key, err := currentLinkLen.Add(showLnk)
if err == nil {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
logger.Println("requesting template :", r.Host+"showLink")
t, ok := templateMap[r.Host+"#showLink"]
if !ok {
logger.Println("ERROR getting template template :", r.Host+"showLink")
http.Error(w, errServerError, http.StatusInternalServerError)
return
}
tmplArgs := showLinkVars{Domain: scheme + "://" + r.Host, Data: scheme + "://" + r.Host + "/" + key, Timeout: showLnk.Timeout.Format("Mon 2006-01-02 15:04 MST")}
err = t.ExecuteTemplate(w, "showLink.tmpl", tmplArgs)
if err != nil {
logger.Println("ERROR executing template template showLink.tmpl for host :", r.Host, "with args: ", tmplArgs, "with the error: ", err)
http.Error(w, errServerError, http.StatusInternalServerError)
}
logOK(r, http.StatusOK)
return
}
return
case "text":
if lowRAM() {
logErrors(w, r, errServerError, http.StatusInternalServerError, errLowRAM)
return
}
textBlob := r.Form.Get("text")
isCompressed := false
if len(textBlob) > minSizeToGzip {
compressed, err := compress(textBlob)
if err == nil && len(textBlob) > len(compressed) {
textBlob = compressed
isCompressed = true
}
}
currentLinkLen.Mutex.RLock()
currentLinkLenTimeout := currentLinkLen.Timeout
currentLinkLen.Mutex.RUnlock()
showLnk := &Link{Key: customKey, LinkType: "text", Data: textBlob, IsCompressed: isCompressed, Times: xTimes, Timeout: time.Now().Add(currentLinkLenTimeout)}
key, err := currentLinkLen.Add(showLnk)
if err == nil {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
t, ok := templateMap[r.Host+"#showLink"]
if !ok {
http.Error(w, errServerError, http.StatusInternalServerError)
return
}
tmplArgs := showLinkVars{Domain: scheme + "://" + r.Host, Data: scheme + "://" + r.Host + "/" + key, Timeout: showLnk.Timeout.Format("Mon 2006-01-02 15:04 MST")}
err = t.ExecuteTemplate(w, "showLink.tmpl", tmplArgs)
if err != nil {
logger.Println("ERROR executing template template showLink.tmpl for host :", r.Host, "with args: ", tmplArgs)
http.Error(w, errServerError, http.StatusInternalServerError)
}
logOK(r, http.StatusOK)
return
}
return
default:
logErrors(w, r, errNotImplemented, http.StatusNotImplemented, "Error: Invalid requestType argument.")
return
}
}
// If the request is not handled previously redirect to index, note that Host has been validated earlier
logOK(r, http.StatusSeeOther)
http.Redirect(w, r, scheme+"://"+r.Host, http.StatusSeeOther)
}
// handleGET will handle GET requests and redirect to the saved link for a key, return a saved textblob or return a file
func handleGET(w http.ResponseWriter, r *http.Request) {
if !validRequest(r) {
logErrors(w, r, errServerError, http.StatusInternalServerError, "Error: invalid request.")
return
}
// remove / from the beginning of url and remove any character after the key
key := r.URL.Path[1:]
extradataindex := strings.IndexAny(key, "/")
if extradataindex >= 0 {
key = key[:extradataindex]
}
// Return Index page if GET request without a key
if len(key) == 0 {
indexTmpl, ok := templateMap[r.Host+"#index"]
if !ok || indexTmpl == nil {
logErrors(w, r, errServerError, http.StatusInternalServerError, "Unable to load index template: "+r.Host+"#index")
return
}
err := indexTmpl.Execute(w, nil)
if err != nil {
logErrors(w, r, errServerError, http.StatusInternalServerError, "Unable to Execute index template: "+r.Host+"#index")
return
}
logOK(r, http.StatusOK)
return
}
// verify that key only consists of valid characters
if !validate(key) {
logErrors(w, r, errInvalidKey, http.StatusInternalServerError, "")
return
}
// quick check if request is quickAddURL request
if len(r.URL.RawQuery) > 0 {
if key == "listactive~" {
listActiveLinks(w, r)
return
}
if validURL(r.URL.RawQuery) {
quickAddURL(w, r, r.URL.RawQuery, key)
return
} else {
logErrors(w, r, "Invalid Quick Add URL request", http.StatusInternalServerError, "Invalid Quick Add URL request, please use the following syntax: \""+r.Host+"?http://example.com/\". where http://example.com/ is your link.\nAlso note that only \"http://\" and \"https://\" url schemes are allowed.")
return
}
}
var showLink bool
if key[len(key)-1] == '~' {
key = key[:len(key)-1]
showLink = true
}
// start by checking static key map
if lnk, ok := config.StaticLinks[key]; ok {
logOK(r, http.StatusPermanentRedirect)
http.Redirect(w, r, lnk, http.StatusPermanentRedirect)
return
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
var lnk *Link
var ok bool
switch keylen := len(key); {
case keylen == 1:
domainLinkLens[r.Host].LinkLen1.Mutex.RLock()
if lnk, ok = domainLinkLens[r.Host].LinkLen1.LinkMap[key]; !ok {
domainLinkLens[r.Host].LinkLen1.Mutex.RUnlock()
http.Error(w, errInvalidKey, http.StatusInternalServerError)
return
}
domainLinkLens[r.Host].LinkLen1.Mutex.RUnlock()
case keylen == 2:
domainLinkLens[r.Host].LinkLen2.Mutex.RLock()
if lnk, ok = domainLinkLens[r.Host].LinkLen2.LinkMap[key]; !ok {
domainLinkLens[r.Host].LinkLen2.Mutex.RUnlock()
http.Error(w, errInvalidKey, http.StatusInternalServerError)
return
}
domainLinkLens[r.Host].LinkLen2.Mutex.RUnlock()
case keylen == 3:
domainLinkLens[r.Host].LinkLen3.Mutex.RLock()
if lnk, ok = domainLinkLens[r.Host].LinkLen3.LinkMap[key]; !ok {
domainLinkLens[r.Host].LinkLen3.Mutex.RUnlock()
http.Error(w, errInvalidKey, http.StatusInternalServerError)
return
}
domainLinkLens[r.Host].LinkLen3.Mutex.RUnlock()
case keylen > 3 && keylen < maxKeyLen:
// key is validated previously
domainLinkLens[r.Host].LinkCustom.Mutex.RLock()
if lnk, ok = domainLinkLens[r.Host].LinkCustom.LinkMap[key]; !ok {
domainLinkLens[r.Host].LinkCustom.Mutex.RUnlock()
http.Error(w, errInvalidKey, http.StatusInternalServerError)
return
}
domainLinkLens[r.Host].LinkCustom.Mutex.RUnlock()
default:
http.Error(w, errInvalidKey, http.StatusInternalServerError)
return
}
if lnk == nil {
http.Error(w, errInvalidKey, http.StatusInternalServerError)
return
}
switch lnk.LinkType {
case "url":
if showLink {
logOK(r, http.StatusOK)
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, r.Host+"/"+key+"\n\nis pointing to \n\n"+html.EscapeString(lnk.Data))
return
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
t, ok := templateMap[r.Host+"#showLink"]
if !ok {
http.Error(w, errServerError, http.StatusInternalServerError)
}
tmplArgs := showLinkVars{Domain: scheme + "://" + r.Host, Data: lnk.Data, Timeout: lnk.Timeout.Format("Mon 2006-01-02 15:04 MST")}
err := t.ExecuteTemplate(w, "showLink.tmpl", tmplArgs)
if err != nil {
http.Error(w, errServerError, http.StatusInternalServerError)
}
logOK(r, http.StatusTemporaryRedirect)
return
case "text":
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
if showLink {
logOK(r, http.StatusOK)
fmt.Fprint(w, r.Host+"/"+key+"\n\nis pointing to a "+r.Host+" Text dump")
return
}
if lnk.IsCompressed {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Add("content-encoding", "gzip")
logOK(r, http.StatusOK)
fmt.Fprint(w, lnk.Data)
return
} else {
returnDecompressed(lnk, w, r) // defined in misc.go
return
}
}
logOK(r, http.StatusOK)
fmt.Fprint(w, lnk.Data)
return
default:
logErrors(w, r, errServerError, http.StatusInternalServerError, "invalid LinkType "+url.QueryEscape(lnk.LinkType))
}
}
func handleCSS(mux *http.ServeMux) {
f, err := ioutil.ReadFile(filepath.Join(config.BaseDir, "css", "shorter.css"))
if err != nil {
log.Fatalln("Missing shorter.css in Template dir/css/")
}
mux.HandleFunc("/shorter.css", getSingleFileHandler(f, "text/css"))
}
func getSingleFileHandler(f []byte, mimeType string) (handleFile func(w http.ResponseWriter, r *http.Request)) {
var buf bytes.Buffer
tryGzip := true
zw := gzip.NewWriter(&buf)
_, err := zw.Write(f)
if err != nil {
tryGzip = false
}
zw.Close()
cf := buf.Bytes()
handleFile = func(w http.ResponseWriter, r *http.Request) {
addHeaders(w, r)
if validRequest(r) {
w.Header().Add("Content-Type", mimeType)
w.Header().Add("Cache-Control", "max-age=2592000, public")
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && tryGzip && false {
w.Header().Add("content-encoding", "gzip")
fmt.Fprintf(w, "%s", cf)
return
}
fmt.Fprintf(w, "%s", f)
return
}
http.Error(w, errServerError, http.StatusInternalServerError)
}
return
}
func getImgHandler(img string, mimeType string) (handleImgFile func(w http.ResponseWriter, r *http.Request)) {
handleImgFile = func(w http.ResponseWriter, r *http.Request) {
addHeaders(w, r)
if validRequest(r) {
w.Header().Add("Content-Type", mimeType)
w.Header().Add("Cache-Control", "max-age=2592000, public")
fmt.Fprintf(w, "%s", ImageMap[r.Host+img])
return
}
http.Error(w, errServerError, http.StatusInternalServerError)
}
return
}
// handleImages adds /logo.png, /favicon.ico and /favicon.png to all domains specified in config, if a domain is missing a image it will fall back to the default image
func handleImages(mux *http.ServeMux) {
ImageMap = make(map[string][]byte)
defaultLogo := []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x04, 0x03, 0x00, 0x00, 0x00, 0x31, 0x10, 0x7c, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0x17, 0x9c, 0xf2, 0x8a, 0xc2, 0xf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 0x0b, 0xac, 0x1b, 0xc7, 0xbe, 0x8c, 0xeb, 0x46, 0xca, 0x87, 0x4e, 0x95, 0xc0, 0xb6, 0xf7, 0x73, 0x84, 0xa1, 0x12, 0x18, 0xc3, 0x7e, 0x0a, 0xfb, 0x9b, 0x89, 0x00, 0x5e, 0x4f, 0x80, 0xcb, 0xf3, 0xe3, 0xbd, 0xab, 0x04, 0xee, 0xef, 0xf7, 0xd3, 0xf5, 0x9a, 0x1f, 0x07, 0x34, 0x30, 0x05, 0x61, 0xb0, 0xc0, 0xa0, 0x03, 0x8f, 0x49, 0x95, 0x47, 0x21, 0x41, 0x11, 0x90, 0x46, 0x7e, 0x7a, 0xdc, 0x83, 0x0d, 0x68, 0x60, 0x02, 0x00, 0xf4, 0x30, 0x01, 0x00, 0x70, 0x02, 0x1d, 0x20, 0x12, 0xe8, 0x40, 0x5b, 0x4a, 0x70, 0x59, 0x17, 0x88, 0x79, 0xac, 0x17, 0x13, 0xf4, 0xfb, 0x9f, 0xa9, 0x40, 0x4e, 0x10, 0x6c, 0x40, 0x79, 0x11, 0x2b, 0x80, 0x7c, 0x05, 0xa3, 0x11, 0xb8, 0x95, 0x12, 0x54, 0x00, 0xb1, 0x98, 0xa0, 0x02, 0x10, 0xf6, 0x31, 0x0f, 0x94, 0x13, 0x54, 0x00, 0xe5, 0x04, 0x3c, 0x20, 0x24, 0xe0, 0x81, 0xb6, 0x9c, 0x80, 0x07, 0x84, 0x04, 0x3c, 0x20, 0xdd, 0xca, 0x2c, 0x90, 0x17, 0xf1, 0xf3, 0x56, 0x66, 0x81, 0x5e, 0x48, 0x40, 0x03, 0xc2, 0x22, 0xd2, 0x40, 0x94, 0x12, 0xb0, 0x80, 0x98, 0x80, 0x05, 0xc4, 0x04, 0x2c, 0x20, 0x26, 0x20, 0x01, 0x39, 0x01, 0x09, 0xb4, 0x62, 0x02, 0x12, 0x90, 0x13, 0x90, 0x40, 0x12, 0x13, 0x70, 0xc0, 0xeb, 0xf7, 0x85, 0x60, 0x03, 0x94, 0x04, 0x1c, 0x90, 0x17, 0x71, 0xb4, 0x01, 0x51, 0x49, 0x40, 0x01, 0x5a, 0x02, 0x0a, 0x50, 0x16, 0x91, 0x03, 0xb4, 0x04, 0x0c, 0xa0, 0xec, 0x63, 0x0e, 0x50, 0x13, 0x30, 0x80, 0x9a, 0x80, 0x01, 0x92, 0x96, 0x80, 0x00, 0x1a, 0x35, 0x01, 0x01, 0x68, 0xfb, 0x98, 0x02, 0xf4, 0x04, 0x04, 0xa0, 0xed, 0x63, 0x06, 0x00, 0x09, 0x30, 0xd0, 0xeb, 0x09, 0x30, 0x70, 0xd3, 0x13, 0x40, 0x20, 0x82, 0x04, 0x10, 0x40, 0x09, 0x20, 0x80, 0x12, 0x40, 0x20, 0x81, 0x04, 0x08, 0x80, 0x09, 0x10, 0xf0, 0xf9, 0xe5, 0xbc, 0x1a, 0x80, 0x09, 0x10, 0xa0, 0xdf, 0xca, 0x18, 0x68, 0x60, 0x02, 0x00, 0xf4, 0x30, 0x01, 0x00, 0xd0, 0x3e, 0x46, 0x40, 0xc4, 0x09, 0x74, 0x80, 0x48, 0xa0, 0x03, 0x44, 0x02, 0x1d, 0x20, 0x12, 0xa8, 0x00, 0x93, 0x40, 0x05, 0x5e, 0xfb, 0xb8, 0xb3, 0x01, 0x78, 0x1f, 0x03, 0x00, 0xde, 0xca, 0x00, 0x60, 0x16, 0x51, 0x07, 0xf2, 0xdf, 0x5e, 0x94, 0x04, 0x55, 0x5f, 0xff, 0x99, 0x71, 0x00, 0x07, 0x30, 0x03, 0xdf, 0x7f, 0xdb, 0xe7, 0x06, 0xbe, 0xff, 0xce, 0xd5, 0xff, 0xda, 0xd8, 0xfd, 0xe2, 0xda, 0xff, 0xea, 0xdc, 0xfd, 0xf2, 0xde, 0xf7, 0xef, 0x03, 0xff, 0x2b, 0xec, 0x86, 0x52, 0x86, 0x8e, 0xac, 0x41, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44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, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82}
for _, domain := range config.DomainNames {
logo, err := ioutil.ReadFile(filepath.Join(config.BaseDir, domain, "logo.png"))
if err != nil {
if logger != nil {
logger.Println("Missing /" + domain + "/logo.png in Template dir, fallback to default logo.png")
}
ImageMap[domain+"-logo"] = defaultLogo
} else {
ImageMap[domain+"-logo"] = logo
}
favicon, err := ioutil.ReadFile(filepath.Join(config.BaseDir, domain, "favicon.png"))
if err != nil {
if logger != nil {
logger.Println("Missing /" + domain + "/favicon.png in Template dir, fallback to default favicon.png")
}
ImageMap[domain+"-favicon"] = defaultFavicon
} else {
ImageMap[domain+"-favicon"] = favicon
}
}
mux.HandleFunc("/logo.png", getImgHandler("-logo", "image/png"))
mux.HandleFunc("/favicon.png", getImgHandler("-favicon", "image/png"))
mux.HandleFunc("/favicon.ico", getImgHandler("-favicon", "image/png"))
}
// handleRobots will return the robots.txt located in the Template dir specified in the config file, if no robots.txt file is found we return a 404 error
func handleRobots(mux *http.ServeMux) {
f, err := ioutil.ReadFile(filepath.Join(config.BaseDir, "robots.txt"))
if err != nil {
if logger != nil {
logger.Println("Missing robots.txt in Template dir, fallback to returning 404 on requests for robots.txt")
}
handler404 := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", http.StatusNotFound)
}
mux.HandleFunc("/robots.txt", handler404)
return
}
handleRobots := func(w http.ResponseWriter, r *http.Request) {
addHeaders(w, r)
if validRequest(r) {
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.Header().Add("Cache-Control", "max-age=2592000, public")
fmt.Fprintf(w, "%s", f)
return
}
http.Error(w, errServerError, http.StatusInternalServerError)
}
mux.HandleFunc("/robots.txt", handleRobots)
}
func quickAddURL(w http.ResponseWriter, r *http.Request, url, key string) {
var urlLink *LinkLen
// Remove keys of invalid size, note that key has been validated to only contain valid characters previously
if len(key) <= 3 || len(key) >= maxKeyLen {
key = ""
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
// Try to quickAddURL for first len 1, if all are full then try len 2 and lastly len 3
for i := 0; i <= 3; i++ {
switch i {
case 0:
if key == "" {
continue
}
urlLink = &domainLinkLens[r.Host].LinkCustom
if _, used := urlLink.LinkMap[key]; used {
http.Error(w, errInvalidKeyUsed, http.StatusInternalServerError)
return
}
case 1:
urlLink = &domainLinkLens[r.Host].LinkLen1
case 2:
urlLink = &domainLinkLens[r.Host].LinkLen2
case 3:
urlLink = &domainLinkLens[r.Host].LinkLen3
default:
}
urlLink.Mutex.RLock()
linkTimeout := urlLink.Timeout
urlLink.Mutex.RUnlock()
isCompressed := false
showLink := &Link{Key: key, LinkType: "url", Data: url, IsCompressed: isCompressed, Times: -1, Timeout: time.Now().Add(linkTimeout)}
_, err := urlLink.Add(showLink)
if err == nil {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
t, ok := templateMap[r.Host+"#showLink"]
if !ok {
http.Error(w, errServerError, http.StatusInternalServerError)
return
}
tmplArgs := showLinkVars{Domain: scheme + "://" + r.Host, Data: showLink.Data, Timeout: showLink.Timeout.Format("Mon 2006-01-02 15:04 MST")}
err = t.ExecuteTemplate(w, "showLink.tmpl", tmplArgs)
if err != nil {
http.Error(w, errServerError, http.StatusInternalServerError)
}
logOK(r, http.StatusOK)
return
}
}
}