def test_cancel_during_response_hook(tctx): """ Test that we properly handle the case of the following event sequence: - we receive a server response - we trigger the response hook - the client cancels the stream - the response hook completes Given that we have already triggered the response hook, we don't want to trigger the error hook. """ playbook, cff = start_h2_client(tctx) flow = Placeholder(HTTPFlow) server = Placeholder(Server) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=[ "END_STREAM" ]).serialize()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None) << SendData( server, b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n') >> DataReceived(server, b"HTTP/1.1 204 No Content\r\n\r\n") << http.HttpResponseHeadersHook(flow) << CloseConnection(server) >> reply(to=-2) << http.HttpResponseHook(flow) >> DataReceived( tctx.client, cff.build_rst_stream_frame( 1, ErrorCodes.CANCEL).serialize()) >> reply(to=-2))
def test_https_proxy(strategy, tctx): """Test a CONNECT request, followed by a HTTP GET /""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular)) tctx.options.connection_strategy = strategy (playbook >> DataReceived(tctx.client, b"CONNECT example.proxy:80 HTTP/1.1\r\n\r\n") << http.HttpConnectHook(Placeholder()) >> reply()) if strategy == "eager": (playbook << OpenConnection(server) >> reply(None)) (playbook << SendData(tctx.client, b'HTTP/1.1 200 Connection established\r\n\r\n') >> DataReceived(tctx.client, b"GET /foo?hello=1 HTTP/1.1\r\nHost: example.com\r\n\r\n") << layer.NextLayerHook(Placeholder()) >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply()) if strategy == "lazy": (playbook << OpenConnection(server) >> reply(None)) (playbook << SendData( server, b"GET /foo?hello=1 HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived( server, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!") << http.HttpResponseHeadersHook(flow) >> reply() << http.HttpResponseHook(flow) >> reply() << SendData( tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!")) assert playbook
def test_rst_then_close(tctx): """ Test that we properly handle the case of a client that first causes protocol errors and then disconnects. Adapted from h2spec http2/5.1/5. """ playbook, cff = start_h2_client(tctx) flow = Placeholder(HTTPFlow) server = Placeholder(Server) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=[ "END_STREAM" ]).serialize()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> DataReceived( tctx.client, cff.build_data_frame(b"unexpected data frame").serialize()) << SendData( tctx.client, cff.build_rst_stream_frame( 1, ErrorCodes.STREAM_CLOSED).serialize()) >> ConnectionClosed(tctx.client) << CloseConnection( tctx.client) >> reply("connection cancelled", to=-5) << http.HttpErrorHook(flow) >> reply()) assert flow().error.msg == "connection cancelled"
def test_server_unreachable(tctx, connect): """Test the scenario where the target server is unreachable.""" tctx.options.connection_strategy = "eager" server = Placeholder(Server) flow = Placeholder(HTTPFlow) err = Placeholder(bytes) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) if connect: playbook >> DataReceived(tctx.client, b"CONNECT example.com:443 HTTP/1.1\r\n\r\n") else: playbook >> DataReceived(tctx.client, b"GET http://example.com/ HTTP/1.1\r\n\r\n") playbook << OpenConnection(server) playbook >> reply("Connection failed") if not connect: # Our API isn't ideal here, there is no error hook for CONNECT requests currently. # We could fix this either by having CONNECT request go through all our regular hooks, # or by adding dedicated ok/error hooks. playbook << http.HttpErrorHook(flow) playbook >> reply() playbook << SendData(tctx.client, err) if not connect: playbook << CloseConnection(tctx.client) assert playbook if not connect: assert flow().error assert b"502 Bad Gateway" in err() assert b"Connection failed" in err()
def test_proxy_chain(tctx, strategy): server = Placeholder(Server) tctx.options.connection_strategy = strategy playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) playbook >> DataReceived(tctx.client, b"CONNECT proxy:8080 HTTP/1.1\r\n\r\n") if strategy == "eager": playbook << OpenConnection(server) playbook >> reply(None) playbook << SendData(tctx.client, b"HTTP/1.1 200 Connection established\r\n\r\n") playbook >> DataReceived(tctx.client, b"CONNECT second-proxy:8080 HTTP/1.1\r\n\r\n") playbook << layer.NextLayerHook(Placeholder()) playbook >> reply_next_layer( lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) playbook << SendData( tctx.client, b"HTTP/1.1 502 Bad Gateway\r\n" b"content-length: 198\r\n" b"\r\n" b"mitmproxy received an HTTP CONNECT request even though it is not running in regular/upstream mode. " b"This usually indicates a misconfiguration, please see the mitmproxy mode documentation for details." ) assert playbook
def test_cancel_then_server_disconnect(tctx): """ Test that we properly handle the case of the following event sequence: - client cancels a stream - we start an error hook - server disconnects - error hook completes. """ playbook, cff = start_h2_client(tctx) flow = Placeholder(HTTPFlow) server = Placeholder(Server) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=[ "END_STREAM" ]).serialize()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None) << SendData( server, b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n') >> DataReceived( tctx.client, cff.build_rst_stream_frame(1, ErrorCodes.CANCEL).serialize()) << CloseConnection(server) << http.HttpErrorHook(flow) >> reply() >> ConnectionClosed(server) << None)
def test_h2_to_h1(tctx): """Test HTTP/2 -> HTTP/1 request translation""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) conn, playbook = h2_client(tctx) conn.send_headers(1, example_request_headers, end_stream=True) response = Placeholder(bytes) assert ( playbook >> DataReceived(tctx.client, conn.data_to_send()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\n") << http.HttpResponseHeadersHook(flow) >> reply() >> DataReceived( server, b"Hello World!") << http.HttpResponseHook(flow) << CloseConnection(server) >> reply(to=-2) << SendData( tctx.client, response)) events = conn.receive_data(response()) assert event_types(events) == [ h2.events.ResponseReceived, h2.events.DataReceived, h2.events.DataReceived, h2.events.StreamEnded ] resp: h2.events.ResponseReceived = events[0] body: h2.events.DataReceived = events[1] assert resp.headers == [(b':status', b'200'), (b'content-length', b'12')] assert body.data == b"Hello World!"
def test_disconnect_while_intercept(tctx): """Test a server disconnect while a request is intercepted.""" tctx.options.connection_strategy = "eager" server1 = Placeholder(Server) server2 = Placeholder(Server) flow = Placeholder(HTTPFlow) assert ( Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) >> DataReceived(tctx.client, b"CONNECT example.com:80 HTTP/1.1\r\n\r\n") << http.HttpConnectHook(Placeholder(HTTPFlow)) >> reply() << OpenConnection(server1) >> reply(None) << SendData( tctx.client, b'HTTP/1.1 200 Connection established\r\n\r\n') >> DataReceived( tctx.client, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") << layer.NextLayerHook(Placeholder()) >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) << http.HttpRequestHook(flow) >> ConnectionClosed(server1) << CloseConnection(server1) >> reply(to=-3) << OpenConnection(server2) >> reply(None) << SendData( server2, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived( server2, b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << SendData(tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")) assert server1() != server2() assert flow().server_conn == server2()
def test_pipelining(tctx, transfer_encoding): """Test that multiple requests can be processed over the same connection""" tctx.server.address = ("example.com", 80) tctx.server.state = ConnectionState.OPEN req = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" if transfer_encoding == "identity": resp = (b"HTTP/1.1 200 OK\r\n" b"Content-Length: 12\r\n" b"\r\n" b"Hello World!") else: resp = (b"HTTP/1.1 200 OK\r\n" b"Transfer-Encoding: chunked\r\n" b"\r\n" b"c\r\n" b"Hello World!\r\n" b"0\r\n" b"\r\n") assert (Playbook(http.HttpLayer(tctx, HTTPMode.transparent), hooks=False) # Roundtrip 1 >> DataReceived(tctx.client, req) << SendData(tctx.server, req) >> DataReceived(tctx.server, resp) << SendData(tctx.client, resp) # Roundtrip 2 >> DataReceived(tctx.client, req) << SendData(tctx.server, req) >> DataReceived(tctx.server, resp) << SendData(tctx.client, resp))
def test_simple(tctx): playbook, cff = start_h2_client(tctx) flow = Placeholder(HTTPFlow) server = Placeholder(Server) initial = Placeholder(bytes) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=[ "END_STREAM" ]).serialize()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None, side_effect=make_h2) << SendData(server, initial)) frames = decode_frames(initial()) assert [type(x) for x in frames] == [ hyperframe.frame.SettingsFrame, hyperframe.frame.HeadersFrame, ] sff = FrameFactory() assert ( playbook # a conforming h2 server would send settings first, we disregard this for now. >> DataReceived( server, sff.build_headers_frame(example_response_headers).serialize()) << http.HttpResponseHeadersHook(flow) >> reply() >> DataReceived( server, sff.build_data_frame(b"Hello, World!", flags=["END_STREAM" ]).serialize()) << http.HttpResponseHook(flow) >> reply() << SendData( tctx.client, cff.build_headers_frame(example_response_headers).serialize() + cff.build_data_frame(b"Hello, World!").serialize() + cff.build_data_frame(b"", flags=["END_STREAM"]).serialize())) assert flow().request.url == "http://example.com/" assert flow().response.text == "Hello, World!"
def test_no_data_on_closed_stream(self, tctx): frame_factory = FrameFactory() req = Request.make("GET", "http://example.com/") resp = {":status": 200} assert ( Playbook(Http2Client(tctx)) << SendData( tctx.server, Placeholder(bytes)) # preamble + initial settings frame >> DataReceived( tctx.server, frame_factory.build_settings_frame({}, ack=True).serialize()) >> http.RequestHeaders(1, req, end_stream=True) << SendData( tctx.server, b"\x00\x00\x06\x01\x05\x00\x00\x00\x01\x82\x86\x84\\\x81\x07") >> http.RequestEndOfMessage(1) >> DataReceived( tctx.server, frame_factory.build_headers_frame(resp).serialize()) << http.ReceiveHttp(Placeholder( http.ResponseHeaders)) >> http.RequestProtocolError( 1, "cancelled", code=status_codes.CLIENT_CLOSED_REQUEST) << SendData( tctx.server, frame_factory.build_rst_stream_frame( 1, ErrorCodes.CANCEL).serialize()) >> DataReceived( tctx.server, frame_factory.build_data_frame(b"foo").serialize()) << SendData( tctx.server, frame_factory.build_rst_stream_frame( 1, ErrorCodes.STREAM_CLOSED).serialize()) ) # important: no ResponseData event here!
def test_h1_to_h2(tctx): """Test HTTP/1 -> HTTP/2 request translation""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular)) conf = h2.config.H2Configuration(client_side=False) conn = h2.connection.H2Connection(conf) conn.initiate_connection() request = Placeholder(bytes) assert (playbook >> DataReceived( tctx.client, b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n") << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None, side_effect=make_h2) << SendData(server, request)) events = conn.receive_data(request()) assert event_types(events) == [ h2.events.RemoteSettingsChanged, h2.events.RequestReceived, h2.events.StreamEnded ] conn.send_headers(1, example_response_headers) conn.send_data(1, b"Hello World!", end_stream=True) settings_ack = Placeholder(bytes) assert ( playbook >> DataReceived(server, conn.data_to_send()) << http.HttpResponseHeadersHook(flow) << SendData(server, settings_ack) >> reply(to=-2) << http.HttpResponseHook(flow) >> reply() << SendData( tctx.client, b"HTTP/1.1 200 OK\r\n\r\nHello World!") << CloseConnection(tctx.client)) assert settings_ack() == b'\x00\x00\x00\x04\x01\x00\x00\x00\x00'
def test_fragmented(ws_testdata): tctx, playbook = ws_testdata flow = Placeholder(WebSocketFlow) assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.server, b"\x01\x03foo") >> DataReceived( tctx.server, b"\x80\x03bar") << websocket.WebsocketMessageHook(flow) >> reply() << SendData( tctx.client, b"\x01\x03foo") << SendData( tctx.client, b"\x80\x03bar")) assert flow().messages[-1].content == "foobar"
def test_http_proxy_relative_request(tctx): """Test handling of a relative-form "GET /" in regular proxy mode.""" server = Placeholder(Server) assert (Playbook(http.HttpLayer( tctx, HTTPMode.regular), hooks=False) >> DataReceived( tctx.client, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server, b"HTTP/1.1 204 No Content\r\n\r\n") << SendData(tctx.client, b"HTTP/1.1 204 No Content\r\n\r\n")) assert server().address == ("example.com", 80)
def test_no_headers(tctx): """Test that we can correctly reassemble requests/responses with no headers.""" server = Placeholder(Server) assert ( Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) >> DataReceived(tctx.client, b"GET http://example.com/ HTTP/1.1\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\n\r\n") >> DataReceived( server, b"HTTP/1.1 204 No Content\r\n\r\n") << SendData( tctx.client, b"HTTP/1.1 204 No Content\r\n\r\n")) assert server().address == ("example.com", 80)
def start_h2_client(tctx: Context) -> Tuple[Playbook, FrameFactory]: tctx.client.alpn = b"h2" frame_factory = FrameFactory() playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular)) assert ( playbook << SendData(tctx.client, Placeholder()) # initial settings frame >> DataReceived(tctx.client, frame_factory.preamble()) >> DataReceived( tctx.client, frame_factory.build_settings_frame({}, ack=True).serialize())) return playbook, frame_factory
def test_receive_data_after_half_close(tctx): """ data received after the other connection has been half-closed should still be forwarded. """ assert (Playbook(tcp.TCPLayer(tctx), hooks=False) << OpenConnection(tctx.server) >> reply(None) >> DataReceived( tctx.client, b"eof-delimited-request") << SendData( tctx.server, b"eof-delimited-request") >> ConnectionClosed( tctx.client) << CloseConnection(tctx.server, half_close=True) >> DataReceived( tctx.server, b"i'm late") << SendData(tctx.client, b"i'm late") >> ConnectionClosed(tctx.server) << CloseConnection(tctx.client))
def test_response_until_eof(tctx): """Test scenario where the server response body is terminated by EOF.""" server = Placeholder(Server) assert (Playbook(http.HttpLayer( tctx, HTTPMode.regular), hooks=False) >> DataReceived( tctx.client, b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server, b"HTTP/1.1 200 OK\r\n\r\nfoo") >> ConnectionClosed(server) << CloseConnection(server) << SendData( tctx.client, b"HTTP/1.1 200 OK\r\n\r\nfoo") << CloseConnection( tctx.client))
def test_max_concurrency(tctx): playbook, cff = start_h2_client(tctx) server = Placeholder(Server) req1_bytes = Placeholder(bytes) settings_ack_bytes = Placeholder(bytes) req2_bytes = Placeholder(bytes) playbook.hooks = False sff = FrameFactory() assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=["END_STREAM"], stream_id=1).serialize()) << OpenConnection(server) >> reply(None, side_effect=make_h2) << SendData(server, req1_bytes) >> DataReceived( server, sff.build_settings_frame({ h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 1 }).serialize()) << SendData( server, settings_ack_bytes) >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=["END_STREAM"], stream_id=3).serialize()) # Can't send it upstream yet, all streams in use! >> DataReceived( server, sff.build_headers_frame(example_response_headers, flags=["END_STREAM"], stream_id=1).serialize()) # But now we can! << SendData(server, req2_bytes) << SendData(tctx.client, Placeholder(bytes)) >> DataReceived( server, sff.build_headers_frame(example_response_headers, flags=["END_STREAM"], stream_id=3).serialize()) << SendData( tctx.client, Placeholder(bytes))) settings, req1 = decode_frames(req1_bytes()) settings_ack, = decode_frames(settings_ack_bytes()) req2, = decode_frames(req2_bytes()) assert type(settings) == hyperframe.frame.SettingsFrame assert type(req1) == hyperframe.frame.HeadersFrame assert type(settings_ack) == hyperframe.frame.SettingsFrame assert type(req2) == hyperframe.frame.HeadersFrame assert req1.stream_id == 1 assert req2.stream_id == 3
def test_redirect(strategy, https_server, https_client, tctx, monkeypatch): """Test redirects between http:// and https:// in regular proxy mode.""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) tctx.options.connection_strategy = strategy p = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) if https_server: monkeypatch.setattr(tls, "ServerTLSLayer", tls.MockTLSLayer) def redirect(flow: HTTPFlow): if https_server: flow.request.url = "https://redirected.site/" else: flow.request.url = "http://redirected.site/" if https_client: p >> DataReceived(tctx.client, b"CONNECT example.com:80 HTTP/1.1\r\n\r\n") if strategy == "eager": p << OpenConnection(Placeholder()) p >> reply(None) p << SendData(tctx.client, b'HTTP/1.1 200 Connection established\r\n\r\n') p >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") p << layer.NextLayerHook(Placeholder()) p >> reply_next_layer( lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) else: p >> DataReceived( tctx.client, b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n") p << http.HttpRequestHook(flow) p >> reply(side_effect=redirect) p << OpenConnection(server) p >> reply(None) p << SendData(server, b"GET / HTTP/1.1\r\nHost: redirected.site\r\n\r\n") p >> DataReceived( server, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!") p << SendData( tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!") assert p if https_server: assert server().address == ("redirected.site", 443) else: assert server().address == ("redirected.site", 80)
def test_simple(tctx): """open connection, receive data, send it to peer""" f = Placeholder(TCPFlow) assert ( Playbook(tcp.TCPLayer(tctx)) << tcp.TcpStartHook(f) >> reply() << OpenConnection(tctx.server) >> reply(None) >> DataReceived( tctx.client, b"hello!") << tcp.TcpMessageHook(f) >> reply() << SendData(tctx.server, b"hello!") >> DataReceived( tctx.server, b"hi") << tcp.TcpMessageHook(f) >> reply() << SendData(tctx.client, b"hi") >> ConnectionClosed(tctx.server) << CloseConnection(tctx.client, half_close=True) >> ConnectionClosed( tctx.client) << CloseConnection(tctx.server) << tcp.TcpEndHook(f) >> reply() >> ConnectionClosed(tctx.client) << None) assert len(f().messages) == 2
def test_kill_stream(tctx): """Test that we can kill individual streams.""" playbook, cff = start_h2_client(tctx) flow1 = Placeholder(HTTPFlow) flow2 = Placeholder(HTTPFlow) req_headers_hook_1 = http.HttpRequestHeadersHook(flow1) def kill(flow: HTTPFlow): # Can't use flow.kill() here because that currently still depends on a reply object. flow.error = Error(Error.KILLED_MESSAGE) server = Placeholder(Server) data_req1 = Placeholder(bytes) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame( example_request_headers, flags=["END_STREAM"], stream_id=1).serialize() + cff.build_headers_frame( example_request_headers, flags=["END_STREAM"], stream_id=3).serialize()) << req_headers_hook_1 << http.HttpRequestHeadersHook(flow2) >> reply(side_effect=kill) << http.HttpErrorHook(flow2) >> reply() << SendData( tctx.client, cff.build_rst_stream_frame( 3, error_code=ErrorCodes.INTERNAL_ERROR).serialize()) >> reply(to=req_headers_hook_1) << http.HttpRequestHook(flow1) >> reply() << OpenConnection(server) >> reply( None, side_effect=make_h2) << SendData(server, data_req1)) frames = decode_frames(data_req1()) assert [type(x) for x in frames] == [ hyperframe.frame.SettingsFrame, hyperframe.frame.HeadersFrame, ]
def test_http_client_aborts(tctx, stream): """Test handling of the case where a client aborts during request transmission.""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=True) def enable_streaming(flow: HTTPFlow): flow.request.stream = True assert (playbook >> DataReceived( tctx.client, b"POST http://example.com/ HTTP/1.1\r\n" b"Host: example.com\r\n" b"Content-Length: 6\r\n" b"\r\n" b"abc") << http.HttpRequestHeadersHook(flow)) if stream: assert (playbook >> reply(side_effect=enable_streaming) << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None) << SendData( server, b"POST / HTTP/1.1\r\n" b"Host: example.com\r\n" b"Content-Length: 6\r\n" b"\r\n" b"abc")) else: assert playbook >> reply() (playbook >> ConnectionClosed(tctx.client) << CloseConnection(tctx.client)) if stream: playbook << CloseConnection(server) assert (playbook << http.HttpErrorHook(flow) >> reply() << None) assert "peer closed connection" in flow().error.msg
def test_protocol_error(ws_testdata): tctx, playbook = ws_testdata flow = Placeholder(WebSocketFlow) assert ( playbook << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.server, b"\x01\x03foo") >> DataReceived(tctx.server, b"\x02\x03bar") << SendData( tctx.server, masked( b"\x88/\x03\xeaexpected CONTINUATION, got <Opcode.BINARY: 2>")) << CloseConnection(tctx.server) << SendData( tctx.client, b"\x88/\x03\xeaexpected CONTINUATION, got <Opcode.BINARY: 2>") << CloseConnection( tctx.client) << websocket.WebsocketErrorHook(flow) >> reply()) assert not flow().messages
def test_receive_data_before_server_connected(tctx): """ assert that data received before a server connection is established will still be forwarded. """ assert (Playbook(tcp.TCPLayer(tctx), hooks=False) << OpenConnection( tctx.server) >> DataReceived(tctx.client, b"hello!") >> reply( None, to=-2) << SendData(tctx.server, b"hello!"))
def test_drop_message(ws_testdata): tctx, playbook = ws_testdata flow = Placeholder(WebSocketFlow) assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.server, b"\x81\x03foo") << websocket.WebsocketMessageHook(flow)) flow().messages[-1].content = "" assert (playbook >> reply() << None)
def test_http_proxy_relative_request_no_host_header(tctx): """Test handling of a relative-form "GET /" in regular proxy mode, but without a host header.""" assert (Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\n\r\n") << SendData( tctx.client, b"HTTP/1.1 400 Bad Request\r\n" b"content-length: 53\r\n" b"\r\n" b"HTTP request has no host header, destination unknown."))
def test_server_aborts(tctx, data): """Test the scenario where the server doesn't serve a response""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) err = Placeholder(bytes) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) assert (playbook >> DataReceived( tctx.client, b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")) if data: playbook >> DataReceived(server, data) assert (playbook >> ConnectionClosed(server) << CloseConnection(server) << http.HttpErrorHook(flow) >> reply() << SendData( tctx.client, err) << CloseConnection(tctx.client)) assert flow().error assert b"502 Bad Gateway" in err()
def test_connection_close_header(tctx, client_close, server_close): """Test that we correctly close connections if we have a `Connection: close` header.""" if not client_close and not server_close: return server = Placeholder(Server) assert (Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) >> DataReceived( tctx.client, b"GET http://example/ HTTP/1.1\r\n" b"Host: example\r\n" + client_close + b"\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\n" b"Host: example\r\n" + client_close + b"\r\n") >> DataReceived( server, b"HTTP/1.1 200 OK\r\n" b"Content-Length: 0\r\n" + server_close + b"\r\n") << CloseConnection(server) << SendData( tctx.client, b"HTTP/1.1 200 OK\r\n" b"Content-Length: 0\r\n" + server_close + b"\r\n") << CloseConnection(tctx.client))
def test_close_during_connect_hook(tctx): flow = Placeholder(HTTPFlow) assert (Playbook(http.HttpLayer(tctx, HTTPMode.regular)) >> DataReceived( tctx.client, b'CONNECT hi.ls:443 HTTP/1.1\r\n' b'Proxy-Connection: keep-alive\r\n' b'Connection: keep-alive\r\n' b'Host: hi.ls:443\r\n\r\n') << http.HttpConnectHook(flow) >> ConnectionClosed(tctx.client) << CloseConnection( tctx.client) >> reply(to=-3))