<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import socket <EOL> from tornado . escape import native_str <EOL> from tornado . http1connection import HTTP1ServerConnection , HTTP1ConnectionParameters <EOL> from tornado import gen <EOL> from tornado import httputil <EOL> from tornado import iostream <EOL> from tornado import netutil <EOL> from tornado . tcpserver import TCPServer <EOL> from tornado . util import Configurable <EOL> class HTTPServer ( TCPServer , Configurable , <EOL> httputil . HTTPServerConnectionDelegate ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> pass <EOL> def initialize ( self , request_callback , no_keep_alive = False , io_loop = None , <EOL> xheaders = False , ssl_options = None , protocol = None , <EOL> decompress_request = False , <EOL> chunk_size = None , max_header_size = None , <EOL> idle_connection_timeout = None , body_timeout = None , <EOL> max_body_size = None , max_buffer_size = None ) : <EOL> self . request_callback = request_callback <EOL> self . no_keep_alive = no_keep_alive <EOL> self . xheaders = xheaders <EOL> self . protocol = protocol <EOL> self . conn_params = HTTP1ConnectionParameters ( <EOL> decompress = decompress_request , <EOL> chunk_size = chunk_size , <EOL> max_header_size = max_header_size , <EOL> header_timeout = idle_connection_timeout or <NUM_LIT> , <EOL> max_body_size = max_body_size , <EOL> body_timeout = body_timeout ) <EOL> TCPServer . __init__ ( self , io_loop = io_loop , ssl_options = ssl_options , <EOL> max_buffer_size = max_buffer_size , <EOL> read_chunk_size = chunk_size ) <EOL> self . _connections = set ( ) <EOL> @ classmethod <EOL> def configurable_base ( cls ) : <EOL> return HTTPServer <EOL> @ classmethod <EOL> def configurable_default ( cls ) : <EOL> return HTTPServer <EOL> @ gen . coroutine <EOL> def close_all_connections ( self ) : <EOL> while self . _connections : <EOL> conn = next ( iter ( self . _connections ) ) <EOL> yield conn . close ( ) <EOL> def handle_stream ( self , stream , address ) : <EOL> context = _HTTPRequestContext ( stream , address , <EOL> self . protocol ) <EOL> conn = HTTP1ServerConnection ( <EOL> stream , self . conn_params , context ) <EOL> self . _connections . add ( conn ) <EOL> conn . start_serving ( self ) <EOL> def start_request ( self , server_conn , request_conn ) : <EOL> return _ServerRequestAdapter ( self , server_conn , request_conn ) <EOL> def on_close ( self , server_conn ) : <EOL> self . _connections . remove ( server_conn ) <EOL> class _HTTPRequestContext ( object ) : <EOL> def __init__ ( self , stream , address , protocol ) : <EOL> self . address = address <EOL> if stream . socket is not None : <EOL> self . address_family = stream . socket . family <EOL> else : <EOL> self . address_family = None <EOL> if ( self . address_family in ( socket . AF_INET , socket . AF_INET6 ) and <EOL> address is not None ) : <EOL> self . remote_ip = address [ <NUM_LIT:0> ] <EOL> else : <EOL> self . remote_ip = '<STR_LIT>' <EOL> if protocol : <EOL> self . protocol = protocol <EOL> elif isinstance ( stream , iostream . SSLIOStream ) : <EOL> self . protocol = "<STR_LIT>" <EOL> else : <EOL> self . protocol = "<STR_LIT:http>" <EOL> self . _orig_remote_ip = self . remote_ip <EOL> self . _orig_protocol = self . protocol <EOL> def __str__ ( self ) : <EOL> if self . address_family in ( socket . AF_INET , socket . AF_INET6 ) : <EOL> return self . remote_ip <EOL> elif isinstance ( self . address , bytes ) : <EOL> return native_str ( self . address ) <EOL> else : <EOL> return str ( self . address ) <EOL> def _apply_xheaders ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> ip = headers . get ( "<STR_LIT>" , self . remote_ip ) <EOL> ip = ip . split ( '<STR_LIT:U+002C>' ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> ip = headers . get ( "<STR_LIT>" , ip ) <EOL> if netutil . is_valid_ip ( ip ) : <EOL> self . remote_ip = ip <EOL> proto_header = headers . get ( <EOL> "<STR_LIT>" , headers . get ( "<STR_LIT>" , <EOL> self . protocol ) ) <EOL> if proto_header in ( "<STR_LIT:http>" , "<STR_LIT>" ) : <EOL> self . protocol = proto_header <EOL> def _unapply_xheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> self . remote_ip = self . _orig_remote_ip <EOL> self . protocol = self . _orig_protocol <EOL> class _ServerRequestAdapter ( httputil . HTTPMessageDelegate ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server , server_conn , request_conn ) : <EOL> self . server = server <EOL> self . connection = request_conn <EOL> self . request = None <EOL> if isinstance ( server . request_callback , <EOL> httputil . HTTPServerConnectionDelegate ) : <EOL> self . delegate = server . request_callback . start_request ( <EOL> server_conn , request_conn ) <EOL> self . _chunks = None <EOL> else : <EOL> self . delegate = None <EOL> self . _chunks = [ ] <EOL> def headers_received ( self , start_line , headers ) : <EOL> if self . server . xheaders : <EOL> self . connection . context . _apply_xheaders ( headers ) <EOL> if self . delegate is None : <EOL> self . request = httputil . HTTPServerRequest ( <EOL> connection = self . connection , start_line = start_line , <EOL> headers = headers ) <EOL> else : <EOL> return self . delegate . headers_received ( start_line , headers ) <EOL> def data_received ( self , chunk ) : <EOL> if self . delegate is None : <EOL> self . _chunks . append ( chunk ) <EOL> else : <EOL> return self . delegate . data_received ( chunk ) <EOL> def finish ( self ) : <EOL> if self . delegate is None : <EOL> self . request . body = b'<STR_LIT>' . join ( self . _chunks ) <EOL> self . request . _parse_body ( ) <EOL> self . server . request_callback ( self . request ) <EOL> else : <EOL> self . delegate . finish ( ) <EOL> self . _cleanup ( ) <EOL> def on_connection_close ( self ) : <EOL> if self . delegate is None : <EOL> self . _chunks = None <EOL> else : <EOL> self . delegate . on_connection_close ( ) <EOL> self . _cleanup ( ) <EOL> def _cleanup ( self ) : <EOL> if self . server . xheaders : <EOL> self . connection . context . _unapply_xheaders ( ) <EOL> HTTPRequest = httputil . HTTPServerRequest </s>
