Exemplo n.º 1
0
    def _classifyHTTPError(self, verb, args, kwargs):
        """
        There are a few cases where input will cause an error which needs
        appropriate classification:
            - request uses a globally unsupported verb (raise 501)
            - request is to a non-existant method (raise 404)
            - request is to a valid method, and uses a valid VERB but the
              VERB isn't supported by the method
        """

        method = args[0]

        he = False
        # Make sure the verb and method are well named
        try:
            check("^[A-Z]+$", verb)
            check("^[a-z][A-Za-z0-9]+$", method)
        except:
            he = HTTPError(400, 'bad VERB or method')

        # Checks whether verb is supported, if not return 501 error
        if verb not in self.methods.keys():
            data = "Unsupported verb: %s" % (verb)
            he = HTTPError(501, data)
        else:
            # We know how to deal with this VERB
            # Do we know the method?
            method_for_verb = method in self.methods[verb].keys()

            if method_for_verb:
                return
            # Is the method supported for a VERB different to the the requests?
            unsupported_verb = False

            other_verbs = self.methods.keys()
            other_verbs.remove(verb)

            for v in other_verbs:
                unsupported_verb = unsupported_verb | (
                    method in self.methods[v].keys())

            # Checks whether method exists but used wrong verb
            if unsupported_verb:
                data = "Unsupported method for %s: %s" % (verb, method)
                he = HTTPError(405, data)

            # The method doesn't exist
            elif not method_for_verb and not he:
                data = "Method not found for the verb %s: %s" % (verb, method)
                he = HTTPError(404, data)

        if he:
            self.debug(he[1])
            self.debug(traceback.format_exc())
            raise he
Exemplo n.º 2
0
    def _classifyHTTPError(self, verb, args, kwargs):
        """
        There are a few cases where input will cause an error which needs
        appropriate classification:
            - request uses a globally unsupported verb (raise 501)
            - request is to a non-existant method (raise 404)
            - request is to a valid method, and uses a valid VERB but the
              VERB isn't supported by the method
        """

        method = args[0]

        he = False
        # Make sure the verb and method are well named
        try:
            check("^[A-Z]+$", verb)
            check("^[a-z][A-Za-z0-9]+$", method)
        except:
            he = HTTPError(400, 'bad VERB or method')

        # Checks whether verb is supported, if not return 501 error
        if verb not in self.methods.keys():
            data =  "Unsupported verb: %s" % (verb)
            he = HTTPError(501, data)
        else:
            # We know how to deal with this VERB
            # Do we know the method?
            method_for_verb = method in self.methods[verb].keys()

            if method_for_verb:
                return
            # Is the method supported for a VERB different to the the requests?
            unsupported_verb = False

            other_verbs = self.methods.keys()
            other_verbs.remove(verb)

            for v in other_verbs:
                unsupported_verb = unsupported_verb | (method in self.methods[v].keys())

            # Checks whether method exists but used wrong verb
            if unsupported_verb:
                data =  "Unsupported method for %s: %s" % (verb, method)
                he = HTTPError(405, data)

            # The method doesn't exist
            elif not method_for_verb and not he:
                data = "Method not found for the verb %s: %s" % (verb, method)
                he = HTTPError(404, data)

        if he:
            self.debug(he[1])
            self.debug(traceback.format_exc())
            raise he