<s> from werkzeug import url_decode <EOL> class HTTPMethodOverrideMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> bodyless_methods = frozenset ( [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , app , header_name = None , <EOL> querystring_param = None , allowed_methods = None ) : <EOL> header_name = header_name or '<STR_LIT>' <EOL> self . app = app <EOL> self . header_name = '<STR_LIT>' + header_name . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> self . querystring_param = querystring_param or '<STR_LIT>' <EOL> self . allowed_methods = frozenset ( <EOL> allowed_methods or <EOL> [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> def _get_from_querystring ( self , environ ) : <EOL> if self . querystring_param in environ . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> args = url_decode ( environ [ '<STR_LIT>' ] ) <EOL> return args . get ( self . querystring_param ) <EOL> return None <EOL> def _get_method_override ( self , environ ) : <EOL> return environ . get ( self . header_name ) or self . _get_from_querystring ( environ ) or '<STR_LIT>' <EOL> def __call__ ( self , environ , start_response ) : <EOL> method = self . _get_method_override ( environ ) . upper ( ) <EOL> if method in self . allowed_methods : <EOL> method = method . encode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> environ [ '<STR_LIT>' ] = method <EOL> if method in self . bodyless_methods : <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> return self . app ( environ , start_response ) </s>
