-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
71 lines (55 loc) · 1.47 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
/*
Copyright © 2025 Seednode <seednode@seedno.de>
*/
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/julienschmidt/httprouter"
)
func serveHTTPStatusCode(errorChannel chan<- Error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
securityHeaders(w)
var text string = ""
trimmed := strings.TrimSuffix(strings.TrimPrefix(p.ByName("status"), "/"), "/")
value, err := strconv.Atoi(trimmed)
if err == nil {
text = http.StatusText(value)
}
if verbose {
fmt.Printf("%s | %s => %s\n",
startTime.Format(timeFormats["RFC3339"]),
realIP(r, true),
r.RequestURI)
}
if text == "" {
w.WriteHeader(http.StatusBadRequest)
_, err = w.Write([]byte("Invalid status code requested\n"))
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
}
} else {
w.WriteHeader(value)
_, err = w.Write([]byte(text + "\n"))
if err != nil {
errorChannel <- Error{err, realIP(r, true), r.URL.Path}
}
}
}
}
func registerHTTPStatus(mux *httprouter.Router, usage *sync.Map, errorChannel chan<- Error) {
const module = "http"
mux.GET("/http/", serveUsage(module, usage, errorChannel))
mux.GET("/http/status/:status", serveHTTPStatusCode(errorChannel))
mux.GET("/http/status/", serveUsage(module, usage, errorChannel))
usage.Store(module, []string{
"/http/status/200",
"/http/status/404",
"/http/status/500",
})
}