<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import urllib <EOL> import urlparse <EOL> import posixpath <EOL> import mimetypes <EOL> from itertools import chain <EOL> from zlib import adler32 <EOL> from time import time , mktime <EOL> from datetime import datetime <EOL> from werkzeug . _internal import _patch_wrapper <EOL> from werkzeug . http import is_resource_modified , http_date <EOL> def responder ( f ) : <EOL> """<STR_LIT>""" <EOL> return _patch_wrapper ( f , lambda * a : f ( * a ) ( * a [ - <NUM_LIT:2> : ] ) ) <EOL> def get_current_url ( environ , root_only = False , strip_querystring = False , <EOL> host_only = False ) : <EOL> """<STR_LIT>""" <EOL> tmp = [ environ [ '<STR_LIT>' ] , '<STR_LIT>' , get_host ( environ ) ] <EOL> cat = tmp . append <EOL> if host_only : <EOL> return '<STR_LIT>' . join ( tmp ) + '<STR_LIT:/>' <EOL> cat ( urllib . quote ( environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . rstrip ( '<STR_LIT:/>' ) ) ) <EOL> if root_only : <EOL> cat ( '<STR_LIT:/>' ) <EOL> else : <EOL> cat ( urllib . quote ( '<STR_LIT:/>' + environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) ) ) <EOL> if not strip_querystring : <EOL> qs = environ . get ( '<STR_LIT>' ) <EOL> if qs : <EOL> cat ( '<STR_LIT:?>' + qs ) <EOL> return '<STR_LIT>' . join ( tmp ) <EOL> def get_host ( environ ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in environ : <EOL> return environ [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in environ : <EOL> return environ [ '<STR_LIT>' ] <EOL> result = environ [ '<STR_LIT>' ] <EOL> if ( environ [ '<STR_LIT>' ] , environ [ '<STR_LIT>' ] ) not in ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:http>' , '<STR_LIT>' ) ) : <EOL> result += '<STR_LIT::>' + environ [ '<STR_LIT>' ] <EOL> return result <EOL> def pop_path_info ( environ ) : <EOL> """<STR_LIT>""" <EOL> path = environ . get ( '<STR_LIT>' ) <EOL> if not path : <EOL> return None <EOL> script_name = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> old_path = path <EOL> path = path . lstrip ( '<STR_LIT:/>' ) <EOL> if path != old_path : <EOL> script_name += '<STR_LIT:/>' * ( len ( old_path ) - len ( path ) ) <EOL> if '<STR_LIT:/>' not in path : <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> environ [ '<STR_LIT>' ] = script_name + path <EOL> return path <EOL> segment , path = path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT:/>' + path <EOL> environ [ '<STR_LIT>' ] = script_name + segment <EOL> return segment <EOL> def peek_path_info ( environ ) : <EOL> """<STR_LIT>""" <EOL> segments = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if segments : <EOL> return segments [ <NUM_LIT:0> ] <EOL> def extract_path_info ( environ_or_baseurl , path_or_url , charset = '<STR_LIT:utf-8>' , <EOL> errors = '<STR_LIT:replace>' , collapse_http_schemes = True ) : <EOL> """<STR_LIT>""" <EOL> from werkzeug . urls import uri_to_iri , url_fix <EOL> def _as_iri ( obj ) : <EOL> if not isinstance ( obj , unicode ) : <EOL> return uri_to_iri ( obj , charset , errors ) <EOL> return obj <EOL> def _normalize_netloc ( scheme , netloc ) : <EOL> parts = netloc . split ( u'<STR_LIT:@>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] . split ( u'<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> netloc , port = parts <EOL> if ( scheme == u'<STR_LIT:http>' and port == u'<STR_LIT>' ) or ( scheme == u'<STR_LIT>' and port == u'<STR_LIT>' ) : <EOL> port = None <EOL> else : <EOL> netloc = parts [ <NUM_LIT:0> ] <EOL> port = None <EOL> if port is not None : <EOL> netloc += u'<STR_LIT::>' + port <EOL> return netloc <EOL> path = _as_iri ( path_or_url ) <EOL> if isinstance ( environ_or_baseurl , dict ) : <EOL> environ_or_baseurl = get_current_url ( environ_or_baseurl , <EOL> root_only = True ) <EOL> base_iri = _as_iri ( environ_or_baseurl ) <EOL> base_scheme , base_netloc , base_path , = urlparse . urlsplit ( base_iri ) [ : <NUM_LIT:3> ] <EOL> cur_scheme , cur_netloc , cur_path , = urlparse . urlsplit ( urlparse . urljoin ( base_iri , path ) ) [ : <NUM_LIT:3> ] <EOL> base_netloc = _normalize_netloc ( base_scheme , base_netloc ) <EOL> cur_netloc = _normalize_netloc ( cur_scheme , cur_netloc ) <EOL> if collapse_http_schemes : <EOL> for scheme in base_scheme , cur_scheme : <EOL> if scheme not in ( u'<STR_LIT:http>' , u'<STR_LIT>' ) : <EOL> return None <EOL> else : <EOL> if not ( base_scheme in ( u'<STR_LIT:http>' , u'<STR_LIT>' ) and base_scheme == cur_scheme ) : <EOL> return None <EOL> if base_netloc != cur_netloc : <EOL> return None <EOL> base_path = base_path . rstrip ( u'<STR_LIT:/>' ) <EOL> if not cur_path . startswith ( base_path ) : <EOL> return None <EOL> return u'<STR_LIT:/>' + cur_path [ len ( base_path ) : ] . lstrip ( u'<STR_LIT:/>' ) <EOL> class SharedDataMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , exports , disallow = None , cache = True , <EOL> cache_timeout = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:12> , fallback_mimetype = '<STR_LIT>' ) : <EOL> self . app = app <EOL> self . exports = { } <EOL> self . cache = cache <EOL> self . cache_timeout = cache_timeout <EOL> for key , value in exports . iteritems ( ) : <EOL> if isinstance ( value , tuple ) : <EOL> loader = self . get_package_loader ( * value ) <EOL> elif isinstance ( value , basestring ) : <EOL> if os . path . isfile ( value ) : <EOL> loader = self . get_file_loader ( value ) <EOL> else : <EOL> loader = self . get_directory_loader ( value ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % value ) <EOL> self . exports [ key ] = loader <EOL> if disallow is not None : <EOL> from fnmatch import fnmatch <EOL> self . is_allowed = lambda x : not fnmatch ( x , disallow ) <EOL> self . fallback_mimetype = fallback_mimetype <EOL> def is_allowed ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def _opener ( self , filename ) : <EOL> return lambda : ( <EOL> open ( filename , '<STR_LIT:rb>' ) , <EOL> datetime . utcfromtimestamp ( os . path . getmtime ( filename ) ) , <EOL> int ( os . path . getsize ( filename ) ) <EOL> ) <EOL> def get_file_loader ( self , filename ) : <EOL> return lambda x : ( os . path . basename ( filename ) , self . _opener ( filename ) ) <EOL> def get_package_loader ( self , package , package_path ) : <EOL> from pkg_resources import DefaultProvider , ResourceManager , get_provider <EOL> loadtime = datetime . utcnow ( ) <EOL> provider = get_provider ( package ) <EOL> manager = ResourceManager ( ) <EOL> filesystem_bound = isinstance ( provider , DefaultProvider ) <EOL> def loader ( path ) : <EOL> if path is None : <EOL> return None , None <EOL> path = posixpath . join ( package_path , path ) <EOL> if not provider . has_resource ( path ) : <EOL> return None , None <EOL> basename = posixpath . basename ( path ) <EOL> if filesystem_bound : <EOL> return basename , self . _opener ( <EOL> provider . get_resource_filename ( manager , path ) ) <EOL> return basename , lambda : ( <EOL> provider . get_resource_stream ( manager , path ) , <EOL> loadtime , <EOL> <NUM_LIT:0> <EOL> ) <EOL> return loader <EOL> def get_directory_loader ( self , directory ) : <EOL> def loader ( path ) : <EOL> if path is not None : <EOL> path = os . path . join ( directory , path ) <EOL> else : <EOL> path = directory <EOL> if os . path . isfile ( path ) : <EOL> return os . path . basename ( path ) , self . _opener ( path ) <EOL> return None , None <EOL> return loader <EOL> def generate_etag ( self , mtime , file_size , real_filename ) : <EOL> return '<STR_LIT>' % ( <EOL> mktime ( mtime . timetuple ( ) ) , <EOL> file_size , <EOL> adler32 ( real_filename ) & <NUM_LIT> <EOL> ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> cleaned_path = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( '<STR_LIT:/>' ) <EOL> for sep in os . sep , os . altsep : <EOL> if sep and sep != '<STR_LIT:/>' : <EOL> cleaned_path = cleaned_path . replace ( sep , '<STR_LIT:/>' ) <EOL> path = '<STR_LIT:/>' . join ( [ '<STR_LIT>' ] + [ x for x in cleaned_path . split ( '<STR_LIT:/>' ) <EOL> if x and x != '<STR_LIT:..>' ] ) <EOL> file_loader = None <EOL> for search_path , loader in self . exports . iteritems ( ) : <EOL> if search_path == path : <EOL> real_filename , file_loader = loader ( None ) <EOL> if file_loader is not None : <EOL> break <EOL> if not search_path . endswith ( '<STR_LIT:/>' ) : <EOL> search_path += '<STR_LIT:/>' <EOL> if path . startswith ( search_path ) : <EOL> real_filename , file_loader = loader ( path [ len ( search_path ) : ] ) <EOL> if file_loader is not None : <EOL> break <EOL> if file_loader is None or not self . is_allowed ( real_filename ) : <EOL> return self . app ( environ , start_response ) <EOL> guessed_type = mimetypes . guess_type ( real_filename ) <EOL> mime_type = guessed_type [ <NUM_LIT:0> ] or self . fallback_mimetype <EOL> f , mtime , file_size = file_loader ( ) <EOL> headers = [ ( '<STR_LIT>' , http_date ( ) ) ] <EOL> if self . cache : <EOL> timeout = self . cache_timeout <EOL> etag = self . generate_etag ( mtime , file_size , real_filename ) <EOL> headers += [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' % etag ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' % timeout ) <EOL> ] <EOL> if not is_resource_modified ( environ , etag , last_modified = mtime ) : <EOL> f . close ( ) <EOL> start_response ( '<STR_LIT>' , headers ) <EOL> return [ ] <EOL> headers . append ( ( '<STR_LIT>' , http_date ( time ( ) + timeout ) ) ) <EOL> else : <EOL> headers . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> headers . extend ( ( <EOL> ( '<STR_LIT:Content-Type>' , mime_type ) , <EOL> ( '<STR_LIT>' , str ( file_size ) ) , <EOL> ( '<STR_LIT>' , http_date ( mtime ) ) <EOL> ) ) <EOL> start_response ( '<STR_LIT>' , headers ) <EOL> return wrap_file ( environ , f ) <EOL> class DispatcherMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , mounts = None ) : <EOL> self . app = app <EOL> self . mounts = mounts or { } <EOL> def __call__ ( self , environ , start_response ) : <EOL> script = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> path_info = '<STR_LIT>' <EOL> while '<STR_LIT:/>' in script : <EOL> if script in self . mounts : <EOL> app = self . mounts [ script ] <EOL> break <EOL> items = script . split ( '<STR_LIT:/>' ) <EOL> script = '<STR_LIT:/>' . join ( items [ : - <NUM_LIT:1> ] ) <EOL> path_info = '<STR_LIT>' % ( items [ - <NUM_LIT:1> ] , path_info ) <EOL> else : <EOL> app = self . mounts . get ( script , self . app ) <EOL> original_script_name = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> environ [ '<STR_LIT>' ] = original_script_name + script <EOL> environ [ '<STR_LIT>' ] = path_info <EOL> return app ( environ , start_response ) <EOL> class ClosingIterator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterable , callbacks = None ) : <EOL> iterator = iter ( iterable ) <EOL> self . _next = iterator . next <EOL> if callbacks is None : <EOL> callbacks = [ ] <EOL> elif callable ( callbacks ) : <EOL> callbacks = [ callbacks ] <EOL> else : <EOL> callbacks = list ( callbacks ) <EOL> iterable_close = getattr ( iterator , '<STR_LIT>' , None ) <EOL> if iterable_close : <EOL> callbacks . insert ( <NUM_LIT:0> , iterable_close ) <EOL> self . _callbacks = callbacks <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . _next ( ) <EOL> def close ( self ) : <EOL> for callback in self . _callbacks : <EOL> callback ( ) <EOL> def wrap_file ( environ , file , buffer_size = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return environ . get ( '<STR_LIT>' , FileWrapper ) ( file , buffer_size ) <EOL> class FileWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file , buffer_size = <NUM_LIT> ) : <EOL> self . file = file <EOL> self . buffer_size = buffer_size <EOL> def close ( self ) : <EOL> if hasattr ( self . file , '<STR_LIT>' ) : <EOL> self . file . close ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> data = self . file . read ( self . buffer_size ) <EOL> if data : <EOL> return data <EOL> raise StopIteration ( ) <EOL> def make_limited_stream ( stream , limit ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( stream , LimitedStream ) : <EOL> if limit is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> stream = LimitedStream ( stream , limit ) <EOL> return stream <EOL> def make_line_iter ( stream , limit = None , buffer_size = <NUM_LIT:10> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> stream = make_limited_stream ( stream , limit ) <EOL> def _iter_basic_lines ( ) : <EOL> _read = stream . read <EOL> buffer = [ ] <EOL> while <NUM_LIT:1> : <EOL> if len ( buffer ) > <NUM_LIT:1> : <EOL> yield buffer . pop ( ) <EOL> continue <EOL> chunks = _read ( buffer_size ) . splitlines ( True ) <EOL> chunks . reverse ( ) <EOL> first_chunk = buffer and buffer [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> if chunks : <EOL> if first_chunk and first_chunk [ - <NUM_LIT:1> ] in '<STR_LIT:\r\n>' : <EOL> yield first_chunk <EOL> first_chunk = '<STR_LIT>' <EOL> first_chunk += chunks . pop ( ) <EOL> else : <EOL> yield first_chunk <EOL> break <EOL> buffer = chunks <EOL> if not buffer and first_chunk [ - <NUM_LIT:1> ] not in '<STR_LIT:\r\n>' : <EOL> buffer = [ first_chunk ] <EOL> else : <EOL> yield first_chunk <EOL> previous = '<STR_LIT>' <EOL> for item in _iter_basic_lines ( ) : <EOL> if item == '<STR_LIT:\n>' and previous [ - <NUM_LIT:1> : ] == '<STR_LIT:\r>' : <EOL> previous += '<STR_LIT:\n>' <EOL> item = '<STR_LIT>' <EOL> if previous : <EOL> yield previous <EOL> previous = item <EOL> if previous : <EOL> yield previous <EOL> def make_chunk_iter ( stream , separator , limit = None , buffer_size = <NUM_LIT:10> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> stream = make_limited_stream ( stream , limit ) <EOL> _read = stream . read <EOL> _split = re . compile ( r'<STR_LIT>' % re . escape ( separator ) ) . split <EOL> buffer = [ ] <EOL> while <NUM_LIT:1> : <EOL> new_data = _read ( buffer_size ) <EOL> if not new_data : <EOL> break <EOL> chunks = _split ( new_data ) <EOL> new_buf = [ ] <EOL> for item in chain ( buffer , chunks ) : <EOL> if item == separator : <EOL> yield '<STR_LIT>' . join ( new_buf ) <EOL> new_buf = [ ] <EOL> else : <EOL> new_buf . append ( item ) <EOL> buffer = new_buf <EOL> if buffer : <EOL> yield '<STR_LIT>' . join ( buffer ) <EOL> class LimitedStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream , limit , silent = True ) : <EOL> self . _read = stream . read <EOL> self . _readline = stream . readline <EOL> self . _pos = <NUM_LIT:0> <EOL> self . limit = limit <EOL> self . silent = silent <EOL> if not silent : <EOL> from warnings import warn <EOL> warn ( DeprecationWarning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , stacklevel = <NUM_LIT:2> ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> @ property <EOL> def is_exhausted ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pos >= self . limit <EOL> def on_exhausted ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . silent : <EOL> return '<STR_LIT>' <EOL> from werkzeug . exceptions import BadRequest <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> def on_disconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> from werkzeug . exceptions import ClientDisconnected <EOL> raise ClientDisconnected ( ) <EOL> def exhaust ( self , chunk_size = <NUM_LIT> * <NUM_LIT:16> ) : <EOL> """<STR_LIT>""" <EOL> to_read = self . limit - self . _pos <EOL> chunk = chunk_size <EOL> while to_read > <NUM_LIT:0> : <EOL> chunk = min ( to_read , chunk ) <EOL> self . read ( chunk ) <EOL> to_read -= chunk <EOL> def read ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _pos >= self . limit : <EOL> return self . on_exhausted ( ) <EOL> if size is None or size == - <NUM_LIT:1> : <EOL> size = self . limit <EOL> to_read = min ( self . limit - self . _pos , size ) <EOL> try : <EOL> read = self . _read ( to_read ) <EOL> except ( IOError , ValueError ) : <EOL> return self . on_disconnect ( ) <EOL> if to_read and len ( read ) != to_read : <EOL> return self . on_disconnect ( ) <EOL> self . _pos += len ( read ) <EOL> return read <EOL> def readline ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _pos >= self . limit : <EOL> return self . on_exhausted ( ) <EOL> if size is None : <EOL> size = self . limit - self . _pos <EOL> else : <EOL> size = min ( size , self . limit - self . _pos ) <EOL> try : <EOL> line = self . _readline ( size ) <EOL> except ( ValueError , IOError ) : <EOL> return self . on_disconnect ( ) <EOL> if size and not line : <EOL> return self . on_disconnect ( ) <EOL> self . _pos += len ( line ) <EOL> return line <EOL> def readlines ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> last_pos = self . _pos <EOL> result = [ ] <EOL> if size is not None : <EOL> end = min ( self . limit , last_pos + size ) <EOL> else : <EOL> end = self . limit <EOL> while <NUM_LIT:1> : <EOL> if size is not None : <EOL> size -= last_pos - self . _pos <EOL> if self . _pos >= end : <EOL> break <EOL> result . append ( self . readline ( size ) ) <EOL> if size is not None : <EOL> last_pos = self . _pos <EOL> return result <EOL> def next ( self ) : <EOL> line = self . readline ( ) <EOL> if line is None : <EOL> raise StopIteration ( ) <EOL> return line </s>
