Fuyukai / Kyoukai

[OLD] A fully async web framework for Python3.5+ using asyncio
https://mirai.veriny.tf
MIT License
298 stars 14 forks source link

Kyoukai prints 404 to console when a 200 is sent in errorhandler #11

Closed Martmists-GH closed 7 years ago

Martmists-GH commented 7 years ago

Pls fix

@app.errorhandler(404)
async def handle_404(ctx, exc):
    try:
        resp = open(ctx.request.path).read()
        resp = Response(200, resp)
    except Exception:
        resp = Response(404, "404")
    return resp
ccubed commented 7 years ago

This is not an error. There is never a case where a server should decide to send a 404 and then resolve a 404 into a 200. That's not how the specification works.

Martmists-GH commented 7 years ago

So how do you suggest I add functionality like this whenever a page isn't found in the routes defined by the code?

ccubed commented 7 years ago

You should either return a '404 page' like most sites or just return the 404 with no content and let the web browser give its own default not found page. To be clear, neither of these require you to return a 200. It's perfectly acceptable to return page content on a 404.

Fuyukai commented 7 years ago

The logging call is before the error handler is hit.

If you want to do weird stuff like this, use a custom router, with the other changes in 1.10.

Martmists-GH commented 7 years ago

What I'm saying is that I want kyoukai to look if a file exists, and only 404 if even the file isn't found. I might get a lot of pages and I'm not gonna add routes to all of them (even regex would be too much)

ccubed commented 7 years ago

That is not how a web server (or hell, even a file server) works. Thank you, please come again once you understand how these things work.

Fuyukai commented 7 years ago

You can literally make a catch-all route with /(.*). It will catch ANY request not caught by other requests, as long as you define it LAST in the order.