示例#1
0
    def __call__(self, environ, start_response):
        """Called by WSGI when a request comes in."""

        url = URLGenerator(self.mapper, environ)
        environ['routes.url'] = url

        request = Request(environ)
        response = Response()
        WSGIApplication.active_instance = self
        # Match the path against registered routes.
        kargs = self.mapper.match(request.path)
        if kargs is None:
            raise TypeError(
                'No routes match. Provide a fallback to avoid this.')
        # Extract the module and controller names from the route.
        try:
            module_name, class_name = kargs['controller'].split(':', 1)
            del kargs['controller']
        except:
            raise TypeError(
                'Controller is not set, or not formatted in the form "my.module.name:MyControllerName".'
            )
        # Initialize matched controller from given module.
        try:
            __import__(module_name)
            module = sys.modules[module_name]
            controller = getattr(module, class_name)()
            controller.initialize(request, response)
        except:
            raise ImportError(
                'Controller %s from module %s could not be initialized.' %
                (class_name, module_name))
        # Use the action set in the route, or the HTTP method.
        if 'action' in kargs:
            action = kargs['action']
            del kargs['action']
        else:
            action = environ['REQUEST_METHOD'].lower()
            if action not in [
                    'get', 'post', 'head', 'options', 'put', 'delete', 'trace'
            ]:
                action = None
        if controller and action:
            try:
                # Execute the requested action, passing the route dictionary as
                # named parameters.
                getattr(controller, action)(**kargs)
            except Exception, e:
                controller.handle_exception(e, self.__debug)
            response.wsgi_write(start_response)
            return ['']
示例#2
0
    def __call__(self, environ, start_response):
        """Called by WSGI when a request comes in."""

        request = Request(environ)
        response = Response()

        # BidiX 2008-09-22 : try to setup a firewall at least a nop
        if banned_ip.is_banned(request.remote_addr):
            response.set_status(403)
            response.wsgi_write(start_response)
            return ['']

        WSGIApplication.active_instance = self

        handler = None
        groups = ()
        for regexp, handler_class in self._url_mapping:
            match = regexp.match(request.path)
            if match:
                handler = handler_class()
                handler.match = match  # BidiX 2008-05-22
                handler.initialize(request, response)
                groups = match.groups()
                break
        self.current_request_args = groups

        if handler:
            try:
                method = environ['REQUEST_METHOD']
                if method == 'GET':
                    handler.get(*groups)
                elif method == 'POST':
                    handler.post(*groups)
                elif method == 'HEAD':
                    handler.head(*groups)
                elif method == 'OPTIONS':
                    handler.options(*groups)
                elif method == 'PUT':
                    handler.put(*groups)
                elif method == 'DELETE':
                    handler.delete(*groups)
                elif method == 'TRACE':
                    handler.trace(*groups)
                else:
                    handler.error(501)
            except Exception, e:
                #handler.handle_exception(e, self.__debug) # BidiX 2008-05-22
                handler.handle_exception(e, True)  # BidiX 2008-05-22
示例#3
0
	def __call__(self, environ, start_response):
		"""Called by WSGI when a request comes in."""

		request = Request(environ)
		response = Response()
		
		# BidiX 2008-09-22 : try to setup a firewall at least a nop
		if banned_ip.is_banned(request.remote_addr):
			response.set_status(403)
			response.wsgi_write(start_response)
			return ['']

		WSGIApplication.active_instance = self

		handler = None
		groups = ()
		for regexp, handler_class in self._url_mapping:
			match = regexp.match(request.path)
			if match:
				handler = handler_class()
				handler.match = match	# BidiX 2008-05-22
				handler.initialize(request, response)
				groups = match.groups()
				break
		self.current_request_args = groups

		if handler:
			try:
				method = environ['REQUEST_METHOD']
				if method == 'GET':
					handler.get(*groups)
				elif method == 'POST':
					handler.post(*groups)
				elif method == 'HEAD':
					handler.head(*groups)
				elif method == 'OPTIONS':
					handler.options(*groups)
				elif method == 'PUT':
					handler.put(*groups)
				elif method == 'DELETE':
					handler.delete(*groups)
				elif method == 'TRACE':
					handler.trace(*groups)
				else:
					handler.error(501)
			except Exception, e:
				#handler.handle_exception(e, self.__debug) # BidiX 2008-05-22
				handler.handle_exception(e, True) # BidiX 2008-05-22
示例#4
0
    def __call__(self, environ, start_response):
        """Called by WSGI when a request comes in."""

        url = URLGenerator(self.mapper, environ)
        environ['routes.url'] = url

        request = Request(environ)
        response = Response()
        WSGIApplication.active_instance = self
        # Match the path against registered routes.
        kargs = self.mapper.match(request.path)
        if kargs is None:
            raise TypeError('No routes match. Provide a fallback to avoid this.')
        # Extract the module and controller names from the route.
        try:
            module_name, class_name = kargs['controller'].split(':', 1)
            del kargs['controller']
        except:
            raise TypeError('Controller is not set, or not formatted in the form "my.module.name:MyControllerName".')
        # Initialize matched controller from given module.
        try:
            __import__(module_name)
            module = sys.modules[module_name]
            controller = getattr(module, class_name)()
            controller.initialize(request, response)
        except:
            raise ImportError('Controller %s from module %s could not be initialized.' % (class_name, module_name))
        # Use the action set in the route, or the HTTP method.
        if 'action' in kargs:
            action = kargs['action']
            del kargs['action']
        else:
            action = environ['REQUEST_METHOD'].lower()
            if action not in ['get', 'post', 'head', 'options', 'put', 'delete', 'trace']:
                action = None
        if controller and action:
            try:
                # Execute the requested action, passing the route dictionary as
                # named parameters.
                getattr(controller, action)(**kargs)
            except Exception, e:
                controller.handle_exception(e, self.__debug)
            response.wsgi_write(start_response)
            return ['']