Exemplo n.º 1
0
 def test_simple_message(self):
     data = """
         HTTP/1.1 200 OK
     """
     assert self.tst(data, "GET", None) == http.Response(
         (1, 1), 200, 'OK', Headers(), ''
     )
Exemplo n.º 2
0
 def test_simple(self):
     data = """
         HTTP/1.1 200
     """
     assert self.tst(data, "GET", None) == http.Response(
         (1, 1), 200, '', odict.ODictCaseless(), ''
     )
Exemplo n.º 3
0
 def test_with_body(self):
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(
         http.Response((2, 0), 200, '', Headers(foo="bar"), 'foobar'))
     assert len(bytes) == 2
     assert bytes[0] ==\
         '00000901040000000288408294e7838c767f'.decode('hex')
     assert bytes[1] ==\
         '000006000100000002666f6f626172'.decode('hex')
Exemplo n.º 4
0
 def test_simple(self):
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(http.Response(
         (2, 0),
         200,
     ))
     assert len(bytes) == 1
     assert bytes[0] ==\
         '00000101050000000288'.decode('hex')
 def test_simple(self):
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(http.Response(
         b"HTTP/2.0",
         200,
     ))
     assert len(bytes) == 1
     assert bytes[0] ==\
         codecs.decode('00000101050000000288', 'hex_codec')
Exemplo n.º 6
0
    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(), ''
        )
Exemplo n.º 7
0
 def test_with_body(self):
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(
         http.Response(b"HTTP/2.0", 200, b'', http.Headers(foo=b"bar"),
                       b'foobar'))
     assert len(bytes) == 2
     assert bytes[0] ==\
         codecs.decode('00000901040000000288408294e7838c767f', 'hex_codec')
     assert bytes[1] ==\
         codecs.decode('000006000100000002666f6f626172', 'hex_codec')
Exemplo n.º 8
0
 def test_with_stream_id(self):
     resp = http.Response(
         b"HTTP/2.0",
         200,
     )
     resp.stream_id = 0x42
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(resp)
     assert len(bytes) == 1
     assert bytes[0] ==\
         codecs.decode('00000101050000004288', 'hex_codec')
Exemplo n.º 9
0
def tresp(**kwargs):
    """
    Returns:
        netlib.http.Response
    """
    default = dict(
        http_version=b"HTTP/1.1",
        status_code=200,
        reason=b"OK",
        headers=http.Headers(((b"header-response", b"svalue"), (b"content-length", b"7"))),
        content=b"message",
        timestamp_start=time.time(),
        timestamp_end=time.time(),
    )
    default.update(kwargs)
    return http.Response(**default)
Exemplo n.º 10
0
    def read_response(
        self,
        request_method='',
        body_size_limit=None,
        include_body=True,
        stream_id=None,
    ):
        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(
            stream_id=stream_id,
            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

        if include_body:
            timestamp_end = time.time()
        else:
            timestamp_end = None

        response = http.Response(
            (2, 0),
            int(headers.get(':status', 502)),
            "",
            headers,
            body,
            timestamp_start=timestamp_start,
            timestamp_end=timestamp_end,
        )
        response.stream_id = stream_id

        return response
Exemplo n.º 11
0
    def values(self, settings):
        if self.rendered_values:
            return self.rendered_values
        else:
            headers = Headers([header.values(settings) for header in self.headers])

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

            resp = http.Response(
                b'HTTP/2.0',
                self.status_code.string(),
                b'',
                headers,
                body,
            )
            resp.stream_id = self.stream_id

            self.rendered_values = settings.protocol.assemble(resp)
            return self.rendered_values
Exemplo n.º 12
0
        def handle(self):
            try:
                request = http.http1.read_request(self.rfile)
                assert websockets.check_handshake(request.headers)

                response = http.Response(
                    "HTTP/1.1",
                    101,
                    reason=http.status_codes.RESPONSES.get(101),
                    headers=http.Headers(
                        connection='upgrade',
                        upgrade='websocket',
                        sec_websocket_accept=b'',
                    ),
                    content=b'',
                )
                self.wfile.write(http.http1.assemble_response(response))
                self.wfile.flush()

                self.server.handle_websockets(self.rfile, self.wfile)
            except:
                traceback.print_exc()
Exemplo n.º 13
0
def test_response():
    r = http.Response("HTTP/1.1", 200, "Message", {}, None, None)
    assert repr(r)
Exemplo n.º 14
0
    def read_response(
        self,
        request_method,
        body_size_limit=None,
        include_body=True,
    ):
        """
            Returns an http.Response

            By default, both response header and body are read.
            If include_body=False is specified, body may be one of the
            following:
            - None, if the response is technically allowed to have a response body
            - "", if the response must not have a response body (e.g. it's a
            response to a HEAD request)
        """
        timestamp_start = time.time()
        if hasattr(self.tcp_handler.rfile, "reset_timestamps"):
            self.tcp_handler.rfile.reset_timestamps()

        line = self.tcp_handler.rfile.readline()
        # Possible leftover from previous message
        if line == "\r\n" or line == "\n":
            line = self.tcp_handler.rfile.readline()
        if not line:
            raise HttpErrorConnClosed(502, "Server disconnect.")
        parts = self.parse_response_line(line)
        if not parts:
            raise HttpError(502, "Invalid server response: %s" % repr(line))
        proto, code, msg = parts
        httpversion = self._parse_http_protocol(proto)
        if httpversion is None:
            raise HttpError(502,
                            "Invalid HTTP version in line: %s" % repr(proto))
        headers = self.read_headers()
        if headers is None:
            raise HttpError(502, "Invalid headers.")

        if include_body:
            body = self.read_http_body(headers, body_size_limit,
                                       request_method, code, False)
        else:
            # if include_body==False then a None body means the body should be
            # read separately
            body = None

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

        if include_body:
            timestamp_end = time.time()
        else:
            timestamp_end = None

        return http.Response(
            httpversion,
            code,
            msg,
            headers,
            body,
            timestamp_start=timestamp_start,
            timestamp_end=timestamp_end,
        )