<s> from __future__ import absolute_import , unicode_literals , division <EOL> import six <EOL> from armet import http <EOL> import flask <EOL> from flask . globals import current_app <EOL> from werkzeug . wsgi import get_current_url <EOL> class RequestHeaders ( http . request . Headers ) : <EOL> def __init__ ( self , handle ) : <EOL> self . _handle = handle <EOL> super ( RequestHeaders , self ) . __init__ ( ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . _handle . headers [ name ] <EOL> def __iter__ ( self ) : <EOL> return ( key for key , _ in self . _handle . headers ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _handle . headers ) <EOL> def __contains__ ( self , name ) : <EOL> return name in self . _handle . headers <EOL> class Request ( http . Request ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> request = flask . request <EOL> async = kwargs [ '<STR_LIT>' ] <EOL> self . _handle = request if not async else flask . Request ( request . environ ) <EOL> self . headers = RequestHeaders ( self . _handle ) <EOL> kwargs . update ( method = request . method ) <EOL> super ( Request , self ) . __init__ ( * args , ** kwargs ) <EOL> def _read ( self ) : <EOL> return self . _handle . stream . read ( ) <EOL> @ property <EOL> def protocol ( self ) : <EOL> return self . _handle . scheme . upper ( ) <EOL> @ property <EOL> def mount_point ( self ) : <EOL> path = self . _handle . path <EOL> return path [ : path . rfind ( self . path ) ] if self . path else path <EOL> @ property <EOL> def query ( self ) : <EOL> if isinstance ( self . _handle . query_string , six . binary_type ) : <EOL> return self . _handle . query_string . decode ( '<STR_LIT:utf8>' ) <EOL> return self . _handle . query_string <EOL> @ property <EOL> def uri ( self ) : <EOL> return get_current_url ( self . _handle . environ ) <EOL> class ResponseHeaders ( http . response . Headers ) : <EOL> def __init__ ( self , response , handle ) : <EOL> self . _response = response <EOL> self . _handle = handle <EOL> super ( ResponseHeaders , self ) . __init__ ( ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . _handle . headers [ name ] <EOL> def __setitem__ ( self , name , value ) : <EOL> self . _response . require_open ( ) <EOL> self . _handle . headers [ self . normalize ( name ) ] = value <EOL> def __contains__ ( self , name ) : <EOL> return name in self . _handle . headers <EOL> def __delitem__ ( self , name ) : <EOL> self . _response . require_open ( ) <EOL> del self . _handle . headers [ name ] <EOL> def __iter__ ( self ) : <EOL> return ( key for key , _ in self . _handle . headers ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _handle . headers ) <EOL> class Response ( http . Response ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _handle = current_app . response_class ( ) <EOL> super ( Response , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . asynchronous : <EOL> from gevent import queue <EOL> self . _queue = queue . Queue ( ) <EOL> self . headers = ResponseHeaders ( self , self . _handle ) <EOL> def __iter__ ( self ) : <EOL> return self . _queue <EOL> @ property <EOL> def status ( self ) : <EOL> return self . _handle . status_code <EOL> @ status . setter <EOL> def status ( self , value ) : <EOL> self . require_open ( ) <EOL> self . _handle . status_code = value <EOL> @ http . Response . body . setter <EOL> def body ( self , value ) : <EOL> if value : <EOL> if self . asynchronous : <EOL> super ( Response , Response ) . body . __set__ ( self , None ) <EOL> self . _queue . put ( value ) <EOL> return <EOL> super ( Response , Response ) . body . __set__ ( self , value ) <EOL> def close ( self ) : <EOL> super ( Response , self ) . close ( ) <EOL> if self . asynchronous : <EOL> self . _queue . put ( StopIteration ) </s>
