<s> """<STR_LIT>""" <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> import os <EOL> import Queue <EOL> import re <EOL> quoted_slash = re . compile ( "<STR_LIT>" ) <EOL> import rfc822 <EOL> import socket <EOL> import sys <EOL> if '<STR_LIT>' in sys . platform and not hasattr ( socket , '<STR_LIT>' ) : <EOL> socket . IPPROTO_IPV6 = <NUM_LIT> <EOL> try : <EOL> import cStringIO as StringIO <EOL> except ImportError : <EOL> import StringIO <EOL> _fileobject_uses_str_type = isinstance ( socket . _fileobject ( None ) . _rbuf , basestring ) <EOL> import threading <EOL> import time <EOL> import traceback <EOL> from urllib import unquote <EOL> from urlparse import urlparse <EOL> import warnings <EOL> import errno <EOL> def plat_specific_errors ( * errnames ) : <EOL> """<STR_LIT>""" <EOL> errno_names = dir ( errno ) <EOL> nums = [ getattr ( errno , k ) for k in errnames if k in errno_names ] <EOL> return dict . fromkeys ( nums ) . keys ( ) <EOL> socket_error_eintr = plat_specific_errors ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> socket_errors_to_ignore = plat_specific_errors ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> ) <EOL> socket_errors_to_ignore . append ( "<STR_LIT>" ) <EOL> socket_errors_to_ignore . append ( "<STR_LIT>" ) <EOL> socket_errors_nonblocking = plat_specific_errors ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> comma_separated_headers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def read_headers ( rfile , hdict = None ) : <EOL> """<STR_LIT>""" <EOL> if hdict is None : <EOL> hdict = { } <EOL> while True : <EOL> line = rfile . readline ( ) <EOL> if not line : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if line == CRLF : <EOL> break <EOL> if not line . endswith ( CRLF ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if line [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> v = line . strip ( ) <EOL> else : <EOL> try : <EOL> k , v = line . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> print "<STR_LIT>" <EOL> continue <EOL> k = k . strip ( ) . title ( ) <EOL> v = v . strip ( ) <EOL> hname = k <EOL> if k in comma_separated_headers : <EOL> existing = hdict . get ( hname ) <EOL> if existing : <EOL> v = "<STR_LIT:U+002CU+0020>" . join ( ( existing , v ) ) <EOL> hdict [ hname ] = v <EOL> return hdict <EOL> class MaxSizeExceeded ( Exception ) : <EOL> pass <EOL> class SizeCheckWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rfile , maxlen ) : <EOL> self . rfile = rfile <EOL> self . maxlen = maxlen <EOL> self . bytes_read = <NUM_LIT:0> <EOL> def _check_length ( self ) : <EOL> if self . maxlen and self . bytes_read > self . maxlen : <EOL> raise MaxSizeExceeded ( ) <EOL> def read ( self , size = None ) : <EOL> data = self . rfile . read ( size ) <EOL> self . bytes_read += len ( data ) <EOL> self . _check_length ( ) <EOL> return data <EOL> def readline ( self , size = None ) : <EOL> if size is not None : <EOL> data = self . rfile . readline ( size ) <EOL> self . bytes_read += len ( data ) <EOL> self . _check_length ( ) <EOL> return data <EOL> res = [ ] <EOL> while True : <EOL> data = self . rfile . readline ( <NUM_LIT> ) <EOL> self . bytes_read += len ( data ) <EOL> self . _check_length ( ) <EOL> res . append ( data ) <EOL> if len ( data ) < <NUM_LIT> or data [ - <NUM_LIT:1> : ] == "<STR_LIT:\n>" : <EOL> return '<STR_LIT>' . join ( res ) <EOL> def readlines ( self , sizehint = <NUM_LIT:0> ) : <EOL> total = <NUM_LIT:0> <EOL> lines = [ ] <EOL> line = self . readline ( ) <EOL> while line : <EOL> lines . append ( line ) <EOL> total += len ( line ) <EOL> if <NUM_LIT:0> < sizehint <= total : <EOL> break <EOL> line = self . readline ( ) <EOL> return lines <EOL> def close ( self ) : <EOL> self . rfile . close ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> data = self . rfile . next ( ) <EOL> self . bytes_read += len ( data ) <EOL> self . _check_length ( ) <EOL> return data <EOL> class KnownLengthRFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rfile , content_length ) : <EOL> self . rfile = rfile <EOL> self . remaining = content_length <EOL> def read ( self , size = None ) : <EOL> if self . remaining == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> if size is None : <EOL> size = self . remaining <EOL> else : <EOL> size = min ( size , self . remaining ) <EOL> data = self . rfile . read ( size ) <EOL> self . remaining -= len ( data ) <EOL> return data <EOL> def readline ( self , size = None ) : <EOL> if self . remaining == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> if size is None : <EOL> size = self . remaining <EOL> else : <EOL> size = min ( size , self . remaining ) <EOL> data = self . rfile . readline ( size ) <EOL> self . remaining -= len ( data ) <EOL> return data <EOL> def readlines ( self , sizehint = <NUM_LIT:0> ) : <EOL> total = <NUM_LIT:0> <EOL> lines = [ ] <EOL> line = self . readline ( sizehint ) <EOL> while line : <EOL> lines . append ( line ) <EOL> total += len ( line ) <EOL> if <NUM_LIT:0> < sizehint <= total : <EOL> break <EOL> line = self . readline ( sizehint ) <EOL> return lines <EOL> def close ( self ) : <EOL> self . rfile . close ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> data = next ( self . rfile ) <EOL> self . remaining -= len ( data ) <EOL> return data <EOL> class MaxSizeExceeded ( Exception ) : <EOL> pass <EOL> class ChunkedRFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rfile , maxlen , bufsize = <NUM_LIT> ) : <EOL> self . rfile = rfile <EOL> self . maxlen = maxlen <EOL> self . bytes_read = <NUM_LIT:0> <EOL> self . buffer = '<STR_LIT>' <EOL> self . bufsize = bufsize <EOL> self . closed = False <EOL> def _fetch ( self ) : <EOL> if self . closed : <EOL> return <EOL> line = self . rfile . readline ( ) <EOL> self . bytes_read += len ( line ) <EOL> if self . maxlen and self . bytes_read > self . maxlen : <EOL> raise MaxSizeExceeded ( "<STR_LIT>" , self . maxlen ) <EOL> line = line . strip ( ) . split ( "<STR_LIT:;>" , <NUM_LIT:1> ) <EOL> try : <EOL> chunk_size = line . pop ( <NUM_LIT:0> ) <EOL> chunk_size = int ( chunk_size , <NUM_LIT:16> ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" + repr ( chunk_size ) ) <EOL> if chunk_size <= <NUM_LIT:0> : <EOL> self . closed = True <EOL> return <EOL> if self . maxlen and self . bytes_read + chunk_size > self . maxlen : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> chunk = self . rfile . read ( chunk_size ) <EOL> self . bytes_read += len ( chunk ) <EOL> self . buffer += chunk <EOL> crlf = self . rfile . read ( <NUM_LIT:2> ) <EOL> if crlf != CRLF : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" + repr ( crlf ) + "<STR_LIT:)>" ) <EOL> def read ( self , size = None ) : <EOL> data = '<STR_LIT>' <EOL> while True : <EOL> if size and len ( data ) >= size : <EOL> return data <EOL> if not self . buffer : <EOL> self . _fetch ( ) <EOL> if not self . buffer : <EOL> return data <EOL> if size : <EOL> remaining = size - len ( data ) <EOL> data += self . buffer [ : remaining ] <EOL> self . buffer = self . buffer [ remaining : ] <EOL> else : <EOL> data += self . buffer <EOL> def readline ( self , size = None ) : <EOL> data = '<STR_LIT>' <EOL> while True : <EOL> if size and len ( data ) >= size : <EOL> return data <EOL> if not self . buffer : <EOL> self . _fetch ( ) <EOL> if not self . buffer : <EOL> return data <EOL> newline_pos = self . buffer . find ( '<STR_LIT:\n>' ) <EOL> if size : <EOL> if newline_pos == - <NUM_LIT:1> : <EOL> remaining = size - len ( data ) <EOL> data += self . buffer [ : remaining ] <EOL> self . buffer = self . buffer [ remaining : ] <EOL> else : <EOL> remaining = min ( size - len ( data ) , newline_pos ) <EOL> data += self . buffer [ : remaining ] <EOL> self . buffer = self . buffer [ remaining : ] <EOL> else : <EOL> if newline_pos == - <NUM_LIT:1> : <EOL> data += self . buffer <EOL> else : <EOL> data += self . buffer [ : newline_pos ] <EOL> self . buffer = self . buffer [ newline_pos : ] <EOL> def readlines ( self , sizehint = <NUM_LIT:0> ) : <EOL> total = <NUM_LIT:0> <EOL> lines = [ ] <EOL> line = self . readline ( sizehint ) <EOL> while line : <EOL> lines . append ( line ) <EOL> total += len ( line ) <EOL> if <NUM_LIT:0> < sizehint <= total : <EOL> break <EOL> line = self . readline ( sizehint ) <EOL> return lines <EOL> def read_trailer_lines ( self ) : <EOL> if not self . closed : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> while True : <EOL> line = self . rfile . readline ( ) <EOL> if not line : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . bytes_read += len ( line ) <EOL> if self . maxlen and self . bytes_read > self . maxlen : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> if line == CRLF : <EOL> break <EOL> if not line . endswith ( CRLF ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> yield line <EOL> def close ( self ) : <EOL> self . rfile . close ( ) <EOL> def __iter__ ( self ) : <EOL> total = <NUM_LIT:0> <EOL> line = self . readline ( sizehint ) <EOL> while line : <EOL> yield line <EOL> total += len ( line ) <EOL> if <NUM_LIT:0> < sizehint <= total : <EOL> break <EOL> line = self . readline ( sizehint ) <EOL> class HTTPRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server , conn ) : <EOL> self . server = server <EOL> self . conn = conn <EOL> self . ready = False <EOL> self . started_request = False <EOL> self . scheme = "<STR_LIT:http>" <EOL> if self . server . ssl_adapter is not None : <EOL> self . scheme = "<STR_LIT>" <EOL> self . response_protocol = '<STR_LIT>' <EOL> self . inheaders = { } <EOL> self . status = "<STR_LIT>" <EOL> self . outheaders = [ ] <EOL> self . sent_headers = False <EOL> self . close_connection = False <EOL> self . chunked_read = False <EOL> self . chunked_write = False <EOL> def parse_request ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rfile = SizeCheckWrapper ( self . conn . rfile , <EOL> self . server . max_request_header_size ) <EOL> try : <EOL> self . read_request_line ( ) <EOL> except MaxSizeExceeded : <EOL> self . simple_response ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> success = self . read_request_headers ( ) <EOL> except MaxSizeExceeded : <EOL> self . simple_response ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> else : <EOL> if not success : <EOL> return <EOL> self . ready = True <EOL> def read_request_line ( self ) : <EOL> request_line = self . rfile . readline ( ) <EOL> self . started_request = True <EOL> if not request_line : <EOL> self . ready = False <EOL> return <EOL> if request_line == CRLF : <EOL> request_line = self . rfile . readline ( ) <EOL> if not request_line : <EOL> self . ready = False <EOL> return <EOL> if not request_line . endswith ( CRLF ) : <EOL> self . simple_response ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> method , uri , req_protocol = request_line . strip ( ) . split ( "<STR_LIT:U+0020>" , <NUM_LIT:2> ) <EOL> except ValueError : <EOL> self . simple_response ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> self . uri = uri <EOL> self . method = method <EOL> scheme , authority , path = self . parse_request_uri ( uri ) <EOL> if '<STR_LIT:#>' in path : <EOL> self . simple_response ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> return <EOL> if scheme : <EOL> self . scheme = scheme <EOL> qs = '<STR_LIT>' <EOL> if '<STR_LIT:?>' in path : <EOL> path , qs = path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> try : <EOL> atoms = [ unquote ( x ) for x in quoted_slash . split ( path ) ] <EOL> except ValueError , ex : <EOL> self . simple_response ( "<STR_LIT>" , ex . args [ <NUM_LIT:0> ] ) <EOL> return <EOL> path = "<STR_LIT>" . join ( atoms ) <EOL> self . path = path <EOL> self . qs = qs <EOL> rp = int ( req_protocol [ <NUM_LIT:5> ] ) , int ( req_protocol [ <NUM_LIT:7> ] ) <EOL> sp = int ( self . server . protocol [ <NUM_LIT:5> ] ) , int ( self . server . protocol [ <NUM_LIT:7> ] ) <EOL> if sp [ <NUM_LIT:0> ] != rp [ <NUM_LIT:0> ] : <EOL> self . simple_response ( "<STR_LIT>" ) <EOL> return <EOL> self . request_protocol = req_protocol <EOL> self . response_protocol = "<STR_LIT>" % min ( rp , sp ) <EOL> def read_request_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> read_headers ( self . rfile , self . inheaders ) <EOL> except ValueError , ex : <EOL> self . simple_response ( "<STR_LIT>" , ex . args [ <NUM_LIT:0> ] ) <EOL> return False <EOL> mrbs = self . server . max_request_body_size <EOL> if mrbs and int ( self . inheaders . get ( "<STR_LIT>" , <NUM_LIT:0> ) ) > mrbs : <EOL> self . simple_response ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return False <EOL> if self . response_protocol == "<STR_LIT>" : <EOL> if self . inheaders . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" : <EOL> self . close_connection = True <EOL> else : <EOL> if self . inheaders . get ( "<STR_LIT>" , "<STR_LIT>" ) != "<STR_LIT>" : <EOL> self . close_connection = True <EOL> te = None <EOL> if self . response_protocol == "<STR_LIT>" : <EOL> te = self . inheaders . get ( "<STR_LIT>" ) <EOL> if te : <EOL> te = [ x . strip ( ) . lower ( ) for x in te . split ( "<STR_LIT:U+002C>" ) if x . strip ( ) ] <EOL> if te : <EOL> for enc in te : <EOL> if enc == "<STR_LIT>" : <EOL> self . chunked_read = True <EOL> else : <EOL> self . simple_response ( "<STR_LIT>" ) <EOL> self . close_connection = True <EOL> return False <EOL> if self . inheaders . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" : <EOL> msg = self . server . protocol + "<STR_LIT>" <EOL> try : <EOL> self . conn . wfile . sendall ( msg ) <EOL> except socket . error , x : <EOL> if x . args [ <NUM_LIT:0> ] not in socket_errors_to_ignore : <EOL> raise <EOL> return True <EOL> def parse_request_uri ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> if uri == "<STR_LIT:*>" : <EOL> return None , None , uri <EOL> i = uri . find ( '<STR_LIT>' ) <EOL> if i > <NUM_LIT:0> and '<STR_LIT:?>' not in uri [ : i ] : <EOL> scheme , remainder = uri [ : i ] . lower ( ) , uri [ i + <NUM_LIT:3> : ] <EOL> authority , path = remainder . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> return scheme , authority , path <EOL> if uri . startswith ( '<STR_LIT:/>' ) : <EOL> return None , None , uri <EOL> else : <EOL> return None , uri , None <EOL> def respond ( self ) : <EOL> """<STR_LIT>""" <EOL> mrbs = self . server . max_request_body_size <EOL> if self . chunked_read : <EOL> self . rfile = ChunkedRFile ( self . conn . rfile , mrbs ) <EOL> else : <EOL> cl = int ( self . inheaders . get ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> if mrbs and mrbs < cl : <EOL> if not self . sent_headers : <EOL> self . simple_response ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> self . rfile = KnownLengthRFile ( self . conn . rfile , cl ) <EOL> self . server . gateway ( self ) . respond ( ) <EOL> if ( self . ready and not self . sent_headers ) : <EOL> self . sent_headers = True <EOL> self . send_headers ( ) <EOL> if self . chunked_write : <EOL> self . conn . wfile . sendall ( "<STR_LIT>" ) <EOL> def simple_response ( self , status , msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> status = str ( status ) <EOL> buf = [ "<STR_LIT>" % len ( msg ) , <EOL> "<STR_LIT>" ] <EOL> if status [ : <NUM_LIT:3> ] in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . close_connection = True <EOL> if self . response_protocol == '<STR_LIT>' : <EOL> buf . append ( "<STR_LIT>" ) <EOL> else : <EOL> status = "<STR_LIT>" <EOL> buf . append ( CRLF ) <EOL> if msg : <EOL> if isinstance ( msg , unicode ) : <EOL> msg = msg . encode ( "<STR_LIT>" ) <EOL> buf . append ( msg ) <EOL> status_line = self . server . protocol + "<STR_LIT:U+0020>" + status + CRLF <EOL> try : <EOL> self . conn . wfile . sendall ( status_line + "<STR_LIT>" . join ( buf ) ) <EOL> except socket . error , x : <EOL> if x . args [ <NUM_LIT:0> ] not in socket_errors_to_ignore : <EOL> raise <EOL> def write ( self , chunk ) : <EOL> """<STR_LIT>""" <EOL> if self . chunked_write and chunk : <EOL> buf = [ hex ( len ( chunk ) ) [ <NUM_LIT:2> : ] , CRLF , chunk , CRLF ] <EOL> self . conn . wfile . sendall ( "<STR_LIT>" . join ( buf ) ) <EOL> else : <EOL> self . conn . wfile . sendall ( chunk ) <EOL> def send_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> hkeys = [ key . lower ( ) for key , value in self . outheaders ] <EOL> status = int ( self . status [ : <NUM_LIT:3> ] ) <EOL> if status == <NUM_LIT> : <EOL> self . close_connection = True <EOL> elif "<STR_LIT>" not in hkeys : <EOL> if status < <NUM_LIT:200> or status in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> pass <EOL> else : <EOL> if ( self . response_protocol == '<STR_LIT>' <EOL> and self . method != '<STR_LIT>' ) : <EOL> self . chunked_write = True <EOL> self . outheaders . append ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> else : <EOL> self . close_connection = True <EOL> if "<STR_LIT>" not in hkeys : <EOL> if self . response_protocol == '<STR_LIT>' : <EOL> if self . close_connection : <EOL> self . outheaders . append ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> else : <EOL> if not self . close_connection : <EOL> self . outheaders . append ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> if ( not self . close_connection ) and ( not self . chunked_read ) : <EOL> remaining = getattr ( self . rfile , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if remaining > <NUM_LIT:0> : <EOL> self . rfile . read ( remaining ) <EOL> if "<STR_LIT:date>" not in hkeys : <EOL> self . outheaders . append ( ( "<STR_LIT>" , rfc822 . formatdate ( ) ) ) <EOL> if "<STR_LIT>" not in hkeys : <EOL> self . outheaders . append ( ( "<STR_LIT>" , self . server . server_name ) ) <EOL> buf = [ self . server . protocol + "<STR_LIT:U+0020>" + self . status + CRLF ] <EOL> for k , v in self . outheaders : <EOL> buf . append ( k + "<STR_LIT>" + v + CRLF ) <EOL> buf . append ( CRLF ) <EOL> self . conn . wfile . sendall ( "<STR_LIT>" . join ( buf ) ) <EOL> class NoSSLError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class FatalSSLAlert ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> if not _fileobject_uses_str_type : <EOL> class CP_fileobject ( socket . _fileobject ) : <EOL> """<STR_LIT>""" <EOL> def sendall ( self , data ) : <EOL> """<STR_LIT>""" <EOL> while data : <EOL> try : <EOL> bytes_sent = self . send ( data ) <EOL> data = data [ bytes_sent : ] <EOL> except socket . error , e : <EOL> if e . args [ <NUM_LIT:0> ] not in socket_errors_nonblocking : <EOL> raise <EOL> def send ( self , data ) : <EOL> return self . _sock . send ( data ) <EOL> def flush ( self ) : <EOL> if self . _wbuf : <EOL> buffer = "<STR_LIT>" . join ( self . _wbuf ) <EOL> self . _wbuf = [ ] <EOL> self . sendall ( buffer ) <EOL> def recv ( self , size ) : <EOL> while True : <EOL> try : <EOL> return self . _sock . recv ( size ) <EOL> except socket . error , e : <EOL> if ( e . args [ <NUM_LIT:0> ] not in socket_errors_nonblocking <EOL> and e . args [ <NUM_LIT:0> ] not in socket_error_eintr ) : <EOL> raise <EOL> def read ( self , size = - <NUM_LIT:1> ) : <EOL> rbufsize = max ( self . _rbufsize , self . default_bufsize ) <EOL> buf = self . _rbuf <EOL> buf . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> if size < <NUM_LIT:0> : <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> while True : <EOL> data = self . recv ( rbufsize ) <EOL> if not data : <EOL> break <EOL> buf . write ( data ) <EOL> return buf . getvalue ( ) <EOL> else : <EOL> buf_len = buf . tell ( ) <EOL> if buf_len >= size : <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> rv = buf . read ( size ) <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> self . _rbuf . write ( buf . read ( ) ) <EOL> return rv <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> while True : <EOL> left = size - buf_len <EOL> data = self . recv ( left ) <EOL> if not data : <EOL> break <EOL> n = len ( data ) <EOL> if n == size and not buf_len : <EOL> return data <EOL> if n == left : <EOL> buf . write ( data ) <EOL> del data <EOL> break <EOL> assert n <= left , "<STR_LIT>" % ( left , n ) <EOL> buf . write ( data ) <EOL> buf_len += n <EOL> del data <EOL> return buf . getvalue ( ) <EOL> def readline ( self , size = - <NUM_LIT:1> ) : <EOL> buf = self . _rbuf <EOL> buf . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> if buf . tell ( ) > <NUM_LIT:0> : <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> bline = buf . readline ( size ) <EOL> if bline . endswith ( '<STR_LIT:\n>' ) or len ( bline ) == size : <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> self . _rbuf . write ( buf . read ( ) ) <EOL> return bline <EOL> del bline <EOL> if size < <NUM_LIT:0> : <EOL> if self . _rbufsize <= <NUM_LIT:1> : <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> buffers = [ buf . read ( ) ] <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> data = None <EOL> recv = self . recv <EOL> while data != "<STR_LIT:\n>" : <EOL> data = recv ( <NUM_LIT:1> ) <EOL> if not data : <EOL> break <EOL> buffers . append ( data ) <EOL> return "<STR_LIT>" . join ( buffers ) <EOL> buf . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> while True : <EOL> data = self . recv ( self . _rbufsize ) <EOL> if not data : <EOL> break <EOL> nl = data . find ( '<STR_LIT:\n>' ) <EOL> if nl >= <NUM_LIT:0> : <EOL> nl += <NUM_LIT:1> <EOL> buf . write ( data [ : nl ] ) <EOL> self . _rbuf . write ( data [ nl : ] ) <EOL> del data <EOL> break <EOL> buf . write ( data ) <EOL> return buf . getvalue ( ) <EOL> else : <EOL> buf . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> buf_len = buf . tell ( ) <EOL> if buf_len >= size : <EOL> buf . seek ( <NUM_LIT:0> ) <EOL> rv = buf . read ( size ) <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> self . _rbuf . write ( buf . read ( ) ) <EOL> return rv <EOL> self . _rbuf = StringIO . StringIO ( ) <EOL> while True : <EOL> data = self . recv ( self . _rbufsize ) <EOL> if not data : <EOL> break <EOL> left = size - buf_len <EOL> nl = data . find ( '<STR_LIT:\n>' , <NUM_LIT:0> , left ) <EOL> if nl >= <NUM_LIT:0> : <EOL> nl += <NUM_LIT:1> <EOL> self . _rbuf . write ( data [ nl : ] ) <EOL> if buf_len : <EOL> buf . write ( data [ : nl ] ) <EOL> break <EOL> else : <EOL> return data [ : nl ] <EOL> n = len ( data ) <EOL> if n == size and not buf_len : <EOL> return data <EOL> if n >= left : <EOL> buf . write ( data [ : left ] ) <EOL> self . _rbuf . write ( data [ left : ] ) <EOL> break <EOL> buf . write ( data ) <EOL> buf_len += n <EOL> return buf . getvalue ( ) <EOL> else : <EOL> class CP_fileobject ( socket . _fileobject ) : <EOL> """<STR_LIT>""" <EOL> def sendall ( self , data ) : <EOL> """<STR_LIT>""" <EOL> while data : <EOL> try : <EOL> bytes_sent = self . send ( data ) <EOL> data = data [ bytes_sent : ] <EOL> except socket . error , e : <EOL> if e . args [ <NUM_LIT:0> ] not in socket_errors_nonblocking : <EOL> raise <EOL> def send ( self , data ) : <EOL> return self . _sock . send ( data ) <EOL> def flush ( self ) : <EOL> if self . _wbuf : <EOL> buffer = "<STR_LIT>" . join ( self . _wbuf ) <EOL> self . _wbuf = [ ] <EOL> self . sendall ( buffer ) <EOL> def recv ( self , size ) : <EOL> while True : <EOL> try : <EOL> return self . _sock . recv ( size ) <EOL> except socket . error , e : <EOL> if ( e . args [ <NUM_LIT:0> ] not in socket_errors_nonblocking <EOL> and e . args [ <NUM_LIT:0> ] not in socket_error_eintr ) : <EOL> raise <EOL> def read ( self , size = - <NUM_LIT:1> ) : <EOL> if size < <NUM_LIT:0> : <EOL> buffers = [ self . _rbuf ] <EOL> self . _rbuf = "<STR_LIT>" <EOL> if self . _rbufsize <= <NUM_LIT:1> : <EOL> recv_size = self . default_bufsize <EOL> else : <EOL> recv_size = self . _rbufsize <EOL> while True : <EOL> data = self . recv ( recv_size ) <EOL> if not data : <EOL> break <EOL> buffers . append ( data ) <EOL> return "<STR_LIT>" . join ( buffers ) <EOL> else : <EOL> data = self . _rbuf <EOL> buf_len = len ( data ) <EOL> if buf_len >= size : <EOL> self . _rbuf = data [ size : ] <EOL> return data [ : size ] <EOL> buffers = [ ] <EOL> if data : <EOL> buffers . append ( data ) <EOL> self . _rbuf = "<STR_LIT>" <EOL> while True : <EOL> left = size - buf_len <EOL> recv_size = max ( self . _rbufsize , left ) <EOL> data = self . recv ( recv_size ) <EOL> if not data : <EOL> break <EOL> buffers . append ( data ) <EOL> n = len ( data ) <EOL> if n >= left : <EOL> self . _rbuf = data [ left : ] <EOL> buffers [ - <NUM_LIT:1> ] = data [ : left ] <EOL> break <EOL> buf_len += n <EOL> return "<STR_LIT>" . join ( buffers ) <EOL> def readline ( self , size = - <NUM_LIT:1> ) : <EOL> data = self . _rbuf <EOL> if size < <NUM_LIT:0> : <EOL> if self . _rbufsize <= <NUM_LIT:1> : <EOL> assert data == "<STR_LIT>" <EOL> buffers = [ ] <EOL> while data != "<STR_LIT:\n>" : <EOL> data = self . recv ( <NUM_LIT:1> ) <EOL> if not data : <EOL> break <EOL> buffers . append ( data ) <EOL> return "<STR_LIT>" . join ( buffers ) <EOL> nl = data . find ( '<STR_LIT:\n>' ) <EOL> if nl >= <NUM_LIT:0> : <EOL> nl += <NUM_LIT:1> <EOL> self . _rbuf = data [ nl : ] <EOL> return data [ : nl ] <EOL> buffers = [ ] <EOL> if data : <EOL> buffers . append ( data ) <EOL> self . _rbuf = "<STR_LIT>" <EOL> while True : <EOL> data = self . recv ( self . _rbufsize ) <EOL> if not data : <EOL> break <EOL> buffers . append ( data ) <EOL> nl = data . find ( '<STR_LIT:\n>' ) <EOL> if nl >= <NUM_LIT:0> : <EOL> nl += <NUM_LIT:1> <EOL> self . _rbuf = data [ nl : ] <EOL> buffers [ - <NUM_LIT:1> ] = data [ : nl ] <EOL> break <EOL> return "<STR_LIT>" . join ( buffers ) <EOL> else : <EOL> nl = data . find ( '<STR_LIT:\n>' , <NUM_LIT:0> , size ) <EOL> if nl >= <NUM_LIT:0> : <EOL> nl += <NUM_LIT:1> <EOL> self . _rbuf = data [ nl : ] <EOL> return data [ : nl ] <EOL> buf_len = len ( data ) <EOL> if buf_len >= size : <EOL> self . _rbuf = data [ size : ] <EOL> return data [ : size ] <EOL> buffers = [ ] <EOL> if data : <EOL> buffers . append ( data ) <EOL> self . _rbuf = "<STR_LIT>" <EOL> while True : <EOL> data = self . recv ( self . _rbufsize ) <EOL> if not data : <EOL> break <EOL> buffers . append ( data ) <EOL> left = size - buf_len <EOL> nl = data . find ( '<STR_LIT:\n>' , <NUM_LIT:0> , left ) <EOL> if nl >= <NUM_LIT:0> : <EOL> nl += <NUM_LIT:1> <EOL> self . _rbuf = data [ nl : ] <EOL> buffers [ - <NUM_LIT:1> ] = data [ : nl ] <EOL> break <EOL> n = len ( data ) <EOL> if n >= left : <EOL> self . _rbuf = data [ left : ] <EOL> buffers [ - <NUM_LIT:1> ] = data [ : left ] <EOL> break <EOL> buf_len += n <EOL> return "<STR_LIT>" . join ( buffers ) <EOL> class HTTPConnection ( object ) : <EOL> """<STR_LIT>""" <EOL> remote_addr = None <EOL> remote_port = None <EOL> ssl_env = None <EOL> rbufsize = - <NUM_LIT:1> <EOL> RequestHandlerClass = HTTPRequest <EOL> def __init__ ( self , server , sock , makefile = CP_fileobject ) : <EOL> self . server = server <EOL> self . socket = sock <EOL> self . rfile = makefile ( sock , "<STR_LIT:rb>" , self . rbufsize ) <EOL> self . wfile = makefile ( sock , "<STR_LIT:wb>" , - <NUM_LIT:1> ) <EOL> def communicate ( self ) : <EOL> """<STR_LIT>""" <EOL> request_seen = False <EOL> try : <EOL> while True : <EOL> req = None <EOL> req = self . RequestHandlerClass ( self . server , self ) <EOL> req . parse_request ( ) <EOL> if not req . ready : <EOL> return <EOL> request_seen = True <EOL> req . respond ( ) <EOL> if req . close_connection : <EOL> return <EOL> except socket . error , e : <EOL> errnum = e . args [ <NUM_LIT:0> ] <EOL> if errnum == '<STR_LIT>' : <EOL> if ( not request_seen ) or ( req and req . started_request ) : <EOL> if req and not req . sent_headers : <EOL> try : <EOL> req . simple_response ( "<STR_LIT>" ) <EOL> except FatalSSLAlert : <EOL> return <EOL> elif errnum not in socket_errors_to_ignore : <EOL> if req and not req . sent_headers : <EOL> try : <EOL> req . simple_response ( "<STR_LIT>" , <EOL> format_exc ( ) ) <EOL> except FatalSSLAlert : <EOL> return <EOL> return <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except FatalSSLAlert : <EOL> return <EOL> except NoSSLError : <EOL> if req and not req . sent_headers : <EOL> self . wfile = CP_fileobject ( self . socket . _sock , "<STR_LIT:wb>" , - <NUM_LIT:1> ) <EOL> req . simple_response ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . linger = True <EOL> except Exception : <EOL> if req and not req . sent_headers : <EOL> try : <EOL> req . simple_response ( "<STR_LIT>" , format_exc ( ) ) <EOL> except FatalSSLAlert : <EOL> return <EOL> linger = False <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rfile . close ( ) <EOL> if not self . linger : <EOL> if hasattr ( self . socket , '<STR_LIT>' ) : <EOL> self . socket . _sock . close ( ) <EOL> self . socket . close ( ) <EOL> else : <EOL> pass <EOL> def format_exc ( limit = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> etype , value , tb = sys . exc_info ( ) <EOL> return '<STR_LIT>' . join ( traceback . format_exception ( etype , value , tb , limit ) ) <EOL> finally : <EOL> etype = value = tb = None <EOL> _SHUTDOWNREQUEST = None <EOL> class WorkerThread ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> conn = None <EOL> def __init__ ( self , server ) : <EOL> self . ready = False <EOL> self . server = server <EOL> threading . Thread . __init__ ( self ) <EOL> def run ( self ) : <EOL> try : <EOL> self . ready = True <EOL> while True : <EOL> conn = self . server . requests . get ( ) <EOL> if conn is _SHUTDOWNREQUEST : <EOL> return <EOL> self . conn = conn <EOL> try : <EOL> conn . communicate ( ) <EOL> finally : <EOL> conn . close ( ) <EOL> self . conn = None <EOL> except ( KeyboardInterrupt , SystemExit ) , exc : <EOL> self . server . interrupt = exc <EOL> class ThreadPool ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server , min = <NUM_LIT:10> , max = - <NUM_LIT:1> ) : <EOL> self . server = server <EOL> self . min = min <EOL> self . max = max <EOL> self . _threads = [ ] <EOL> self . _queue = Queue . Queue ( ) <EOL> self . get = self . _queue . get <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( self . min ) : <EOL> self . _threads . append ( WorkerThread ( self . server ) ) <EOL> for worker in self . _threads : <EOL> worker . setName ( "<STR_LIT>" + worker . getName ( ) ) <EOL> worker . start ( ) <EOL> for worker in self . _threads : <EOL> while not worker . ready : <EOL> time . sleep ( <NUM_LIT> ) <EOL> def _get_idle ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( [ t for t in self . _threads if t . conn is None ] ) <EOL> idle = property ( _get_idle , doc = _get_idle . __doc__ ) <EOL> def put ( self , obj ) : <EOL> self . _queue . put ( obj ) <EOL> if obj is _SHUTDOWNREQUEST : <EOL> return <EOL> def grow ( self , amount ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( amount ) : <EOL> if self . max > <NUM_LIT:0> and len ( self . _threads ) >= self . max : <EOL> break <EOL> worker = WorkerThread ( self . server ) <EOL> worker . setName ( "<STR_LIT>" + worker . getName ( ) ) <EOL> self . _threads . append ( worker ) <EOL> worker . start ( ) <EOL> def shrink ( self , amount ) : <EOL> """<STR_LIT>""" <EOL> for t in self . _threads : <EOL> if not t . isAlive ( ) : <EOL> self . _threads . remove ( t ) <EOL> amount -= <NUM_LIT:1> <EOL> if amount > <NUM_LIT:0> : <EOL> for i in range ( min ( amount , len ( self . _threads ) - self . min ) ) : <EOL> self . _queue . put ( _SHUTDOWNREQUEST ) <EOL> def stop ( self , timeout = <NUM_LIT:5> ) : <EOL> for worker in self . _threads : <EOL> self . _queue . put ( _SHUTDOWNREQUEST ) <EOL> current = threading . currentThread ( ) <EOL> if timeout and timeout >= <NUM_LIT:0> : <EOL> endtime = time . time ( ) + timeout <EOL> while self . _threads : <EOL> worker = self . _threads . pop ( ) <EOL> if worker is not current and worker . isAlive ( ) : <EOL> try : <EOL> if timeout is None or timeout < <NUM_LIT:0> : <EOL> worker . join ( ) <EOL> else : <EOL> remaining_time = endtime - time . time ( ) <EOL> if remaining_time > <NUM_LIT:0> : <EOL> worker . join ( remaining_time ) <EOL> if worker . isAlive ( ) : <EOL> c = worker . conn <EOL> if c and not c . rfile . closed : <EOL> try : <EOL> c . socket . shutdown ( socket . SHUT_RD ) <EOL> except TypeError : <EOL> c . socket . shutdown ( ) <EOL> worker . join ( ) <EOL> except ( AssertionError , <EOL> KeyboardInterrupt ) , exc1 : <EOL> pass <EOL> try : <EOL> import fcntl <EOL> except ImportError : <EOL> try : <EOL> from ctypes import windll , WinError <EOL> except ImportError : <EOL> def prevent_socket_inheritance ( sock ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> else : <EOL> def prevent_socket_inheritance ( sock ) : <EOL> """<STR_LIT>""" <EOL> if not windll . kernel32 . SetHandleInformation ( sock . fileno ( ) , <NUM_LIT:1> , <NUM_LIT:0> ) : <EOL> raise WinError ( ) <EOL> else : <EOL> def prevent_socket_inheritance ( sock ) : <EOL> """<STR_LIT>""" <EOL> fd = sock . fileno ( ) <EOL> old_flags = fcntl . fcntl ( fd , fcntl . F_GETFD ) <EOL> fcntl . fcntl ( fd , fcntl . F_SETFD , old_flags | fcntl . FD_CLOEXEC ) <EOL> class SSLAdapter ( object ) : <EOL> def __init__ ( self , certificate , private_key , certificate_chain = None ) : <EOL> self . certificate = certificate <EOL> self . private_key = private_key <EOL> self . certificate_chain = certificate_chain <EOL> def wrap ( self , sock ) : <EOL> raise NotImplemented <EOL> def makefile ( self , sock , mode = '<STR_LIT:r>' , bufsize = - <NUM_LIT:1> ) : <EOL> raise NotImplemented <EOL> class HTTPServer ( object ) : <EOL> """<STR_LIT>""" <EOL> protocol = "<STR_LIT>" <EOL> _bind_addr = "<STR_LIT:127.0.0.1>" <EOL> version = "<STR_LIT>" <EOL> response_header = None <EOL> ready = False <EOL> _interrupt = None <EOL> max_request_header_size = <NUM_LIT:0> <EOL> max_request_body_size = <NUM_LIT:0> <EOL> nodelay = True <EOL> ConnectionClass = HTTPConnection <EOL> ssl_adapter = None <EOL> def __init__ ( self , bind_addr , gateway , minthreads = <NUM_LIT:10> , maxthreads = - <NUM_LIT:1> , <EOL> server_name = None ) : <EOL> self . bind_addr = bind_addr <EOL> self . gateway = gateway <EOL> self . requests = ThreadPool ( self , min = minthreads or <NUM_LIT:1> , max = maxthreads ) <EOL> if not server_name : <EOL> server_name = socket . gethostname ( ) <EOL> self . server_name = server_name <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __module__ , self . __class__ . __name__ , <EOL> self . bind_addr ) <EOL> def _get_bind_addr ( self ) : <EOL> return self . _bind_addr <EOL> def _set_bind_addr ( self , value ) : <EOL> if isinstance ( value , tuple ) and value [ <NUM_LIT:0> ] in ( '<STR_LIT>' , None ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _bind_addr = value <EOL> bind_addr = property ( _get_bind_addr , _set_bind_addr , <EOL> doc = """<STR_LIT>""" ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _interrupt = None <EOL> if ( self . ssl_adapter is None and <EOL> getattr ( self , '<STR_LIT>' , None ) and <EOL> getattr ( self , '<STR_LIT>' , None ) ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning <EOL> ) <EOL> try : <EOL> from django_wsgiserver . wsgiserver . ssl_pyopenssl import pyOpenSSLAdapter <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> self . ssl_adapter = pyOpenSSLAdapter ( <EOL> self . ssl_certificate , self . ssl_private_key , <EOL> getattr ( self , '<STR_LIT>' , None ) ) <EOL> if isinstance ( self . bind_addr , basestring ) : <EOL> try : os . unlink ( self . bind_addr ) <EOL> except : pass <EOL> try : os . chmod ( self . bind_addr , <NUM_LIT:0> <NUM_LIT> ) <EOL> except : pass <EOL> info = [ ( socket . AF_UNIX , socket . SOCK_STREAM , <NUM_LIT:0> , "<STR_LIT>" , self . bind_addr ) ] <EOL> else : <EOL> host , port = self . bind_addr <EOL> try : <EOL> info = socket . getaddrinfo ( host , port , socket . AF_UNSPEC , <EOL> socket . SOCK_STREAM , <NUM_LIT:0> , socket . AI_PASSIVE ) <EOL> except socket . gaierror : <EOL> if '<STR_LIT::>' in self . bind_addr [ <NUM_LIT:0> ] : <EOL> info = [ ( socket . AF_INET6 , socket . SOCK_STREAM , <EOL> <NUM_LIT:0> , "<STR_LIT>" , self . bind_addr + ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ] <EOL> else : <EOL> info = [ ( socket . AF_INET , socket . SOCK_STREAM , <EOL> <NUM_LIT:0> , "<STR_LIT>" , self . bind_addr ) ] <EOL> self . socket = None <EOL> msg = "<STR_LIT>" <EOL> for res in info : <EOL> af , socktype , proto , canonname , sa = res <EOL> try : <EOL> self . bind ( af , socktype , proto ) <EOL> except socket . error , msg : <EOL> if self . socket : <EOL> self . socket . close ( ) <EOL> self . socket = None <EOL> continue <EOL> break <EOL> if not self . socket : <EOL> raise socket . error ( msg ) <EOL> self . socket . settimeout ( <NUM_LIT:1> ) <EOL> self . socket . listen ( self . request_queue_size ) <EOL> self . requests . start ( ) <EOL> self . ready = True <EOL> while self . ready : <EOL> self . tick ( ) <EOL> if self . interrupt : <EOL> while self . interrupt is True : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if self . interrupt : <EOL> raise self . interrupt <EOL> def bind ( self , family , type , proto = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . socket = socket . socket ( family , type , proto ) <EOL> prevent_socket_inheritance ( self . socket ) <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> if self . nodelay and not isinstance ( self . bind_addr , str ) : <EOL> self . socket . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> if self . ssl_adapter is not None : <EOL> self . socket = self . ssl_adapter . bind ( self . socket ) <EOL> if ( family == socket . AF_INET6 <EOL> and self . bind_addr [ <NUM_LIT:0> ] in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> try : <EOL> self . socket . setsockopt ( socket . IPPROTO_IPV6 , socket . IPV6_V6ONLY , <NUM_LIT:0> ) <EOL> except ( AttributeError , socket . error ) : <EOL> pass <EOL> self . socket . bind ( self . bind_addr ) <EOL> def tick ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> s , addr = self . socket . accept ( ) <EOL> if not self . ready : <EOL> return <EOL> prevent_socket_inheritance ( s ) <EOL> if hasattr ( s , '<STR_LIT>' ) : <EOL> s . settimeout ( self . timeout ) <EOL> if self . response_header is None : <EOL> self . response_header = "<STR_LIT>" % self . version <EOL> makefile = CP_fileobject <EOL> ssl_env = { } <EOL> if self . ssl_adapter is not None : <EOL> try : <EOL> s , ssl_env = self . ssl_adapter . wrap ( s ) <EOL> except NoSSLError : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> buf = [ "<STR_LIT>" % self . protocol , <EOL> "<STR_LIT>" % len ( msg ) , <EOL> "<STR_LIT>" , <EOL> msg ] <EOL> wfile = CP_fileobject ( s , "<STR_LIT:wb>" , - <NUM_LIT:1> ) <EOL> try : <EOL> wfile . sendall ( "<STR_LIT>" . join ( buf ) ) <EOL> except socket . error , x : <EOL> if x . args [ <NUM_LIT:0> ] not in socket_errors_to_ignore : <EOL> raise <EOL> return <EOL> if not s : <EOL> return <EOL> makefile = self . ssl_adapter . makefile <EOL> conn = self . ConnectionClass ( self , s , makefile ) <EOL> if not isinstance ( self . bind_addr , basestring ) : <EOL> if addr is None : <EOL> if len ( s . getsockname ( ) ) == <NUM_LIT:2> : <EOL> addr = ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> else : <EOL> addr = ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> conn . remote_addr = addr [ <NUM_LIT:0> ] <EOL> conn . remote_port = addr [ <NUM_LIT:1> ] <EOL> conn . ssl_env = ssl_env <EOL> self . requests . put ( conn ) <EOL> except socket . timeout : <EOL> return <EOL> except socket . error , x : <EOL> if x . args [ <NUM_LIT:0> ] in socket_error_eintr : <EOL> return <EOL> if x . args [ <NUM_LIT:0> ] in socket_errors_nonblocking : <EOL> return <EOL> if x . args [ <NUM_LIT:0> ] in socket_errors_to_ignore : <EOL> return <EOL> raise <EOL> def _get_interrupt ( self ) : <EOL> return self . _interrupt <EOL> def _set_interrupt ( self , interrupt ) : <EOL> self . _interrupt = True <EOL> self . stop ( ) <EOL> self . _interrupt = interrupt <EOL> interrupt = property ( _get_interrupt , _set_interrupt , <EOL> doc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ready = False <EOL> sock = getattr ( self , "<STR_LIT>" , None ) <EOL> if sock : <EOL> if not isinstance ( self . bind_addr , basestring ) : <EOL> try : <EOL> host , port = sock . getsockname ( ) [ : <NUM_LIT:2> ] <EOL> except socket . error , x : <EOL> if x . args [ <NUM_LIT:0> ] not in socket_errors_to_ignore : <EOL> raise <EOL> else : <EOL> for res in socket . getaddrinfo ( host , port , socket . AF_UNSPEC , <EOL> socket . SOCK_STREAM ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> s = None <EOL> try : <EOL> s = socket . socket ( af , socktype , proto ) <EOL> s . settimeout ( <NUM_LIT:1.0> ) <EOL> s . connect ( ( host , port ) ) <EOL> s . close ( ) <EOL> except socket . error : <EOL> if s : <EOL> s . close ( ) <EOL> if hasattr ( sock , "<STR_LIT>" ) : <EOL> sock . close ( ) <EOL> self . socket = None <EOL> self . requests . stop ( self . shutdown_timeout ) <EOL> class Gateway ( object ) : <EOL> def __init__ ( self , req ) : <EOL> self . req = req <EOL> def respond ( self ) : <EOL> raise NotImplemented <EOL> ssl_adapters = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def get_ssl_adapter_class ( name = '<STR_LIT>' ) : <EOL> adapter = ssl_adapters [ name . lower ( ) ] <EOL> if isinstance ( adapter , basestring ) : <EOL> last_dot = adapter . rfind ( "<STR_LIT:.>" ) <EOL> attr_name = adapter [ last_dot + <NUM_LIT:1> : ] <EOL> mod_path = adapter [ : last_dot ] <EOL> try : <EOL> mod = sys . modules [ mod_path ] <EOL> if mod is None : <EOL> raise KeyError ( ) <EOL> except KeyError : <EOL> mod = __import__ ( mod_path , globals ( ) , locals ( ) , [ '<STR_LIT>' ] ) <EOL> try : <EOL> adapter = getattr ( mod , attr_name ) <EOL> except AttributeError : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> % ( mod_path , attr_name ) ) <EOL> return adapter <EOL> class CherryPyWSGIServer ( HTTPServer ) : <EOL> wsgi_version = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def __init__ ( self , bind_addr , wsgi_app , numthreads = <NUM_LIT:10> , server_name = None , <EOL> max = - <NUM_LIT:1> , request_queue_size = <NUM_LIT:5> , timeout = <NUM_LIT:10> , shutdown_timeout = <NUM_LIT:5> ) : <EOL> self . requests = ThreadPool ( self , min = numthreads or <NUM_LIT:1> , max = max ) <EOL> self . wsgi_app = wsgi_app <EOL> self . gateway = wsgi_gateways [ self . wsgi_version ] <EOL> self . bind_addr = bind_addr <EOL> if not server_name : <EOL> server_name = socket . gethostname ( ) <EOL> self . server_name = server_name <EOL> self . request_queue_size = request_queue_size <EOL> self . timeout = timeout <EOL> self . shutdown_timeout = shutdown_timeout <EOL> def _get_numthreads ( self ) : <EOL> return self . requests . min <EOL> def _set_numthreads ( self , value ) : <EOL> self . requests . min = value <EOL> numthreads = property ( _get_numthreads , _set_numthreads ) <EOL> class WSGIGateway ( Gateway ) : <EOL> def __init__ ( self , req ) : <EOL> self . req = req <EOL> self . started_response = False <EOL> self . env = self . get_environ ( ) <EOL> def get_environ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented <EOL> def respond ( self ) : <EOL> response = self . req . server . wsgi_app ( self . env , self . start_response ) <EOL> try : <EOL> for chunk in response : <EOL> if chunk : <EOL> if isinstance ( chunk , unicode ) : <EOL> chunk = chunk . encode ( '<STR_LIT>' ) <EOL> self . write ( chunk ) <EOL> finally : <EOL> if hasattr ( response , "<STR_LIT>" ) : <EOL> response . close ( ) <EOL> def start_response ( self , status , headers , exc_info = None ) : <EOL> """<STR_LIT>""" <EOL> if self . started_response and not exc_info : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . started_response = True <EOL> if self . req . sent_headers : <EOL> try : <EOL> raise exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] <EOL> finally : <EOL> exc_info = None <EOL> self . req . status = status <EOL> for k , v in headers : <EOL> if not isinstance ( k , str ) : <EOL> raise TypeError ( "<STR_LIT>" % k ) <EOL> if not isinstance ( v , str ) : <EOL> raise TypeError ( "<STR_LIT>" % v ) <EOL> self . req . outheaders . extend ( headers ) <EOL> return self . write <EOL> def write ( self , chunk ) : <EOL> """<STR_LIT>""" <EOL> if not self . started_response : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> if not self . req . sent_headers : <EOL> self . req . sent_headers = True <EOL> self . req . send_headers ( ) <EOL> self . req . write ( chunk ) <EOL> class WSGIGateway_10 ( WSGIGateway ) : <EOL> def get_environ ( self ) : <EOL> """<STR_LIT>""" <EOL> req = self . req <EOL> env = { <EOL> '<STR_LIT>' : req . server . protocol , <EOL> '<STR_LIT>' : req . path , <EOL> '<STR_LIT>' : req . qs , <EOL> '<STR_LIT>' : req . conn . remote_addr or '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( req . conn . remote_port or '<STR_LIT>' ) , <EOL> '<STR_LIT>' : req . method , <EOL> '<STR_LIT>' : req . uri , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : req . server . server_name , <EOL> '<STR_LIT>' : req . request_protocol , <EOL> '<STR_LIT>' : "<STR_LIT>" % req . server . version , <EOL> '<STR_LIT>' : sys . stderr , <EOL> '<STR_LIT>' : req . rfile , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : req . scheme , <EOL> '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> } <EOL> if isinstance ( req . server . bind_addr , basestring ) : <EOL> env [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> else : <EOL> env [ "<STR_LIT>" ] = str ( req . server . bind_addr [ <NUM_LIT:1> ] ) <EOL> for k , v in req . inheaders . iteritems ( ) : <EOL> env [ "<STR_LIT>" + k . upper ( ) . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) ] = v <EOL> ct = env . pop ( "<STR_LIT>" , None ) <EOL> if ct is not None : <EOL> env [ "<STR_LIT>" ] = ct <EOL> cl = env . pop ( "<STR_LIT>" , None ) <EOL> if cl is not None : <EOL> env [ "<STR_LIT>" ] = cl <EOL> if req . conn . ssl_env : <EOL> env . update ( req . conn . ssl_env ) <EOL> return env <EOL> class WSGIGateway_11 ( WSGIGateway_10 ) : <EOL> def get_environ ( self ) : <EOL> env = WSGIGateway_10 . get_environ ( self ) <EOL> env [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> return env <EOL> class WSGIGateway_u0 ( WSGIGateway_10 ) : <EOL> def get_environ ( self ) : <EOL> """<STR_LIT>""" <EOL> req = self . req <EOL> env_10 = WSGIGateway_10 . get_environ ( self ) <EOL> env = dict ( [ ( k . decode ( '<STR_LIT>' ) , v ) for k , v in env_10 . iteritems ( ) ] ) <EOL> env [ u'<STR_LIT>' ] = ( '<STR_LIT:u>' , <NUM_LIT:0> ) <EOL> env . setdefault ( u'<STR_LIT>' , u'<STR_LIT:utf-8>' ) <EOL> try : <EOL> for key in [ u"<STR_LIT>" , u"<STR_LIT>" , u"<STR_LIT>" ] : <EOL> env [ key ] = env_10 [ str ( key ) ] . decode ( env [ u'<STR_LIT>' ] ) <EOL> except UnicodeDecodeError : <EOL> env [ u'<STR_LIT>' ] = u'<STR_LIT>' <EOL> for key in [ u"<STR_LIT>" , u"<STR_LIT>" , u"<STR_LIT>" ] : <EOL> env [ key ] = env_10 [ str ( key ) ] . decode ( env [ u'<STR_LIT>' ] ) <EOL> for k , v in sorted ( env . items ( ) ) : <EOL> if isinstance ( v , str ) and k not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> env [ k ] = v . decode ( '<STR_LIT>' ) <EOL> return env <EOL> wsgi_gateways = { <EOL> ( <NUM_LIT:1> , <NUM_LIT:0> ) : WSGIGateway_10 , <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> ) : WSGIGateway_11 , <EOL> ( '<STR_LIT:u>' , <NUM_LIT:0> ) : WSGIGateway_u0 , <EOL> } <EOL> class WSGIPathInfoDispatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , apps ) : <EOL> try : <EOL> apps = apps . items ( ) <EOL> except AttributeError : <EOL> pass <EOL> apps . sort ( cmp = lambda x , y : cmp ( len ( x [ <NUM_LIT:0> ] ) , len ( y [ <NUM_LIT:0> ] ) ) ) <EOL> apps . reverse ( ) <EOL> self . apps = [ ( p . rstrip ( "<STR_LIT:/>" ) , a ) for p , a in apps ] <EOL> def __call__ ( self , environ , start_response ) : <EOL> path = environ [ "<STR_LIT>" ] or "<STR_LIT:/>" <EOL> for p , app in self . apps : <EOL> if path . startswith ( p + "<STR_LIT:/>" ) or path == p : <EOL> environ = environ . copy ( ) <EOL> environ [ "<STR_LIT>" ] = environ [ "<STR_LIT>" ] + p <EOL> environ [ "<STR_LIT>" ] = path [ len ( p ) : ] <EOL> return app ( environ , start_response ) <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:0>' ) ] ) <EOL> return [ '<STR_LIT>' ] </s>
