aboutsummaryrefslogtreecommitdiffstats
path: root/test/http/http1/test_protocol.py
blob: f7c615bd9bf35b26737918a407e73a72a1db6034 (plain)
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
import cStringIO
import textwrap

from netlib import http, odict, tcp, tutils
from netlib.http import semantics, Headers
from netlib.http.http1 import HTTP1Protocol
from ... import tservers


class NoContentLengthHTTPHandler(tcp.BaseHandler):
    def handle(self):
        self.wfile.write("HTTP/1.1 200 OK\r\n\r\nbar\r\n\r\n")
        self.wfile.flush()


def mock_protocol(data=''):
    rfile = cStringIO.StringIO(data)
    wfile = cStringIO.StringIO()
    return HTTP1Protocol(rfile=rfile, wfile=wfile)


def match_http_string(data):
    return textwrap.dedent(data).strip().replace('\n', '\r\n')


def test_stripped_chunked_encoding_no_content():
    """
    https://github.com/mitmproxy/mitmproxy/issues/186
    """

    r = tutils.treq(content="")
    r.headers["Transfer-Encoding"] = "chunked"
    assert "Content-Length" in mock_protocol()._assemble_request_headers(r)

    r = tutils.tresp(content="")
    r.headers["Transfer-Encoding"] = "chunked"
    assert "Content-Length" in mock_protocol()._assemble_response_headers(r)


def test_has_chunked_encoding():
    headers = http.Headers()
    assert not HTTP1Protocol.has_chunked_encoding(headers)
    headers["transfer-encoding"] = "chunked"
    assert HTTP1Protocol.has_chunked_encoding(headers)


def test_read_chunked():
    headers = http.Headers()
    headers["transfer-encoding"] = "chunked"

    data = "1\r\na\r\n0\r\n"
    tutils.raises(
        "malformed chunked body",
        mock_protocol(data).read_http_body,
        headers, None, "GET", None, True
    )

    data = "1\r\na\r\n0\r\n\r\n"
    assert mock_protocol(data).read_http_body(headers, None, "GET", None, True) == "a"

    data = "\r\n\r\n1\r\na\r\n0\r\n\r\n"
    assert mock_protocol(data).read_http_body(headers, None, "GET", None, True) == "a"

    data = "\r\n"
    tutils.raises(
        "closed prematurely",
        mock_protocol(data).read_http_body,
        headers, None, "GET", None, True
    )

    data = "1\r\nfoo"
    tutils.raises(
        "malformed chunked body",
        mock_protocol(data).read_http_body,
        headers, None, "GET", None, True
    )

    data = "foo\r\nfoo"
    tutils.raises(
        http.HttpError,
        mock_protocol(data).read_http_body,
        headers, None, "GET", None, True
    )

    data = "5\r\naaaaa\r\n0\r\n\r\n"
    tutils.raises("too large", mock_protocol(data).read_http_body, headers, 2, "GET", None, True)


def test_connection_close():
    headers = Headers()
    assert HTTP1Protocol.connection_close((1, 0), headers)
    assert not HTTP1Protocol.connection_close((1, 1), headers)

    headers["connection"] = "keep-alive"
    assert not HTTP1Protocol.connection_close((1, 1), headers)

    headers["connection"] = "close"
    assert HTTP1Protocol.connection_close((1, 1), headers)


def test_read_http_body_request():
    headers = Headers()
    data = "testing"
    assert mock_protocol(data).read_http_body(headers, None, "GET", None, True) == ""


def test_read_http_body_response():
    headers = Headers()
    data = "testing"
    assert mock_protocol(data).read_http_body(headers, None, "GET", 200, False) == "testing"


def test_read_http_body():
    # test default case
    headers = Headers()
    headers["content-length"] = "7"
    data = "testing"
    assert mock_protocol(data).read_http_body(headers, None, "GET", 200, False) == "testing"

    # test content length: invalid header
    headers["content-length"] = "foo"
    data = "testing"
    tutils.raises(
        http.HttpError,
        mock_protocol(data).read_http_body,
        headers, None, "GET", 200, False
    )

    # test content length: invalid header #2
    headers["content-length"] = "-1"
    data = "testing"
    tutils.raises(
        http.HttpError,
        mock_protocol(data).read_http_body,
        headers, None, "GET", 200, False
    )

    # test content length: content length > actual content
    headers["content-length"] = "5"
    data = "testing"
    tutils.raises(
        http.HttpError,
        mock_protocol(data).read_http_body,
        headers, 4, "GET", 200, False
    )

    # test content length: content length < actual content
    data = "testing"
    assert len(mock_protocol(data).read_http_body(headers, None, "GET", 200, False)) == 5

    # test no content length: limit > actual content
    headers = Headers()
    data = "testing"
    assert len(mock_protocol(data).read_http_body(headers, 100, "GET", 200, False)) == 7

    # test no content length: limit < actual content
    data = "testing"
    tutils.raises(
        http.HttpError,
        mock_protocol(data).read_http_body,
        headers, 4, "GET", 200, False
    )

    # test chunked
    headers = Headers()
    headers["transfer-encoding"] = "chunked"
    data = "5\r\naaaaa\r\n0\r\n\r\n"
    assert mock_protocol(data).read_http_body(headers, 100, "GET", 200, False) == "aaaaa"


def test_expected_http_body_size():
    # gibber in the content-length field
    headers = Headers(content_length="foo")
    assert HTTP1Protocol.expected_http_body_size(headers, False, "GET", 200) is None
    # negative number in the content-length field
    headers = Headers(content_length="-7")
    assert HTTP1Protocol.expected_http_body_size(headers, False, "GET", 200) is None
    # explicit length
    headers = Headers(content_length="5")
    assert HTTP1Protocol.expected_http_body_size(headers, False, "GET", 200) == 5
    # no length
    headers = Headers()
    assert HTTP1Protocol.expected_http_body_size(headers, False, "GET", 200) == -1
    # no length request
    headers = Headers()
    assert HTTP1Protocol.expected_http_body_size(headers, True, "GET", None) == 0


def test_get_request_line():
    data = "\nfoo"
    p = mock_protocol(data)
    assert p._get_request_line() == "foo"
    assert not p._get_request_line()


def test_parse_http_protocol():
    assert HTTP1Protocol._parse_http_protocol("HTTP/1.1") == (1, 1)
    assert HTTP1Protocol._parse_http_protocol("HTTP/0.0") == (0, 0)
    assert not HTTP1Protocol._parse_http_protocol("HTTP/a.1")
    assert not HTTP1Protocol._parse_http_protocol("HTTP/1.a")
    assert not HTTP1Protocol._parse_http_protocol("foo/0.0")
    assert not HTTP1Protocol._parse_http_protocol("HTTP/x")


def test_parse_init_connect():
    assert HTTP1Protocol._parse_init_connect("CONNECT host.com:443 HTTP/1.0")
    assert not HTTP1Protocol._parse_init_connect("C\xfeONNECT host.com:443 HTTP/1.0")
    assert not HTTP1Protocol._parse_init_connect("CONNECT \0host.com:443 HTTP/1.0")
    assert not HTTP1Protocol._parse_init_connect("CONNECT host.com:444444 HTTP/1.0")
    assert not HTTP1Protocol._parse_init_connect("bogus")
    assert not HTTP1Protocol._parse_init_connect("GET host.com:443 HTTP/1.0")
    assert not HTTP1Protocol._parse_init_connect("CONNECT host.com443 HTTP/1.0")
    assert not HTTP1Protocol._parse_init_connect("CONNECT host.com:443 foo/1.0")
    assert not HTTP1Protocol._parse_init_connect("CONNECT host.com:foo HTTP/1.0")


def test_parse_init_proxy():
    u = "GET http://foo.com:8888/test HTTP/1.1"
    m, s, h, po, pa, httpversion = HTTP1Protocol._parse_init_proxy(u)
    assert m == "GET"
    assert s == "http"
    assert h == "foo.com"
    assert po == 8888
    assert pa == "/test"
    assert httpversion == (1, 1)

    u = "G\xfeET http://foo.com:8888/test HTTP/1.1"
    assert not HTTP1Protocol._parse_init_proxy(u)

    assert not HTTP1Protocol._parse_init_proxy("invalid")
    assert not HTTP1Protocol._parse_init_proxy("GET invalid HTTP/1.1")
    assert not HTTP1Protocol._parse_init_proxy("GET http://foo.com:8888/test foo/1.1")


def test_parse_init_http():
    u = "GET /test HTTP/1.1"
    m, u, httpversion = HTTP1Protocol._parse_init_http(u)
    assert m == "GET"
    assert u == "/test"
    assert httpversion == (1, 1)

    u = "G\xfeET /test HTTP/1.1"
    assert not HTTP1Protocol._parse_init_http(u)

    assert not HTTP1Protocol._parse_init_http("invalid")
    assert not HTTP1Protocol._parse_init_http("GET invalid HTTP/1.1")
    assert not HTTP1Protocol._parse_init_http("GET /test foo/1.1")
    assert not HTTP1Protocol._parse_init_http("GET /test\xc0 HTTP/1.1")


class TestReadHeaders:

    def _read(self, data, verbatim=False):
        if not verbatim:
            data = textwrap.dedent(data)
            data = data.strip()
        return mock_protocol(data).read_headers()

    def test_read_simple(self):
        data = """
            Header: one
            Header2: two
            \r\n
        """
        headers = self._read(data)
        assert headers.fields == [["Header", "one"], ["Header2", "two"]]

    def test_read_multi(self):
        data = """
            Header: one
            Header: two
            \r\n
        """
        headers = self._read(data)
        assert headers.fields == [["Header", "one"], ["Header", "two"]]

    def test_read_continued(self):
        data = """
            Header: one
            \ttwo
            Header2: three
            \r\n
        """
        headers = self._read(data)
        assert headers.fields == [["Header", "one\r\n two"], ["Header2", "three"]]

    def test_read_continued_err(self):
        data = "\tfoo: bar\r\n"
        assert self._read(data, True) is None

    def test_read_err(self):
        data = """
            foo
        """
        assert self._read(data) is None


class TestReadRequest(object):

    def tst(self, data, **kwargs):
        return mock_protocol(data).read_request(**kwargs)

    def test_invalid(self):
        tutils.raises(
            "bad http request",
            self.tst,
            "xxx"
        )
        tutils.raises(
            "bad http request line",
            self.tst,
            "get /\xff HTTP/1.1"
        )
        tutils.raises(
            "invalid headers",
            self.tst,
            "get / HTTP/1.1\r\nfoo"
        )
        tutils.raises(
            tcp.NetLibDisconnect,
            self.tst,
            "\r\n"
        )

    def test_empty(self):
        v = self.tst("", allow_empty=True)
        assert isinstance(v, semantics.EmptyRequest)

    def test_asterisk_form_in(self):
        v = self.tst("OPTIONS * HTTP/1.1")
        assert v.form_in == "relative"
        assert v.method == "OPTIONS"

    def test_absolute_form_in(self):
        tutils.raises(
            "Bad HTTP request line",
            self.tst,
            "GET oops-no-protocol.com HTTP/1.1"
        )
        v = self.tst("GET http://address:22/ HTTP/1.1")
        assert v.form_in == "absolute"
        assert v.port == 22
        assert v.host == "address"
        assert v.scheme == "http"

    def test_connect(self):
        tutils.raises(
            "Bad HTTP request line",
            self.tst,
            "CONNECT oops-no-port.com HTTP/1.1"
        )
        v = self.tst("CONNECT foo.com:443 HTTP/1.1")
        assert v.form_in == "authority"
        assert v.method == "CONNECT"
        assert v.port == 443
        assert v.host == "foo.com"

    def test_expect(self):
        data = "".join(
            "GET / HTTP/1.1\r\n"
            "Content-Length: 3\r\n"
            "Expect: 100-continue\r\n\r\n"
            "foobar"
        )

        p = mock_protocol(data)
        v = p.read_request()
        assert p.tcp_handler.wfile.getvalue() == "HTTP/1.1 100 Continue\r\n\r\n"
        assert v.body == "foo"
        assert p.tcp_handler.rfile.read(3) == "bar"


class TestReadResponse(object):
    def tst(self, data, method, body_size_limit, include_body=True):
        data = textwrap.dedent(data)
        return mock_protocol(data).read_response(
            method, body_size_limit, include_body=include_body
        )

    def test_errors(self):
        tutils.raises("server disconnect", self.tst, "", "GET", None)
        tutils.raises("invalid server response", self.tst, "foo", "GET", None)

    def test_simple(self):
        data = """
            HTTP/1.1 200
        """
        assert self.tst(data, "GET", None) == http.Response(
            (1, 1), 200, '', Headers(), ''
        )

    def test_simple_message(self):
        data = """
            HTTP/1.1 200 OK
        """
        assert self.tst(data, "GET", None) == http.Response(
            (1, 1), 200, 'OK', Headers(), ''
        )

    def test_invalid_http_version(self):
        data = """
            HTTP/x 200 OK
        """
        tutils.raises("invalid http version", self.tst, data, "GET", None)

    def test_invalid_status_code(self):
        data = """
            HTTP/1.1 xx OK
        """
        tutils.raises("invalid server response", self.tst, data, "GET", None)

    def test_valid_with_continue(self):
        data = """
            HTTP/1.1 100 CONTINUE

            HTTP/1.1 200 OK
        """
        assert self.tst(data, "GET", None) == http.Response(
            (1, 1), 100, 'CONTINUE', Headers(), ''
        )

    def test_simple_body(self):
        data = """
            HTTP/1.1 200 OK
            Content-Length: 3

            foo
        """
        assert self.tst(data, "GET", None).body == 'foo'
        assert self.tst(data, "HEAD", None).body == ''

    def test_invalid_headers(self):
        data = """
            HTTP/1.1 200 OK
            \tContent-Length: 3

            foo
        """
        tutils.raises("invalid headers", self.tst, data, "GET", None)

    def test_without_body(self):
        data = """
            HTTP/1.1 200 OK
            Content-Length: 3

            foo
        """
        assert self.tst(data, "GET", None, include_body=False).body is None


class TestReadResponseNoContentLength(tservers.ServerTestBase):
    handler = NoContentLengthHTTPHandler

    def test_no_content_length(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        resp = HTTP1Protocol(c).read_response("GET", None)
        assert resp.body == "bar\r\n\r\n"


class TestAssembleRequest(object):
    def test_simple(self):
        req = tutils.treq()
        b = HTTP1Protocol().assemble_request(req)
        assert b == match_http_string("""
            GET /path HTTP/1.1
            header: qvalue
            Host: address:22
            Content-Length: 7

            content""")

    def test_body_missing(self):
        req = tutils.treq(content=semantics.CONTENT_MISSING)
        tutils.raises(http.HttpError, HTTP1Protocol().assemble_request, req)

    def test_not_a_request(self):
        tutils.raises(AssertionError, HTTP1Protocol().assemble_request, 'foo')


class TestAssembleResponse(object):
    def test_simple(self):
        resp = tutils.tresp()
        b = HTTP1Protocol().assemble_response(resp)
        assert b == match_http_string("""
            HTTP/1.1 200 OK
            header_response: svalue
            Content-Length: 7

            message""")

    def test_body_missing(self):
        resp = tutils.tresp(content=semantics.CONTENT_MISSING)
        tutils.raises(http.HttpError, HTTP1Protocol().assemble_response, resp)

    def test_not_a_request(self):
        tutils.raises(AssertionError, HTTP1Protocol().assemble_response, 'foo')