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

Add HTTP::Server::Response#redirect #10412

Conversation

straight-shoota
Copy link
Member

This is a proposal for a method HTTP::Server::Response#redirect which offers a simple way to issue redirects from HTTP server handlers. respond_with_status is a similar convenience method.

Redirects are pretty common and a helper for this would be very useful. In fact, there's already an implementation in StaticFileHandler. Most web frameworks probably provide a helper for this, too (example: kemal). But it would be nice to have this at hand for plain server handlers. And simple implementations like kemal's could be based on or replaced by the stdlib implementation.

Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
@straight-shoota straight-shoota added this to the 1.6.0 milestone Sep 22, 2022
@straight-shoota straight-shoota merged commit abff4f7 into crystal-lang:master Sep 24, 2022
@straight-shoota straight-shoota deleted the feature/http-server-redirect branch September 24, 2022 10:20
@straight-shoota
Copy link
Member Author

Ooops, I mistakenly merged this despite CI errors. 🙈 Sorry. I'll send a fixup right away.

@straight-shoota
Copy link
Member Author

Or scrap that, I'll just revert it for now.

straight-shoota added a commit that referenced this pull request Sep 24, 2022
HertzDevil pushed a commit that referenced this pull request Sep 25, 2022

url = URI.encode_path(url.to_s)
context.response.headers.add "Location", url
context.response.redirect url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this is where the Path comes from. It is hard to say whether this justifies the use of unrestricted Paths in the public API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least I now remember the reason for adding Path to the restriction 😅
But I think I'd still stick to the plan and just call to_s here.

@straight-shoota straight-shoota removed this from the 1.6.0 milestone Sep 25, 2022
@straight-shoota straight-shoota added the status:reverted PR was reverted or reverts another one, and is not part of a milestone label Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature status:reverted PR was reverted or reverts another one, and is not part of a milestone topic:stdlib:networking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a redirect method to HTTP::Server::Context
6 participants