Пример #1
0
    def _handle(self):
        """
            *THIS METHOD SHOULD NOT BE CALLED MANUALLY.*
             - Called by: wsgi()

            This method calls self._match() to find the handler then calls the 
            correct method (GET, POST, etc.) to handle the request.

            @return: The processed request's status and output.
        """
        # Get the HandlerObj
        HandlerObj = self._match(webapi.context["path"])

        # If the HandlerObj is not None then process it.
        if HandlerObj:
            # Create the instance and store the arguments.
            HandlerInst = HandlerObj[0]()
            args = HandlerObj[1]

            # If the HandlerObj has the method function available.
            if hasattr(HandlerInst, webapi.context["method"]):
                # Run the method, passing in the arguments. Capture the output.
                method_func = getattr(HandlerInst,webapi.context["method"])
                webapi.context["output"] = method_func(*args)

            # Otherwise throw an HTTP 405 Method Not Allowed.
            else:
                webapi.status("405 Method Not Allowed")
                # Check if the application has defined a 405 page...
                if "HTTP-405" in self.urlmap:
                    cls = self.urlmap["HTTP-405"]
                    HandlerInst = cls()
                    webapi.context["output"] = HandlerInst.GET()
                # If not, provide one.
                else:
                    webapi.context["output"] = "Method Not Allowed: The method used is not allowed for this resource."

        # The HandlerObj is None.
        else:
            webapi.status("404 Not Found")
            # Check if the application has defined a 405 page...
            if "HTTP-404" in self.urlmap:
                cls = self.urlmap["HTTP-404"]
                HandlerInst = cls()
                webapi.context["output"] = HandlerInst.GET()
            # If not, provide one.
            else:
                webapi.context["output"] = "Not Found: The requested URL was not found."

        # return the status & the output
        return webapi.context["status"], webapi.context["output"]
Пример #2
0
    def _load(self, env):
        """*THIS METHOD SHOULD NOT BE CALLED MANUALLY.*
             - Called by: wsgi()
            
        Loads the Application environment.

            @param env: *dict* A regular python dict that is recieved from the 
            webserver and holds the environment variables.

            @return: None
        """
        # Clean the slate.
        webapi.context.clear()

        # Set the default status.
        webapi.context["status"] = "200 OK"
        webapi.status("200 OK")

        # Initiate the headers, output, and environ
        webapi.context["headers"] = []
        webapi.context["output"] = ''
        webapi.context["environ"] = env

        # Set the host
        webapi.context["host"] = env.get('HTTP_HOST')

        # Set the HTTP Scheme.
        if env.get('wsgi.url_scheme') in ['http', 'https']:
            webapi.context["protocol"] = env['wsgi.url_scheme']
        elif env.get('HTTPS', '').lower() in ['on', 'true', '1']:
            webapi.context["protocol"] = 'https'
        else:
            webapi.context["protocol"] = 'http'

        # Set home domain, home path, and home
        webapi.context["homedomain"] = webapi.context["protocol"] + '://' + \
                                       env.get('HTTP_HOST', '[unknown]')
        webapi.context["homepath"] = os.environ.get(
            'REAL_SCRIPT_NAME', env.get('SCRIPT_NAME', ''))
        webapi.context["home"] = webapi.context["homedomain"] + \
                                 webapi.context["homepath"]
        webapi.context["realhome"] = webapi.context["home"]

        # Set the Requester's IP, the Method, and the path.
        webapi.context["ip"] = env.get('REMOTE_ADDR')
        webapi.context["method"] = env.get('REQUEST_METHOD')
        webapi.context["path"] = env.get('PATH_INFO')

        # To fix empty-path bug.
        if webapi.context["path"] is None:
            webapi.context["path"] = "/"

        # get the requestvars if post/put
        if webapi.context["method"].lower() in ['post', 'put']:
            fp = env.get('wsgi.input')
            webapi.context["requestvars"] = cgi.FieldStorage(
                fp=fp, environ=env, keep_blank_values=1)

        # get the requestvars if get
        if webapi.context["method"].lower() == 'get':
            webapi.context["requestvars"] = cgi.FieldStorage(
                environ=env, keep_blank_values=1)

        # convert all string values to unicode values and replace 
        # malformed data with a suitable replacement marker.
        for k, v in webapi.context.iteritems():
            if isinstance(v, str):
                webapi.context[k] = v.decode('utf-8', 'replace')
Пример #3
0
    def _handle(self):
        """*THIS METHOD SHOULD NOT BE CALLED MANUALLY.*
             - Called by: wsgi()

        This method calls self._match() to find the handler then calls the
        correct method (GET, POST, etc.) to handle the request.

            @return: The processed request's status and output.
        """
        # Get the HandlerObj
        HandlerObj = self._match(webapi.context["path"])

        # If the HandlerObj is not None then process it.
        if HandlerObj:
            # Create the instance and store the arguments.
            HandlerInst = HandlerObj[0]()
            args = HandlerObj[1]

            # If the HandlerObj has the method function available. (upper)
            if hasattr(HandlerInst, webapi.context["method"].upper()):
                # Run the method, passing in the arguments. Capture the output.
                method_func = getattr(HandlerInst,
                                      webapi.context["method"].upper())
                webapi.context["output"] = method_func(*args)

            # If the HandlerObj has the method function available. (lower)
            elif hasattr(HandlerInst, webapi.context["method"].lower()):
                # Run the method, passing in the arguments. Capture the output.
                method_func = getattr(HandlerInst,
                                      webapi.context["method"].lower())
                webapi.context["output"] = method_func(*args)

            # Otherwise throw an HTTP 405 Method Not Allowed.
            else:
                webapi.status("405 Method Not Allowed")
                # Check if the application has defined a 405 page...
                if "HTTP-405" in self.urlmap:
                    cls = self.urlmap["HTTP-405"]
                    HandlerInst = cls()
                    webapi.context["output"] = HandlerInst.GET()
                # If not, provide one.
                else:
                    webapi.context[
                        "output"] = "Method Not Allowed: The method used is " \
                                    "not allowed for this resource."

        # The HandlerObj is None.
        else:
            webapi.status("404 Not Found")
            # Check if the application has defined a 405 page...
            if "HTTP-404" in self.urlmap:
                cls = self.urlmap["HTTP-404"]
                HandlerInst = cls()
                webapi.context["output"] = HandlerInst.GET()
            # If not, provide one.
            else:
                webapi.context[
                    "output"] = "Not Found: The requested URL was not found."

        # return the status & the output
        return webapi.context["status"], webapi.context["output"]