1package http
2
3import (
4 "bytes"
5 "fmt"
6 "io"
7 "net"
8 "net/http"
9 "net/http/httptest"
10 "net/url"
11 "os"
12 "path/filepath"
13 "strings"
14 "testing"
15
16 "golang.org/x/net/websocket"
17
18 "github.com/mjl-/mox/mox-"
19)
20
21func tcheck(t *testing.T, err error, msg string) {
22 t.Helper()
23 if err != nil {
24 t.Fatalf("%s: %s", msg, err)
25 }
26}
27
28func TestWebserver(t *testing.T) {
29 os.RemoveAll("../testdata/webserver/data")
30 mox.ConfigStaticPath = filepath.FromSlash("../testdata/webserver/mox.conf")
31 mox.ConfigDynamicPath = filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
32 mox.MustLoadConfig(true, false)
33
34 loadStaticGzipCache(mox.DataDirPath("tmp/httpstaticcompresscache"), 1024*1024)
35
36 srv := &serve{Webserver: true}
37
38 test := func(method, target string, reqhdrs map[string]string, expCode int, expContent string, expHeaders map[string]string) {
39 t.Helper()
40
41 req := httptest.NewRequest(method, target, nil)
42 for k, v := range reqhdrs {
43 req.Header.Add(k, v)
44 }
45 rw := httptest.NewRecorder()
46 rw.Body = &bytes.Buffer{}
47 srv.ServeHTTP(rw, req)
48 resp := rw.Result()
49 if resp.StatusCode != expCode {
50 t.Fatalf("got statuscode %d, expected %d", resp.StatusCode, expCode)
51 }
52 if expContent != "" {
53 s := rw.Body.String()
54 if s != expContent {
55 t.Fatalf("got response data %q, expected %q", s, expContent)
56 }
57 }
58 for k, v := range expHeaders {
59 if xv := resp.Header.Get(k); xv != v {
60 t.Fatalf("got %q for header %q, expected %q", xv, k, v)
61 }
62 }
63 }
64
65 test("GET", "http://redir.mox.example", nil, http.StatusPermanentRedirect, "", map[string]string{"Location": "https://mox.example/"})
66
67 // http to https redirect, and stay on https afterwards without redirect loop.
68 test("GET", "http://schemeredir.example", nil, http.StatusPermanentRedirect, "", map[string]string{"Location": "https://schemeredir.example/"})
69 test("GET", "https://schemeredir.example", nil, http.StatusNotFound, "", nil)
70
71 accgzip := map[string]string{"Accept-Encoding": "gzip"}
72 test("GET", "http://mox.example/static/", accgzip, http.StatusOK, "", map[string]string{"X-Test": "mox", "Content-Encoding": "gzip"}) // index.html
73 test("GET", "http://mox.example/static/dir/hi.txt", accgzip, http.StatusOK, "", map[string]string{"X-Test": "mox", "Content-Encoding": ""}) // too small to compress
74 test("GET", "http://mox.example/static/dir/", accgzip, http.StatusOK, "", map[string]string{"X-Test": "mox", "Content-Encoding": "gzip"}) // listing
75 test("GET", "http://mox.example/static/dir", accgzip, http.StatusTemporaryRedirect, "", map[string]string{"Location": "/static/dir/"}) // redirect to dir
76 test("GET", "http://mox.example/static/bogus", accgzip, http.StatusNotFound, "", map[string]string{"Content-Encoding": ""})
77
78 test("GET", "http://mox.example/nolist/", nil, http.StatusOK, "", nil) // index.html
79 test("GET", "http://mox.example/nolist/dir/", nil, http.StatusForbidden, "", nil) // no listing
80
81 test("GET", "http://mox.example/tls/", nil, http.StatusPermanentRedirect, "", map[string]string{"Location": "https://mox.example/tls/"}) // redirect to tls
82
83 test("GET", "http://mox.example/baseurl/x?y=2", nil, http.StatusPermanentRedirect, "", map[string]string{"Location": "https://tls.mox.example/baseurl/x?q=1&y=2#fragment"})
84 test("GET", "http://mox.example/pathonly/old/x?q=2", nil, http.StatusTemporaryRedirect, "", map[string]string{"Location": "http://mox.example/pathonly/new/x?q=2"})
85 test("GET", "http://mox.example/baseurlpath/old/x?y=2", nil, http.StatusPermanentRedirect, "", map[string]string{"Location": "//other.mox.example/baseurlpath/new/x?q=1&y=2#fragment"})
86
87 test("GET", "http://mox.example/strip/x", nil, http.StatusBadGateway, "", nil) // no server yet
88 test("GET", "http://mox.example/nostrip/x", nil, http.StatusBadGateway, "", nil) // no server yet
89
90 badForwarded := map[string]string{
91 "Forwarded": "bad",
92 "X-Forwarded-For": "bad",
93 "X-Forwarded-Proto": "bad",
94 "X-Forwarded-Host": "bad",
95 "X-Forwarded-Ext": "bad",
96 }
97
98 // Server that echoes path, and forwarded request headers.
99 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
100 for k, v := range badForwarded {
101 if r.Header.Get(k) == v {
102 w.WriteHeader(http.StatusInternalServerError)
103 return
104 }
105 }
106
107 for k, vl := range r.Header {
108 if k == "Forwarded" || k == "X-Forwarded" || strings.HasPrefix(k, "X-Forwarded-") {
109 w.Header()[k] = vl
110 }
111 }
112 w.Write([]byte(r.URL.Path))
113 }))
114 defer server.Close()
115
116 serverURL, err := url.Parse(server.URL)
117 if err != nil {
118 t.Fatalf("parsing url: %v", err)
119 }
120 serverURL.Path = "/a"
121
122 // warning: it is not normally allowed to access the dynamic config without lock. don't propagate accesses like this!
123 mox.Conf.Dynamic.WebHandlers[len(mox.Conf.Dynamic.WebHandlers)-2].WebForward.TargetURL = serverURL
124 mox.Conf.Dynamic.WebHandlers[len(mox.Conf.Dynamic.WebHandlers)-1].WebForward.TargetURL = serverURL
125
126 test("GET", "http://mox.example/strip/x", badForwarded, http.StatusOK, "/a/x", map[string]string{
127 "X-Test": "mox",
128 "X-Forwarded-For": "192.0.2.1", // IP is hardcoded in Go's src/net/http/httptest/httptest.go
129 "X-Forwarded-Proto": "http",
130 "X-Forwarded-Host": "mox.example",
131 "X-Forwarded-Ext": "",
132 })
133 test("GET", "http://mox.example/nostrip/x", map[string]string{"X-OK": "ok"}, http.StatusOK, "/a/nostrip/x", map[string]string{"X-Test": "mox"})
134
135 test("GET", "http://mox.example/bogus", nil, http.StatusNotFound, "", nil) // path not registered.
136 test("GET", "http://bogus.mox.example/static/", nil, http.StatusNotFound, "", nil) // domain not registered.
137
138 npaths := len(staticgzcache.paths)
139 if npaths != 1 {
140 t.Fatalf("%d file(s) in staticgzcache, expected 1", npaths)
141 }
142 loadStaticGzipCache(mox.DataDirPath("tmp/httpstaticcompresscache"), 1024*1024)
143 npaths = len(staticgzcache.paths)
144 if npaths != 1 {
145 t.Fatalf("%d file(s) in staticgzcache after loading from disk, expected 1", npaths)
146 }
147 loadStaticGzipCache(mox.DataDirPath("tmp/httpstaticcompresscache"), 0)
148 npaths = len(staticgzcache.paths)
149 if npaths != 0 {
150 t.Fatalf("%d file(s) in staticgzcache after setting max size to 0, expected 0", npaths)
151 }
152 loadStaticGzipCache(mox.DataDirPath("tmp/httpstaticcompresscache"), 0)
153 npaths = len(staticgzcache.paths)
154 if npaths != 0 {
155 t.Fatalf("%d file(s) in staticgzcache after setting max size to 0 and reloading from disk, expected 0", npaths)
156 }
157}
158
159func TestWebsocket(t *testing.T) {
160 os.RemoveAll("../testdata/websocket/data")
161 mox.ConfigStaticPath = filepath.FromSlash("../testdata/websocket/mox.conf")
162 mox.ConfigDynamicPath = filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
163 mox.MustLoadConfig(true, false)
164
165 srv := &serve{Webserver: true}
166
167 var handler http.Handler // Active handler during test.
168 backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
169 handler.ServeHTTP(w, r)
170 }))
171
172 defer backend.Close()
173 backendURL, err := url.Parse(backend.URL)
174 if err != nil {
175 t.Fatalf("parsing backend url: %v", err)
176 }
177 backendURL.Path = "/"
178
179 // warning: it is not normally allowed to access the dynamic config without lock. don't propagate accesses like this!
180 mox.Conf.Dynamic.WebHandlers[len(mox.Conf.Dynamic.WebHandlers)-1].WebForward.TargetURL = backendURL
181
182 server := httptest.NewServer(srv)
183 defer server.Close()
184
185 serverURL, err := url.Parse(server.URL)
186 tcheck(t, err, "parsing server url")
187 _, port, err := net.SplitHostPort(serverURL.Host)
188 tcheck(t, err, "parsing host port in server url")
189 wsurl := fmt.Sprintf("ws://%s/ws/", net.JoinHostPort("localhost", port))
190
191 handler = websocket.Handler(func(c *websocket.Conn) {
192 io.Copy(c, c)
193 })
194
195 // Test a correct websocket connection.
196 wsconn, err := websocket.Dial(wsurl, "ignored", "http://ignored.example")
197 tcheck(t, err, "websocket dial")
198 _, err = fmt.Fprint(wsconn, "test")
199 tcheck(t, err, "write to websocket")
200 buf := make([]byte, 128)
201 n, err := wsconn.Read(buf)
202 tcheck(t, err, "read from websocket")
203 if string(buf[:n]) != "test" {
204 t.Fatalf(`got websocket data %q, expected "test"`, buf[:n])
205 }
206 err = wsconn.Close()
207 tcheck(t, err, "closing websocket connection")
208
209 // Test with server.ServeHTTP directly.
210 test := func(method string, reqhdrs map[string]string, expCode int, expHeaders map[string]string) {
211 t.Helper()
212
213 req := httptest.NewRequest(method, wsurl, nil)
214 for k, v := range reqhdrs {
215 req.Header.Add(k, v)
216 }
217 rw := httptest.NewRecorder()
218 rw.Body = &bytes.Buffer{}
219 srv.ServeHTTP(rw, req)
220 resp := rw.Result()
221 if resp.StatusCode != expCode {
222 t.Fatalf("got statuscode %d, expected %d", resp.StatusCode, expCode)
223 }
224 for k, v := range expHeaders {
225 if xv := resp.Header.Get(k); xv != v {
226 t.Fatalf("got %q for header %q, expected %q", xv, k, v)
227 }
228 }
229 }
230
231 wsreqhdrs := map[string]string{
232 "Upgrade": "keep-alive, websocket",
233 "Connection": "X, Upgrade",
234 "Sec-Websocket-Version": "13",
235 "Sec-Websocket-Key": "AAAAAAAAAAAAAAAAAAAAAA==",
236 }
237
238 test("POST", wsreqhdrs, http.StatusBadRequest, nil)
239
240 clone := func(m map[string]string) map[string]string {
241 r := map[string]string{}
242 for k, v := range m {
243 r[k] = v
244 }
245 return r
246 }
247
248 hdrs := clone(wsreqhdrs)
249 hdrs["Sec-Websocket-Version"] = "14"
250 test("GET", hdrs, http.StatusBadRequest, map[string]string{"Sec-Websocket-Version": "13"})
251
252 httpurl := fmt.Sprintf("http://%s/ws/", net.JoinHostPort("localhost", port))
253
254 // Must now do actual HTTP requests and read the HTTP response. Cannot call
255 // ServeHTTP because ResponseRecorder is not a http.Hijacker.
256 test = func(method string, reqhdrs map[string]string, expCode int, expHeaders map[string]string) {
257 t.Helper()
258
259 req, err := http.NewRequest(method, httpurl, nil)
260 tcheck(t, err, "http newrequest")
261 for k, v := range reqhdrs {
262 req.Header.Add(k, v)
263 }
264 resp, err := http.DefaultClient.Do(req)
265 tcheck(t, err, "http transaction")
266 if resp.StatusCode != expCode {
267 t.Fatalf("got statuscode %d, expected %d", resp.StatusCode, expCode)
268 }
269 for k, v := range expHeaders {
270 if xv := resp.Header.Get(k); xv != v {
271 t.Fatalf("got %q for header %q, expected %q", xv, k, v)
272 }
273 }
274 }
275
276 hdrs = clone(wsreqhdrs)
277 hdrs["Sec-Websocket-Key"] = "malformed"
278 test("GET", hdrs, http.StatusBadRequest, nil)
279
280 hdrs = clone(wsreqhdrs)
281 hdrs["Sec-Websocket-Key"] = "c2hvcnQK" // "short"
282 test("GET", hdrs, http.StatusBadRequest, nil)
283
284 // Not responding with a 101, but with regular 200 OK response.
285 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
286 http.Error(w, "bad", http.StatusOK)
287 })
288 test("GET", wsreqhdrs, http.StatusBadRequest, nil)
289
290 // Respond with 101, but other websocket response headers missing.
291 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
292 w.WriteHeader(http.StatusSwitchingProtocols)
293 })
294 test("GET", wsreqhdrs, http.StatusBadRequest, nil)
295
296 // With Upgrade: websocket, without Connection: Upgrade
297 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
298 w.Header().Set("Upgrade", "websocket")
299 w.WriteHeader(http.StatusSwitchingProtocols)
300 })
301 test("GET", wsreqhdrs, http.StatusBadRequest, nil)
302
303 // With malformed Sec-WebSocket-Accept response header.
304 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
305 h := w.Header()
306 h.Set("Upgrade", "websocket")
307 h.Set("Connection", "Upgrade")
308 h.Set("Sec-WebSocket-Accept", "malformed")
309 w.WriteHeader(http.StatusSwitchingProtocols)
310 })
311 test("GET", wsreqhdrs, http.StatusBadRequest, nil)
312
313 // With malformed Sec-WebSocket-Accept response header.
314 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
315 h := w.Header()
316 h.Set("Upgrade", "websocket")
317 h.Set("Connection", "Upgrade")
318 h.Set("Sec-WebSocket-Accept", "YmFk") // "bad"
319 w.WriteHeader(http.StatusSwitchingProtocols)
320 })
321 test("GET", wsreqhdrs, http.StatusBadRequest, nil)
322
323 // All good.
324 wsresphdrs := map[string]string{
325 "Connection": "Upgrade",
326 "Upgrade": "websocket",
327 "Sec-Websocket-Accept": "ICX+Yqv66kxgM0FcWaLWlFLwTAI=",
328 "X-Test": "mox",
329 }
330 handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
331 h := w.Header()
332 h.Set("Upgrade", "websocket")
333 h.Set("Connection", "Upgrade")
334 h.Set("Sec-WebSocket-Accept", "ICX+Yqv66kxgM0FcWaLWlFLwTAI=")
335 w.WriteHeader(http.StatusSwitchingProtocols)
336 })
337 test("GET", wsreqhdrs, http.StatusSwitchingProtocols, wsresphdrs)
338
339}
340