<s> import json <EOL> import os <EOL> import re <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . db . transaction import non_atomic_requests <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . shortcuts import render <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . views . decorators . http import require_POST <EOL> import commonware . log <EOL> import waffle <EOL> from django_statsd . clients import statsd <EOL> from olympia import amo , legacy_api <EOL> from olympia . amo . utils import log_cef <EOL> from . import monitors <EOL> log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> monitor_log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> jp_log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> flash_re = re . compile ( r'<STR_LIT>' , <EOL> re . IGNORECASE ) <EOL> quicktime_re = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> java_re = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> wmp_re = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> @ never_cache <EOL> @ non_atomic_requests <EOL> def monitor ( request , format = None ) : <EOL> status_summary = { } <EOL> results = { } <EOL> checks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:path>' , <EOL> '<STR_LIT>' ] <EOL> for check in checks : <EOL> with statsd . timer ( '<STR_LIT>' % check ) as timer : <EOL> status , result = getattr ( monitors , check ) ( ) <EOL> status_summary [ check ] = { '<STR_LIT:state>' : not status , <EOL> '<STR_LIT:status>' : status } <EOL> results [ '<STR_LIT>' % check ] = result <EOL> results [ '<STR_LIT>' % check ] = timer . ms <EOL> status_code = <NUM_LIT:200> if all ( a [ '<STR_LIT:state>' ] <EOL> for a in status_summary . values ( ) ) else <NUM_LIT> <EOL> if format == '<STR_LIT>' : <EOL> return http . HttpResponse ( json . dumps ( status_summary ) , <EOL> status = status_code ) <EOL> ctx = { } <EOL> ctx . update ( results ) <EOL> ctx [ '<STR_LIT>' ] = status_summary <EOL> return render ( request , '<STR_LIT>' , ctx , status = status_code ) <EOL> @ non_atomic_requests <EOL> def robots ( request ) : <EOL> """<STR_LIT>""" <EOL> _service = ( request . META [ '<STR_LIT>' ] == settings . SERVICES_DOMAIN ) <EOL> if _service or not settings . ENGAGE_ROBOTS : <EOL> template = "<STR_LIT>" <EOL> else : <EOL> template = render ( request , '<STR_LIT>' , { '<STR_LIT>' : amo . APP_USAGE } ) <EOL> return HttpResponse ( template , content_type = "<STR_LIT>" ) <EOL> @ non_atomic_requests <EOL> def contribute ( request ) : <EOL> path = os . path . join ( settings . ROOT , '<STR_LIT>' ) <EOL> return HttpResponse ( open ( path , '<STR_LIT:rb>' ) , content_type = '<STR_LIT:application/json>' ) <EOL> @ non_atomic_requests <EOL> def handler403 ( request ) : <EOL> if request . path_info . startswith ( '<STR_LIT>' ) : <EOL> return legacy_api . views . handler403 ( request ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , status = <NUM_LIT> ) <EOL> @ non_atomic_requests <EOL> def handler404 ( request ) : <EOL> if request . path_info . startswith ( '<STR_LIT>' ) : <EOL> return legacy_api . views . handler404 ( request ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , status = <NUM_LIT> ) <EOL> @ non_atomic_requests <EOL> def handler500 ( request ) : <EOL> if request . path_info . startswith ( '<STR_LIT>' ) : <EOL> return legacy_api . views . handler500 ( request ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , status = <NUM_LIT> ) <EOL> @ non_atomic_requests <EOL> def csrf_failure ( request , reason = '<STR_LIT>' ) : <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT>' in reason } , status = <NUM_LIT> ) <EOL> @ non_atomic_requests <EOL> def loaded ( request ) : <EOL> return http . HttpResponse ( '<STR_LIT:%s>' % request . META [ '<STR_LIT>' ] , <EOL> content_type = '<STR_LIT>' ) <EOL> @ csrf_exempt <EOL> @ require_POST <EOL> @ non_atomic_requests <EOL> def cspreport ( request ) : <EOL> """<STR_LIT>""" <EOL> report = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not waffle . sample_is_active ( '<STR_LIT>' ) : <EOL> return HttpResponse ( ) <EOL> try : <EOL> v = json . loads ( request . body ) [ '<STR_LIT>' ] <EOL> meta = request . META . copy ( ) <EOL> meta [ '<STR_LIT>' ] = v . get ( '<STR_LIT>' , meta [ '<STR_LIT>' ] ) <EOL> v = [ ( k , v [ k ] ) for k in report if k in v ] <EOL> log_cef ( '<STR_LIT>' , <NUM_LIT:5> , meta , username = request . user , <EOL> signature = '<STR_LIT>' , <EOL> msg = '<STR_LIT>' , <EOL> cs6 = v , cs6Label = '<STR_LIT>' ) <EOL> except ( KeyError , ValueError ) , e : <EOL> log . debug ( '<STR_LIT>' % e , exc_info = True ) <EOL> return HttpResponseBadRequest ( ) <EOL> return HttpResponse ( ) <EOL> @ non_atomic_requests <EOL> def version ( request ) : <EOL> path = os . path . join ( settings . ROOT , '<STR_LIT>' ) <EOL> return HttpResponse ( open ( path , '<STR_LIT:rb>' ) , content_type = '<STR_LIT:application/json>' ) </s>
