Exemple #1
0
    def values(self, settings):
        if self.rendered_values:
            return self.rendered_values
        else:
            path = self.path.string()
            if self.nested_response:
                path += self.nested_response.parsed.spec().encode()

            headers = Headers(
                [header.values(settings) for header in self.headers])

            body = self.body
            if body:
                body = body.string()

            req = http.Request(
                b'',
                self.method.string(),
                b'',
                b'',
                b'',
                path,
                (2, 0),
                headers,
                body,
            )
            req.stream_id = self.stream_id

            self.rendered_values = settings.protocol.assemble(req)
            return self.rendered_values
Exemple #2
0
    def _setup_connection(self):
        client = netlib.tcp.TCPClient(("127.0.0.1", self.proxy.port))
        client.connect()

        request = http.Request("authority",
                               "CONNECT",
                               "",
                               "localhost",
                               self.server.server.address.port,
                               "",
                               "HTTP/1.1",
                               content=b'')
        client.wfile.write(http.http1.assemble_request(request))
        client.wfile.flush()

        response = http.http1.read_response(client.rfile, request)

        if self.ssl:
            client.convert_to_ssl()
            assert client.ssl_established

        request = http.Request("relative",
                               "GET",
                               "http",
                               "localhost",
                               self.server.server.address.port,
                               "/ws",
                               "HTTP/1.1",
                               headers=http.Headers(
                                   connection="upgrade",
                                   upgrade="websocket",
                                   sec_websocket_version="13",
                                   sec_websocket_key="1234",
                               ),
                               content=b'')
        client.wfile.write(http.http1.assemble_request(request))
        client.wfile.flush()

        response = http.http1.read_response(client.rfile, request)
        assert websockets.check_handshake(response.headers)

        return client
 def test_request_simple(self):
     bytes = HTTP2Protocol(self.c).assemble_request(http.Request(
         b'',
         b'GET',
         b'https',
         b'',
         b'',
         b'/',
         b"HTTP/2.0",
         (),
         None,
     ))
     assert len(bytes) == 1
     assert bytes[0] == codecs.decode('00000d0105000000018284874188089d5c0b8170dc07', 'hex_codec')
 def test_request_simple(self):
     bytes = HTTP2Protocol(self.c).assemble_request(http.Request(
         '',
         'GET',
         'https',
         '',
         '',
         '/',
         (2, 0),
         None,
         None,
     ))
     assert len(bytes) == 1
     assert bytes[0] == '00000d0105000000018284874188089d5c0b8170dc07'.decode('hex')
 def test_request_with_stream_id(self):
     req = http.Request(
         '',
         'GET',
         'https',
         '',
         '',
         '/',
         (2, 0),
         None,
         None,
     )
     req.stream_id = 0x42
     bytes = HTTP2Protocol(self.c).assemble_request(req)
     assert len(bytes) == 1
     assert bytes[0] == '00000d0105000000428284874188089d5c0b8170dc07'.decode('hex')
 def test_request_with_stream_id(self):
     req = http.Request(
         b'',
         b'GET',
         b'https',
         b'',
         b'',
         b'/',
         b"HTTP/2.0",
         (),
         None,
     )
     req.stream_id = 0x42
     bytes = HTTP2Protocol(self.c).assemble_request(req)
     assert len(bytes) == 1
     assert bytes[0] == codecs.decode('00000d0105000000428284874188089d5c0b8170dc07', 'hex_codec')
Exemple #7
0
def treq(**kwargs):
    """
    Returns:
        netlib.http.Request
    """
    default = dict(first_line_format="relative",
                   method=b"GET",
                   scheme=b"http",
                   host=b"address",
                   port=22,
                   path=b"/path",
                   http_version=b"HTTP/1.1",
                   headers=http.Headers(
                       ((b"header", b"qvalue"), (b"content-length", b"7"))),
                   content=b"content")
    default.update(kwargs)
    return http.Request(**default)
 def test_request_with_body(self):
     bytes = HTTP2Protocol(self.c).assemble_request(http.Request(
         '',
         'GET',
         'https',
         '',
         '',
         '/',
         (2, 0),
         odict.ODictCaseless([('foo', 'bar')]),
         'foobar',
     ))
     assert len(bytes) == 2
     assert bytes[0] ==\
         '0000150104000000018284874188089d5c0b8170dc07408294e7838c767f'.decode('hex')
     assert bytes[1] ==\
         '000006000100000001666f6f626172'.decode('hex')
 def test_request_with_body(self):
     bytes = HTTP2Protocol(self.c).assemble_request(http.Request(
         b'',
         b'GET',
         b'https',
         b'',
         b'',
         b'/',
         b"HTTP/2.0",
         http.Headers([(b'foo', b'bar')]),
         b'foobar',
     ))
     assert len(bytes) == 2
     assert bytes[0] ==\
         codecs.decode('0000150104000000018284874188089d5c0b8170dc07408294e7838c767f', 'hex_codec')
     assert bytes[1] ==\
         codecs.decode('000006000100000001666f6f626172', 'hex_codec')
Exemple #10
0
def treq(content="content", scheme="http", host="address", port=22):
    """
    @return: libmproxy.protocol.http.HTTPRequest
    """
    headers = odict.ODictCaseless()
    headers["header"] = ["qvalue"]
    req = http.Request(
        "relative",
        "GET",
        scheme,
        host,
        port,
        "/path",
        (1, 1),
        headers,
        content,
        None,
        None,
    )
    return req
Exemple #11
0
    def read_request(
        self,
        include_body=True,
        body_size_limit=None,
        allow_empty=False,
    ):
        """
        Parse an HTTP request from a file stream

        Args:
            include_body (bool): Read response body as well
            body_size_limit (bool): Maximum body size
            wfile (file): If specified, HTTP Expect headers are handled
            automatically, by writing a HTTP 100 CONTINUE response to the stream.

        Returns:
            Request: The HTTP request

        Raises:
            HttpError: If the input is invalid.
        """
        timestamp_start = time.time()
        if hasattr(self.tcp_handler.rfile, "reset_timestamps"):
            self.tcp_handler.rfile.reset_timestamps()

        httpversion, host, port, scheme, method, path, headers, body = (
            None, None, None, None, None, None, None, None)

        request_line = self._get_request_line()
        if not request_line:
            if allow_empty:
                return http.EmptyRequest()
            else:
                raise tcp.NetLibDisconnect()

        request_line_parts = self._parse_init(request_line)
        if not request_line_parts:
            raise HttpError(400,
                            "Bad HTTP request line: %s" % repr(request_line))
        method, path, httpversion = request_line_parts

        if path == '*' or path.startswith("/"):
            form_in = "relative"
            if not utils.isascii(path):
                raise HttpError(
                    400, "Bad HTTP request line: %s" % repr(request_line))
        elif method == 'CONNECT':
            form_in = "authority"
            r = self._parse_init_connect(request_line)
            if not r:
                raise HttpError(
                    400, "Bad HTTP request line: %s" % repr(request_line))
            host, port, httpversion = r
            path = None
        else:
            form_in = "absolute"
            r = self._parse_init_proxy(request_line)
            if not r:
                raise HttpError(
                    400, "Bad HTTP request line: %s" % repr(request_line))
            _, scheme, host, port, path, _ = r

        headers = self.read_headers()
        if headers is None:
            raise HttpError(400, "Invalid headers")

        expect_header = headers.get_first("expect", "").lower()
        if expect_header == "100-continue" and httpversion == (1, 1):
            self.tcp_handler.wfile.write('HTTP/1.1 100 Continue\r\n' '\r\n')
            self.tcp_handler.wfile.flush()
            del headers['expect']

        if include_body:
            body = self.read_http_body(headers, body_size_limit, method, None,
                                       True)

        if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):
            # more accurate timestamp_start
            timestamp_start = self.tcp_handler.rfile.first_byte_timestamp

        timestamp_end = time.time()

        return http.Request(
            form_in,
            method,
            scheme,
            host,
            port,
            path,
            httpversion,
            headers,
            body,
            timestamp_start,
            timestamp_end,
        )
Exemple #12
0
    def read_request(
        self,
        include_body=True,
        body_size_limit=None,
        allow_empty=False,
    ):
        if body_size_limit is not None:
            raise NotImplementedError()

        self.perform_connection_preface()

        timestamp_start = time.time()
        if hasattr(self.tcp_handler.rfile, "reset_timestamps"):
            self.tcp_handler.rfile.reset_timestamps()

        stream_id, headers, body = self._receive_transmission(
            include_body=include_body, )

        if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):
            # more accurate timestamp_start
            timestamp_start = self.tcp_handler.rfile.first_byte_timestamp

        timestamp_end = time.time()

        authority = headers.get(':authority', '')
        method = headers.get(':method', 'GET')
        scheme = headers.get(':scheme', 'https')
        path = headers.get(':path', '/')
        host = None
        port = None

        if path == '*' or path.startswith("/"):
            form_in = "relative"
        elif method == 'CONNECT':
            form_in = "authority"
            if ":" in authority:
                host, port = authority.split(":", 1)
            else:
                host = authority
        else:
            form_in = "absolute"
            # FIXME: verify if path or :host contains what we need
            scheme, host, port, _ = utils.parse_url(path)

        if host is None:
            host = 'localhost'
        if port is None:
            port = 80 if scheme == 'http' else 443
        port = int(port)

        request = http.Request(
            form_in,
            method,
            scheme,
            host,
            port,
            path,
            (2, 0),
            headers,
            body,
            timestamp_start,
            timestamp_end,
        )
        # FIXME: We should not do this.
        request.stream_id = stream_id

        return request