def handle_http_connection(self, conn): # Read the trailing HTTP request and process it with protocol_switcher. # We can't rely on ioloop to trigger read because it has been already # triggered for SSL handshake. payload = yield conn.stream.read_bytes(1024, partial=True) logger.debug("Received %r", payload[:128]) # Simulate conn._read_message side effect. This is required by # HTTP1Connection.write_headers() conn._request_start_line = parse_request_start_line('GET / HTTP/1.1') try: start_line, headers = parse_http_headers(payload) conn._request_start_line = start_line request = HTTPRequest( connection=conn, headers=headers, start_line=start_line, ) request.config = self.request_callback.config response = protocol_switcher(request) except Exception as e: logger.error("Failed to switch to HTTPS: %s", e) response = HTTPResponse( request=object(), code=500, headers=HTTPHeaders({'Content-Length': '0'}), effective_url='https://useless_effective_url') yield conn.write_headers( start_line=ResponseStartLine( 'HTTP/1.1', response.code, response.reason, ), headers=response.headers, )
def handle_http_connection(self, conn): # Read the trailing HTTP request and process it with protocol_switcher. # We can't rely on ioloop to trigger read because it has been already # triggered for SSL handshake. addr, port = conn.stream.socket.getsockname() try: # This is not blocking. Just read available bytes. payload = conn.stream.socket.recv(1024) except Exception: # Exception includes EWOULDBLOCK, when no bytes are available. In # this case just skip. payload = "" else: logger.debug("Received %r", payload[:128]) # Simulate conn._read_message side effect. This is required by # HTTP1Connection.write_headers() conn._request_start_line = parse_request_start_line('GET / HTTP/1.1') conn._request_headers = HTTPHeaders() try: start_line, headers = parse_http_headers(payload) conn._request_start_line = start_line request = HTTPRequest( connection=conn, headers=headers, start_line=start_line, ) request.config = self.request_callback.config response = protocol_switcher(request) except Exception as e: logger.error("Failed to switch to HTTPS: %s", e) response = HTTPResponse( request=object(), code=500, headers=HTTPHeaders({'Content-Length': '0'}), effective_url='https://useless_effective_url') yield conn.write_headers( start_line=ResponseStartLine( 'HTTP/1.1', response.code, response.reason, ), headers=response.headers, )