<s> from django . middleware . csrf import get_token <EOL> from django . conf import settings <EOL> from django . middleware . locale import LocaleMiddleware <EOL> from django . core . urlresolvers import is_valid_path , resolve , reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils . cache import patch_vary_headers <EOL> from django . utils import translation <EOL> from django . http import Http404 <EOL> from splunkdj . auth . backends import get_user <EOL> from splunkdj . utility import format_local_tzoffset , create_derived_service <EOL> import datetime <EOL> import time <EOL> import logging , logging . handlers <EOL> import rfc822 <EOL> import sys <EOL> import os <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class SplunkCsrfMiddleware ( object ) : <EOL> def process_view ( self , request , * args , ** kwargs ) : <EOL> get_token ( request ) <EOL> return None <EOL> class SplunkResolvedUrlMiddleware ( object ) : <EOL> def process_request ( self , request ) : <EOL> request . app_name = None <EOL> request . url_name = None <EOL> resolved = None <EOL> try : <EOL> resolved = resolve ( request . path_info ) <EOL> except : <EOL> pass <EOL> if resolved : <EOL> request . app_name = resolved . app_name <EOL> request . url_name = resolved . url_name <EOL> return <EOL> class SplunkAppEnabledMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def _verify_app_is_enabled ( self , service , app_name ) : <EOL> if app_name and app_name == '<STR_LIT>' : <EOL> return <EOL> service = create_derived_service ( service , app = None , owner = None ) <EOL> try : <EOL> app = service . apps [ app_name ] <EOL> if app [ '<STR_LIT>' ] == '<STR_LIT:1>' or app [ '<STR_LIT>' ] == '<STR_LIT:0>' : <EOL> raise Http404 ( "<STR_LIT>" % app_name ) <EOL> except : <EOL> raise Http404 ( "<STR_LIT>" % app_name ) <EOL> def process_request ( self , request ) : <EOL> if request . app_name and request . user . is_authenticated ( ) : <EOL> return self . _verify_app_is_enabled ( request . service , request . app_name ) <EOL> return <EOL> class SplunkLocaleMiddleware ( LocaleMiddleware ) : <EOL> def process_response ( self , request , response ) : <EOL> language = translation . get_language ( ) <EOL> if ( response . status_code == <NUM_LIT> and <EOL> not translation . get_language_from_path ( request . path_info ) <EOL> and self . is_language_prefix_patterns_used ( ) ) : <EOL> urlconf = getattr ( request , '<STR_LIT>' , None ) <EOL> language_path = '<STR_LIT>' % ( language , request . path_info ) <EOL> path_valid = is_valid_path ( language_path , urlconf ) <EOL> if ( not path_valid and settings . APPEND_SLASH <EOL> and not language_path . endswith ( '<STR_LIT:/>' ) ) : <EOL> path_valid = is_valid_path ( "<STR_LIT>" % language_path , urlconf ) <EOL> if path_valid : <EOL> path = request . get_full_path ( ) <EOL> script_mount = request . META . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if path . startswith ( script_mount ) : <EOL> path = path . replace ( script_mount , ( "<STR_LIT>" % ( script_mount , language ) ) , <NUM_LIT:1> ) <EOL> language_url = "<STR_LIT>" % ( <EOL> request . is_secure ( ) and '<STR_LIT>' or '<STR_LIT:http>' , <EOL> request . get_host ( ) , path ) <EOL> return HttpResponseRedirect ( language_url ) <EOL> translation . deactivate ( ) <EOL> patch_vary_headers ( response , ( '<STR_LIT>' , ) ) <EOL> if '<STR_LIT>' not in response : <EOL> response [ '<STR_LIT>' ] = language <EOL> return response <EOL> class SplunkWebSessionMiddleware ( object ) : <EOL> def __init__ ( self , port = <NUM_LIT> , ** kwargs ) : <EOL> self . _initialized = False <EOL> self . _storage_path = None <EOL> if not settings . SPLUNK_WEB_INTEGRATED : <EOL> return <EOL> try : <EOL> from lib . util import splunk_to_cherry_cfg , make_absolute <EOL> cherrypy_cfg = splunk_to_cherry_cfg ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> storage_type = cherrypy_cfg . get ( '<STR_LIT>' ) <EOL> storage_path = None <EOL> if storage_type == '<STR_LIT:file>' : <EOL> storage_path = make_absolute ( cherrypy_cfg [ '<STR_LIT>' ] ) <EOL> else : <EOL> return <EOL> self . _storage_path = storage_path <EOL> self . _initialized = True <EOL> except Exception , e : <EOL> self . _initialized = False <EOL> pass <EOL> def process_request ( self , request ) : <EOL> if not self . _initialized or not self . _storage_path : <EOL> return <EOL> user = None <EOL> session = None <EOL> try : <EOL> from lib . sessions import FileSession <EOL> cookie_name = '<STR_LIT>' % settings . SPLUNK_WEB_PORT <EOL> if cookie_name not in request . COOKIES : <EOL> return <EOL> splunkweb_cookie = request . COOKIES [ cookie_name ] <EOL> session = FileSession ( <EOL> splunkweb_cookie , <EOL> storage_path = self . _storage_path , <EOL> timeout = <NUM_LIT> , <EOL> clean_freq = <NUM_LIT:5> <EOL> ) <EOL> session . acquire_lock ( read_lock = True ) <EOL> try : <EOL> if '<STR_LIT>' in session : <EOL> session_key = "<STR_LIT>" % session [ '<STR_LIT>' ] <EOL> username = None <EOL> if '<STR_LIT:user>' in session and '<STR_LIT:name>' in session [ '<STR_LIT:user>' ] : <EOL> username = session [ '<STR_LIT:user>' ] [ '<STR_LIT:name>' ] <EOL> user = get_user ( username , session_key ) <EOL> if user : <EOL> request . _cached_user = user <EOL> finally : <EOL> if not user : <EOL> session . delete ( ) <EOL> session . release_lock ( ) <EOL> except Exception , e : <EOL> logger . exception ( e ) <EOL> pass <EOL> class SplunkDjangoRequestLoggingMiddleware ( object ) : <EOL> error_logger = None <EOL> access_logger = None <EOL> access_log_format = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . access_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> self . error_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def process_response ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> atoms = { '<STR_LIT:h>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:l>' : '<STR_LIT:->' , <EOL> '<STR_LIT:u>' : getattr ( request , '<STR_LIT:user>' , '<STR_LIT:->' ) , <EOL> '<STR_LIT:t>' : self . access_time ( time . time ( ) ) , <EOL> '<STR_LIT:r>' : "<STR_LIT>" % ( request . method , <EOL> request . get_full_path ( ) , <EOL> request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> '<STR_LIT:s>' : response . status_code , <EOL> '<STR_LIT:b>' : len ( response . content ) or '<STR_LIT:->' , <EOL> '<STR_LIT:f>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:a>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> for k , v in atoms . items ( ) : <EOL> if isinstance ( v , unicode ) : <EOL> v = v . encode ( '<STR_LIT:utf8>' ) <EOL> elif not isinstance ( v , str ) : <EOL> v = str ( v ) <EOL> v = repr ( v ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> atoms [ k ] = v . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> try : <EOL> self . access_logger . info ( self . access_log_format % atoms ) <EOL> except : <EOL> error_logger . log ( "<STR_LIT>" ) <EOL> self ( traceback = true ) <EOL> return response <EOL> def access_time ( self , req_time ) : <EOL> now = datetime . datetime . fromtimestamp ( req_time ) <EOL> month = rfc822 . _monthnames [ now . month - <NUM_LIT:1> ] . capitalize ( ) <EOL> return ( '<STR_LIT>' % <EOL> ( now . day , month , now . year , now . hour , now . minute , now . second , <EOL> now . microsecond / <NUM_LIT:1000> , format_local_tzoffset ( req_time ) ) ) </s>
