Exemple #1
0
    def __call__(self, environ, start_response):

        ctx = Context(environ)
        LocalStorage.push(self.identify, ctx)
        matched = False

        try:
            for pattern, methods, fn in self.processors:

                if pattern is None:
                    fn()
                else:
                    match = pattern.match(ctx.request.path)
                    if match:
                        matched = True

                        if ctx.request.method in methods:
                            ctx.response.body = fn(**match.groupdict())
                        else:
                            raise HTTPError(405)

                            # if not matched:
                            #     raise HTTPError(404)

        except HTTPError as e:
            # ctx.response.body = e.args.get(1, None)
            ctx.response.status = e.args[0]
        finally:
            status = ctx.response.get_status()
            body = ctx.response.get_body()
            header = ctx.response.get_header()

            start_response(status, header)
            return [body.encode('utf-8')]
Exemple #2
0
    def __call__(self, environ, start_response):

        ctx = Context(environ)
        LocalStorage.push(self.identify, ctx)
        matched = False
        override = False
        try:
            for pattern, methods, fn in self.processors:

                if pattern is None:
                    result = fn()
                    if result is not None:
                        ctx.response.body = result
                        override = True
                        break
                else:
                    match = pattern.match(ctx.request.path)
                    if match:
                        matched = True
                        if ctx.request.method not in methods:
                            raise HTTPError(405)
                        else:
                            ctx.response.body = fn(**match.groupdict())

            if not matched and not override:
                raise HTTPError(404)

        except HTTPError as e:
            ctx.response.status = e.args[0]
            ctx.response.body = ctx.response.status_detail

            status_code = str(ctx.response.status)
            if status_code in self.exception_handlers:
                self.exception_handlers[str(status_code)]()
        finally:
            status = ctx.response.status_result
            body = ctx.response.body_result
            header = ctx.response.headers_result
            start_response(status, header)
            return [body.encode('utf-8')]
Exemple #3
0
 def handler():
     raise HTTPError(500)