<s> from cStringIO import StringIO <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import AnonymousUser <EOL> from django . contrib . sites . models import Site <EOL> from django . core . urlresolvers import resolve , reverse <EOL> from django . db import connections <EOL> from django . http import Http404 , HttpResponseRedirect , HttpResponsePermanentRedirect , get_host <EOL> from etc import cache <EOL> from etc . constants import ACCOUNT_COOKIE_SALT , ACCOUNT_COOKIE , SOURCE_COOKIE <EOL> from etc . user import check_cookie <EOL> import logging , os , sys , tempfile <EOL> import logging . handlers <EOL> import re <EOL> from users . models import User <EOL> from urlparse import urlsplit , urlunsplit <EOL> class DetectUserMiddleware ( object ) : <EOL> def process_request ( self , request ) : <EOL> if ACCOUNT_COOKIE in request . COOKIES and ACCOUNT_COOKIE_SALT in request . COOKIES : <EOL> if check_cookie ( request . COOKIES [ ACCOUNT_COOKIE ] , request . COOKIES [ ACCOUNT_COOKIE_SALT ] ) : <EOL> try : <EOL> request . user = cache . get ( User , int ( request . COOKIES [ ACCOUNT_COOKIE ] ) ) <EOL> return None <EOL> except : <EOL> pass <EOL> request . user = AnonymousUser ( ) <EOL> return None <EOL> class AddExceptionMessageMiddleware ( object ) : <EOL> def process_exception ( self , request , exception ) : <EOL> request . exception = exception <EOL> return None <EOL> class ConsoleExceptionMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_exception ( self , request , exception ) : <EOL> if settings . DEBUG and not isinstance ( exception , Http404 ) : <EOL> import traceback <EOL> import sys <EOL> exc_info = sys . exc_info ( ) <EOL> print "<STR_LIT>" <EOL> print '<STR_LIT:\n>' . join ( traceback . format_exception ( * ( exc_info or sys . exc_info ( ) ) ) ) <EOL> print "<STR_LIT>" <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> import pdb <EOL> pdb . post_mortem ( exc_info [ <NUM_LIT:2> ] ) <EOL> class MultipleProxyMiddleware ( object ) : <EOL> FORWARDED_FOR_FIELDS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> for field in self . FORWARDED_FOR_FIELDS : <EOL> if field in request . META : <EOL> if '<STR_LIT:U+002C>' in request . META [ field ] : <EOL> parts = request . META [ field ] . split ( '<STR_LIT:U+002C>' ) <EOL> request . META [ field ] = parts [ - <NUM_LIT:1> ] . strip ( ) <EOL> class LogExceptions ( object ) : <EOL> def __init__ ( self ) : <EOL> log_file = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not log_file : <EOL> self . logger = None <EOL> return <EOL> log_file = log_file + '<STR_LIT:->' + str ( os . getpid ( ) ) <EOL> self . logger = logging . getLogger ( '<STR_LIT>' ) <EOL> self . logger . setLevel ( logging . ERROR ) <EOL> handler = logging . handlers . RotatingFileHandler ( log_file , maxBytes = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:20> , backupCount = <NUM_LIT:5> ) <EOL> self . logger . addHandler ( handler ) <EOL> def process_exception ( self , request , exception ) : <EOL> if not self . logger or isinstance ( exception , Http404 ) : <EOL> return <EOL> self . logger . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> time . strftime ( "<STR_LIT>" ) , <EOL> request . get_full_path ( ) , <EOL> request . GET , <EOL> request . POST , <EOL> exception . __class__ . __name__ , <EOL> getattr ( exception , '<STR_LIT:message>' , '<STR_LIT>' ) , <EOL> traceback . format_exc ( ) <EOL> ) <EOL> def secure ( func ) : <EOL> """<STR_LIT>""" <EOL> def _secure ( * args , ** kwargs ) : <EOL> return func ( * args , ** kwargs ) <EOL> _secure . is_secure = True <EOL> _secure . __name__ = func . __name__ <EOL> return _secure <EOL> HREF_PATTERN = re . compile ( r"""<STR_LIT>""" , re . IGNORECASE ) <EOL> class SSLMiddleware ( object ) : <EOL> def process_view ( self , request , view_func , view_args , view_kwargs ) : <EOL> is_secure = "<STR_LIT>" == request . META . get ( "<STR_LIT>" , "<STR_LIT:http>" ) <EOL> needs_secure = self . _resolves_to_secure_view ( request . path ) <EOL> if needs_secure != is_secure and not settings . IGNORE_HTTPS : <EOL> return self . _redirect ( request , needs_secure ) <EOL> return None <EOL> def process_response ( self , request , response ) : <EOL> if response [ '<STR_LIT:Content-Type>' ] . find ( '<STR_LIT:html>' ) >= <NUM_LIT:0> : <EOL> protocol = request . META . get ( "<STR_LIT>" , "<STR_LIT:http>" ) <EOL> if protocol == "<STR_LIT:http>" : <EOL> return response <EOL> def rewrite_url ( match ) : <EOL> attribute = match . groupdict ( ) [ "<STR_LIT>" ] <EOL> split_url = urlsplit ( match . groupdict ( ) [ "<STR_LIT:url>" ] ) <EOL> if split_url . scheme == '<STR_LIT>' and request . path . startswith ( reverse ( '<STR_LIT>' ) ) : <EOL> return '<STR_LIT>' % ( attribute , split_url . geturl ( ) ) <EOL> host = split_url . netloc if split_url . netloc else settings . HTTP_HOST <EOL> request_path = request . path if request . path and request . path [ - <NUM_LIT:1> ] != "<STR_LIT:/>" else request . path [ : - <NUM_LIT:1> ] <EOL> path = split_url . path if split_url . path and split_url . path [ <NUM_LIT:0> ] == "<STR_LIT:/>" else "<STR_LIT>" % ( request_path , split_url . path ) <EOL> new_url = urlunsplit ( ( protocol , host , path , split_url [ <NUM_LIT:3> ] , split_url [ <NUM_LIT:4> ] ) ) <EOL> return '<STR_LIT>' % ( attribute , new_url ) <EOL> try : <EOL> decoded_content = response . content . decode ( '<STR_LIT:utf-8>' ) <EOL> except UnicodeDecodeError : <EOL> decoded_content = response . content <EOL> response . content = HREF_PATTERN . sub ( rewrite_url , decoded_content ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return response <EOL> def _redirect ( self , request , needs_secure ) : <EOL> protocol = needs_secure and "<STR_LIT>" or "<STR_LIT:http>" <EOL> new_url = self . _add_protocol ( request , protocol ) <EOL> if settings . DEBUG and request . method == "<STR_LIT:POST>" : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> return HttpResponseRedirect ( new_url ) <EOL> def _add_protocol ( self , request , protocol ) : <EOL> return "<STR_LIT>" % ( protocol , get_host ( request ) , request . get_full_path ( ) ) <EOL> def _resolves_to_secure_view ( self , url ) : <EOL> try : <EOL> view_func , args , kwargs = resolve ( url ) <EOL> except : <EOL> return None <EOL> else : <EOL> return getattr ( view_func , '<STR_LIT>' , False ) <EOL> class TerminalLogging : <EOL> def process_response ( self , request , response ) : <EOL> from sys import stdout <EOL> if stdout . isatty ( ) : <EOL> for query in connections [ '<STR_LIT:default>' ] . queries : <EOL> print "<STR_LIT>" % ( query [ '<STR_LIT:time>' ] , <EOL> "<STR_LIT:U+0020>" . join ( query [ '<STR_LIT>' ] . split ( ) ) ) <EOL> return response <EOL> class SourceTagCollectionMiddleware : <EOL> def process_view ( self , request , view_func , view_args , view_kwargs ) : <EOL> if request . GET . has_key ( '<STR_LIT:src>' ) : <EOL> url_params = request . GET . copy ( ) <EOL> sources = url_params . pop ( '<STR_LIT:src>' ) <EOL> new_qs = url_params . urlencode ( ) <EOL> redirect = HttpResponseRedirect ( '<STR_LIT>' % ( request . path , new_qs ) ) <EOL> redirect . set_cookie ( SOURCE_COOKIE , request . GET . urlencode ( ) ) <EOL> return redirect <EOL> return None </s>
