예제 #1
0
    def __call__(self, environ, start_response):
        """Called by WSGI when a request comes in."""
        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)
        except (KeyError, ValueError):
            module_name = kargs['controller']
            class_name = module_name
        del kargs['controller']
        module_name = _CONTROLLERS_MODULE_PREFIX + '.' + module_name

        # 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 (ImportError, AttributeError):
            logging.exception('Could not import controller %s:%s', module_name,
                              class_name)
            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 error.AccessDenied, acl_e:
                logging.exception(acl_e)
                response.set_status(404)
            except Exception, e:
                # We want to catch any exception thrown by the controller and
                # pass it on to the controller's own exception handler.
                controller.handle_exception(e, self.__debug)
예제 #2
0
파일: wsgi.py 프로젝트: Circyl/MobiPerf
  def __call__(self, environ, start_response):
    """Called by WSGI when a request comes in."""
    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)
    except (KeyError, ValueError):
      module_name = kargs['controller']
      class_name = module_name
    del kargs['controller']
    module_name = _CONTROLLERS_MODULE_PREFIX + '.' + module_name

    # 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 (ImportError, AttributeError):
      logging.exception('Could not import controller %s:%s',
                        module_name, class_name)
      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 error.AccessDenied, acl_e:
        logging.exception(acl_e)
        response.set_status(404)
      except Exception, e:
        # We want to catch any exception thrown by the controller and
        # pass it on to the controller's own exception handler.
        controller.handle_exception(e, self.__debug)
예제 #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