def handle_read(self): self.response_buffer += self.recv(deproxy.MAX_MESSAGE_SIZE).decode() if not self.response_buffer: return tf_cfg.dbg(4, '\tDeproxy: Client: Receive response.') tf_cfg.dbg(5, self.response_buffer) while len(self.response_buffer) > 0 and self.nrreq > self.nrresp: try: method = self.methods[self.nrresp] response = deproxy.Response(self.response_buffer, method=method, keep_original_data=self.keep_original_data) self.response_buffer = \ self.response_buffer[response.original_length:] except deproxy.IncompleteMessage: return except deproxy.ParseError: tf_cfg.dbg(4, ('Deproxy: Client: Can\'t parse message\n' '<<<<<\n%s>>>>>' % self.response_buffer)) raise self.receive_response(response) self.nrresp += 1 if self.nrreq == self.nrresp and len(self.response_buffer) > 0: raise deproxy.ParseError('Garbage after response' ' end:\n```\n%s\n```\n' % \ self.response_buffer)
def test_response_plain(self): response = deproxy.Response( "HTTP/1.1 200 OK\r\n" "Server: SimpleHTTP/0.6 Python/3.6.0\r\n" "Content-type: text/html\r\n" "Content-Length: 138\r\n" "Last-Modified: Mon, 12 Dec 2016 13:59:39 GMT\r\n" "\r\n" "<html>\r\n" "<head>\r\n" " <title>An Example Page</title>\r\n" "</head>\r\n" "<body>\r\n" " Hello World, this is a very simple HTML document.\r\n" "</body>\r\n" "</html>\r\n") headers = [ 'Server: SimpleHTTP/0.6 Python/3.6.0', 'Content-type: text/html', 'Content-Length: 138', 'Last-Modified: Mon, 12 Dec 2016 13:59:39 GMT' ] body = ("<html>\r\n" "<head>\r\n" " <title>An Example Page</title>\r\n" "</head>\r\n" "<body>\r\n" " Hello World, this is a very simple HTML document.\r\n" "</body>\r\n" "</html>\r\n") created = deproxy.Response.create(200, headers, body=body) self.assertEqual(response, created)
def try_body(self, response_text, body_text, trailer_headers=None): response = deproxy.Response(response_text) self.assertEqual(response.body, body_text) if not trailer_headers: self.assertEqual(len(response.trailer), 0) else: for header, value in trailer_headers: self.assertEqual(response.trailer[header], value.strip())
def make_302(request): response = deproxy.Response( 'HTTP/1.1 302 Found\r\n' 'Content-Length: 0\r\n' 'Location: http://%s%s\r\n' 'Connection: keep-alive\r\n' '\r\n' % (tf_cfg.cfg.get('Tempesta', 'ip'), request.uri)) return response
def receive_request(self, request, connection): id = request.uri r, close = deproxy_server.StaticDeproxyServer.receive_request( self, request, connection) resp = deproxy.Response(r) resp.body = id resp.headers['Content-Length'] = len(resp.body) resp.build_message() return resp.msg, close
def setUp(self): self.plain = deproxy.Response(PLAIN) self.reordered = deproxy.Response(REORDERED) self.o_body = deproxy.Response(OTHER_BODY) self.duplicated = deproxy.Response(DUPLICATED) self.o_status = deproxy.Response(OTHER_STATUS) self.trailer = deproxy.Response(TRAILER) self.o_trailer = deproxy.Response(OTHER_TRAILER)
def __init__(self, clients, servers): deproxy.Deproxy.__init__(self, None, None, servers, register=False) self.clients = clients request = deproxy.Request("GET / HTTP/1.1\r\n" "Host: host\r\n" "User-Agent: curl/7.53.1\r\n" "\r\n") response = deproxy.Response() self.current_chain = deproxy.MessageChain(request, response, server_response=response) self.register_tester()
def receive_request(self, request, connection): self.nka += 1 tf_cfg.dbg(5, "\trequests = %i of %i" % (self.nka, self.ka)) r, close = DeproxyEchoServer.receive_request(self, request, connection) if self.nka < self.ka and not close: return r, False resp = deproxy.Response(r) resp.headers['Connection'] = "close" resp.build_message() tf_cfg.dbg(3, "\tDeproxy: keepalive closing") self.nka = 0 return resp.msg, True
def setUp(self): deproxy.HeaderCollection._disable_report_wrong_is_expected = True self.plain = deproxy.Response(PLAIN) self.reordered = deproxy.Response(REORDERED) self.o_body = deproxy.Response(OTHER_BODY) self.duplicated = deproxy.Response(DUPLICATED) self.o_status = deproxy.Response(OTHER_STATUS) self.trailer = deproxy.Response(TRAILER) self.o_trailer = deproxy.Response(OTHER_TRAILER)
def test_deproxy_srvclient_direct_check(self): """ Simple test with deproxy server """ dsrv = self.get_server('deproxy') dsrv.start() cl = self.get_client('deproxy_direct') cl.start() self.deproxy_manager.start() cl.make_request('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') cl.wait_for_response(timeout=5) # expected response send = deproxy.Response(dsrv.response) send.set_expected() self.assertEqual(cl.last_response, send)
def handle_read(self): self.response_buffer += self.recv(deproxy.MAX_MESSAGE_SIZE).decode() if not self.response_buffer: return tf_cfg.dbg(4, '\tDeproxy: Client: Receive response from Tempesta.') tf_cfg.dbg(5, self.response_buffer) method = self.method while len(self.response_buffer) > 0: try: response = deproxy.Response(self.response_buffer, method=method) self.response_buffer = self.response_buffer[len(response.msg):] method = "GET" except deproxy.ParseError: tf_cfg.dbg(4, ('Deproxy: Client: Can\'t parse message\n' '<<<<<\n%s>>>>>' % self.response_buffer)) raise if self.tester: self.tester.received_response(response) self.response_buffer = '' raise asyncore.ExitNow
def test_no_crlf_before_body(self): chain = chains.proxy() chain.server_response.msg = chain.server_response.msg.replace( '\r\n\r\n', '\r\n', 1) chain.response = deproxy.Response() self.generic_test_routine(self.config, [chain])
def test_incomplite(self): message_1 = "HTTP/1.1 20" message_2 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Enco") message_3 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n") message_4 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Transfer-Encoding: compress, gzip, chunked\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n") message_5 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Transfer-Encoding: compress, gzip, chunked\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n" "6\r\n" "<html>\r\n" "0\r\n" "Expires: Wed, 21 Oct 2015 07:28:00 GMT") message_6 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Content-Length: 1000\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n" "<html>\r\n") incomplite = [(message_1, 'header'), (message_2, 'header'), (message_3, 'header: no CRLF'), (message_4, 'body: no last-chunk'), (message_5, 'trailer: no CRLF'), (message_6, 'body: too short')] for message, reason in incomplite: msg = ('Message parsed, but it has incomplite %s. Message:\n%s' % (reason, message)) parsed = True try: deproxy.Response(message) except deproxy.ParseError: parsed = False self.assertFalse(parsed, msg)
def test_valid(self): message_1 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "\r\n") message_2 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Transfer-Encoding: compress, gzip, chunked\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n" "0\r\n" "\r\n") message_3 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Transfer-Encoding: compress, gzip, chunked\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n" "6\r\n" "<html>\r\n" "0\r\n" "\r\n") message_4 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Transfer-Encoding: compress, gzip, chunked\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n" "6\r\n" "<html>\r\n" "0\r\n" "\r\n" "Expires: Wed, 21 Oct 2015 07:28:00 GMT\r\n" "\r\n") message_5 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Content-Length: 6\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n" "<html>" "\r\n") message_6 = ("HTTP/1.1 200 OK\r\n" "Date: Mon, 23 May 2005 22:38:34 GMT\r\n" "Content-Type: text/html; charset=UTF-8\r\n" "Content-Encoding: UTF-8\r\n" "Content-Length: 0\r\n" "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n" "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n" """ETag: "3f80f-1b6-3e1cb03b"\r\n""" "Accept-Ranges: bytes\r\n" "Connection: close\r\n" "\r\n") valid_messages = [ message_1, message_2, message_3, message_4, message_5, message_6 ] for message in valid_messages: try: deproxy.Response(message) except deproxy.ParseError: print('Error happen when processed message\n%s' % message) raise
def __init__(self, *args, **kwargs): TesterIgnoreCookies.__init__(self, *args, **kwargs) self.message_chains[0].response = make_302( self.message_chains[0].request) self.message_chains[0].server_response = deproxy.Response() self.message_chains[0].fwd_request = deproxy.Request()
def make_502(): response = deproxy.Response('HTTP/1.1 502 Bad Gateway\r\n' 'Content-Length: 0\r\n' 'Connection: keep-alive\r\n' '\r\n') return response