def connection_handler(socket, address): hostname = peek_http_host(socket) if not hostname: send_http_error(socket, 'No hostname', '400 Bad Request') return if hostname == Tunnel.domain_suffix: meta.server.process_request((socket, address)) return tunnel = Tunnel.get_by_hostname(hostname) if not tunnel: send_http_error(socket, 'No tunnel for {0}'.format(hostname), '410 Gone') return conn, proxy_used = tunnel.pop_proxy_conn(timeout=2) if not conn: send_http_error(socket, 'No proxy connections', '502 Bad Gateway') return protocol.send_message(conn, protocol.proxy_reply()) pool = util.join_sockets(conn, socket) proxy_used.send(pool) logging.debug("popped connection:\"{0}\" for frontend:\"{1}\"".format( tunnel.name, hostname)) pool.waitall()
def connection_handler(socket, address): hostname = peek_http_host(socket) if not hostname: send_http_error(socket, "No hostname", "400 Bad Request") return if hostname == Tunnel.domain_suffix: meta.server.process_request((socket, address)) return tunnel = Tunnel.get_by_hostname(hostname) if not tunnel: send_http_error(socket, "No tunnel for {0}".format(hostname), "410 Gone") return conn, proxy_used = tunnel.pop_proxy_conn(timeout=2) if not conn: send_http_error(socket, "No proxy connections", "502 Bad Gateway") return protocol.send_message(conn, protocol.proxy_reply()) pool = util.join_sockets(conn, socket) proxy_used.send(pool) logging.debug('popped connection:"{0}" for frontend:"{1}"'.format(tunnel.name, hostname)) pool.waitall()
def frontend_handler(socket, address): hostname = '' hostheader = re.compile('host: ([^\(\);:,<>]+)', re.I) # Peek up to 512 bytes into data for the Host header for n in [128, 256, 512]: bytes = socket.recv(n, MSG_PEEK) if not bytes: socket.close() return for line in bytes.split('\r\n'): match = hostheader.match(line) if match: hostname = match.group(1) if hostname: break hostname = hostname.split(':')[0] if not hostname: logging.debug("!frontend: no hostname, closing") socket.close() return if hostname.startswith('_version.'): data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1} """.format(len(VERSION), VERSION).strip() socket.sendall(data) socket.close() logging.debug("version request") return if hostname.startswith('_backend.'): port = os.environ.get('DOTCLOUD_SERVER_BACKEND_PORT', Tunnel.backend_port) data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1} """.format(len(str(port)), port).strip() socket.sendall(data) socket.close() return tunnel = Tunnel.get_by_hostname(hostname) if not tunnel: logging.debug("!frontend: no tunnel, closing ({0})".format(hostname)) socket.close() return conn = tunnel.pop_proxy_backend(timeout=2) if not conn: logging.debug("!frontend: no backend, closing") socket.close() return protocol.send_message(conn, protocol.proxy_reply()) pool = util.join_sockets(conn, socket) logging.debug("popped backend:\"{0}\" for frontend:\"{1}\"".format( tunnel.name, hostname)) pool.waitall()
def frontend_handler(socket, address): hostname = '' hostheader = re.compile('host: ([^\(\);:,<>]+)', re.I) # Peek up to 512 bytes into data for the Host header for n in [128, 256, 512]: bytes = socket.recv(n, MSG_PEEK) if not bytes: socket.close() return for line in bytes.split('\r\n'): match = hostheader.match(line) if match: hostname = match.group(1) if hostname: break hostname = hostname.split(':')[0] if not hostname: logging.debug("!frontend: no hostname, closing") socket.close() return if hostname.startswith('_version.'): data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1} """.format(len(VERSION), VERSION).strip() socket.sendall(data) socket.close() logging.debug("version request") return if hostname.startswith('_backend.'): port = os.environ.get('DOTCLOUD_SERVER_BACKEND_PORT', Tunnel.backend_port) data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1} """.format(len(str(port)), port).strip() socket.sendall(data) socket.close() return tunnel = Tunnel.get_by_hostname(hostname) if not tunnel: logging.debug("!frontend: no tunnel, closing ({0})".format( hostname)) socket.close() return conn = tunnel.pop_proxy_backend(timeout=2) if not conn: logging.debug("!frontend: no backend, closing") socket.close() return protocol.send_message(conn, protocol.proxy_reply()) pool = util.join_sockets(conn, socket) logging.debug("popped backend:\"{0}\" for frontend:\"{1}\"".format( tunnel.name, hostname)) pool.waitall()
def connection_handler(socket, address): host = peek_http_host(socket) hostname = host.split(':')[0] if not hostname: logging.debug("!no hostname, closing") socket.close() return if hostname.startswith('_version.'): send_http_response(socket, __version__) socket.close() logging.debug("version request from {0}".format(address[0])) return if hostname.startswith('_backend.'): port = os.environ.get('DOTCLOUD_SERVER_BACKEND_PORT', Tunnel.backend_port) send_http_response(socket, port) socket.close() return if hostname.startswith('_metrics.'): content = json.dumps(metrics.dump_metrics(), sort_keys=True, indent=2, separators=(',', ': ')) send_http_response(socket, content) socket.close() logging.debug("metrics request from {0}".format(address[0])) return tunnel = Tunnel.get_by_hostname(hostname) if not tunnel: logging.debug("!no tunnel, closing ({0})".format(hostname)) socket.close() return conn, proxy_used = tunnel.pop_proxy_conn(timeout=2) if not conn: logging.debug("!no proxy connection, closing") socket.close() return protocol.send_message(conn, protocol.proxy_reply()) pool = util.join_sockets(conn, socket) proxy_used.send(pool) logging.debug("popped connection:\"{0}\" for frontend:\"{1}\"".format( tunnel.name, hostname)) pool.waitall()
def connection_handler(socket, address): host = peek_http_host(socket) hostname = host.split(':')[0] if not hostname: logging.debug("!no hostname, closing") socket.close() return if hostname.startswith('_version.'): send_http_response(socket, __version__) socket.close() logging.debug("version request from {0}".format(address[0])) return if hostname.startswith('_backend.'): port = os.environ.get('DOTCLOUD_SERVER_BACKEND_PORT', Tunnel.backend_port) send_http_response(socket, port) socket.close() return if hostname.startswith('_metrics.'): content = json.dumps(metrics.dump_metrics(), sort_keys=True, indent=2, separators=(',', ': ')) send_http_response(socket, content) socket.close() logging.debug("metrics request from {0}".format(address[0])) return tunnel = Tunnel.get_by_hostname(hostname) if not tunnel: logging.debug("!no tunnel, closing ({0})".format( hostname)) socket.close() return conn, proxy_used = tunnel.pop_proxy_conn(timeout=2) if not conn: logging.debug("!no proxy connection, closing") socket.close() return protocol.send_message(conn, protocol.proxy_reply()) pool = util.join_sockets(conn, socket) proxy_used.send(pool) logging.debug("popped connection:\"{0}\" for frontend:\"{1}\"".format( tunnel.name, hostname)) pool.waitall()