<s> from django . conf import settings <EOL> from django import http <EOL> from django . utils . http import urlquote <EOL> from django . core import urlresolvers <EOL> class UrlMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> host = request . get_host ( ) <EOL> old_url = [ host , request . path ] <EOL> new_url = old_url [ : ] <EOL> if ( settings . REMOVE_WWW and old_url [ <NUM_LIT:0> ] and old_url [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) ) : <EOL> new_url [ <NUM_LIT:0> ] = old_url [ <NUM_LIT:0> ] [ <NUM_LIT:4> : ] <EOL> if new_url != old_url : <EOL> try : <EOL> urlresolvers . resolve ( new_url [ <NUM_LIT:1> ] ) <EOL> except urlresolvers . Resolver404 : <EOL> pass <EOL> else : <EOL> if new_url [ <NUM_LIT:0> ] : <EOL> newurl = "<STR_LIT>" % ( <EOL> request . is_secure ( ) and '<STR_LIT>' or '<STR_LIT:http>' , <EOL> new_url [ <NUM_LIT:0> ] , urlquote ( new_url [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> newurl = urlquote ( new_url [ <NUM_LIT:1> ] ) <EOL> if request . GET : <EOL> newurl += '<STR_LIT:?>' + request . GET . urlencode ( ) <EOL> return http . HttpResponsePermanentRedirect ( newurl ) <EOL> return None </s>
