<s> import __builtin__ <EOL> import functools <EOL> import json <EOL> import logging <EOL> import yaml <EOL> from google . appengine . api import urlfetch <EOL> from django . conf import settings <EOL> from django . core . exceptions import MiddlewareNotUsed <EOL> class ApiSecurityException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def find_argument_index ( function , argument ) : <EOL> args = function . func_code . co_varnames [ : function . func_code . co_argcount ] <EOL> return args . index ( argument ) <EOL> def get_default_argument ( function , argument ) : <EOL> argument_index = find_argument_index ( function , argument ) <EOL> num_positional_args = ( function . func_code . co_argcount - len ( function . func_defaults ) ) <EOL> default_position = argument_index - num_positional_args <EOL> if default_position < <NUM_LIT:0> : <EOL> return None <EOL> return function . func_defaults [ default_position ] <EOL> def replace_default_argument ( function , argument , replacement ) : <EOL> argument_index = find_argument_index ( function , argument ) <EOL> num_positional_args = ( function . func_code . co_argcount - <EOL> len ( function . func_defaults ) ) <EOL> default_position = argument_index - num_positional_args <EOL> if default_position < <NUM_LIT:0> : <EOL> raise ApiSecurityException ( '<STR_LIT>' ) <EOL> new_defaults = list ( function . func_defaults ) <EOL> new_defaults [ default_position ] = replacement <EOL> function . func_defaults = tuple ( new_defaults ) <EOL> _JSON_CHARACTER_REPLACEMENT_MAPPING = [ <EOL> ( '<STR_LIT:<>' , '<STR_LIT>' % ord ( '<STR_LIT:<>' ) ) , <EOL> ( '<STR_LIT:>>' , '<STR_LIT>' % ord ( '<STR_LIT:>>' ) ) , <EOL> ( '<STR_LIT:&>' , '<STR_LIT>' % ord ( '<STR_LIT:&>' ) ) , <EOL> ] <EOL> class _JsonEncoderForHtml ( json . JSONEncoder ) : <EOL> def encode ( self , o ) : <EOL> chunks = self . iterencode ( o , _one_shot = True ) <EOL> if not isinstance ( chunks , ( list , tuple ) ) : <EOL> chunks = list ( chunks ) <EOL> return '<STR_LIT>' . join ( chunks ) <EOL> def iterencode ( self , o , _one_shot = False ) : <EOL> chunks = super ( _JsonEncoderForHtml , self ) . iterencode ( o , _one_shot ) <EOL> for chunk in chunks : <EOL> for ( character , replacement ) in _JSON_CHARACTER_REPLACEMENT_MAPPING : <EOL> chunk = chunk . replace ( character , replacement ) <EOL> yield chunk <EOL> def _HttpUrlLoggingWrapper ( func ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( func ) <EOL> def _CheckAndLog ( * args , ** kwargs ) : <EOL> try : <EOL> arg_index = find_argument_index ( func , '<STR_LIT:url>' ) <EOL> except ValueError : <EOL> return func ( * args , ** kwargs ) <EOL> if arg_index < len ( args ) : <EOL> arg_value = args [ arg_index ] <EOL> elif '<STR_LIT:url>' in kwargs : <EOL> arg_value = kwargs [ '<STR_LIT:url>' ] <EOL> elif '<STR_LIT:url>' not in kwargs : <EOL> arg_value = get_default_argument ( func , '<STR_LIT:url>' ) <EOL> if arg_value and not arg_value . startswith ( '<STR_LIT>' ) : <EOL> logging . warn ( '<STR_LIT>' , arg_value ) <EOL> return func ( * args , ** kwargs ) <EOL> return _CheckAndLog <EOL> PATCHES_APPLIED = False <EOL> class AppEngineSecurityMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> global PATCHES_APPLIED <EOL> if not PATCHES_APPLIED : <EOL> replace_default_argument ( json . dump , '<STR_LIT>' , _JsonEncoderForHtml ) <EOL> replace_default_argument ( json . dumps , '<STR_LIT>' , _JsonEncoderForHtml ) <EOL> replace_default_argument ( yaml . compose , '<STR_LIT>' , yaml . loader . SafeLoader ) <EOL> replace_default_argument ( yaml . compose_all , '<STR_LIT>' , yaml . loader . SafeLoader ) <EOL> replace_default_argument ( yaml . load , '<STR_LIT>' , yaml . loader . SafeLoader ) <EOL> replace_default_argument ( yaml . load_all , '<STR_LIT>' , yaml . loader . SafeLoader ) <EOL> replace_default_argument ( yaml . parse , '<STR_LIT>' , yaml . loader . SafeLoader ) <EOL> replace_default_argument ( yaml . scan , '<STR_LIT>' , yaml . loader . SafeLoader ) <EOL> replace_default_argument ( urlfetch . fetch , '<STR_LIT>' , True ) <EOL> replace_default_argument ( urlfetch . make_fetch_call , '<STR_LIT>' , True ) <EOL> urlfetch . fetch = _HttpUrlLoggingWrapper ( urlfetch . fetch ) <EOL> urlfetch . make_fetch_call = _HttpUrlLoggingWrapper ( urlfetch . make_fetch_call ) <EOL> for setting in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if not getattr ( settings , setting , False ) : <EOL> logging . warning ( "<STR_LIT>" , setting ) <EOL> PATCHES_APPLIED = True <EOL> raise MiddlewareNotUsed ( ) </s>
