def handle(self): try: request = http.http1.read_request(self.rfile) assert websocket_utils.check_handshake(request.headers) response = http.Response( http_version=b"HTTP/1.1", status_code=101, reason=http.status_codes.RESPONSES.get(101).encode(), headers=http.Headers( connection='upgrade', upgrade='websocket', sec_websocket_accept=b'', sec_websocket_extensions='permessage-deflate' if "permessage-deflate" in request.headers.values() else '' ), content=b'', trailers=None, timestamp_start=0, timestamp_end=0, ) self.wfile.write(http.http1.assemble_response(response)) self.wfile.flush() self.server.handle_websockets(self.rfile, self.wfile) except: traceback.print_exc()
def test_simple(self): bytes = HTTP2StateProtocol(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')
def test_with_body(self): bytes = HTTP2StateProtocol(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')
def test_with_stream_id(self): resp = http.Response( b"HTTP/2.0", 200, ) resp.stream_id = 0x42 bytes = HTTP2StateProtocol(self.c, is_server=True).assemble_response(resp) assert len(bytes) == 1 assert bytes[0] ==\ codecs.decode('00000101050000004288', 'hex_codec')
def make_connect_response(http_version): # Do not send any response headers as it breaks proxying non-80 ports on # Android emulators using the -http-proxy option. return http.Response( http_version, 200, b"Connection established", http.Headers(), b"", None, time.time(), time.time(), )
def test_simple(self): data = HTTP2StateProtocol(self.c, is_server=True).assemble_response(http.Response( http_version=b"HTTP/2.0", status_code=200, reason=b"", headers=(), content=b"", trailers=None, timestamp_start=0, timestamp_end=0, )) assert len(data) == 1 assert data[0] == bytes.fromhex("00000101050000000288")
def test_with_body(self): data = HTTP2StateProtocol(self.c, is_server=True).assemble_response(http.Response( http_version=b"HTTP/2.0", status_code=200, reason=b'', headers=http.Headers(foo=b"bar"), content=b'foobar', trailers=None, timestamp_start=0, timestamp_end=0, )) assert len(data) == 2 assert data[0] == bytes.fromhex("00000901040000000288408294e7838c767f") assert data[1] == bytes.fromhex("000006000100000002666f6f626172")
def tresp(**kwargs): """ Returns: mitmproxy.net.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)
def tresp(**kwargs) -> http.Response: """ Returns: mitmproxy.net.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", trailers=None, timestamp_start=946681202, timestamp_end=946681203, ) default.update(kwargs) return http.Response(**default) # type: ignore
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()
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', int(self.status_code.string()), b'', headers, body, ) resp.stream_id = self.stream_id self.rendered_values = settings.protocol.assemble(resp) return self.rendered_values
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(http_version=b'HTTP/2.0', status_code=int(self.status_code.string()), reason=b'', headers=headers, content=body, trailers=None, timestamp_start=0, timestamp_end=0) resp.stream_id = self.stream_id self.rendered_values = settings.protocol.assemble(resp) return self.rendered_values
def test_response(): r = http.Response(b"HTTP/1.1", 200, b"Message", {}, None, None) assert repr(r)