<s> """<STR_LIT>""" <EOL> try : <EOL> from io import DEFAULT_BUFFER_SIZE <EOL> except ImportError : <EOL> DEFAULT_BUFFER_SIZE = <NUM_LIT> <EOL> import re <EOL> import sys <EOL> import tempfile <EOL> try : <EOL> from urllib import unquote_plus <EOL> except ImportError : <EOL> def unquote_plus ( bs ) : <EOL> """<STR_LIT>""" <EOL> bs = bs . replace ( ntob ( '<STR_LIT:+>' ) , ntob ( '<STR_LIT:U+0020>' ) ) <EOL> atoms = bs . split ( ntob ( '<STR_LIT:%>' ) ) <EOL> for i in range ( <NUM_LIT:1> , len ( atoms ) ) : <EOL> item = atoms [ i ] <EOL> try : <EOL> pct = int ( item [ : <NUM_LIT:2> ] , <NUM_LIT:16> ) <EOL> atoms [ i ] = bytes ( [ pct ] ) + item [ <NUM_LIT:2> : ] <EOL> except ValueError : <EOL> pass <EOL> return ntob ( '<STR_LIT>' ) . join ( atoms ) <EOL> import cherrypy <EOL> from cherrypy . _cpcompat import basestring , ntob , ntou <EOL> from cherrypy . lib import httputil <EOL> def process_urlencoded ( entity ) : <EOL> """<STR_LIT>""" <EOL> qs = entity . fp . read ( ) <EOL> for charset in entity . attempt_charsets : <EOL> try : <EOL> params = { } <EOL> for aparam in qs . split ( ntob ( '<STR_LIT:&>' ) ) : <EOL> for pair in aparam . split ( ntob ( '<STR_LIT:;>' ) ) : <EOL> if not pair : <EOL> continue <EOL> atoms = pair . split ( ntob ( '<STR_LIT:=>' ) , <NUM_LIT:1> ) <EOL> if len ( atoms ) == <NUM_LIT:1> : <EOL> atoms . append ( ntob ( '<STR_LIT>' ) ) <EOL> key = unquote_plus ( atoms [ <NUM_LIT:0> ] ) . decode ( charset ) <EOL> value = unquote_plus ( atoms [ <NUM_LIT:1> ] ) . decode ( charset ) <EOL> if key in params : <EOL> if not isinstance ( params [ key ] , list ) : <EOL> params [ key ] = [ params [ key ] ] <EOL> params [ key ] . append ( value ) <EOL> else : <EOL> params [ key ] = value <EOL> except UnicodeDecodeError : <EOL> pass <EOL> else : <EOL> entity . charset = charset <EOL> break <EOL> else : <EOL> raise cherrypy . HTTPError ( <EOL> <NUM_LIT> , "<STR_LIT>" <EOL> "<STR_LIT>" % repr ( entity . attempt_charsets ) ) <EOL> for key , value in params . items ( ) : <EOL> if key in entity . params : <EOL> if not isinstance ( entity . params [ key ] , list ) : <EOL> entity . params [ key ] = [ entity . params [ key ] ] <EOL> entity . params [ key ] . append ( value ) <EOL> else : <EOL> entity . params [ key ] = value <EOL> def process_multipart ( entity ) : <EOL> """<STR_LIT>""" <EOL> ib = "<STR_LIT>" <EOL> if '<STR_LIT>' in entity . content_type . params : <EOL> ib = entity . content_type . params [ '<STR_LIT>' ] . strip ( '<STR_LIT:">' ) <EOL> if not re . match ( "<STR_LIT>" , ib ) : <EOL> raise ValueError ( '<STR_LIT>' % ( ib , ) ) <EOL> ib = ( '<STR_LIT>' + ib ) . encode ( '<STR_LIT:ascii>' ) <EOL> while True : <EOL> b = entity . readline ( ) <EOL> if not b : <EOL> return <EOL> b = b . strip ( ) <EOL> if b == ib : <EOL> break <EOL> while True : <EOL> part = entity . part_class . from_fp ( entity . fp , ib ) <EOL> entity . parts . append ( part ) <EOL> part . process ( ) <EOL> if part . fp . done : <EOL> break <EOL> def process_multipart_form_data ( entity ) : <EOL> """<STR_LIT>""" <EOL> process_multipart ( entity ) <EOL> kept_parts = [ ] <EOL> for part in entity . parts : <EOL> if part . name is None : <EOL> kept_parts . append ( part ) <EOL> else : <EOL> if part . filename is None : <EOL> value = part . fullvalue ( ) <EOL> else : <EOL> value = part <EOL> if part . name in entity . params : <EOL> if not isinstance ( entity . params [ part . name ] , list ) : <EOL> entity . params [ part . name ] = [ entity . params [ part . name ] ] <EOL> entity . params [ part . name ] . append ( value ) <EOL> else : <EOL> entity . params [ part . name ] = value <EOL> entity . parts = kept_parts <EOL> def _old_process_multipart ( entity ) : <EOL> """<STR_LIT>""" <EOL> process_multipart ( entity ) <EOL> params = entity . params <EOL> for part in entity . parts : <EOL> if part . name is None : <EOL> key = ntou ( '<STR_LIT>' ) <EOL> else : <EOL> key = part . name <EOL> if part . filename is None : <EOL> value = part . fullvalue ( ) <EOL> else : <EOL> value = part <EOL> if key in params : <EOL> if not isinstance ( params [ key ] , list ) : <EOL> params [ key ] = [ params [ key ] ] <EOL> params [ key ] . append ( value ) <EOL> else : <EOL> params [ key ] = value <EOL> class Entity ( object ) : <EOL> """<STR_LIT>""" <EOL> attempt_charsets = [ '<STR_LIT:utf-8>' ] <EOL> """<STR_LIT>""" <EOL> charset = None <EOL> """<STR_LIT>""" <EOL> content_type = None <EOL> """<STR_LIT>""" <EOL> default_content_type = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> filename = None <EOL> """<STR_LIT>""" <EOL> fp = None <EOL> """<STR_LIT>""" <EOL> headers = None <EOL> """<STR_LIT>""" <EOL> length = None <EOL> """<STR_LIT>""" <EOL> name = None <EOL> """<STR_LIT>""" <EOL> params = None <EOL> """<STR_LIT>""" <EOL> processors = { '<STR_LIT>' : process_urlencoded , <EOL> '<STR_LIT>' : process_multipart_form_data , <EOL> '<STR_LIT>' : process_multipart , <EOL> } <EOL> """<STR_LIT>""" <EOL> parts = None <EOL> """<STR_LIT>""" <EOL> part_class = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fp , headers , params = None , parts = None ) : <EOL> self . processors = self . processors . copy ( ) <EOL> self . fp = fp <EOL> self . headers = headers <EOL> if params is None : <EOL> params = { } <EOL> self . params = params <EOL> if parts is None : <EOL> parts = [ ] <EOL> self . parts = parts <EOL> self . content_type = headers . elements ( '<STR_LIT:Content-Type>' ) <EOL> if self . content_type : <EOL> self . content_type = self . content_type [ <NUM_LIT:0> ] <EOL> else : <EOL> self . content_type = httputil . HeaderElement . from_str ( <EOL> self . default_content_type ) <EOL> dec = self . content_type . params . get ( "<STR_LIT>" , None ) <EOL> if dec : <EOL> self . attempt_charsets = [ dec ] + [ c for c in self . attempt_charsets <EOL> if c != dec ] <EOL> else : <EOL> self . attempt_charsets = self . attempt_charsets [ : ] <EOL> self . length = None <EOL> clen = headers . get ( '<STR_LIT>' , None ) <EOL> if clen is not None and '<STR_LIT>' not in headers . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> self . length = int ( clen ) <EOL> except ValueError : <EOL> pass <EOL> self . name = None <EOL> self . filename = None <EOL> disp = headers . elements ( '<STR_LIT>' ) <EOL> if disp : <EOL> disp = disp [ <NUM_LIT:0> ] <EOL> if '<STR_LIT:name>' in disp . params : <EOL> self . name = disp . params [ '<STR_LIT:name>' ] <EOL> if self . name . startswith ( '<STR_LIT:">' ) and self . name . endswith ( '<STR_LIT:">' ) : <EOL> self . name = self . name [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if '<STR_LIT:filename>' in disp . params : <EOL> self . filename = disp . params [ '<STR_LIT:filename>' ] <EOL> if self . filename . startswith ( '<STR_LIT:">' ) and self . filename . endswith ( '<STR_LIT:">' ) : <EOL> self . filename = self . filename [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> type = property ( lambda self : self . content_type , <EOL> doc = """<STR_LIT>""" ) <EOL> def read ( self , size = None , fp_out = None ) : <EOL> return self . fp . read ( size , fp_out ) <EOL> def readline ( self , size = None ) : <EOL> return self . fp . readline ( size ) <EOL> def readlines ( self , sizehint = None ) : <EOL> return self . fp . readlines ( sizehint ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> line = self . readline ( ) <EOL> if not line : <EOL> raise StopIteration <EOL> return line <EOL> def next ( self ) : <EOL> return self . __next__ ( ) <EOL> def read_into_file ( self , fp_out = None ) : <EOL> """<STR_LIT>""" <EOL> if fp_out is None : <EOL> fp_out = self . make_file ( ) <EOL> self . read ( fp_out = fp_out ) <EOL> return fp_out <EOL> def make_file ( self ) : <EOL> """<STR_LIT>""" <EOL> return tempfile . TemporaryFile ( ) <EOL> def fullvalue ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . file : <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> value = self . file . read ( ) <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> else : <EOL> value = self . value <EOL> return value <EOL> def process ( self ) : <EOL> """<STR_LIT>""" <EOL> proc = None <EOL> ct = self . content_type . value <EOL> try : <EOL> proc = self . processors [ ct ] <EOL> except KeyError : <EOL> toptype = ct . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> try : <EOL> proc = self . processors [ toptype ] <EOL> except KeyError : <EOL> pass <EOL> if proc is None : <EOL> self . default_proc ( ) <EOL> else : <EOL> proc ( self ) <EOL> def default_proc ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Part ( Entity ) : <EOL> """<STR_LIT>""" <EOL> attempt_charsets = [ '<STR_LIT>' , '<STR_LIT:utf-8>' ] <EOL> """<STR_LIT>""" <EOL> boundary = None <EOL> """<STR_LIT>""" <EOL> default_content_type = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> maxrambytes = <NUM_LIT:1000> <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fp , headers , boundary ) : <EOL> Entity . __init__ ( self , fp , headers ) <EOL> self . boundary = boundary <EOL> self . file = None <EOL> self . value = None <EOL> def from_fp ( cls , fp , boundary ) : <EOL> headers = cls . read_headers ( fp ) <EOL> return cls ( fp , headers , boundary ) <EOL> from_fp = classmethod ( from_fp ) <EOL> def read_headers ( cls , fp ) : <EOL> headers = httputil . HeaderMap ( ) <EOL> while True : <EOL> line = fp . readline ( ) <EOL> if not line : <EOL> raise EOFError ( "<STR_LIT>" ) <EOL> if line == ntob ( '<STR_LIT:\r\n>' ) : <EOL> break <EOL> if not line . endswith ( ntob ( '<STR_LIT:\r\n>' ) ) : <EOL> raise ValueError ( "<STR_LIT>" % line ) <EOL> if line [ <NUM_LIT:0> ] in ntob ( '<STR_LIT>' ) : <EOL> v = line . strip ( ) . decode ( '<STR_LIT>' ) <EOL> else : <EOL> k , v = line . split ( ntob ( "<STR_LIT::>" ) , <NUM_LIT:1> ) <EOL> k = k . strip ( ) . decode ( '<STR_LIT>' ) <EOL> v = v . strip ( ) . decode ( '<STR_LIT>' ) <EOL> existing = headers . get ( k ) <EOL> if existing : <EOL> v = "<STR_LIT:U+002CU+0020>" . join ( ( existing , v ) ) <EOL> headers [ k ] = v <EOL> return headers <EOL> read_headers = classmethod ( read_headers ) <EOL> def read_lines_to_boundary ( self , fp_out = None ) : <EOL> """<STR_LIT>""" <EOL> endmarker = self . boundary + ntob ( "<STR_LIT>" ) <EOL> delim = ntob ( "<STR_LIT>" ) <EOL> prev_lf = True <EOL> lines = [ ] <EOL> seen = <NUM_LIT:0> <EOL> while True : <EOL> line = self . fp . readline ( <NUM_LIT:1> << <NUM_LIT:16> ) <EOL> if not line : <EOL> raise EOFError ( "<STR_LIT>" ) <EOL> if line . startswith ( ntob ( "<STR_LIT>" ) ) and prev_lf : <EOL> strippedline = line . strip ( ) <EOL> if strippedline == self . boundary : <EOL> break <EOL> if strippedline == endmarker : <EOL> self . fp . finish ( ) <EOL> break <EOL> line = delim + line <EOL> if line . endswith ( ntob ( "<STR_LIT:\r\n>" ) ) : <EOL> delim = ntob ( "<STR_LIT:\r\n>" ) <EOL> line = line [ : - <NUM_LIT:2> ] <EOL> prev_lf = True <EOL> elif line . endswith ( ntob ( "<STR_LIT:\n>" ) ) : <EOL> delim = ntob ( "<STR_LIT:\n>" ) <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> prev_lf = True <EOL> else : <EOL> delim = ntob ( "<STR_LIT>" ) <EOL> prev_lf = False <EOL> if fp_out is None : <EOL> lines . append ( line ) <EOL> seen += len ( line ) <EOL> if seen > self . maxrambytes : <EOL> fp_out = self . make_file ( ) <EOL> for line in lines : <EOL> fp_out . write ( line ) <EOL> else : <EOL> fp_out . write ( line ) <EOL> if fp_out is None : <EOL> result = ntob ( '<STR_LIT>' ) . join ( lines ) <EOL> for charset in self . attempt_charsets : <EOL> try : <EOL> result = result . decode ( charset ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> else : <EOL> self . charset = charset <EOL> return result <EOL> else : <EOL> raise cherrypy . HTTPError ( <EOL> <NUM_LIT> , "<STR_LIT>" <EOL> "<STR_LIT>" % repr ( self . attempt_charsets ) ) <EOL> else : <EOL> fp_out . seek ( <NUM_LIT:0> ) <EOL> return fp_out <EOL> def default_proc ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . filename : <EOL> self . file = self . read_into_file ( ) <EOL> else : <EOL> result = self . read_lines_to_boundary ( ) <EOL> if isinstance ( result , basestring ) : <EOL> self . value = result <EOL> else : <EOL> self . file = result <EOL> def read_into_file ( self , fp_out = None ) : <EOL> """<STR_LIT>""" <EOL> if fp_out is None : <EOL> fp_out = self . make_file ( ) <EOL> self . read_lines_to_boundary ( fp_out = fp_out ) <EOL> return fp_out <EOL> Entity . part_class = Part <EOL> try : <EOL> inf = float ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> class Infinity ( object ) : <EOL> def __cmp__ ( self , other ) : <EOL> return <NUM_LIT:1> <EOL> def __sub__ ( self , other ) : <EOL> return self <EOL> inf = Infinity ( ) <EOL> comma_separated_headers = [ '<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>' , <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> class SizedReader : <EOL> def __init__ ( self , fp , length , maxbytes , bufsize = DEFAULT_BUFFER_SIZE , has_trailers = False ) : <EOL> self . fp = fp <EOL> self . length = length <EOL> self . maxbytes = maxbytes <EOL> self . buffer = ntob ( '<STR_LIT>' ) <EOL> self . bufsize = bufsize <EOL> self . bytes_read = <NUM_LIT:0> <EOL> self . done = False <EOL> self . has_trailers = has_trailers <EOL> def read ( self , size = None , fp_out = None ) : <EOL> """<STR_LIT>""" <EOL> if self . length is None : <EOL> if size is None : <EOL> remaining = inf <EOL> else : <EOL> remaining = size <EOL> else : <EOL> remaining = self . length - self . bytes_read <EOL> if size and size < remaining : <EOL> remaining = size <EOL> if remaining == <NUM_LIT:0> : <EOL> self . finish ( ) <EOL> if fp_out is None : <EOL> return ntob ( '<STR_LIT>' ) <EOL> else : <EOL> return None <EOL> chunks = [ ] <EOL> if self . buffer : <EOL> if remaining is inf : <EOL> data = self . buffer <EOL> self . buffer = ntob ( '<STR_LIT>' ) <EOL> else : <EOL> data = self . buffer [ : remaining ] <EOL> self . buffer = self . buffer [ remaining : ] <EOL> datalen = len ( data ) <EOL> remaining -= datalen <EOL> self . bytes_read += datalen <EOL> if self . maxbytes and self . bytes_read > self . maxbytes : <EOL> raise cherrypy . HTTPError ( <NUM_LIT> ) <EOL> if fp_out is None : <EOL> chunks . append ( data ) <EOL> else : <EOL> fp_out . write ( data ) <EOL> while remaining > <NUM_LIT:0> : <EOL> chunksize = min ( remaining , self . bufsize ) <EOL> try : <EOL> data = self . fp . read ( chunksize ) <EOL> except Exception : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . __class__ . __name__ == '<STR_LIT>' : <EOL> raise cherrypy . HTTPError ( <EOL> <NUM_LIT> , "<STR_LIT>" % e . args [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise <EOL> if not data : <EOL> self . finish ( ) <EOL> break <EOL> datalen = len ( data ) <EOL> remaining -= datalen <EOL> self . bytes_read += datalen <EOL> if self . maxbytes and self . bytes_read > self . maxbytes : <EOL> raise cherrypy . HTTPError ( <NUM_LIT> ) <EOL> if fp_out is None : <EOL> chunks . append ( data ) <EOL> else : <EOL> fp_out . write ( data ) <EOL> if fp_out is None : <EOL> return ntob ( '<STR_LIT>' ) . join ( chunks ) <EOL> def readline ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> chunks = [ ] <EOL> while size is None or size > <NUM_LIT:0> : <EOL> chunksize = self . bufsize <EOL> if size is not None and size < self . bufsize : <EOL> chunksize = size <EOL> data = self . read ( chunksize ) <EOL> if not data : <EOL> break <EOL> pos = data . find ( ntob ( '<STR_LIT:\n>' ) ) + <NUM_LIT:1> <EOL> if pos : <EOL> chunks . append ( data [ : pos ] ) <EOL> remainder = data [ pos : ] <EOL> self . buffer += remainder <EOL> self . bytes_read -= len ( remainder ) <EOL> break <EOL> else : <EOL> chunks . append ( data ) <EOL> return ntob ( '<STR_LIT>' ) . join ( chunks ) <EOL> def readlines ( self , sizehint = None ) : <EOL> """<STR_LIT>""" <EOL> if self . length is not None : <EOL> if sizehint is None : <EOL> sizehint = self . length - self . bytes_read <EOL> else : <EOL> sizehint = min ( sizehint , self . length - self . bytes_read ) <EOL> lines = [ ] <EOL> seen = <NUM_LIT:0> <EOL> while True : <EOL> line = self . readline ( ) <EOL> if not line : <EOL> break <EOL> lines . append ( line ) <EOL> seen += len ( line ) <EOL> if seen >= sizehint : <EOL> break <EOL> return lines <EOL> def finish ( self ) : <EOL> self . done = True <EOL> if self . has_trailers and hasattr ( self . fp , '<STR_LIT>' ) : <EOL> self . trailers = { } <EOL> try : <EOL> for line in self . fp . read_trailer_lines ( ) : <EOL> if line [ <NUM_LIT:0> ] in ntob ( '<STR_LIT>' ) : <EOL> v = line . strip ( ) <EOL> else : <EOL> try : <EOL> k , v = line . split ( ntob ( "<STR_LIT::>" ) , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> k = k . strip ( ) . title ( ) <EOL> v = v . strip ( ) <EOL> if k in comma_separated_headers : <EOL> existing = self . trailers . get ( envname ) <EOL> if existing : <EOL> v = ntob ( "<STR_LIT:U+002CU+0020>" ) . join ( ( existing , v ) ) <EOL> self . trailers [ k ] = v <EOL> except Exception : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . __class__ . __name__ == '<STR_LIT>' : <EOL> raise cherrypy . HTTPError ( <EOL> <NUM_LIT> , "<STR_LIT>" % e . args [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise <EOL> class RequestBody ( Entity ) : <EOL> """<STR_LIT>""" <EOL> bufsize = <NUM_LIT:8> * <NUM_LIT> <EOL> """<STR_LIT>""" <EOL> default_content_type = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> maxbytes = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fp , headers , params = None , request_params = None ) : <EOL> Entity . __init__ ( self , fp , headers , params ) <EOL> if self . content_type . value . startswith ( '<STR_LIT>' ) : <EOL> for c in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if c in self . attempt_charsets : <EOL> break <EOL> else : <EOL> self . attempt_charsets . append ( '<STR_LIT>' ) <EOL> self . processors [ '<STR_LIT>' ] = _old_process_multipart <EOL> if request_params is None : <EOL> request_params = { } <EOL> self . request_params = request_params <EOL> def process ( self ) : <EOL> """<STR_LIT>""" <EOL> h = cherrypy . serving . request . headers <EOL> if '<STR_LIT>' not in h and '<STR_LIT>' not in h : <EOL> raise cherrypy . HTTPError ( <NUM_LIT> ) <EOL> self . fp = SizedReader ( self . fp , self . length , <EOL> self . maxbytes , bufsize = self . bufsize , <EOL> has_trailers = '<STR_LIT>' in h ) <EOL> super ( RequestBody , self ) . process ( ) <EOL> request_params = self . request_params <EOL> for key , value in self . params . items ( ) : <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> if isinstance ( key , unicode ) : <EOL> key = key . encode ( '<STR_LIT>' ) <EOL> if key in request_params : <EOL> if not isinstance ( request_params [ key ] , list ) : <EOL> request_params [ key ] = [ request_params [ key ] ] <EOL> request_params [ key ] . append ( value ) <EOL> else : <EOL> request_params [ key ] = value </s>
