class EmojiInjectionPlugin(HttpProxyBasePlugin):
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.response = HttpParser(httpParserTypes.RESPONSE_PARSER)

    def before_upstream_connection(self, request: HttpParser) -> Optional[HttpParser]:
        return request

    def handle_client_request(self, request: HttpParser) -> Optional[HttpParser]:
        return request

    def handle_upstream_chunk(self, chunk: memoryview) -> memoryview:
        self.response.parse(chunk.tobytes())
        if self.response.state == httpParserStates.COMPLETE:
            self.handle_response_data()
            new_content_length = len(self.response.body)
            bytes_header_name = b'content-length'
            self.response.add_header(bytes_header_name, bytes_(new_content_length))
            self.client.queue(memoryview(self.response.build_response()))
        return memoryview(b'')

    def on_upstream_connection_close(self) -> None:
        pass

    def handle_response_data(self) -> None:
        if 'text/html' in self.response.header(b'content-type').decode():
            response_handler = HtmlResponseHandler(self.response)
        elif 'application/json' == self.response.header(b'content-type').decode():
            response_handler = JsonResponseHandler(self.response)
        else:
            return
        response_handler.insert_emoji_to_response_data()
Example #2
0
    def before_upstream_connection(
            self, request: HttpParser) -> Optional[HttpParser]:
        if request.has_header(b'Host'):
            print(request.headers)
            if request.headers[b'host'] == (b'Host', b'github.com'):
                repo_name, _ = request.path.split(b'.git', 1)
                repo_url = b'[email protected]:'+repo_name[1:]+b'.git'
                print('repo name: ', repo_name)
                if not os.path.exists((b'repos'+repo_name).decode()):
                    print('repo path: ', (b'repos'+repo_name).decode())
                    os.makedirs((b'repos'+repo_name).decode(), exist_ok=True)
                    print(*['git', 'clone', repo_url.decode(), '.'+repo_name.decode()], sep=' ')
                    subprocess.check_call(['git', '--bare', 'clone', repo_url.decode(), '.'+repo_name.decode()], cwd='repos')
                    subprocess.check_call(['mv',  '.'+repo_name.decode()+'/.git', '.'+repo_name.decode()+'.git'], cwd='repos')

                    print('path: ', (b'repos'+repo_name+b'/.git').decode())
                    subprocess.check_call(['git', '--bare', 'update-server-info'], cwd=(b'repos'+repo_name+b'.git').decode())


        # Redirect all non-https requests to inbuilt WebServer.
        if request.method != httpMethods.CONNECT:
            request.set_url(self.UPSTREAM_SERVER+request.url.path)
            print(request.url)
            # Update Host header too, otherwise upstream can reject our request
            if request.has_header(b'Host'):
                request.del_header(b'Host')
            request.add_header(
                b'Host', urlparse.urlsplit(
                    self.UPSTREAM_SERVER).netloc)
        return request
Example #3
0
    def before_upstream_connection(
            self, request: HttpParser) -> Optional[HttpParser]:
        """Avoid upstream connection of server in the request.
        Initialize, connection to upstream proxy.
        """
        if 'Zeus-proxy' in request.headers:
            [host, port, username,
             password] = request.header['Zeus-proxy'].split(':')
            del request.header['Zeus-proxy']
            request.add_header('Proxy-Authorization',
                               _basic_auth_str(self.username, self.password))
            self.conn = new_socket_connection((host, int(port)))
        else:
            raise Exception('Could not find Zeus-proxy header')

        # Implement your own logic here e.g. round-robin, least connection etc.
        return None
class TestHttpParser(unittest.TestCase):
    def setUp(self) -> None:
        self.parser = HttpParser(httpParserTypes.REQUEST_PARSER)

    def test_urlparse(self) -> None:
        self.parser.parse(b'CONNECT httpbin.org:443 HTTP/1.1\r\n')
        self.assertEqual(self.parser.host, b'httpbin.org')
        self.assertEqual(self.parser.port, 443)

    def test_build_request(self) -> None:
        self.assertEqual(
            build_http_request(b'GET', b'http://localhost:12345', b'HTTP/1.1'),
            CRLF.join([b'GET http://localhost:12345 HTTP/1.1', CRLF]))
        self.assertEqual(
            build_http_request(b'GET',
                               b'http://localhost:12345',
                               b'HTTP/1.1',
                               headers={b'key': b'value'}),
            CRLF.join(
                [b'GET http://localhost:12345 HTTP/1.1', b'key: value', CRLF]))
        self.assertEqual(
            build_http_request(b'GET',
                               b'http://localhost:12345',
                               b'HTTP/1.1',
                               headers={b'key': b'value'},
                               body=b'Hello from py'),
            CRLF.join([
                b'GET http://localhost:12345 HTTP/1.1', b'key: value', CRLF
            ]) + b'Hello from py')

    def test_build_response(self) -> None:
        self.assertEqual(
            build_http_response(200,
                                reason=b'OK',
                                protocol_version=b'HTTP/1.1'),
            CRLF.join([b'HTTP/1.1 200 OK', CRLF]))
        self.assertEqual(
            build_http_response(200,
                                reason=b'OK',
                                protocol_version=b'HTTP/1.1',
                                headers={b'key': b'value'}),
            CRLF.join([b'HTTP/1.1 200 OK', b'key: value', CRLF]))

    def test_build_response_adds_content_length_header(self) -> None:
        body = b'Hello world!!!'
        self.assertEqual(
            build_http_response(200,
                                reason=b'OK',
                                protocol_version=b'HTTP/1.1',
                                headers={b'key': b'value'},
                                body=body),
            CRLF.join([
                b'HTTP/1.1 200 OK', b'key: value',
                b'Content-Length: ' + bytes_(len(body)), CRLF
            ]) + body)

    def test_build_header(self) -> None:
        self.assertEqual(build_http_header(b'key', b'value'), b'key: value')

    def test_header_raises(self) -> None:
        with self.assertRaises(KeyError):
            self.parser.header(b'not-found')

    def test_has_header(self) -> None:
        self.parser.add_header(b'key', b'value')
        self.assertFalse(self.parser.has_header(b'not-found'))
        self.assertTrue(self.parser.has_header(b'key'))

    def test_set_host_port_raises(self) -> None:
        with self.assertRaises(KeyError):
            self.parser.set_line_attributes()

    def test_find_line(self) -> None:
        self.assertEqual(
            find_http_line(b'CONNECT python.org:443 HTTP/1.0\r\n\r\n'),
            (b'CONNECT python.org:443 HTTP/1.0', CRLF))

    def test_find_line_returns_None(self) -> None:
        self.assertEqual(find_http_line(b'CONNECT python.org:443 HTTP/1.0'),
                         (None, b'CONNECT python.org:443 HTTP/1.0'))

    def test_connect_request_with_crlf_as_separate_chunk(self) -> None:
        """See https://github.com/abhinavsingh/py/issues/70 for background."""
        raw = b'CONNECT pypi.org:443 HTTP/1.0\r\n'
        self.parser.parse(raw)
        self.assertEqual(self.parser.state, httpParserStates.LINE_RCVD)
        self.parser.parse(CRLF)
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_get_full_parse(self) -> None:
        raw = CRLF.join([b'GET %s HTTP/1.1', b'Host: %s', CRLF])
        pkt = raw % (b'https://example.com/path/dir/?a=b&c=d#p=q',
                     b'example.com')
        self.parser.parse(pkt)
        self.assertEqual(self.parser.total_size, len(pkt))
        self.assertEqual(self.parser.build_path(), b'/path/dir/?a=b&c=d#p=q')
        self.assertEqual(self.parser.method, b'GET')
        assert self.parser.url
        self.assertEqual(self.parser.url.hostname, b'example.com')
        self.assertEqual(self.parser.url.port, None)
        self.assertEqual(self.parser.version, b'HTTP/1.1')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)
        self.assertEqual(self.parser.headers[b'host'],
                         (b'Host', b'example.com'))
        self.parser.del_headers([b'host'])
        self.parser.add_headers([(b'Host', b'example.com')])
        self.assertEqual(raw % (b'/path/dir/?a=b&c=d#p=q', b'example.com'),
                         self.parser.build())

    def test_build_url_none(self) -> None:
        self.assertEqual(self.parser.build_path(), b'/None')

    def test_line_rcvd_to_rcving_headers_state_change(self) -> None:
        pkt = b'GET http://localhost HTTP/1.1'
        self.parser.parse(pkt)
        self.assertEqual(self.parser.total_size, len(pkt))
        self.assert_state_change_with_crlf(httpParserStates.INITIALIZED,
                                           httpParserStates.LINE_RCVD,
                                           httpParserStates.COMPLETE)

    def test_get_partial_parse1(self) -> None:
        pkt = CRLF.join([b'GET http://localhost:8080 HTTP/1.1'])
        self.parser.parse(pkt)
        self.assertEqual(self.parser.total_size, len(pkt))
        self.assertEqual(self.parser.method, None)
        self.assertEqual(self.parser.url, None)
        self.assertEqual(self.parser.version, None)
        self.assertEqual(self.parser.state, httpParserStates.INITIALIZED)

        self.parser.parse(CRLF)
        self.assertEqual(self.parser.total_size, len(pkt) + len(CRLF))
        self.assertEqual(self.parser.method, b'GET')
        assert self.parser.url
        self.assertEqual(self.parser.url.hostname, b'localhost')
        self.assertEqual(self.parser.url.port, 8080)
        self.assertEqual(self.parser.version, b'HTTP/1.1')
        self.assertEqual(self.parser.state, httpParserStates.LINE_RCVD)

        host_hdr = b'Host: localhost:8080'
        self.parser.parse(host_hdr)
        self.assertEqual(self.parser.total_size,
                         len(pkt) + len(CRLF) + len(host_hdr))
        self.assertDictEqual(self.parser.headers, dict())
        self.assertEqual(self.parser.buffer, b'Host: localhost:8080')
        self.assertEqual(self.parser.state, httpParserStates.LINE_RCVD)

        self.parser.parse(CRLF * 2)
        self.assertEqual(self.parser.total_size,
                         len(pkt) + (3 * len(CRLF)) + len(host_hdr))
        self.assertEqual(self.parser.headers[b'host'],
                         (b'Host', b'localhost:8080'))
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_get_partial_parse2(self) -> None:
        self.parser.parse(
            CRLF.join([b'GET http://localhost:8080 HTTP/1.1', b'Host: ']))
        self.assertEqual(self.parser.method, b'GET')
        assert self.parser.url
        self.assertEqual(self.parser.url.hostname, b'localhost')
        self.assertEqual(self.parser.url.port, 8080)
        self.assertEqual(self.parser.version, b'HTTP/1.1')
        self.assertEqual(self.parser.buffer, b'Host: ')
        self.assertEqual(self.parser.state, httpParserStates.LINE_RCVD)

        self.parser.parse(b'localhost:8080' + CRLF)
        self.assertEqual(self.parser.headers[b'host'],
                         (b'Host', b'localhost:8080'))
        self.assertEqual(self.parser.buffer, b'')
        self.assertEqual(self.parser.state, httpParserStates.RCVING_HEADERS)

        self.parser.parse(b'Content-Type: text/plain' + CRLF)
        self.assertEqual(self.parser.buffer, b'')
        self.assertEqual(self.parser.headers[b'content-type'],
                         (b'Content-Type', b'text/plain'))
        self.assertEqual(self.parser.state, httpParserStates.RCVING_HEADERS)

        self.parser.parse(CRLF)
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_post_full_parse(self) -> None:
        raw = CRLF.join([
            b'POST %s HTTP/1.1', b'Host: localhost', b'Content-Length: 7',
            b'Content-Type: application/x-www-form-urlencoded' + CRLF,
            b'a=b&c=d'
        ])
        self.parser.parse(raw % b'http://localhost')
        self.assertEqual(self.parser.method, b'POST')
        assert self.parser.url
        self.assertEqual(self.parser.url.hostname, b'localhost')
        self.assertEqual(self.parser.url.port, None)
        self.assertEqual(self.parser.version, b'HTTP/1.1')
        self.assertEqual(
            self.parser.headers[b'content-type'],
            (b'Content-Type', b'application/x-www-form-urlencoded'))
        self.assertEqual(self.parser.headers[b'content-length'],
                         (b'Content-Length', b'7'))
        self.assertEqual(self.parser.body, b'a=b&c=d')
        self.assertEqual(self.parser.buffer, b'')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)
        self.assertEqual(len(self.parser.build()), len(raw % b'/'))

    def assert_state_change_with_crlf(self, initial_state: int,
                                      next_state: int,
                                      final_state: int) -> None:
        self.assertEqual(self.parser.state, initial_state)
        self.parser.parse(CRLF)
        self.assertEqual(self.parser.state, next_state)
        self.parser.parse(CRLF)
        self.assertEqual(self.parser.state, final_state)

    def test_post_partial_parse(self) -> None:
        self.parser.parse(
            CRLF.join([
                b'POST http://localhost HTTP/1.1', b'Host: localhost',
                b'Content-Length: 7',
                b'Content-Type: application/x-www-form-urlencoded'
            ]))
        self.assertEqual(self.parser.method, b'POST')
        assert self.parser.url
        self.assertEqual(self.parser.url.hostname, b'localhost')
        self.assertEqual(self.parser.url.port, None)
        self.assertEqual(self.parser.version, b'HTTP/1.1')
        self.assert_state_change_with_crlf(httpParserStates.RCVING_HEADERS,
                                           httpParserStates.RCVING_HEADERS,
                                           httpParserStates.HEADERS_COMPLETE)

        self.parser.parse(b'a=b')
        self.assertEqual(self.parser.state, httpParserStates.RCVING_BODY)
        self.assertEqual(self.parser.body, b'a=b')
        self.assertEqual(self.parser.buffer, b'')

        self.parser.parse(b'&c=d')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)
        self.assertEqual(self.parser.body, b'a=b&c=d')
        self.assertEqual(self.parser.buffer, b'')

    def test_connect_request_without_host_header_request_parse(self) -> None:
        """Case where clients can send CONNECT request without a Host header field.

        Example:
            1. pip3 --proxy http://localhost:8899 install <package name>
               Uses HTTP/1.0, Host header missing with CONNECT requests
            2. Android Emulator
               Uses HTTP/1.1, Host header missing with CONNECT requests

        See https://github.com/abhinavsingh/py/issues/5 for details.
        """
        self.parser.parse(b'CONNECT pypi.org:443 HTTP/1.0\r\n\r\n')
        self.assertEqual(self.parser.method, b'CONNECT')
        self.assertEqual(self.parser.version, b'HTTP/1.0')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_request_parse_without_content_length(self) -> None:
        """Case when incoming request doesn't contain a content-length header.

        From http://w3-org.9356.n7.nabble.com/POST-with-empty-body-td103965.html
        'A POST with no content-length and no body is equivalent to a POST with Content-Length: 0
        and nothing following, as could perfectly happen when you upload an empty file for instance.'

        See https://github.com/abhinavsingh/py/issues/20 for details.
        """
        self.parser.parse(
            CRLF.join([
                b'POST http://localhost HTTP/1.1', b'Host: localhost',
                b'Content-Type: application/x-www-form-urlencoded', CRLF
            ]))
        self.assertEqual(self.parser.method, b'POST')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_response_parse_without_content_length(self) -> None:
        """Case when server response doesn't contain a content-length header for non-chunk response types.

        HttpParser by itself has no way to know if more data should be expected.
        In example below, parser reaches state httpParserStates.HEADERS_COMPLETE
        and it is responsibility of callee to change state to httpParserStates.COMPLETE
        when server stream closes.

        See https://github.com/abhinavsingh/proxy.py/issues/20 for details.

        Post commit https://github.com/abhinavsingh/proxy.py/commit/269484df2e89bc659124177d339d4fc59f280cba
        HttpParser would reach state COMPLETE also for RESPONSE_PARSER types and no longer
        it is callee responsibility to change state on stream close.  This was important because
        pipelined responses not trigger stream close but may receive multiple responses.
        """
        self.parser.type = httpParserTypes.RESPONSE_PARSER
        self.parser.parse(b'HTTP/1.0 200 OK' + CRLF)
        self.assertEqual(self.parser.code, b'200')
        self.assertEqual(self.parser.version, b'HTTP/1.0')
        self.assertEqual(self.parser.state, httpParserStates.LINE_RCVD)
        self.parser.parse(
            CRLF.join([
                b'Server: BaseHTTP/0.3 Python/2.7.10',
                b'Date: Thu, 13 Dec 2018 16:24:09 GMT', CRLF
            ]))
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_response_parse(self) -> None:
        self.parser.type = httpParserTypes.RESPONSE_PARSER
        self.parser.parse(b''.join([
            b'HTTP/1.1 301 Moved Permanently\r\n',
            b'Location: http://www.google.com/\r\n',
            b'Content-Type: text/html; charset=UTF-8\r\n',
            b'Date: Wed, 22 May 2013 14:07:29 GMT\r\n',
            b'Expires: Fri, 21 Jun 2013 14:07:29 GMT\r\n',
            b'Cache-Control: public, max-age=2592000\r\n', b'Server: gws\r\n',
            b'Content-Length: 219\r\n', b'X-XSS-Protection: 1; mode=block\r\n',
            b'X-Frame-Options: SAMEORIGIN\r\n\r\n',
            b'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n'
            + b'<TITLE>301 Moved</TITLE></HEAD>',
            b'<BODY>\n<H1>301 Moved</H1>\nThe document has moved\n' +
            b'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
        ]))
        self.assertEqual(self.parser.code, b'301')
        self.assertEqual(self.parser.reason, b'Moved Permanently')
        self.assertEqual(self.parser.version, b'HTTP/1.1')
        self.assertEqual(
            self.parser.body,
            b'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n'
            +
            b'<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n'
            +
            b'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
        )
        self.assertEqual(self.parser.headers[b'content-length'],
                         (b'Content-Length', b'219'))
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_response_partial_parse(self) -> None:
        self.parser.type = httpParserTypes.RESPONSE_PARSER
        self.parser.parse(b''.join([
            b'HTTP/1.1 301 Moved Permanently\r\n',
            b'Location: http://www.google.com/\r\n',
            b'Content-Type: text/html; charset=UTF-8\r\n',
            b'Date: Wed, 22 May 2013 14:07:29 GMT\r\n',
            b'Expires: Fri, 21 Jun 2013 14:07:29 GMT\r\n',
            b'Cache-Control: public, max-age=2592000\r\n', b'Server: gws\r\n',
            b'Content-Length: 219\r\n', b'X-XSS-Protection: 1; mode=block\r\n',
            b'X-Frame-Options: SAMEORIGIN\r\n'
        ]))
        self.assertEqual(self.parser.headers[b'x-frame-options'],
                         (b'X-Frame-Options', b'SAMEORIGIN'))
        self.assertEqual(self.parser.state, httpParserStates.RCVING_HEADERS)
        self.parser.parse(b'\r\n')
        self.assertEqual(self.parser.state, httpParserStates.HEADERS_COMPLETE)
        self.parser.parse(
            b'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n'
            + b'<TITLE>301 Moved</TITLE></HEAD>')
        self.assertEqual(self.parser.state, httpParserStates.RCVING_BODY)
        self.parser.parse(
            b'<BODY>\n<H1>301 Moved</H1>\nThe document has moved\n' +
            b'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
        )
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_chunked_response_parse(self) -> None:
        self.parser.type = httpParserTypes.RESPONSE_PARSER
        self.parser.parse(b''.join([
            b'HTTP/1.1 200 OK\r\n', b'Content-Type: application/json\r\n',
            b'Date: Wed, 22 May 2013 15:08:15 GMT\r\n',
            b'Server: gunicorn/0.16.1\r\n', b'transfer-encoding: chunked\r\n',
            b'Connection: keep-alive\r\n\r\n', b'4\r\n', b'Wiki\r\n', b'5\r\n',
            b'pedia\r\n', b'E\r\n', b' in\r\n\r\nchunks.\r\n', b'0\r\n',
            b'\r\n'
        ]))
        self.assertEqual(self.parser.body, b'Wikipedia in\r\n\r\nchunks.')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_pipelined_response_parse(self) -> None:
        response = build_http_response(
            httpStatusCodes.OK,
            reason=b'OK',
            headers={b'Content-Length': b'15'},
            body=b'{"key":"value"}',
        )
        self.assert_pipeline_response(response)

    def test_pipelined_chunked_response_parse(self) -> None:
        response = build_http_response(
            httpStatusCodes.OK,
            reason=b'OK',
            headers={
                b'Transfer-Encoding': b'chunked',
                b'Content-Type': b'application/json',
            },
            body=b'f\r\n{"key":"value"}\r\n0\r\n\r\n')
        self.assert_pipeline_response(response)

    def assert_pipeline_response(self, response: bytes) -> None:
        self.parser = HttpParser(httpParserTypes.RESPONSE_PARSER)
        self.parser.parse(response + response)
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)
        self.assertEqual(self.parser.body, b'{"key":"value"}')
        self.assertEqual(self.parser.buffer, response)

        # parse buffer
        parser = HttpParser(httpParserTypes.RESPONSE_PARSER)
        parser.parse(self.parser.buffer)
        self.assertEqual(parser.state, httpParserStates.COMPLETE)
        self.assertEqual(parser.body, b'{"key":"value"}')
        self.assertEqual(parser.buffer, b'')

    def test_chunked_request_parse(self) -> None:
        self.parser.parse(
            build_http_request(httpMethods.POST,
                               b'http://example.org/',
                               headers={
                                   b'Transfer-Encoding': b'chunked',
                                   b'Content-Type': b'application/json',
                               },
                               body=b'f\r\n{"key":"value"}\r\n0\r\n\r\n'))
        self.assertEqual(self.parser.body, b'{"key":"value"}')
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)
        self.assertEqual(
            self.parser.build(),
            build_http_request(httpMethods.POST,
                               b'/',
                               headers={
                                   b'Transfer-Encoding': b'chunked',
                                   b'Content-Type': b'application/json',
                               },
                               body=b'f\r\n{"key":"value"}\r\n0\r\n\r\n'))

    def test_is_http_1_1_keep_alive(self) -> None:
        self.parser.parse(build_http_request(httpMethods.GET, b'/'))
        self.assertTrue(self.parser.is_http_1_1_keep_alive())

    def test_is_http_1_1_keep_alive_with_non_close_connection_header(
            self) -> None:
        self.parser.parse(
            build_http_request(httpMethods.GET,
                               b'/',
                               headers={
                                   b'Connection': b'keep-alive',
                               }))
        self.assertTrue(self.parser.is_http_1_1_keep_alive())

    def test_is_not_http_1_1_keep_alive_with_close_header(self) -> None:
        self.parser.parse(
            build_http_request(httpMethods.GET,
                               b'/',
                               headers={
                                   b'Connection': b'close',
                               }))
        self.assertFalse(self.parser.is_http_1_1_keep_alive())

    def test_is_not_http_1_1_keep_alive_for_http_1_0(self) -> None:
        self.parser.parse(
            build_http_request(
                httpMethods.GET,
                b'/',
                protocol_version=b'HTTP/1.0',
            ))
        self.assertFalse(self.parser.is_http_1_1_keep_alive())

    def test_paramiko_doc(self) -> None:
        response = b'HTTP/1.1 304 Not Modified\r\nDate: Tue, 03 Dec 2019 02:31:55 GMT\r\nConnection: keep-alive' \
                   b'\r\nLast-Modified: Sun, 23 Jun 2019 22:58:21 GMT\r\nETag: "5d10040d-1af2c"' \
                   b'\r\nX-Cname-TryFiles: True\r\nX-Served: Nginx\r\nX-Deity: web02\r\nCF-Cache-Status: DYNAMIC' \
                   b'\r\nServer: cloudflare\r\nCF-RAY: 53f2208c6fef6c38-SJC\r\n\r\n'
        self.parser = HttpParser(httpParserTypes.RESPONSE_PARSER)
        self.parser.parse(response)
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)
class TestHttpParser(unittest.TestCase):
    def setUp(self) -> None:
        self.parser = HttpParser(httpParserTypes.REQUEST_PARSER)

    def test_issue_127(self) -> None:
        with self.assertRaises(HttpProtocolException):
            self.parser.parse(CRLF)

        with self.assertRaises(HttpProtocolException):
            raw = b'qwqrqw!@!#@!#ad adfad\r\n'
            while True:
                self.parser.parse(raw)

    def test_issue_398(self) -> None:
        p = HttpParser(httpParserTypes.RESPONSE_PARSER)
        p.parse(HTTP_1_0 + b' 200 OK' + CRLF)
        self.assertEqual(p.version, HTTP_1_0)
        self.assertEqual(p.code, b'200')
        self.assertEqual(p.reason, b'OK')
        self.assertEqual(p.state, httpParserStates.LINE_RCVD)
        p.parse(
            b'CP=CAO PSA OUR' + CRLF + b'Cache-Control:private,max-age=0;' +
            CRLF + b'X-Frame-Options:SAMEORIGIN' + CRLF +
            b'X-Content-Type-Options:nosniff' + CRLF +
            b'X-XSS-Protection:1; mode=block' + CRLF +
            b'Content-Security-Policy:default-src \'self\' \'unsafe-inline\' \'unsafe-eval\''
            + CRLF +
            b'Strict-Transport-Security:max-age=2592000; includeSubdomains' +
            CRLF + b'Set-Cookie: lang=eng; path=/;HttpOnly;' + CRLF +
            b'Content-type:text/html;charset=UTF-8;' + CRLF + CRLF +
            b'<!-- HTML RESPONSE HERE -->', )
        self.assertEqual(p.body, b'<!-- HTML RESPONSE HERE -->')
        self.assertEqual(p.state, httpParserStates.RCVING_BODY)

    def test_urlparse(self) -> None:
        self.parser.parse(b'CONNECT httpbin.org:443 HTTP/1.1\r\n')
        self.assertTrue(self.parser.is_https_tunnel)
        self.assertFalse(self.parser.is_connection_upgrade)
        self.assertTrue(self.parser.is_http_1_1_keep_alive)
        self.assertFalse(self.parser.content_expected)
        self.assertFalse(self.parser.body_expected)
        self.assertEqual(self.parser.host, b'httpbin.org')
        self.assertEqual(self.parser.port, 443)
        self.assertNotEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_urlparse_on_invalid_connect_request(self) -> None:
        self.parser.parse(b'CONNECT / HTTP/1.0\r\n\r\n')
        self.assertTrue(self.parser.is_https_tunnel)
        self.assertEqual(self.parser.host, None)
        self.assertEqual(self.parser.port, 443)
        self.assertEqual(self.parser.state, httpParserStates.COMPLETE)

    def test_unicode_character_domain_connect(self) -> None:
        self.parser.parse(bytes_('CONNECT ççç.org:443 HTTP/1.1\r\n'))
        self.assertTrue(self.parser.is_https_tunnel)
        self.assertEqual(self.parser.host, bytes_('ççç.org'))
        self.assertEqual(self.parser.port, 443)

    def test_invalid_ipv6_in_request_line(self) -> None:
        self.parser.parse(
            bytes_(
                'CONNECT 2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF:443 HTTP/1.1\r\n'
            ), )
        self.assertTrue(self.parser.is_https_tunnel)
        self.assertEqual(
            self.parser.host,
            bytes_('[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]', ),
        )
        self.assertEqual(self.parser.port, 443)

    def test_valid_ipv6_in_request_line(self) -> None:
        self.parser.parse(
            bytes_(
                'CONNECT [2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:443 HTTP/1.1\r\n',
            ), )
        self.assertTrue(self.parser.is_https_tunnel)
        self.assertEqual(
            self.parser.host,
            bytes_('[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]', ),
        )
        self.assertEqual(self.parser.port, 443)

    def test_build_request(self) -> None:
        self.assertEqual(
            build_http_request(
                b'GET',
                b'http://*****:*****@nm>;tag=root\r\nTo: <sip:nm2@nm2>\r\n' +
                b'Call-ID: 50000\r\n' + b'CSeq: 42 OPTIONS\r\n' +
                b'Max-Forwards: 70\r\n' + b'Content-Length: 0\r\n' +
                b'Contact: <sip:nm@nm>\r\n' + b'Accept: application/sdp\r\n' +
                b'\r\n', )