<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:input>" , "<STR_LIT:data>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:OK>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<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>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> import sys , cgi , Cookie , pprint , urlparse , urllib <EOL> from utils import storage , storify , threadeddict , dictadd , intget , safestr <EOL> config = storage ( ) <EOL> config . __doc__ = """<STR_LIT>""" <EOL> class HTTPError ( Exception ) : <EOL> def __init__ ( self , status , headers = { } , data = "<STR_LIT>" ) : <EOL> ctx . status = status <EOL> for k , v in headers . items ( ) : <EOL> header ( k , v ) <EOL> self . data = data <EOL> Exception . __init__ ( self , status ) <EOL> def _status_code ( status , data = None , classname = None , docstring = None ) : <EOL> if data is None : <EOL> data = status . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> classname = status . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> docstring = docstring or '<STR_LIT>' % status <EOL> def __init__ ( self , data = data , headers = { } ) : <EOL> HTTPError . __init__ ( self , status , headers , data ) <EOL> return type ( classname , ( HTTPError , object ) , { <EOL> '<STR_LIT>' : docstring , <EOL> '<STR_LIT>' : __init__ <EOL> } ) <EOL> ok = OK = _status_code ( "<STR_LIT>" , data = "<STR_LIT>" ) <EOL> created = Created = _status_code ( "<STR_LIT>" ) <EOL> accepted = Accepted = _status_code ( "<STR_LIT>" ) <EOL> class Redirect ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , status = '<STR_LIT>' , absolute = False ) : <EOL> """<STR_LIT>""" <EOL> newloc = urlparse . urljoin ( ctx . path , url ) <EOL> if newloc . startswith ( '<STR_LIT:/>' ) : <EOL> if absolute : <EOL> home = ctx . realhome <EOL> else : <EOL> home = ctx . home <EOL> newloc = home + newloc <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : newloc <EOL> } <EOL> HTTPError . __init__ ( self , status , headers , "<STR_LIT>" ) <EOL> redirect = Redirect <EOL> class Found ( Redirect ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , absolute = False ) : <EOL> Redirect . __init__ ( self , url , '<STR_LIT>' , absolute = absolute ) <EOL> found = Found <EOL> class SeeOther ( Redirect ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , absolute = False ) : <EOL> Redirect . __init__ ( self , url , '<STR_LIT>' , absolute = absolute ) <EOL> seeother = SeeOther <EOL> class NotModified ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> HTTPError . __init__ ( self , "<STR_LIT>" ) <EOL> notmodified = NotModified <EOL> class TempRedirect ( Redirect ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , absolute = False ) : <EOL> Redirect . __init__ ( self , url , '<STR_LIT>' , absolute = absolute ) <EOL> tempredirect = TempRedirect <EOL> class BadRequest ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self , message = None ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , message or self . message ) <EOL> badrequest = BadRequest <EOL> class Unauthorized ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> unauthorized = Unauthorized <EOL> class Forbidden ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> forbidden = Forbidden <EOL> class _NotFound ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self , message = None ) : <EOL> status = '<STR_LIT>' <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , message or self . message ) <EOL> def NotFound ( message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> return _NotFound ( message ) <EOL> elif ctx . get ( '<STR_LIT>' ) : <EOL> return ctx . app_stack [ - <NUM_LIT:1> ] . notfound ( ) <EOL> else : <EOL> return _NotFound ( ) <EOL> notfound = NotFound <EOL> class NoMethod ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls = None ) : <EOL> status = '<STR_LIT>' <EOL> headers = { } <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if cls : <EOL> methods = [ method for method in methods if hasattr ( cls , method ) ] <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( methods ) <EOL> data = None <EOL> HTTPError . __init__ ( self , status , headers , data ) <EOL> nomethod = NoMethod <EOL> class NotAcceptable ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> notacceptable = NotAcceptable <EOL> class Conflict ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> conflict = Conflict <EOL> class Gone ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = '<STR_LIT>' <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> gone = Gone <EOL> class PreconditionFailed ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> preconditionfailed = PreconditionFailed <EOL> class UnsupportedMediaType ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> status = "<STR_LIT>" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , self . message ) <EOL> unsupportedmediatype = UnsupportedMediaType <EOL> class _InternalError ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> def __init__ ( self , message = None ) : <EOL> status = '<STR_LIT>' <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> HTTPError . __init__ ( self , status , headers , message or self . message ) <EOL> def InternalError ( message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> return _InternalError ( message ) <EOL> elif ctx . get ( '<STR_LIT>' ) : <EOL> return ctx . app_stack [ - <NUM_LIT:1> ] . internalerror ( ) <EOL> else : <EOL> return _InternalError ( ) <EOL> internalerror = InternalError <EOL> def header ( hdr , value , unique = False ) : <EOL> """<STR_LIT>""" <EOL> hdr , value = safestr ( hdr ) , safestr ( value ) <EOL> if '<STR_LIT:\n>' in hdr or '<STR_LIT:\r>' in hdr or '<STR_LIT:\n>' in value or '<STR_LIT:\r>' in value : <EOL> raise ValueError , '<STR_LIT>' <EOL> if unique is True : <EOL> for h , v in ctx . headers : <EOL> if h . lower ( ) == hdr . lower ( ) : return <EOL> ctx . headers . append ( ( hdr , value ) ) <EOL> def rawinput ( method = None ) : <EOL> """<STR_LIT>""" <EOL> method = method or "<STR_LIT>" <EOL> from cStringIO import StringIO <EOL> def dictify ( fs ) : <EOL> if fs . list is None : <EOL> fs . list = [ ] <EOL> return dict ( [ ( k , fs [ k ] ) for k in fs . keys ( ) ] ) <EOL> e = ctx . env . copy ( ) <EOL> a = b = { } <EOL> if method . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if e [ '<STR_LIT>' ] in [ '<STR_LIT:POST>' , '<STR_LIT>' ] : <EOL> if e . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> a = ctx . get ( '<STR_LIT>' ) <EOL> if not a : <EOL> fp = e [ '<STR_LIT>' ] <EOL> a = cgi . FieldStorage ( fp = fp , environ = e , keep_blank_values = <NUM_LIT:1> ) <EOL> ctx . _fieldstorage = a <EOL> else : <EOL> fp = StringIO ( data ( ) ) <EOL> a = cgi . FieldStorage ( fp = fp , environ = e , keep_blank_values = <NUM_LIT:1> ) <EOL> a = dictify ( a ) <EOL> if method . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> e [ '<STR_LIT>' ] = '<STR_LIT:GET>' <EOL> b = dictify ( cgi . FieldStorage ( environ = e , keep_blank_values = <NUM_LIT:1> ) ) <EOL> def process_fieldstorage ( fs ) : <EOL> if isinstance ( fs , list ) : <EOL> return [ process_fieldstorage ( x ) for x in fs ] <EOL> elif fs . filename is None : <EOL> return fs . value <EOL> else : <EOL> return fs <EOL> return storage ( [ ( k , process_fieldstorage ( v ) ) for k , v in dictadd ( b , a ) . items ( ) ] ) <EOL> def input ( * requireds , ** defaults ) : <EOL> """<STR_LIT>""" <EOL> _method = defaults . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> out = rawinput ( _method ) <EOL> try : <EOL> defaults . setdefault ( '<STR_LIT>' , True ) <EOL> return storify ( out , * requireds , ** defaults ) <EOL> except KeyError : <EOL> raise badrequest ( ) <EOL> def data ( ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:data>' not in ctx : <EOL> cl = intget ( ctx . env . get ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> ctx . data = ctx . env [ '<STR_LIT>' ] . read ( cl ) <EOL> return ctx . data <EOL> def setcookie ( name , value , expires = '<STR_LIT>' , domain = None , <EOL> secure = False , httponly = False , path = None ) : <EOL> """<STR_LIT>""" <EOL> morsel = Cookie . Morsel ( ) <EOL> name , value = safestr ( name ) , safestr ( value ) <EOL> morsel . set ( name , value , urllib . quote ( value ) ) <EOL> if expires < <NUM_LIT:0> : <EOL> expires = - <NUM_LIT> <EOL> morsel [ '<STR_LIT>' ] = expires <EOL> morsel [ '<STR_LIT:path>' ] = path or ctx . homepath + '<STR_LIT:/>' <EOL> if domain : <EOL> morsel [ '<STR_LIT>' ] = domain <EOL> if secure : <EOL> morsel [ '<STR_LIT>' ] = secure <EOL> value = morsel . OutputString ( ) <EOL> if httponly : <EOL> value += '<STR_LIT>' <EOL> header ( '<STR_LIT>' , value ) <EOL> def decode_cookie ( value ) : <EOL> r"""<STR_LIT>""" <EOL> try : <EOL> return unicode ( value , '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> try : <EOL> return unicode ( value , '<STR_LIT:utf-8>' ) <EOL> except UnicodeError : <EOL> return unicode ( value , '<STR_LIT>' , '<STR_LIT:ignore>' ) <EOL> def parse_cookies ( http_cookie ) : <EOL> r"""<STR_LIT>""" <EOL> if '<STR_LIT:">' in http_cookie : <EOL> cookie = Cookie . SimpleCookie ( ) <EOL> try : <EOL> cookie . load ( http_cookie ) <EOL> except Cookie . CookieError : <EOL> cookie = Cookie . SimpleCookie ( ) <EOL> for attr_value in http_cookie . split ( '<STR_LIT:;>' ) : <EOL> try : <EOL> cookie . load ( attr_value ) <EOL> except Cookie . CookieError : <EOL> pass <EOL> cookies = dict ( ( k , urllib . unquote ( v . value ) ) for k , v in cookie . iteritems ( ) ) <EOL> else : <EOL> cookies = { } <EOL> for key_value in http_cookie . split ( '<STR_LIT:;>' ) : <EOL> key_value = key_value . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( key_value ) == <NUM_LIT:2> : <EOL> key , value = key_value <EOL> cookies [ key . strip ( ) ] = urllib . unquote ( value . strip ( ) ) <EOL> return cookies <EOL> def cookies ( * requireds , ** defaults ) : <EOL> r"""<STR_LIT>""" <EOL> if defaults . get ( "<STR_LIT>" ) is True : <EOL> defaults [ '<STR_LIT>' ] = decode_cookie <EOL> if '<STR_LIT>' not in ctx : <EOL> http_cookie = ctx . env . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ctx . _parsed_cookies = parse_cookies ( http_cookie ) <EOL> try : <EOL> return storify ( ctx . _parsed_cookies , * requireds , ** defaults ) <EOL> except KeyError : <EOL> badrequest ( ) <EOL> raise StopIteration <EOL> def debug ( * args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> out = ctx . environ [ '<STR_LIT>' ] <EOL> except : <EOL> out = sys . stderr <EOL> for arg in args : <EOL> print >> out , pprint . pformat ( arg ) <EOL> return '<STR_LIT>' <EOL> def _debugwrite ( x ) : <EOL> try : <EOL> out = ctx . environ [ '<STR_LIT>' ] <EOL> except : <EOL> out = sys . stderr <EOL> out . write ( x ) <EOL> debug . write = _debugwrite <EOL> ctx = context = threadeddict ( ) <EOL> ctx . __doc__ = """<STR_LIT>""" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
