def client_handshake_headers(version=None, key=None, protocol=None, extensions=None): """ Create the headers for a valid HTTP upgrade request. If Key is not specified, it is generated, and can be found in sec-websocket-key in the returned header set. Returns an instance of http.Headers """ if version is None: version = VERSION if key is None: key = base64.b64encode(os.urandom(16)).decode('ascii') h = http.Headers( connection="upgrade", upgrade="websocket", sec_websocket_version=version, sec_websocket_key=key, ) if protocol is not None: h['sec-websocket-protocol'] = protocol if extensions is not None: h['sec-websocket-extensions'] = extensions return h
def server_handshake_headers(self, key): """ The server response is a valid HTTP 101 response. """ return http.Headers(sec_websocket_accept=self.create_server_nonce(key), connection="Upgrade", upgrade="websocket")
def test_set_cookies(self): r = tutils.treq() r.headers = http.Headers(cookie="cookiename=cookievalue") result = r.get_cookies() result["cookiename"] = ["foo"] r.set_cookies(result) assert r.get_cookies()["cookiename"] == ["foo"]
def test_get_cookies_simple(self): resp = tutils.tresp() resp.headers = http.Headers(set_cookie="cookiename=cookievalue") result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()]
def make_error_response(status_code, message, headers=None): response = http.status_codes.RESPONSES.get(status_code, "Unknown") body = """ <html> <head> <title>%d %s</title> </head> <body>%s</body> </html> """.strip() % (status_code, response, cgi.escape(message)) body = body.encode("utf8", "replace") if not headers: headers = http.Headers(Server=version.MITMPROXY, Connection="close", Content_Length=str(len(body)), Content_Type="text/html") return HTTPResponse( b"HTTP/1.1", status_code, response, headers, body, )
def test_get_cookies_withequalsign(self): r = tutils.treq() r.headers = http.Headers( cookie="cookiename=coo=kievalue;othercookiename=othercookievalue") result = r.get_cookies() assert len(result) == 2 assert result['cookiename'] == ['coo=kievalue'] assert result['othercookiename'] == ['othercookievalue']
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')
def test_get_cookies_twocookies(self): resp = tutils.tresp() resp.headers = http.Headers([["Set-Cookie", "cookiename=cookievalue"], ["Set-Cookie", "othercookie=othervalue"]]) result = resp.get_cookies() assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()]
def authenticate(self, request): if self.config.authenticator: if self.config.authenticator.authenticate(request.headers): self.config.authenticator.clean(request.headers) else: if self.mode == "transparent": self.send_response( models.make_error_response( 401, "Authentication Required", http.Headers(**self.config.authenticator. auth_challenge_headers()))) else: self.send_response( models.make_error_response( 407, "Proxy Authentication Required", http.Headers(**self.config.authenticator. auth_challenge_headers()))) return False return True
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 HTTPResponse( http_version, 200, b"Connection established", http.Headers(), b"", )
def test_get_cookies_no_value(self): resp = tutils.tresp() resp.headers = http.Headers( set_cookie= "cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/") result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "" assert len(result["cookiename"][0][1]) == 2
def test_create_headers_multiple_frames(self): headers = http.Headers([(b':method', b'GET'), (b':path', b'/'), (b':scheme', b'https'), (b'foo', b'bar'), (b'server', b'version')]) protocol = HTTP2Protocol(self.c) protocol.http2_settings[ SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] = 8 bytes = protocol._create_headers(headers, 1, end_stream=True) assert len(bytes) == 3 assert bytes[0] == '000008010000000001828487408294e783'.decode('hex') assert bytes[1] == '0000080900000000018c767f7685ee5b10'.decode('hex') assert bytes[2] == '00000209050000000163d5'.decode('hex')
def start_response(status, headers, exc_info=None): if exc_info: if state["headers_sent"]: raise exc_info[1] elif state["status"]: raise AssertionError('Response already started') state["status"] = status state["headers"] = http.Headers( [[strutils.always_bytes(k), strutils.always_bytes(v)] for k, v in headers]) if exc_info: self.error_page(soc, state["headers_sent"], traceback.format_tb(exc_info[2])) state["headers_sent"] = True
def create_request(self, method, scheme, host, port, path): """ this method creates a new artificial and minimalist request also adds it to flowlist """ c = models.ClientConnection.make_dummy(("", 0)) s = models.ServerConnection.make_dummy((host, port)) f = models.HTTPFlow(c, s) headers = http.Headers() req = models.HTTPRequest("absolute", method, scheme, host, port, path, b"HTTP/1.1", headers, b"") f.request = req self.load_flow(f) return f
def test_create_headers(self): headers = http.Headers([(b':method', b'GET'), (b':path', b'index.html'), (b':scheme', b'https'), (b'foo', b'bar')]) bytes = HTTP2Protocol(self.c)._create_headers(headers, 1, end_stream=True) assert b''.join(bytes) ==\ codecs.decode('000014010500000001824488355217caf3a69a3f87408294e7838c767f', 'hex_codec') bytes = HTTP2Protocol(self.c)._create_headers(headers, 1, end_stream=False) assert b''.join(bytes) ==\ codecs.decode('000014010400000001824488355217caf3a69a3f87408294e7838c767f', 'hex_codec')
def client_handshake_headers(self, key=None, version=VERSION): """ Create the headers for a valid HTTP upgrade request. If Key is not specified, it is generated, and can be found in sec-websocket-key in the returned header set. Returns an instance of http.Headers """ if not key: key = base64.b64encode(os.urandom(16)).decode('ascii') return http.Headers( sec_websocket_key=key, sec_websocket_version=version, connection="Upgrade", upgrade="websocket", )
def test_get_cookies_with_parameters(self): resp = tutils.tresp() resp.headers = http.Headers( set_cookie= "cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly" ) result = resp.get_cookies() assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "cookievalue" attrs = result["cookiename"][0][1] assert len(attrs) == 4 assert attrs["domain"] == ["example.com"] assert attrs["expires"] == ["Wed Oct 21 16:29:41 2015"] assert attrs["path"] == ["/"] assert attrs["httponly"] == [None]
def server_handshake_headers(client_key, protocol=None, extensions=None): """ The server response is a valid HTTP 101 response. Returns an instance of http.Headers """ h = http.Headers( connection="upgrade", upgrade="websocket", sec_websocket_accept=create_server_nonce(client_key), ) if protocol is not None: h['sec-websocket-protocol'] = protocol if extensions is not None: h['sec-websocket-extensions'] = extensions return h
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)
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')
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), http.Headers([('foo', 'bar')]), 'foobar', )) assert len(bytes) == 2 assert bytes[0] ==\ '0000150104000000018284874188089d5c0b8170dc07408294e7838c767f'.decode('hex') assert bytes[1] ==\ '000006000100000001666f6f626172'.decode('hex')
def tresp(content="message"): """ @return: libmproxy.protocol.http.HTTPResponse """ headers = http.Headers() headers["header_response"] = "svalue" resp = http.semantics.Response( (1, 1), 200, "OK", headers, content, timestamp_start=time.time(), timestamp_end=time.time(), ) return resp
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 treq(content="content", scheme="http", host="address", port=22): """ @return: libmproxy.protocol.http.HTTPRequest """ headers = http.Headers() headers["header"] = "qvalue" req = http.Request( "relative", "GET", scheme, host, port, "/path", (1, 1), headers, content, None, None, ) return req
def test_read_chunked(): headers = http.Headers() headers["transfer-encoding"] = "chunked" data = "1\r\na\r\n0\r\n" tutils.raises( "malformed chunked body", mock_protocol(data).read_http_body, headers, None, "GET", None, True ) data = "1\r\na\r\n0\r\n\r\n" assert mock_protocol(data).read_http_body(headers, None, "GET", None, True) == "a" data = "\r\n\r\n1\r\na\r\n0\r\n\r\n" assert mock_protocol(data).read_http_body(headers, None, "GET", None, True) == "a" data = "\r\n" tutils.raises( "closed prematurely", mock_protocol(data).read_http_body, headers, None, "GET", None, True ) data = "1\r\nfoo" tutils.raises( "malformed chunked body", mock_protocol(data).read_http_body, headers, None, "GET", None, True ) data = "foo\r\nfoo" tutils.raises( http.HttpError, mock_protocol(data).read_http_body, headers, None, "GET", None, True ) data = "5\r\naaaaa\r\n0\r\n\r\n" tutils.raises("too large", mock_protocol(data).read_http_body, headers, 2, "GET", None, True)
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 _receive_transmission(self, stream_id=None, include_body=True): if not include_body: raise NotImplementedError() body_expected = True header_block_fragment = b'' body = b'' while True: frm = self.read_frame() if ((isinstance(frm, frame.HeadersFrame) or isinstance(frm, frame.ContinuationFrame)) and (stream_id is None or frm.stream_id == stream_id)): stream_id = frm.stream_id header_block_fragment += frm.header_block_fragment if frm.flags & frame.Frame.FLAG_END_STREAM: body_expected = False if frm.flags & frame.Frame.FLAG_END_HEADERS: break else: self._handle_unexpected_frame(frm) while body_expected: frm = self.read_frame() if isinstance(frm, frame.DataFrame) and frm.stream_id == stream_id: body += frm.payload if frm.flags & frame.Frame.FLAG_END_STREAM: break else: self._handle_unexpected_frame(frm) headers = http.Headers( [[str(k), str(v)] for k, v in self.decoder.decode(header_block_fragment)]) return stream_id, headers, body
def test_get_cookies_none(self): resp = tutils.tresp() resp.headers = http.Headers() assert not resp.get_cookies()
def test_get_cookies_single(self): r = tutils.treq() r.headers = http.Headers(cookie="cookiename=cookievalue") result = r.get_cookies() assert len(result) == 1 assert result['cookiename'] == ['cookievalue']