<s> import weakref <EOL> from xierpa3 . toolbox . parsers . c_json import cjson <EOL> from xierpa3 . descriptors . state import State <EOL> from xierpa3 . toolbox . formdict import FormDict <EOL> import urllib <EOL> class Environment ( State ) : <EOL> u"""<STR_LIT>""" <EOL> HTTP_HOST = '<STR_LIT>' <EOL> HTTPHEADERS = '<STR_LIT>' <EOL> COOKIES = '<STR_LIT>' <EOL> PATH = '<STR_LIT:path>' <EOL> FULLPATH = '<STR_LIT>' <EOL> PARAM_UPLOADPREFIX = '<STR_LIT>' <EOL> HTTPKEYS = { <EOL> PATH : '<STR_LIT>' , <EOL> HTTPHEADERS : '<STR_LIT>' , <EOL> COOKIES : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , ** kwargs ) : <EOL> State . __init__ ( self ) <EOL> for key , value in kwargs . items ( ) : <EOL> self [ key ] = value <EOL> self . initializeForm ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __getitem__ ( self , key ) : <EOL> return self . __dict__ . get ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . __dict__ [ key ] = value <EOL> def initializeForm ( self ) : <EOL> u"""<STR_LIT>""" <EOL> self . form = FormDict ( ) <EOL> self . handleGetPostParams ( ) <EOL> self . handleUrlParams ( ) <EOL> def addComponent ( self , component ) : <EOL> selector = component . selector <EOL> if component . id == selector and self . _components . has_key ( selector ) : <EOL> print '<STR_LIT>' % selector <EOL> if not self . _components . has_key ( selector ) : <EOL> self . _components [ selector ] = [ ] <EOL> self . _components [ selector ] . append ( weakref . ref ( component ) ) <EOL> def setParams ( self , params ) : <EOL> u"""<STR_LIT>""" <EOL> if params : <EOL> params = params [ <NUM_LIT:0> ] <EOL> self . params = params <EOL> def handleGetPostParams ( self ) : <EOL> u"""<STR_LIT>""" <EOL> try : <EOL> path = self . request . path <EOL> args = self . request . args <EOL> except AttributeError : <EOL> return <EOL> self [ self . PATH ] = path <EOL> for key in args . keys ( ) : <EOL> uvalues = [ ] <EOL> for value in args [ key ] : <EOL> if not key . startswith ( self . PARAM_UPLOADPREFIX ) : <EOL> value = value . decode ( '<STR_LIT:utf-8>' ) <EOL> if not self . isCodeInjection ( value ) : <EOL> uvalues . append ( value ) <EOL> self . form [ key ] = uvalues <EOL> """<STR_LIT>""" <EOL> def isCodeInjection ( self , value ) : <EOL> u"""<STR_LIT>""" <EOL> if value . startswith ( '<STR_LIT:<>' ) : <EOL> return True <EOL> elif value . startswith ( '<STR_LIT:">' ) : <EOL> return True <EOL> return False <EOL> def handleUrlParams ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> path = self . request . path <EOL> except AttributeError : <EOL> return <EOL> path = urllib . unquote ( path ) <EOL> for query in path . split ( '<STR_LIT:/>' ) : <EOL> if not query : <EOL> continue <EOL> parts = query . split ( '<STR_LIT:->' ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> self . form [ query ] = '<STR_LIT:1>' <EOL> else : <EOL> newparts = [ ] <EOL> for part in parts [ <NUM_LIT:1> : ] : <EOL> if not self . isCodeInjection ( part ) : <EOL> newparts . append ( part ) <EOL> self . form [ parts [ <NUM_LIT:0> ] ] = '<STR_LIT:->' . join ( newparts ) <EOL> def getRequestValue ( self , key ) : <EOL> u"""<STR_LIT>""" <EOL> return self . request . received_headers [ key ] <EOL> def getPort ( self ) : <EOL> return self . request . host . port <EOL> def getHost ( self ) : <EOL> port = self . getPort ( ) <EOL> if port == <NUM_LIT> : <EOL> port = '<STR_LIT>' <EOL> else : <EOL> port = '<STR_LIT>' % port <EOL> return self . getDomain ( ) + port <EOL> def getDomain ( self ) : <EOL> return self . request . received_headers . _headers . _rawHeaders [ '<STR_LIT:host>' ] [ <NUM_LIT:0> ] . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> def getFullUrl ( self ) : <EOL> return self . getHostUrl ( ) + self . getFullPath ( ) <EOL> def getHostUrl ( self ) : <EOL> return self . getPrefix ( ) + self . getHost ( ) <EOL> def isSsl ( self ) : <EOL> return False <EOL> def getPrefix ( self ) : <EOL> if self . isSsl ( ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def getFullPath ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return self [ self . PATH ] or '<STR_LIT:/>' <EOL> def getServer ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return self [ self . HTTP_HOST ] <EOL> def getServerClient ( self ) : <EOL> u"""<STR_LIT>""" <EOL> try : <EOL> return self . request . transport . server . factory . resource <EOL> except : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def getUserAgent ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return self . request . received_headers [ '<STR_LIT>' ] <EOL> def setHeader ( self , name , value ) : <EOL> self . request . setHeader ( name , value ) <EOL> def getCookies ( self ) : <EOL> u"""<STR_LIT>""" <EOL> import urllib <EOL> result = { } <EOL> for k , v in self . request . received_cookies . items ( ) : <EOL> try : <EOL> result [ k ] = cjson . decode ( urllib . unquote ( v ) ) <EOL> except : <EOL> result [ k ] = v <EOL> return result <EOL> def getCookie ( self , name ) : <EOL> u"""<STR_LIT>""" <EOL> try : <EOL> return self . getCookies ( ) . get ( name ) <EOL> except AttributeError : <EOL> pass <EOL> return None <EOL> def setCookie ( self , value = None , expires = None , url = '<STR_LIT:/>' , name = None , ** args ) : <EOL> u"""<STR_LIT>""" <EOL> import datetime , urllib <EOL> if not name : <EOL> name = self . COOKIENAME <EOL> if expires and not isinstance ( expires , basestring ) : <EOL> try : <EOL> days = int ( expires ) <EOL> if days > <NUM_LIT:0> : <EOL> expiry = datetime . datetime . now ( ) + datetime . timedelta ( days = days ) <EOL> expires = expiry . strftime ( "<STR_LIT>" ) <EOL> except : <EOL> expires = None <EOL> value = urllib . quote ( cjson . encode ( value ) ) <EOL> self . request . addCookie ( name , value , expires = expires , path = url , ** args ) <EOL> self . request . received_cookies [ name ] = value </s>
