Esempio n. 1
0
 def render(self, context):
     host = self.host
     request = context.get("request")
     if self.allow_override and request and request.META.get("X_OVERRIDE_SERVER_NAME"):
         host = request.META.get("X_OVERRIDE_SERVER_NAME")
     host = host or settings.HOSTS["app"]
     return build_url(request, host=host, path=self.path)
Esempio n. 2
0
 def process_view(self, request, view_func, view_args, view_kwargs):
     """
     Process the request, and attempt to normalize these components:
     http vs https
     canonical hostname
     port number
     add trailing slash (if required)
     """
     increment()
     
     # test slow ajax
     #sleep(randint(0, 4))
     
     if 'location' in view_kwargs:
         return HttpResponsePermanentRedirect(view_kwargs['location'])
     
     redirect = False
     
     is_secure = request.is_secure()
     if 'secure' in view_kwargs:
         if is_secure != bool(view_kwargs['secure']):
             is_secure = not is_secure
             redirect = True
         del view_kwargs['secure']
     
     host = request.META['SERVER_NAME']
     if 'host' in view_kwargs:
         if host != view_kwargs['host']:
             change = True
             if hasattr(settings, 'HOST_FILTERS'):
                 for filter in settings.HOST_FILTERS:
                     if filter.search(host):
                         change = False
                         request.META['X_OVERRIDE_SERVER_NAME'] = host
                         break
             if change:
                 host = view_kwargs['host']
                 redirect = True
         del view_kwargs['host']
     
     port = request.META['SERVER_PORT']
     if 'port' in view_kwargs:
         if port != view_kwargs['port']:
             port = view_kwargs['port']
             redirect = True
         del view_kwargs['port']
     
     # clean up path
     path = RE_SLASHES.sub(u'/', unicode(request.path))
     
     # redirect to specific path
     if 'path' in view_kwargs:
         if path != view_kwargs['path']:
             path = view_kwargs['path']
         del view_kwargs['path']
     
     # auto view
     else:
         try:
             view_kwargs['template'] = loader.get_template(TEMPLATE_PATH + path)
         except (TemplateDoesNotExist, UnicodeError):
             try:
                 view_kwargs['template'] = loader.get_template(TEMPLATE_PATH + path + u'/' + INDEX_TEMPLATE)
                 if not path.endswith(u'/'):
                     path += '/'
             except (TemplateDoesNotExist, UnicodeError):
                 if not path.endswith(u'/'):
                     if view_func == self._get_view_func(path + u'/'):
                         path += u'/'
     
     # redirect if path has changed
     if path != request.path:
         redirect = True
     
     query_string = request.META['QUERY_STRING']
     
     if redirect:
         url = build_url(request, is_secure, host, port, path, query_string)
         return HttpResponsePermanentRedirect(url)
Esempio n. 3
0
 def process_view(self, request, view_func, view_args, view_kwargs):
   """
   Process the request, and attempt to normalize these components:
   http vs https
   canonical hostname
   port number
   add trailing slash (if required)
   """
   increment()
   
   # set script prefix
   set_script_prefix(request.build_absolute_uri('/'))
   
   # test slow ajax
   #sleep(randint(0, 4))
   
   if 'location' in view_kwargs:
     return HttpResponsePermanentRedirect(view_kwargs['location'])
   
   redirect = False
   
   is_secure = request.is_secure()
   if 'secure' in view_kwargs:
     if is_secure != bool(view_kwargs['secure']):
       is_secure = not is_secure
       redirect = True
     del view_kwargs['secure']
   
   host = request.META['SERVER_NAME']
   if hasattr(settings, 'STRIP_WWW') and settings.STRIP_WWW:
     host2 = RE_START_WWW.sub('', host)
     if host2 != host:
       host = host2
       redirect = True
   if 'host' in view_kwargs:
     if host != view_kwargs['host']:
       change = True
       if hasattr(settings, 'HOST_FILTERS'):
         for filter in settings.HOST_FILTERS:
           if filter.search(host):
             change = False
             request.META['X_OVERRIDE_SERVER_NAME'] = host
             break
       if hasattr(settings, 'HOST_SUFFIX') and len(settings.HOST_SUFFIX) > 0:
         request.META['X_IMPLICIT_SERVER_NAME'] = host.rsplit(settings.HOST_SUFFIX)[0]
       if change:
         host = view_kwargs['host']
         redirect = True
     del view_kwargs['host']
   
   port = request.META['SERVER_PORT']
   if 'port' in view_kwargs:
     if port != view_kwargs['port']:
       port = view_kwargs['port']
       redirect = True
     del view_kwargs['port']
       
   # handle slashes
   path = RE_SLASHES.sub(u'/', unicode(request.path))
   path_ends_with_slash = path.endswith(u'/')
   append_slash = hasattr(settings, 'APPEND_SLASH') and settings.APPEND_SLASH
   if not append_slash and path_ends_with_slash and path != u'/':
     path = path[:-1]
   
   # redirect to specific path
   if 'path' in view_kwargs:
     if path != view_kwargs['path']:
       path = view_kwargs['path']
     del view_kwargs['path']
   
   # auto view
   else:
     template_base = TEMPLATE_PATH + path
     
     try:
       view_kwargs['template'] = loader.get_template(template_base)
     except (TemplateDoesNotExist, UnicodeError):
       try:
         view_kwargs['template'] = loader.select_template((template_base + u'.html', template_base + u'/index.html'))
         if append_slash and not path_ends_with_slash:
           path += '/'
       except (TemplateDoesNotExist, UnicodeError):
         if append_slash and not path_ends_with_slash:
           if view_func == self._get_view_func(path + u'/'):
             path += u'/'
     
     if view_kwargs.get('template'):
       path = RE_END_HTML.sub(u'', path)
   
   # redirect if path has changed
   if path != request.path:
     redirect = True
   
   query_string = request.META['QUERY_STRING']
   
   if redirect:
     url = build_url(request, is_secure, host, port, path, query_string)
     return HttpResponsePermanentRedirect(url)