def test_garbage_in(self): # Connect without SSL regardless of server.scheme c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) c._output(b'gjkgjklsgjklsgjkljklsg') c._send_output() response = c.response_class(c.sock, method='GET') try: response.begin() self.assertEqual(response.status, 400) self.assertEqual(response.fp.read(22), b'Malformed Request-Line') c.close() except socket.error: e = sys.exc_info()[1] # "Connection reset by peer" is also acceptable. if e.errno != errno.ECONNRESET: raise
def test_garbage_in(self): # Connect without SSL regardless of server.scheme c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) c._output(b'gjkgjklsgjklsgjkljklsg') c._send_output() response = c.response_class(c.sock, method='GET') try: response.begin() self.assertEqual(response.status, 400) self.assertEqual(response.fp.read(22), b'Malformed Request-Line') c.close() except socket.error: e = sys.exc_info()[1] # "Connection reset by peer" is also acceptable. if e.errno != errno.ECONNRESET: raise
def test_http_over_https(self): if self.scheme != 'https': return self.skip('skipped (not running HTTPS)... ') # Try connecting without SSL. conn = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) conn.putrequest('GET', '/', skip_host=True) conn.putheader('Host', self.HOST) conn.endheaders() response = conn.response_class(conn.sock, method='GET') try: response.begin() self.assertEqual(response.status, 400) self.body = response.read() self.assertBody('The client sent a plain HTTP request, but this ' 'server only speaks HTTPS on this port.') except socket.error: e = sys.exc_info()[1] # "Connection reset by peer" is also acceptable. if e.errno != errno.ECONNRESET: raise
def test_http_over_https(self): if self.scheme != 'https': return self.skip('skipped (not running HTTPS)... ') # Try connecting without SSL. conn = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) conn.putrequest('GET', '/', skip_host=True) conn.putheader('Host', self.HOST) conn.endheaders() response = conn.response_class(conn.sock, method='GET') try: response.begin() self.assertEqual(response.status, 400) self.body = response.read() self.assertBody('The client sent a plain HTTP request, but this ' 'server only speaks HTTPS on this port.') except socket.error: e = sys.exc_info()[1] # "Connection reset by peer" is also acceptable. if e.errno != errno.ECONNRESET: raise
class Pipeline(list): "Gawd why am I being so elaborate?" def __init__(self, threshold=10): "threshold is how many requests in parallel to pipeline" self.threshold = threshold self.sent = True def __enter__(self): self.reopen() return self def __exit__(self,typ,exn,trace): self.send() self.drain() def reopen(self): self.c = HTTPConnection(server) self.send() def append(self,url,recv,diemessage): self.sent = False super().append(Penguin(url,recv,diemessage)) if len(self) > self.threshold: self.send() self.drain() def trydrain(self): for penguin in self: print('drain',penguin.url) try: penguin.response.begin() penguin.recv(penguin.response) except BadStatusLine as e: print('derped requesting',penguin.url) return False except HTTPException as e: die(penguin.diemessage+' '+repr(e)+' (url='+penguin.url+')') self.clear() return True def drain(self): print('draining pipeline...',len(self)) assert self.sent, "Can't drain without sending the requests!" self.sent = False while self.trydrain() is not True: self.c.close() print('drain failed, trying again') time.sleep(1) self.reopen() def trysend(self): for penguin in pipeline: print('fill',penguin.url) try: self.c.request("GET", penguin.url) self.c._HTTPConnection__state = _CS_IDLE penguin.response = self.c.response_class(self.c.sock, method="GET") # begin LATER so we can send multiple requests w/out response headers except BadStatusLine: return False except HTTPException as e: die(diemessage+' because of a '+repr(e)) return True def send(self): if self.sent: return print('filling pipeline...',len(self)) while self.trysend() is not True: self.c.close() print('derped resending') time.sleep(1) self.reopen() self.sent = True
class Pipeline(list): "Gawd why am I being so elaborate?" def __init__(self, threshold=10): "threshold is how many requests in parallel to pipeline" self.threshold = threshold self.sent = True def __enter__(self): self.reopen() return self def __exit__(self, typ, exn, trace): self.send() self.drain() def reopen(self): self.c = HTTPConnection(server) self.send() def append(self, url, recv, diemessage): self.sent = False super().append(Penguin(url, recv, diemessage)) if len(self) > self.threshold: self.send() self.drain() def trydrain(self): for penguin in self: print('drain', penguin.url) try: penguin.response.begin() penguin.recv(penguin.response) except BadStatusLine as e: print('derped requesting', penguin.url) return False except HTTPException as e: die(penguin.diemessage + ' ' + repr(e) + ' (url=' + penguin.url + ')') self.clear() return True def drain(self): print('draining pipeline...', len(self)) assert self.sent, "Can't drain without sending the requests!" self.sent = False while self.trydrain() is not True: self.c.close() print('drain failed, trying again') time.sleep(1) self.reopen() def trysend(self): for penguin in pipeline: print('fill', penguin.url) try: self.c.request("GET", penguin.url) self.c._HTTPConnection__state = _CS_IDLE penguin.response = self.c.response_class(self.c.sock, method="GET") # begin LATER so we can send multiple requests w/out response headers except BadStatusLine: return False except HTTPException as e: die(diemessage + ' because of a ' + repr(e)) return True def send(self): if self.sent: return print('filling pipeline...', len(self)) while self.trysend() is not True: self.c.close() print('derped resending') time.sleep(1) self.reopen() self.sent = True