How to use the emmett.http.HTTP function in emmett

To help you get started, we’ve selected a few emmett examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github emmett-framework / emmett / tests / test_routing.py View on Github external
async def test_routing_exception_route(app):
    with current_ctx(app, '/test_404'):
        with pytest.raises(HTTP) as excinfo:
            await app._router_http.dispatch()
        assert excinfo.value.status_code == 404
        assert excinfo.value.body == 'Not found, dude'
github emmett-framework / emmett / tests / test_routing.py View on Github external
async def test_routing_not_found_route(app):
    with current_ctx(app, '/'):
        with pytest.raises(HTTP) as excinfo:
            await app._router_http.dispatch()
        assert excinfo.value.status_code == 404
        assert excinfo.value.body == 'Resource not found\n'
github emmett-framework / emmett / emmett / asgi / handlers.py View on Github external
def _static_handler(self, scope, receive, send):
        path = scope['emt.path']
        #: handle internal assets
        if path.startswith('/__emmett__'):
            file_name = path[12:]
            static_file = os.path.join(
                os.path.dirname(__file__), '..', 'assets', file_name)
            if os.path.splitext(static_file)[1] == 'html':
                return HTTP(404)
            return self._static_response(static_file)
        #: handle app assets
        static_file, version = self.static_matcher(path)
        if static_file:
            return self._static_response(static_file)
        return self.dynamic_handler(scope, receive, send)
github emmett-framework / emmett / emmett / routing / response.py View on Github external
output['url'] = output.get('url', url)
            output['asis'] = output.get('asis', asis)
            output['load_component'] = output.get(
                'load_component', load_component)
        elif output is None:
            is_template = True
            output = {
                'current': current, 'url': url, 'asis': asis,
                'load_component': load_component
            }
        if is_template:
            try:
                return self.route.app.templater.render(
                    self.route.template, output)
            except TemplateMissingError as exc:
                raise HTTP(404, body="{}\n".format(exc.message))
        elif isinstance(output, str) or hasattr(output, '__iter__'):
            return output
        return str(output)
github emmett-framework / emmett / emmett / wrappers / request.py View on Github external
def append(self, data):
        if data == b'':
            return
        self._data.extend(data)
        if (
            self._max_content_length is not None and
            len(self._data) > self._max_content_length
        ):
            raise HTTP(413, 'Request entity too large')
github emmett-framework / emmett / emmett / routing / routes.py View on Github external
def _parse_date_reqarg(args, route_args):
        try:
            for arg in args:
                route_args[arg] = pendulum.DateTime.strptime(
                    route_args[arg], "%Y-%m-%d")
        except Exception:
            raise HTTP(404)
github emmett-framework / emmett / emmett / routing / routes.py View on Github external
def _parse_date_reqarg_opt(args, route_args):
        try:
            for arg in args:
                if route_args[arg] is None:
                    continue
                route_args[arg] = pendulum.DateTime.strptime(
                    route_args[arg], "%Y-%m-%d")
        except Exception:
            raise HTTP(404)
github emmett-framework / emmett / emmett / http.py View on Github external
async def send(self, scope, send):
        try:
            stat_data = os.stat(self.file_path)
            if not stat.S_ISREG(stat_data.st_mode):
                await HTTP(403).send(scope, send)
                return
            self._headers.update(self._get_stat_headers(stat_data))
            await self._send_headers(send)
            await self._send_body(send)
        except IOError as e:
            if e.errno == errno.EACCES:
                await HTTP(403).send(scope, send)
            else:
                await HTTP(404).send(scope, send)
github emmett-framework / emmett / emmett / asgi / handlers.py View on Github external
async def dynamic_handler(self, scope, receive, send):
        try:
            http = await self.app._router_http.dispatch()
        except HTTPResponse as http_exception:
            http = http_exception
            #: render error with handlers if in app
            error_handler = self.app.error_handlers.get(http.status_code)
            if error_handler:
                http = HTTP(
                    http.status_code, await error_handler(), response.headers)
            #: always set cookies
            http.set_cookies(response.cookies)
        return http
github emmett-framework / emmett / emmett / asgi / handlers.py View on Github external
def _prefix_handler(self, scope, receive, send):
        path = request.path
        if not path.startswith(self.app._router_http._prefix_main):
            return HTTP(404)
        request.path = scope['emt.path'] = (
            path[self.app._router_http._prefix_main_len:] or '/')
        return self.static_handler(scope, receive, send)