-
Notifications
You must be signed in to change notification settings - Fork 86
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
encode and decode path segments in urls when generating and recognizing, respectively #55
Conversation
Updated to handle the two complementary sides (recognizing urls and generating urls). This code will now fix the ember issue linked to in the description. Added a link to a sample rails repository showing that this the the way rails routing works, too. |
@machty - Can you review? |
@bantic this is the correct thing to do but will break an app that is manually encoding/decoding the dynamic segment. I would love to land this though because your test would show that the other PRs are flawed that are trying to do something similar in this area. |
@krisselden What can I do to help move it along? Are you suggesting I can should/can the fix here so that an app that is manually decoding/encoding won't be broken? |
@bantic - I think that I would personally consider this a BUGFIX in Ember, even though it would break for folks that have already been manually working around the bug. I think we could add some sort of flag here to opt-in to the behavior so that we can land as a feature in Ember (which will give us more time to test and figure out an appropriate path forward). Looks like we also need a rebase when you have time... |
18012da
to
7a9da30
Compare
@rwjblue @krisselden Rebased. I will add a flag to opt out of the behavior. How about |
@bantic - Seems good to me, we can bikeshed the name if needed 😈 ... |
5507657
to
3ae32bc
Compare
@rwjblue Done. I decided |
Ya, on Ember's side, we will need to:
|
👍 I'll make a PR there, hopefully be the end of the day today. |
With any flag, we need a plan in place to get off it. Can I assume that is just going to be when the feature flag in ember is removed, and the code moves to stable? |
@stefanpenner - Confirm. We will have to come up with a very tailored path for this feature. Using the feature in Ember to toggle the flag here is only the first step. The issue here is that this is likely a breaking change for folks already working around the bug that this fixes. We definitely want to fix this for others so they don't need to also rely on the broken behavior, but need to tackle the whole situation with care... |
@rwjblue 👍 |
This allows handlers like "/foo/:bar" to match a path with a url encoded segment such as "/foo/abc%Fdef" -> { params: { bar: "abc/def" } } See related issue emberjs/ember.js#11497
Setting this to false allows consumers to opt-out of the encoding/decoding code (in case they were relying on manually handling URLs with uri-encoded segments).
I'm working on a more robust PR that adds some coverage for more complex types of paths and also adds tests for star ( |
Awesome, thanks @bantic! |
3ae32bc
to
8790609
Compare
dc29b1d
to
ba99b23
Compare
please do not merge The current code in master includes a line
For example, with the dynamic path "
If, when recognizing, the path is "normalized" using
Removing the
In order to fix this, this PR changes the process so that instead of decoding-to-normalize the path when recognizing, it attempts to encode static path segments when they are added to the router so that the router always has (internally) a valid URI for each static path segment that it knows about. Because it seems unlikely that users should be required to always pass in encoded paths when adding routes, this code adds a heuristic that attempts to determine when a static segment has been added that should be encoded (it should be encoded if: it has characters that need encoding or it appears to have "%" characters that are not intended to indicate percent-encoded sequences) and encodes the static segments (using I would like to get some feedback on whether this approach seems correct. I can improve the heuristic (or change or remove it) if necessary. |
|
||
function decodeURIWithoutPercents(string) { | ||
return string.split(percentEncodedPercent).map(function(part) { | ||
return decodeURI(part); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this closure doesn't need to be created every time this is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tring.split(percentEncodedPercent).map(decoeURI)
I just want to chime in to highlight a bug that I found using ember-cli-mirage that I think it's related to this. In the server, doing
But in ember-cli-mirage, doing I've tracked this to the parseQueryString method on this library. I think that this method should be like the jQuery This behaves the same way (afaik) than rails when parsing params, and probably other frameworks too: Rack::Utils.parse_nested_query("filter%5Bname%5D=John") #=> {"filter"=>{"name"=>"John"}} Is this issue fixed by this PR? |
@cibernox This PR doesn't address query params, so it won't fix your issue, but that is definitely a bug. |
This PR has grown in scope since it was originally opened, so I'm going to close it and re-open a new PR with a better description of the problem space and the attempted solution. |
This allows handlers like "/foo/:bar" to match a path with a url encoded
segment such as "/foo/abc%2Fdef" ->
{ params: { bar: "abc/def" } }
(instead of incorrectly recognizing the value of ":bar" as "abc%2Fdef").It also changes the route-recognizer to encode segments in the same way when generating a url.
For example, generating a
postShow
route with the path "/posts/:id" and params{id: 'abc/def'}
will return the url "/posts/abc%2Fdef" instead of the incorrect "/posts/abc/def".This code will fix this related ember issue: emberjs/ember.js#11497
Decoding and encoding URI segments is the way the rails router also works. I put together a demo Rails app to demonstrate.
Adds a flag
RouteRecognizer.ENCODE_AND_DECODE_PATH_SEGMENTS
that is initially true and can be set to false by consumers to opt-out of this bugfix.