<s> import re <EOL> from . message import Request <EOL> from . util import normalize_line_endings <EOL> try : <EOL> from urllib . parse import urlparse <EOL> from urllib . parse import parse_qs <EOL> from urllib . parse import quote <EOL> except ImportError : <EOL> from urlparse import urlparse <EOL> from urlparse import parse_qs <EOL> from urllib import quote <EOL> def _read_request_line_dict ( line ) : <EOL> """<STR_LIT>""" <EOL> words = line . split ( "<STR_LIT:U+0020>" ) <EOL> method = "<STR_LIT:GET>" <EOL> if len ( words ) == <NUM_LIT:1> : <EOL> uri = words [ <NUM_LIT:0> ] <EOL> else : <EOL> method = words [ <NUM_LIT:0> ] <EOL> uri = words [ <NUM_LIT:1> ] <EOL> return { <EOL> "<STR_LIT>" : method , <EOL> "<STR_LIT>" : uri <EOL> } <EOL> class RequestParser : <EOL> def __init__ ( self , settings , eol ) : <EOL> self . settings = settings <EOL> self . eol = eol <EOL> self . request = None <EOL> def get_request ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . request = Request ( ) <EOL> default_headers = self . settings . get ( "<STR_LIT>" , { } ) <EOL> if isinstance ( default_headers , dict ) : <EOL> for header in default_headers : <EOL> self . request . headers . append ( ( header , default_headers [ header ] ) ) <EOL> elif default_headers : <EOL> for header in default_headers : <EOL> self . request . headers . append ( header ) <EOL> self . request . host = self . settings . get ( "<STR_LIT:host>" , None ) <EOL> self . request . port = self . settings . get ( "<STR_LIT:port>" , None ) <EOL> self . request . protocol = self . settings . get ( "<STR_LIT>" , None ) <EOL> text = normalize_line_endings ( text , self . eol ) <EOL> lines = text . split ( self . eol ) <EOL> for i in range ( len ( lines ) ) : <EOL> line = lines [ i ] . strip ( ) <EOL> if line == "<STR_LIT>" or line [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> pass <EOL> else : <EOL> lines = lines [ i : ] <EOL> break <EOL> self . _parse_request_line ( lines [ <NUM_LIT:0> ] ) <EOL> has_body = False <EOL> i = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:1> , len ( lines ) ) : <EOL> if lines [ i ] == "<STR_LIT>" : <EOL> has_body = True <EOL> break <EOL> if has_body : <EOL> header_lines = lines [ <NUM_LIT:1> : i ] <EOL> self . request . body = self . eol . join ( lines [ i + <NUM_LIT:1> : ] ) <EOL> else : <EOL> header_lines = lines [ <NUM_LIT:1> : ] <EOL> self . _parse_header_lines ( header_lines ) <EOL> if not self . request . host : <EOL> host = self . request . get_header ( "<STR_LIT:host>" ) <EOL> if host : <EOL> self . request . host = host <EOL> if not self . request . host and self . request . path : <EOL> uri = urlparse ( "<STR_LIT>" + self . request . path ) <EOL> self . request . host = uri . netloc <EOL> self . request . path = uri . path <EOL> if not self . request . path : <EOL> self . request . path = "<STR_LIT:/>" <EOL> return self . request <EOL> def _parse_header_lines ( self , header_lines ) : <EOL> headers = [ ] <EOL> for header in header_lines : <EOL> header = header . lstrip ( ) <EOL> if header [ <NUM_LIT:0> ] in ( "<STR_LIT:#>" , "<STR_LIT:@>" ) : <EOL> pass <EOL> elif header [ <NUM_LIT:0> ] in ( "<STR_LIT:?>" , "<STR_LIT:&>" ) : <EOL> if "<STR_LIT:=>" in header : <EOL> ( key , value ) = header [ <NUM_LIT:1> : ] . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> elif "<STR_LIT::>" in header : <EOL> ( key , value ) = header [ <NUM_LIT:1> : ] . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> else : <EOL> key , value = None , None <EOL> if key and value : <EOL> key = key . strip ( ) <EOL> value = quote ( value . strip ( ) ) <EOL> if key in self . request . query : <EOL> self . request . query [ key ] . append ( value ) <EOL> else : <EOL> self . request . query [ key ] = [ value ] <EOL> elif "<STR_LIT::>" in header : <EOL> ( key , value ) = header . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> headers . append ( ( key , value . strip ( ) ) ) <EOL> self . request . headers . extend ( headers ) <EOL> def _parse_request_line ( self , line ) : <EOL> request_line = _read_request_line_dict ( line ) <EOL> if not request_line : <EOL> return <EOL> uri = urlparse ( request_line [ "<STR_LIT>" ] ) <EOL> if uri . scheme : <EOL> self . request . protocol = uri . scheme <EOL> if uri . netloc : <EOL> if "<STR_LIT::>" in uri . netloc : <EOL> ( self . request . host , self . request . port ) = uri . netloc . split ( "<STR_LIT::>" ) <EOL> else : <EOL> self . request . host = uri . netloc <EOL> if uri . port : <EOL> self . request . port = uri . port <EOL> if uri . path : <EOL> self . request . path = uri . path <EOL> if uri . query : <EOL> query = parse_qs ( uri . query ) <EOL> for key in query : <EOL> self . request . query [ key ] = query [ key ] <EOL> if "<STR_LIT>" in request_line : <EOL> self . request . method = request_line [ "<STR_LIT>" ] </s>
