<s> import re <EOL> import urllib <EOL> import urlparse <EOL> import httplib <EOL> import wsgiref . util <EOL> import base <EOL> import configuration <EOL> INSECURE_PATHS = set ( [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def decodeURIComponent ( text ) : <EOL> """<STR_LIT>""" <EOL> return urllib . unquote_plus ( text ) <EOL> class NoDefault : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MovedTemporarily ( Exception ) : <EOL> def __init__ ( self , location , no_cache = False ) : <EOL> self . location = location <EOL> self . no_cache = no_cache <EOL> class NeedLogin ( MovedTemporarily ) : <EOL> def __init__ ( self , source , optional = False ) : <EOL> if isinstance ( source , Request ) : <EOL> target = source . getTargetURL ( ) <EOL> else : <EOL> target = str ( source ) <EOL> location = "<STR_LIT>" % urllib . quote ( target ) <EOL> if optional : <EOL> location += "<STR_LIT>" <EOL> super ( NeedLogin , self ) . __init__ ( location , no_cache = True ) <EOL> class DoExternalAuthentication ( Exception ) : <EOL> def __init__ ( self , provider_name , target_url = None ) : <EOL> self . provider_name = provider_name <EOL> self . target_url = target_url <EOL> def execute ( self , db , req ) : <EOL> import auth <EOL> provider = auth . PROVIDERS [ self . provider_name ] <EOL> if not provider . start ( db , req , self . target_url ) : <EOL> req . setStatus ( <NUM_LIT> ) <EOL> req . start ( ) <EOL> class DisplayMessage ( base . Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , title , body = None , review = None , html = False , status = <NUM_LIT:200> ) : <EOL> self . title = title <EOL> self . body = body <EOL> self . review = review <EOL> self . html = html <EOL> self . status = status <EOL> class InvalidParameterValue ( DisplayMessage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value , expected ) : <EOL> DisplayMessage . __init__ ( self , "<STR_LIT>" , "<STR_LIT>" % ( name , value , expected ) , status = <NUM_LIT> ) <EOL> class MissingParameter ( DisplayMessage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> DisplayMessage . __init__ ( self , "<STR_LIT>" , "<STR_LIT>" % name , status = <NUM_LIT> ) <EOL> class MissingWSGIRemoteUser ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Request : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> self . __environ = environ <EOL> self . __start_response = start_response <EOL> self . __status = None <EOL> self . __content_type = None <EOL> self . __response_headers = [ ] <EOL> self . __started = False <EOL> content_length = environ . get ( "<STR_LIT>" ) <EOL> self . __request_body_length = int ( content_length ) if content_length else <NUM_LIT:0> <EOL> self . __request_body_read = <NUM_LIT:0> <EOL> self . server_name = self . getRequestHeader ( "<STR_LIT>" ) or environ . get ( "<STR_LIT>" ) or configuration . base . HOSTNAME <EOL> self . method = environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . path = environ . get ( "<STR_LIT>" , "<STR_LIT>" ) . lstrip ( "<STR_LIT:/>" ) <EOL> self . original_path = self . path <EOL> self . query = environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . parsed_query = urlparse . parse_qs ( self . query , keep_blank_values = True ) <EOL> self . original_query = self . query <EOL> self . user = None <EOL> self . cookies = { } <EOL> header = self . getRequestHeader ( "<STR_LIT>" ) <EOL> if header : <EOL> for cookie in map ( str . strip , header . split ( "<STR_LIT:;>" ) ) : <EOL> name , _ , value = cookie . partition ( "<STR_LIT:=>" ) <EOL> if name and value : <EOL> self . cookies [ name ] = value <EOL> def updateQuery ( self , items ) : <EOL> self . parsed_query . update ( items ) <EOL> self . query = urllib . urlencode ( <EOL> sorted ( self . parsed_query . items ( ) ) , doseq = True ) <EOL> def setUser ( self , db ) : <EOL> if configuration . base . AUTHENTICATION_MODE == "<STR_LIT:host>" : <EOL> try : <EOL> self . user = self . __environ [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> if configuration . base . ALLOW_ANONYMOUS_USER : <EOL> return <EOL> raise MissingWSGIRemoteUser <EOL> else : <EOL> session_type = configuration . base . SESSION_TYPE <EOL> authorization_header = self . getRequestHeader ( "<STR_LIT>" ) <EOL> if authorization_header is not None and not self . cookies : <EOL> session_type = "<STR_LIT>" <EOL> if session_type == "<STR_LIT>" : <EOL> sid = self . cookies . get ( "<STR_LIT>" ) <EOL> if not sid : <EOL> return <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( sid , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> if self . path != "<STR_LIT>" : <EOL> cookie = "<STR_LIT>" <EOL> self . addResponseHeader ( "<STR_LIT>" , cookie ) <EOL> if self . path in INSECURE_PATHS : <EOL> return <EOL> raise NeedLogin ( self , optional = True ) <EOL> user , session_age = row <EOL> if configuration . base . SESSION_MAX_AGE == <NUM_LIT:0> or session_age < configuration . base . SESSION_MAX_AGE : <EOL> self . user = user <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( sid , ) ) <EOL> db . commit ( ) <EOL> else : <EOL> import auth <EOL> import base64 <EOL> self . user = None <EOL> if not authorization_header : return <EOL> authtype , base64_credentials = authorization_header . split ( ) <EOL> if authtype != "<STR_LIT>" : return <EOL> credentials = base64 . b64decode ( base64_credentials ) . split ( "<STR_LIT::>" ) <EOL> if len ( credentials ) < <NUM_LIT:2> : return <EOL> for index in range ( <NUM_LIT:1> , len ( credentials ) ) : <EOL> username = "<STR_LIT::>" . join ( credentials [ : index ] ) <EOL> password = "<STR_LIT::>" . join ( credentials [ index : ] ) <EOL> try : <EOL> auth . checkPassword ( db , username , password ) <EOL> self . user = username <EOL> return <EOL> except auth . CheckFailed : pass <EOL> def getTargetURL ( self ) : <EOL> target = "<STR_LIT:/>" + self . path <EOL> if self . query : <EOL> target += "<STR_LIT:?>" + self . query <EOL> return target <EOL> def getRequestURI ( self ) : <EOL> return wsgiref . util . request_uri ( self . __environ ) <EOL> def getEnvironment ( self ) : <EOL> return self . __environ <EOL> def getUser ( self , db ) : <EOL> import dbutils <EOL> return dbutils . User . fromName ( db , self . user ) <EOL> def getParameter ( self , name , default = NoDefault , filter = lambda value : value ) : <EOL> """<STR_LIT>""" <EOL> value = self . parsed_query . get ( name ) <EOL> if value is None : <EOL> if default is NoDefault : <EOL> raise MissingParameter ( name ) <EOL> return default <EOL> def filter_value ( value ) : <EOL> try : <EOL> return filter ( value ) <EOL> except base . Error : <EOL> raise <EOL> except Exception : <EOL> if filter is int : <EOL> expected = "<STR_LIT>" <EOL> else : <EOL> expected = "<STR_LIT>" <EOL> raise InvalidParameterValue ( name , value , expected ) <EOL> value = [ filter_value ( element ) for element in value ] <EOL> if len ( value ) == <NUM_LIT:1> : <EOL> return value [ <NUM_LIT:0> ] <EOL> return value <EOL> def getParameters ( self ) : <EOL> return { name : value [ <NUM_LIT:0> ] if len ( value ) == <NUM_LIT:1> else value <EOL> for name , value in self . parsed_query . items ( ) } <EOL> def getRequestHeader ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . __environ . get ( "<STR_LIT>" + name . upper ( ) . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) , default ) <EOL> def getRequestHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = { } <EOL> for name , value in self . __environ . items ( ) : <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> headers [ name [ <NUM_LIT:5> : ] . lower ( ) . replace ( "<STR_LIT:_>" , "<STR_LIT:->" ) ] = value <EOL> return headers <EOL> def getReferrer ( self ) : <EOL> try : return self . getRequestHeader ( "<STR_LIT>" ) <EOL> except : return "<STR_LIT>" <EOL> def read ( self , bufsize = None ) : <EOL> """<STR_LIT>""" <EOL> if self . __request_body_length : <EOL> max_bufsize = self . __request_body_length - self . __request_body_read <EOL> if bufsize is None : <EOL> bufsize = max_bufsize <EOL> else : <EOL> bufsize = min ( bufsize , max_bufsize ) <EOL> if "<STR_LIT>" not in self . __environ or not bufsize : <EOL> return "<STR_LIT>" <EOL> data = self . __environ [ "<STR_LIT>" ] . read ( bufsize ) <EOL> self . __request_body_read += len ( data ) <EOL> return data <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . __write ( data ) <EOL> def setStatus ( self , code , message = None ) : <EOL> """<STR_LIT>""" <EOL> assert not self . __started , "<STR_LIT>" <EOL> if message is None : message = httplib . responses [ code ] <EOL> self . __status = "<STR_LIT>" % ( code , message ) <EOL> def hasContentType ( self ) : <EOL> return self . __content_type is not None <EOL> def setContentType ( self , content_type ) : <EOL> """<STR_LIT>""" <EOL> assert not self . __started , "<STR_LIT>" <EOL> if content_type . startswith ( "<STR_LIT>" ) and "<STR_LIT>" not in content_type : content_type += "<STR_LIT>" <EOL> self . __content_type = content_type <EOL> def addResponseHeader ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> assert not self . __started , "<STR_LIT>" <EOL> assert name . lower ( ) != "<STR_LIT>" , "<STR_LIT>" <EOL> self . __response_headers . append ( ( name , value ) ) <EOL> def setCookie ( self , name , value , secure = False ) : <EOL> if secure and configuration . base . ACCESS_SCHEME != "<STR_LIT:http>" : <EOL> modifier = "<STR_LIT>" <EOL> else : <EOL> modifier = "<STR_LIT>" <EOL> self . addResponseHeader ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % ( name , value , modifier ) ) <EOL> def deleteCookie ( self , name ) : <EOL> self . addResponseHeader ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % name ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . __started : <EOL> if self . __status is None : <EOL> self . setStatus ( <NUM_LIT:200> ) <EOL> if self . __content_type is None : <EOL> self . setContentType ( "<STR_LIT>" ) <EOL> headers = [ ( "<STR_LIT:Content-Type>" , self . __content_type ) ] <EOL> headers . extend ( self . __response_headers ) <EOL> self . __write = self . __start_response ( self . __status , headers ) <EOL> self . __started = True <EOL> def isStarted ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __started <EOL> def getContentType ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __content_type <EOL> def ensureSecure ( self ) : <EOL> if configuration . base . ACCESS_SCHEME != "<STR_LIT:http>" : <EOL> current_url = self . getRequestURI ( ) <EOL> secure_url = re . sub ( "<STR_LIT>" , "<STR_LIT>" , current_url ) <EOL> if current_url != secure_url : <EOL> raise MovedTemporarily ( secure_url , True ) <EOL> def requestHTTPAuthentication ( self , realm = "<STR_LIT>" ) : <EOL> self . setStatus ( <NUM_LIT> ) <EOL> self . addResponseHeader ( "<STR_LIT>" , "<STR_LIT>" % realm ) <EOL> self . start ( ) </s>
