<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import socket <EOL> import ssl <EOL> import time <EOL> from tornado . escape import native_str , parse_qs_bytes <EOL> from tornado import httputil <EOL> from tornado import iostream <EOL> from tornado . log import gen_log <EOL> from tornado import netutil <EOL> from tornado . tcpserver import TCPServer <EOL> from tornado import stack_context <EOL> from tornado . util import bytes_type <EOL> try : <EOL> import Cookie <EOL> except ImportError : <EOL> import http . cookies as Cookie <EOL> class HTTPServer ( TCPServer ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , request_callback , no_keep_alive = False , io_loop = None , <EOL> xheaders = False , ssl_options = None , protocol = None , ** kwargs ) : <EOL> self . request_callback = request_callback <EOL> self . no_keep_alive = no_keep_alive <EOL> self . xheaders = xheaders <EOL> self . protocol = protocol <EOL> TCPServer . __init__ ( self , io_loop = io_loop , ssl_options = ssl_options , <EOL> ** kwargs ) <EOL> def handle_stream ( self , stream , address ) : <EOL> HTTPConnection ( stream , address , self . request_callback , <EOL> self . no_keep_alive , self . xheaders , self . protocol ) <EOL> class _BadRequestException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HTTPConnection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream , address , request_callback , no_keep_alive = False , <EOL> xheaders = False , protocol = None ) : <EOL> self . stream = stream <EOL> self . address = address <EOL> self . address_family = stream . socket . family <EOL> self . request_callback = request_callback <EOL> self . no_keep_alive = no_keep_alive <EOL> self . xheaders = xheaders <EOL> self . protocol = protocol <EOL> self . _clear_request_state ( ) <EOL> self . _header_callback = stack_context . wrap ( self . _on_headers ) <EOL> self . stream . set_close_callback ( self . _on_connection_close ) <EOL> self . stream . read_until ( b"<STR_LIT>" , self . _header_callback ) <EOL> def _clear_request_state ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _request = None <EOL> self . _request_finished = False <EOL> self . _write_callback = None <EOL> self . _close_callback = None <EOL> def set_close_callback ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _close_callback = stack_context . wrap ( callback ) <EOL> def _on_connection_close ( self ) : <EOL> if self . _close_callback is not None : <EOL> callback = self . _close_callback <EOL> self . _close_callback = None <EOL> callback ( ) <EOL> self . _header_callback = None <EOL> self . _clear_request_state ( ) <EOL> def close ( self ) : <EOL> self . stream . close ( ) <EOL> self . _header_callback = None <EOL> self . _clear_request_state ( ) <EOL> def write ( self , chunk , callback = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . stream . closed ( ) : <EOL> self . _write_callback = stack_context . wrap ( callback ) <EOL> self . stream . write ( chunk , self . _on_write_complete ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _request_finished = True <EOL> self . stream . set_nodelay ( True ) <EOL> if not self . stream . writing ( ) : <EOL> self . _finish_request ( ) <EOL> def _on_write_complete ( self ) : <EOL> if self . _write_callback is not None : <EOL> callback = self . _write_callback <EOL> self . _write_callback = None <EOL> callback ( ) <EOL> if self . _request_finished and not self . stream . writing ( ) : <EOL> self . _finish_request ( ) <EOL> def _finish_request ( self ) : <EOL> if self . no_keep_alive or self . _request is None : <EOL> disconnect = True <EOL> else : <EOL> connection_header = self . _request . headers . get ( "<STR_LIT>" ) <EOL> if connection_header is not None : <EOL> connection_header = connection_header . lower ( ) <EOL> if self . _request . supports_http_1_1 ( ) : <EOL> disconnect = connection_header == "<STR_LIT>" <EOL> elif ( "<STR_LIT>" in self . _request . headers <EOL> or self . _request . method in ( "<STR_LIT>" , "<STR_LIT:GET>" ) ) : <EOL> disconnect = connection_header != "<STR_LIT>" <EOL> else : <EOL> disconnect = True <EOL> self . _clear_request_state ( ) <EOL> if disconnect : <EOL> self . close ( ) <EOL> return <EOL> try : <EOL> self . stream . read_until ( b"<STR_LIT>" , self . _header_callback ) <EOL> self . stream . set_nodelay ( False ) <EOL> except iostream . StreamClosedError : <EOL> self . close ( ) <EOL> def _on_headers ( self , data ) : <EOL> try : <EOL> data = native_str ( data . decode ( '<STR_LIT>' ) ) <EOL> eol = data . find ( "<STR_LIT:\r\n>" ) <EOL> start_line = data [ : eol ] <EOL> try : <EOL> method , uri , version = start_line . split ( "<STR_LIT:U+0020>" ) <EOL> except ValueError : <EOL> raise _BadRequestException ( "<STR_LIT>" ) <EOL> if not version . startswith ( "<STR_LIT>" ) : <EOL> raise _BadRequestException ( "<STR_LIT>" ) <EOL> try : <EOL> headers = httputil . HTTPHeaders . parse ( data [ eol : ] ) <EOL> except ValueError : <EOL> raise _BadRequestException ( "<STR_LIT>" ) <EOL> if self . address_family in ( socket . AF_INET , socket . AF_INET6 ) : <EOL> remote_ip = self . address [ <NUM_LIT:0> ] <EOL> else : <EOL> remote_ip = '<STR_LIT>' <EOL> self . _request = HTTPRequest ( <EOL> connection = self , method = method , uri = uri , version = version , <EOL> headers = headers , remote_ip = remote_ip , protocol = self . protocol ) <EOL> content_length = headers . get ( "<STR_LIT>" ) <EOL> if content_length : <EOL> content_length = int ( content_length ) <EOL> if content_length > self . stream . max_buffer_size : <EOL> raise _BadRequestException ( "<STR_LIT>" ) <EOL> if headers . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> self . stream . write ( b"<STR_LIT>" ) <EOL> self . stream . read_bytes ( content_length , self . _on_request_body ) <EOL> return <EOL> self . request_callback ( self . _request ) <EOL> except _BadRequestException as e : <EOL> gen_log . info ( "<STR_LIT>" , <EOL> self . address [ <NUM_LIT:0> ] , e ) <EOL> self . close ( ) <EOL> return <EOL> def _on_request_body ( self , data ) : <EOL> self . _request . body = data <EOL> if self . _request . method in ( "<STR_LIT:POST>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> httputil . parse_body_arguments ( <EOL> self . _request . headers . get ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) , data , <EOL> self . _request . arguments , self . _request . files ) <EOL> self . request_callback ( self . _request ) <EOL> class HTTPRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method , uri , version = "<STR_LIT>" , headers = None , <EOL> body = None , remote_ip = None , protocol = None , host = None , <EOL> files = None , connection = None ) : <EOL> self . method = method <EOL> self . uri = uri <EOL> self . version = version <EOL> self . headers = headers or httputil . HTTPHeaders ( ) <EOL> self . body = body or "<STR_LIT>" <EOL> self . remote_ip = remote_ip <EOL> if protocol : <EOL> self . protocol = protocol <EOL> elif connection and isinstance ( connection . stream , <EOL> iostream . SSLIOStream ) : <EOL> self . protocol = "<STR_LIT>" <EOL> else : <EOL> self . protocol = "<STR_LIT:http>" <EOL> if connection and connection . xheaders : <EOL> ip = self . headers . get ( "<STR_LIT>" , self . remote_ip ) <EOL> ip = ip . split ( '<STR_LIT:U+002C>' ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> ip = self . headers . get ( <EOL> "<STR_LIT>" , ip ) <EOL> if netutil . is_valid_ip ( ip ) : <EOL> self . remote_ip = ip <EOL> proto = self . headers . get ( <EOL> "<STR_LIT>" , self . headers . get ( "<STR_LIT>" , self . protocol ) ) <EOL> if proto in ( "<STR_LIT:http>" , "<STR_LIT>" ) : <EOL> self . protocol = proto <EOL> self . host = host or self . headers . get ( "<STR_LIT>" ) or "<STR_LIT:127.0.0.1>" <EOL> self . files = files or { } <EOL> self . connection = connection <EOL> self . _start_time = time . time ( ) <EOL> self . _finish_time = None <EOL> self . path , sep , self . query = uri . partition ( '<STR_LIT:?>' ) <EOL> self . arguments = parse_qs_bytes ( self . query , keep_blank_values = True ) <EOL> def supports_http_1_1 ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . version == "<STR_LIT>" <EOL> @ property <EOL> def cookies ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _cookies = Cookie . SimpleCookie ( ) <EOL> if "<STR_LIT>" in self . headers : <EOL> try : <EOL> self . _cookies . load ( <EOL> native_str ( self . headers [ "<STR_LIT>" ] ) ) <EOL> except Exception : <EOL> self . _cookies = { } <EOL> return self . _cookies <EOL> def write ( self , chunk , callback = None ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( chunk , bytes_type ) <EOL> self . connection . write ( chunk , callback = callback ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> self . connection . finish ( ) <EOL> self . _finish_time = time . time ( ) <EOL> def full_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . protocol + "<STR_LIT>" + self . host + self . uri <EOL> def request_time ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _finish_time is None : <EOL> return time . time ( ) - self . _start_time <EOL> else : <EOL> return self . _finish_time - self . _start_time <EOL> def get_ssl_certificate ( self , binary_form = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . connection . stream . socket . getpeercert ( <EOL> binary_form = binary_form ) <EOL> except ssl . SSLError : <EOL> return None <EOL> def __repr__ ( self ) : <EOL> attrs = ( "<STR_LIT>" , "<STR_LIT:host>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:version>" , "<STR_LIT>" ) <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( n , getattr ( self , n ) ) for n in attrs ] ) <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , args , dict ( self . headers ) ) </s>
