Ejemplo n.º 1
0
    def __call__(self, request):
        """
        Called by HTTPServer to execute the request.
        monkey-patched to allow handler to be string, not class
        """
        transforms = [t(request) for t in self.transforms]
        handler = None
        args = []
        handlers = self._get_host_handlers(request)

        def get_args(handler_class, args):
            args = list(args)
            reps = re.findall(r"\\(\d+)", handler_class)
            for rep in reps:
                del (args[(int(rep) - 1)])
            return args

        if not handlers:
            handler = RedirectHandler(request,
                                      "http://" + self.default_host + "/")
        else:
            if request.path != "/":
                request.path = request.path.rstrip('/')
            for spec in handlers:
                match = spec.regex.match(request.path)
                if match:
                    if not callable(spec.handler_class):
                        args = get_args(spec.handler_class, match.groups())
                        handler_class = re.sub(spec.regex, spec.handler_class,
                                               request.path)
                        mod_name, handler_classname = get_mod_handler(
                            handler_class)
                        handler_class = import_module(mod_name,
                                                      handler_classname)
                        handler = handler_class(self, request, **spec.kwargs)
                    break
            if not handler:
                handler = MissingHandler(self, request)

        # In debug mode, re-compile templates and reload static files on every
        # request so you don't need to restart to see changes
        if self.settings.get("debug"):
            RequestHandler._templates = None
            RequestHandler._static_hashes = {}

        handler._execute(transforms, *args)
        return handler
Ejemplo n.º 2
0
 def __call__(self, request):
     """
     Called by HTTPServer to execute the request.
     monkey-patched to allow handler to be string, not class
     """
     transforms = [t(request) for t in self.transforms]
     handler = None
     args = []
     handlers = self._get_host_handlers(request)
     
     def get_args(handler_class, args):
         args = list(args)
         reps = re.findall(r"\\(\d+)", handler_class)
         for rep in reps:
             del(args[(int(rep)-1)])
         return args
         
     if not handlers:
         handler = RedirectHandler(
             request, "http://" + self.default_host + "/")
     else:
         if request.path != "/":
             request.path = request.path.rstrip('/')
         for spec in handlers:
             match = spec.regex.match(request.path)
             if match:
                 if not callable(spec.handler_class):
                     args = get_args(spec.handler_class, match.groups())
                     handler_class = re.sub(spec.regex, spec.handler_class, request.path)
                     mod_name, handler_classname = get_mod_handler(handler_class)
                     handler_class = import_module(mod_name, handler_classname)
                     handler = handler_class(self, request, **spec.kwargs)
                 break
         if not handler:
             handler = MissingHandler(self, request)
     
     # In debug mode, re-compile templates and reload static files on every
     # request so you don't need to restart to see changes
     if self.settings.get("debug"):
         RequestHandler._templates = None
         RequestHandler._static_hashes = {}
     
     handler._execute(transforms, *args)
     return handler
Ejemplo n.º 3
0
 def __call__(self, request): 
     #print """Called by HTTPServer to execute the request."""
     #print "##################################################"
     if not hasattr(self, "add_slash"):
         setattr(self, "add_slash", False)
     _add_slash = getattr(self, "add_slash")
     transforms = [t(request) for t in self.transforms]
     handler = None
     args = []
     kwargs = {}
     if _add_slash == False:
         # removeslash
         if request.path.endswith("/"):
             if request.method in ("GET", "HEAD"):
                 uri = request.path.rstrip("/")
                 if uri:  # don't try to redirect '/' to ''
                     if request.query:
                         uri += "?" + request.query
                     hdr = RedirectHandler(self, request, url=uri)
                     hdr._execute(transforms, *args, **kwargs)
                     return hdr
             else:
                 raise HTTPError(404)
     else:
         # addslash
         li = [_compile for _compile in self._static_urls if re.compile(_compile).match(request.path)]
         if len(li) == 0 and not request.path.endswith("/"):
             if request.method in ("GET", "HEAD"):
                 uri = request.path + "/"
                 if request.query:
                     uri += "?" + request.query
                 hdr = RedirectHandler(self, request, url=uri)
                 hdr._execute(transforms, *args, **kwargs)
                 return hdr
             raise HTTPError(404)
     super(WebApplication, self).__call__(request)