def http_connect(self, connect_to): req = net_http.Request( host=connect_to[0], port=connect_to[1], method=b'CONNECT', scheme=b"", authority=f"{connect_to[0]}:{connect_to[1]}".encode(), path=b"", http_version=b'HTTP/1.1', headers=((b"Host", connect_to[0].encode("idna")),), content=b'', trailers=None, timestamp_start=0, timestamp_end=0, ) self.wfile.write(net_http.http1.assemble_request(req)) self.wfile.flush() try: resp = self.protocol.read_response(self.rfile, req) if resp.status_code != 200: raise exceptions.HttpException("Unexpected status code: %s" % resp.status_code) except exceptions.HttpException as e: raise PathocError( "Proxy CONNECT failed: %s" % repr(e) )
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( "", 0, self.method.string(), b'http', b'', path, b"HTTP/2.0", headers, body, None, 0, 0, ) req.stream_id = self.stream_id self.rendered_values = settings.protocol.assemble(req) return self.rendered_values
def http(self, r): """ Performs a single request. r: A language.http.Request object, or a string representing one request. Returns Response if we have a non-ignored response. May raise a exceptions.NetlibException """ logger = log.ConnectionLogger( self.fp, self.hexdump, False, self.rfile if self.showresp else None, self.wfile if self.showreq else None, ) with logger.ctx() as lg: lg(">> %s" % r) resp, req = None, None try: req = language.serve(r, self.wfile, self.settings) self.wfile.flush() # build a dummy request to read the reponse # ideally this would be returned directly from language.serve dummy_req = net_http.Request( first_line_format="relative", method=req["method"], scheme=b"http", host=b"localhost", port=80, path=b"/", http_version=b"HTTP/1.1", content=b'', ) resp = self.protocol.read_response(self.rfile, dummy_req) resp.sslinfo = self.sslinfo except exceptions.HttpException as v: lg("Invalid server response: %s" % v) raise except exceptions.TcpTimeout: if self.ignoretimeout: lg("Timeout (ignored)") return None lg("Timeout") raise finally: if resp: lg("<< %s %s: %s bytes" % (resp.status_code, strutils.escape_control_characters(resp.reason) if resp.reason else "", len(resp.content))) if resp.status_code in self.ignorecodes: lg.suppress() return resp
def _setup_connection(self): client = tcp.TCPClient(("127.0.0.1", self.proxy.port)) client.connect() request = http.Request( "authority", "CONNECT", "", "127.0.0.1", self.server.server.address[1], "", "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", "127.0.0.1", self.server.server.address[1], "/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 setup_connection(self, extension=False): self.client = tcp.TCPClient(("127.0.0.1", self.proxy.port)) self.client.connect() request = http.Request( "authority", "CONNECT", "", "127.0.0.1", self.server.server.address[1], "", "HTTP/1.1", content=b'') self.client.wfile.write(http.http1.assemble_request(request)) self.client.wfile.flush() response = http.http1.read_response(self.client.rfile, request) if self.ssl: self.client.convert_to_tls() assert self.client.tls_established request = http.Request( "relative", "GET", "http", "127.0.0.1", self.server.server.address[1], "/ws", "HTTP/1.1", headers=http.Headers( connection="upgrade", upgrade="websocket", sec_websocket_version="13", sec_websocket_key="1234", sec_websocket_extensions="permessage-deflate" if extension else "" ), content=b'') self.client.wfile.write(http.http1.assemble_request(request)) self.client.wfile.flush() response = http.http1.read_response(self.client.rfile, request) assert websockets.check_handshake(response.headers)
def test_request_simple(self): bytes = HTTP2StateProtocol(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 make_connect_request(address: Tuple[str, int]) -> http.Request: return http.Request( host=address[0], port=address[1], method=b"CONNECT", scheme=b"", authority=f"{address[0]}:{address[1]}".encode(), path=b"", http_version=b"HTTP/1.1", headers=http.Headers(), content=b"", trailers=None, timestamp_start=time.time(), timestamp_end=time.time(), )
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 = HTTP2StateProtocol(self.c).assemble_request(req) assert len(bytes) == 1 assert bytes[0] == codecs.decode('00000d0105000000428284874188089d5c0b8170dc07', 'hex_codec')
def test_request_with_body(self): bytes = HTTP2StateProtocol(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 test_request_simple(self): data = HTTP2StateProtocol(self.c).assemble_request(http.Request( host="", port=0, method=b'GET', scheme=b'https', authority=b'', path=b'/', http_version=b"HTTP/2.0", headers=(), content=None, trailers=None, timestamp_start=0, timestamp_end=0 )) assert len(data) == 1 assert data[0] == bytes.fromhex('00000d0105000000018284874188089d5c0b8170dc07')
def treq(**kwargs): """ Returns: mitmproxy.net.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): data = HTTP2StateProtocol(self.c).assemble_request(http.Request( host="", port=0, method=b'GET', scheme=b'https', authority=b'', path=b'/', http_version=b"HTTP/2.0", headers=http.Headers([(b'foo', b'bar')]), content=b'foobar', trailers=None, timestamp_start=0, timestamp_end=None, )) assert len(data) == 2 assert data[0] == bytes.fromhex("0000150104000000018284874188089d5c0b8170dc07408294e7838c767f") assert data[1] == bytes.fromhex("000006000100000001666f6f626172")
def http_connect(self, connect_to): req = net_http.Request( first_line_format='authority', method='CONNECT', scheme=None, host=connect_to[0].encode("idna"), port=connect_to[1], path=None, http_version='HTTP/1.1', content=b'', ) self.wfile.write(net_http.http1.assemble_request(req)) self.wfile.flush() try: resp = self.protocol.read_response(self.rfile, req) if resp.status_code != 200: raise exceptions.HttpException("Unexpected status code: %s" % resp.status_code) except exceptions.HttpException as e: raise PathocError("Proxy CONNECT failed: %s" % repr(e))
def treq(**kwargs) -> http.Request: """ Returns: mitmproxy.net.http.Request """ default = dict( host="address", port=22, method=b"GET", scheme=b"http", authority=b"", path=b"/path", http_version=b"HTTP/1.1", headers=http.Headers(((b"header", b"qvalue"), (b"content-length", b"7"))), content=b"content", trailers=None, timestamp_start=946681200, timestamp_end=946681201, ) default.update(kwargs) return http.Request(**default) # type: ignore
def setup_connection(self, extension=False): self.client = tcp.TCPClient(("127.0.0.1", self.proxy.port)) self.client.connect() request = make_connect_request(("127.0.0.1", self.server.server.address[1])) self.client.wfile.write(http.http1.assemble_request(request)) self.client.wfile.flush() response = http.http1.read_response(self.client.rfile, request) if self.ssl: self.client.convert_to_tls() assert self.client.tls_established request = http.Request( host="127.0.0.1", port=self.server.server.address[1], method=b"GET", scheme=b"http", authority=b"", path=b"/ws", http_version=b"HTTP/1.1", headers=http.Headers( connection="upgrade", upgrade="websocket", sec_websocket_version="13", sec_websocket_key="1234", sec_websocket_extensions="permessage-deflate" if extension else "" ), content=b'', trailers=None, timestamp_start=0, timestamp_end=0, ) self.client.wfile.write(http.http1.assemble_request(request)) self.client.wfile.flush() response = http.http1.read_response(self.client.rfile, request) assert websocket_utils.check_handshake(response.headers)