Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite request and re-route #2839

Open
1 task done
Tronic opened this issue Oct 15, 2023 · 2 comments
Open
1 task done

Rewrite request and re-route #2839

Tronic opened this issue Oct 15, 2023 · 2 comments

Comments

@Tronic
Copy link
Member

Tronic commented Oct 15, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe.

Web servers such as Apache, Nginx and Caddy make heavy use of rewrites, which are essentially internal redirects. Rather than ask the client do another request some place else (redirect), the server while handling a request itself determines a new location and handles it internally, and the client doesn't see any of this but gets a response from the rewritten request.

Typically the path of the request is altered in a redirect, although other changes are also possible.

I am seeing use for this at least within Sanic's own static files handler, that may need rewrite to index.html after determining the target was a directory, and then re-enter itself (rather than bail out by simple return file(path / "index.html") that lacks the features of the normal static file handler. But this is in general a powerful tool for implementing some odd flows.

Describe the solution you'd like

In a handler or middleware one could

return sanic.response.rewrite(request, url="/new/path", headers=...)

After this Sanic would re-run routing to find a matching handler and restart as if it were a new request.

Alternatively this could be a request accessor: (I think I prefer this)

return request.rewrite(**changes)

Additional context

Partially read body, or streaming if body has already been consumed are hard to support. Presumably the connection handling (http1.py etc) keep track of request body and response status even if the Request object is altered, making this less of an issue (even in presence of incorrect content-length header).

Probably needs to keep a count of rewrites per request to avoid accidental infinite loops.

@ahopkins
Copy link
Member

(I think I prefer this)

I would agree. The response module seems more like a place for creating response objects which is not really what this proposal is doing. My initial impression would be to raise a RewriteException and catch that inside the app's handle_request, swap out the path on a new Request object and call handle_request again.

@shaoerkuai
Copy link

shaoerkuai commented Jun 26, 2024

Hopefully Sanic supports this, I can ditch the routing createWebHistory mode in the Nginx hosted Vue.js application and instead use app.static("/"...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants