def testPipelining(self): # Tests the use of several requests issued at once. s = ("GET / HTTP/1.0\r\n" "Connection: %s\r\n" "Content-Length: %d\r\n" "\r\n" "%s") to_send = [] count = 25 for n in range(count): body = "Response #%d\r\n" % (n + 1) if n + 1 < count: conn = 'keep-alive' else: conn = 'close' to_send.append(s % (conn, len(body), body)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) for n, req in enumerate(to_send): sock.send(req.encode('ascii')) expect_body = ("Response #%d\r\n" % (n + 1)).encode('ascii') response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) length = int(response.getheader('Content-Length', '0')) response_body = response.read(length) self.assertEqual(length, len(response_body)) self.assertEqual(response_body, expect_body)
def _read_status(self): if self._status_tuple is not None: status_tuple = self._status_tuple self._status_tuple = None return status_tuple else: return HTTPResponse._read_status(self)
def _parse_ws_response(self): http_response = self.__ws.recv() response = HTTPResponse(FakeSocket(http_response)) response.begin() while response.length > len(http_response): http_response += self.__ws.recv() response = HTTPResponse(FakeSocket(http_response)) self.__transport.verbose = 0 response.begin() return self.__transport.parse_response(response)
def testWithoutCRLF(self): # Tests the use of just newlines rather than CR/LFs. data = "Echo\nthis\r\nplease" s = ("GET / HTTP/1.0\n" "Connection: close\n" "Content-Length: %d\n" "\n" "%s") % (len(data), data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) sock.send(s.encode('ascii')) response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) length = int(response.getheader('Content-Length', '0')) response_body = response.read(length) self.assertEqual(length, len(data)) self.assertEqual(response_body, data.encode("ascii"))
def testKeepaliveHttp10(self): # Handling of Keep-Alive within HTTP 1.0 data = "Default: Don't keep me alive" s = ("GET / HTTP/1.0\n" "Content-Length: %d\n" "\n" "%s") % (len(data), data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) sock.send(s.encode('ascii')) response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) connection = response.getheader('Connection', '') # We sent no Connection: Keep-Alive header # Connection: close (or no header) is default. self.assertNotEqual(connection, 'Keep-Alive') # If header Connection: Keep-Alive is explicitly sent, # we want to keept the connection open, we also need to return # the corresponding header data = "Keep me alive" s = ("GET / HTTP/1.0\n" "Connection: Keep-Alive\n" "Content-Length: %d\n" "\n" "%s") % (len(data), data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) sock.send(s.encode('ascii')) response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) connection = response.getheader('Connection', '') self.assertEqual(connection, 'Keep-Alive')
def testKeepaliveHttp11(self): # Handling of Keep-Alive within HTTP 1.1 # All connections are kept alive, unless stated otherwise data = "Default: Keep me alive" s = ("GET / HTTP/1.1\n" "Content-Length: %d\n" "\n" "%s") % (len(data), data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) sock.send(s.encode('ascii')) response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) self.assertNotEqual(response.getheader('connection'), 'close') # Explicitly set keep-alive data = "Default: Keep me alive" s = ("GET / HTTP/1.1\n" "Connection: keep-alive\n" "Content-Length: %d\n" "\n" "%s") % (len(data), data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) sock.send(s.encode('ascii')) response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) self.assertNotEqual(response.getheader('connection'), 'close') # no idea why the test publisher handles this request incorrectly # it would be less typing in the test :) # h = self._makeConnection() # h.request("GET", "/") # response = h.getresponse() # self.assertEqual(int(response.status), 200) # self.failUnless(response.getheader('connection') != 'close') # specifying Connection: close explicitly data = "Don't keep me alive" s = ("GET / HTTP/1.1\n" "Connection: close\n" "Content-Length: %d\n" "\n" "%s") % (len(data), data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.LOCALHOST, self.port)) self.addCleanup(sock.close) sock.send(s.encode('ascii')) response = ClientHTTPResponse(sock) response.begin() self.assertEqual(int(response.status), 200) self.assertEqual(response.getheader('connection'), 'close')
def __init__(self, *args, **kwargs): self._status_tuple = kwargs.pop('status_tuple') HTTPResponse.__init__(self, *args, **kwargs)