def on_before_handle(self, protocol, source, dest, to_backend): # consume the socket and hang data = get_data(source) while data: data = get_data(source) while True: gevent.sleep(1.)
def on_before_handle(self, protocol, source, dest, to_backend): if self.current < self.option('warmup'): self.current += 1 return super(Error, self).on_before_handle(protocol, source, dest, to_backend) # read the data data = get_data(source) if not data: return False # error out if protocol.name in 'http' and to_backend: # we'll just send back a random error if self.option('inject'): self.inject(dest, to_backend, data, http=True) return True, True else: source.sendall(random_http_error()) source.close() source._closed = True return False, False if self.option('inject'): self.inject(dest, to_backend, data, ) return True, True else: if not to_backend: # XXX find how to handle errors (which errors should we send) # depends on the protocol dest.sendall(os.urandom(1000)) else: # sending the data tp the backend dest.sendall(data) return True, True return True, True
def _get_data(self, sock, buffer=None): if buffer is None: buffer = self.option('buffer') return get_data(sock, buffer)
def handle(self, client_sock, address): client_sock.setblocking(0) dest = None try: # getting the query data = get_data(client_sock) if not data: return # finding out what backend we want data = data.split('\r\n') PATH = data[0].split() elmts = PATH[1].split('/') try: port = int(elmts[1]) except ValueError: client_sock.sendall(http_error(404, 'Not Found')) return NEW_PATH = '/'.join(elmts[0:1] + elmts[2:]) data[0] = ' '.join(PATH[0:1] + [NEW_PATH] + PATH[2:]) try: dest = create_connection((self.host, port)) except error: client_sock.sendall(http_error(503, '%d not responding' % port)) return # sending it to the backend dest.sendall('\r\n'.join(data)) # Receiving the response buffer = get_data(dest) client_sock.sendall(buffer) # Reading the HTTP Headers while EOH not in buffer: data = get_data(dest) buffer += data client_sock.sendall(data) # content-length header - to see if we need to suck more # data. match = RE_LEN.search(buffer) if match: resp_len = int(match.group(1)) left_to_read = resp_len - len(buffer) if left_to_read > 0: for chunk in chunked(left_to_read, 1024): data = get_data(dest, chunk) buffer += data client_sock.sendall(data) else: # embarrassing... # just sucking until recv() returns '' while True: data = get_data(dest) if data == '': break client_sock.sendall(data) finally: client_sock.close() if dest is not None: dest.close()