def pipe(self): self.chunks = [] headers = self.request.headers LOG.debug('reqs: [%s] %s', self.request.method, self.request.uri) if tornado_options.debug: map(lambda x: LOG.debug('\t=> %s:%s', *x), headers.iteritems()) request_id = db_api.add_request(self.request) try: # body must be none for GET request if self.request.method == 'GET': body = None else: body = self.request.body # replace proxy-connection header with connection if 'Proxy-Connection' in headers: headers['Connection'] = headers.pop('Proxy-Connection') if tornado_options.debug: map(lambda x: LOG.debug('\t|> %s:%s', *x), headers.iteritems()) uri, host = self.remap_hostname(self.request.uri) if host: headers['Host'] = host req = HTTPRequest(uri, method=self.request.method, body=body, headers=headers, streaming_callback=self.data_out, header_callback=self.header_out, follow_redirects=False, decompress_response=False, allow_nonstandard_methods=True) http_client = AsyncHTTPClient(max_clients=512) yield http_client.fetch(req, raise_error=False) except Exception as e: self.set_status(502) self.write('Proxy Server Error: %s' % e) LOG.warn('Proxy Server Error: %s' % e) finally: LOG.debug('done: [%s] %s', self.request.method, self.request.uri) status_code, headers, body = self.current_response() db_api.add_response(request_id, status_code, headers, body)
def ssl_passthrough(self): try: request_id = db_api.add_request(self.request) db_api.add_response(request_id, 000, {}, '') local = self.request.connection.stream s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) remote = tornado.iostream.IOStream(s) host, port = self.request.uri.split(':') LOG.debug('connecting to %s:%s' % (host, port)) yield remote.connect((host, int(port))) LOG.debug('tunnel %s:%s established' % (host, port)) self.ssl_pipe(local, remote) local.write(b'HTTP/1.0 200 Connection established\r\n\r\n') except (tornado.iostream.StreamClosedError, tornado.iostream.UnsatisfiableReadError) as e: LOG.debug('remote connection closed: %s' % (e)) local.close() except Exception as e: LOG.warn('unknown error: %s' % (e)) local.close() raise