-
Notifications
You must be signed in to change notification settings - Fork 11
/
abouthandler.go
185 lines (168 loc) · 4.8 KB
/
abouthandler.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
package j8a
import (
"encoding/json"
"math/rand"
"net/http"
"strings"
"time"
)
var huttese = []string{
"Achuta!",
"Goodde da lodia!",
"H'chu apenkee!",
"Chuba!",
"Ka Cheesa Crispa Greedo?",
"De wanna wanga?",
"Peedunkee, caba dee unko!",
}
var httpResponses = map[int]string{
100: "continue",
101: "switching protocols",
102: "processing",
103: "early hints",
200: "ok",
201: "created",
202: "accepted",
203: "non-authoritative information",
204: "no content",
205: "reset content",
206: "partial content",
207: "multi-status",
208: "already reported",
226: "IM used",
300: "multiple choices",
301: "moved permanently",
302: "found",
303: "see other",
304: "not modified",
305: "use proxy",
306: "switch proxy",
307: "temporary redirect",
308: "permanent redirect",
400: "bad request",
401: "unauthorized",
402: "payment required",
403: "forbidden",
404: "not found",
405: "method not allowed",
406: "not acceptable",
407: "proxy authentication required",
408: "request timeout",
409: "conflict",
410: "gone",
411: "length required",
412: "precondition failed",
413: "payload too large",
414: "URI too long",
415: "unsupported media type",
416: "requested range not satisfiable",
417: "expectation failed",
418: "i'm a teapot",
420: "enhance your calm",
421: "misdirected request",
422: "unprocessable entity",
423: "locked",
424: "failed dependency",
425: "too early",
426: "upgrade required",
428: "precondition required",
429: "too many requests",
431: "request header fields too large",
444: "no response by upstream nginx",
449: "retry with",
450: "blocked by windows parental controls",
451: "unavailable for legal reasons",
499: "client closed request",
500: "internal server error",
501: "not implemented",
502: "bad gateway",
503: "service unavailable",
504: "gateway timeout",
505: "http version not supported",
506: "variant also negotiates",
507: "insufficient storage",
508: "loop detected",
509: "bandwidth limit exceeded",
510: "not extended",
511: "network authentication required",
598: "network read timeout error",
599: "network connect timeout error",
}
// AboutResponse exposes standard environment
type AboutResponse struct {
J8a string
ServerID string
Version string
}
// StatusCodeResponse defines a JSON structure for a canned HTTP response
type StatusCodeResponse struct {
AboutResponse
Code int
Message string
}
// AsJSON renders the status Code response into a JSON string as []byte
func (aboutResponse AboutResponse) AsJSON() []byte {
aboutResponse.ServerID = ID
aboutResponse.Version = Version
aboutResponse.J8a = RandomHuttese()
response, _ := json.Marshal(aboutResponse)
//typo fix so we can continue to use json.Marshal which needs Uppercase struct props
response[2] = 0x6a
return response
}
func (aboutResponse AboutResponse) AsString() string {
return strings.ToLower(string(aboutResponse.AsJSON()))
}
func (statusCodeResponse *StatusCodeResponse) withCode(code int) {
statusCodeResponse.Code = code
if msg, ok := httpResponses[code]; ok {
statusCodeResponse.Message = msg
} else {
statusCodeResponse.Message = "unspecified response code"
}
}
// AsJSON renders the status Code response into a JSON string as []byte
func (statusCodeResponse StatusCodeResponse) AsJSON() []byte {
statusCodeResponse.ServerID = ID
statusCodeResponse.Version = Version
statusCodeResponse.J8a = RandomHuttese()
response, _ := json.Marshal(statusCodeResponse)
//typo fix so we can continue to use json.Marshal which needs Uppercase struct props
response[2] = 0x6a
return response
}
func (statusCodeResponse StatusCodeResponse) AsString() string {
return strings.ToLower(string(statusCodeResponse.AsJSON()))
}
func RandomHuttese() string {
rand.Seed(time.Now().Unix())
return huttese[rand.Int()%len(huttese)]
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
proxy := new(Proxy).
parseIncoming(r).
setOutgoing(w)
if !proxy.Dwn.AcceptEncoding.hasAtLeastOneValidEncoding() {
sendStatusCodeAsJSON(proxy.respondWith(406, formatInvalidAcceptEncoding()))
return
}
proxy.writeStandardResponseHeaders()
proxy.respondWith(200, "ok")
res := AboutResponse{}.AsJSON()
w.Header().Set(contentType, applicationJSON)
if proxy.Dwn.AcceptEncoding.isCompatible(EncIdentity) {
proxy.Dwn.Resp.Body = &res
proxy.Dwn.Resp.ContentEncoding = EncIdentity
} else if proxy.Dwn.AcceptEncoding.isCompatible(EncGzip) {
proxy.Dwn.Resp.Body = Gzip(res)
proxy.Dwn.Resp.ContentEncoding = EncGzip
} else if proxy.Dwn.AcceptEncoding.isCompatible(EncBrotli) {
proxy.Dwn.Resp.Body = BrotliEncode(res)
proxy.Dwn.Resp.ContentEncoding = EncBrotli
}
w.Header().Set(contentEncoding, proxy.Dwn.Resp.ContentEncoding.print())
proxy.setContentLengthHeader()
proxy.sendDownstreamStatusCodeHeader()
proxy.pipeDownstreamResponse()
logHandledDownstreamRoundtrip(proxy)
}