def test_two_responses_chunked_in_parts(): msg = b"HTTP/1.1 200 OK\r\n" + \ b"Content-Type: text/plain; charset=utf-8\r\n" + \ b"Transfer-Encoding: chunked\r\n" + \ b"\r\n" + \ b"4\r\n" + \ b"Wiki\r\n" + \ b"5\r\n" + \ b"pedia\r\n" + \ b"E\r\n" + \ b" in\r\n" + \ b"\r\n" + \ b"chunks.\r\n" + \ b"0\r\n" + \ b"\r\n" msg = msg * 2 parser = intialize_parser(http_parser.get_http_request) msgs = chunks(msg, 15) parsed_messages = [] for data in msgs: parsed_messages += parse(parser, data) assert len(parsed_messages) == 2 for parsed_message in parsed_messages: assert parsed_message.headers[ b'Content-Type'] == b"text/plain; charset=utf-8" assert parsed_message.body == b"Wikipedia in\r\n\r\nchunks."
def test_two_requests_whole(): msg = b"GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\nGET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n" parser = intialize_parser(http_parser.get_http_request) parsed_messages = list(parse(parser, msg)) assert len(parsed_messages) == 2 for parsed_message in parsed_messages: assert parsed_message.headers[b'Host'] == b"www.example.com" assert parsed_message.body == None assert parsed_message.method == b"GET" assert parsed_message.path == b"/"
def test_one_response_no_length(): msg = b"HTTP/1.1 200 OK\r\n" + \ b"Content-Type: text/plain; charset=utf-8\r\n" + \ b"\r\n" + \ b"abcd\r\n" parser = intialize_parser(http_parser.get_http_request) parsed_messages = list(parse(parser, msg)) assert len(parsed_messages) == 1 for parsed_message in parsed_messages: assert parsed_message.headers[ b'Content-Type'] == b"text/plain; charset=utf-8" assert parsed_message.body == b"abcd\r\n"
def test_two_requests_in_pieces(): msg = b"GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\nGET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n" msgs = chunks(msg, 15) parser = intialize_parser(http_parser.get_http_request) parsed_messages = [] for data in msgs: parsed_messages += parse(parser, data) assert len(parsed_messages) == 2 for parsed_message in parsed_messages: assert parsed_message.headers[b'Host'] == b"www.example.com" assert parsed_message.body == None assert parsed_message.method == b"GET" assert parsed_message.path == b"/"
def test_two_responses_in_pieces(): msg = b"HTTP/1.1 200 OK\r\n" + \ b"Content-Type: text/plain; charset=utf-8\r\n" + \ b"Content-Length: 6\r\n" + \ b"\r\n" + \ b"abcd\r\n" msg = msg * 3 parser = intialize_parser(http_parser.get_http_request) msgs = chunks(msg, 15) parsed_messages = [] for data in msgs: parsed_messages += parse(parser, data) assert len(parsed_messages) == 3 for parsed_message in parsed_messages: assert parsed_message.headers[ b'Content-Type'] == b"text/plain; charset=utf-8" assert parsed_message.body == b"abcd\r\n"
def run(self): parser = intialize_parser(http_parser.get_http_request) while 1: try: try: data = self.source.recv(1024) except ConnectionResetError: data = None if self.tag == "request": for msg in parse(parser, data): self.communication.add_message(msg, self.tag) # print(msg) if msg.headers.get(b"Host"): msg.headers[b"Host"] = self.host_header self.send_request(msg) else: for msg in parse(parser, data): self.communication.add_message(msg, self.tag) # print(msg) if msg.headers.get(b"Transfer-Encoding", "") == b"chunked": del msg.headers[b"Transfer-Encoding"] msg.headers[b"Content-Length"] = str(len( msg.body)).encode() self.send_request(msg) if not data: break except Exception as ex: print(ex) break log('%s terminating' % self) PipeThread.pipes.remove(self) log('%s pipes active' % len(PipeThread.pipes)) self.sink.shutdown(socket.SHUT_WR)
def test_one_response_chunked_whole(): msg = b"HTTP/1.1 200 OK\r\n" + \ b"Content-Type: text/plain; charset=utf-8\r\n" + \ b"Transfer-Encoding: chunked\r\n" + \ b"\r\n" + \ b"4\r\n" + \ b"Wiki\r\n" + \ b"5\r\n" + \ b"pedia\r\n" + \ b"E\r\n" + \ b" in\r\n" + \ b"\r\n" + \ b"chunks.\r\n" + \ b"0\r\n" + \ b"\r\n" parser = intialize_parser(http_parser.get_http_request) parsed_messages = list(parse(parser, msg)) assert len(parsed_messages) == 1 for parsed_message in parsed_messages: assert parsed_message.headers[ b'Content-Type'] == b"text/plain; charset=utf-8" assert parsed_message.body == b"Wikipedia in\r\n\r\nchunks."