elm / url

Build and parse URLs. Useful for HTTP and "routing" in single-page apps (SPAs)
https://package.elm-lang.org/packages/elm/url/latest/
BSD 3-Clause "New" or "Revised" License
74 stars 43 forks source link

path should also be escaped #25

Open domenkozar opened 5 years ago

domenkozar commented 5 years ago
Url.Builder.absolute ["file", "/path/bla"] []

should result to

/file/%2Fpath%2Fbla

but currently results to

/file//path/bla

This is especially surprising due to documentation

Use Url.Builder instead! Functions like absolute, relative, and crossOrigin already do this automatically! percentEncode is only available so that extremely custom cases are possible, if needed.

hpate-omicron commented 5 years ago

This bug bit us as well, we were using an email address as a path param and it was working for simple cases, but not when there was a + in the email address.

This is the spec section for this piece that mentions percent encoding the path segements. https://tools.ietf.org/html/rfc3986#section-3.3

joshuakb2 commented 4 years ago

This caused a bug in my code. The behavior directly contradicts the documentation.

drewwestphal commented 4 years ago

Upvote.

This also caused a bug in my code. The part of the documentation that says it escapes is in the function you ultimately need to use to fix your url https://package.elm-lang.org/packages/elm/url/latest/Url#percentEncode

I will say that it DOES escape query parameters... but if you are building an API url for example and some parameters are path params (as they often are) and they contain spaces (as they sometimes do), this bug will get you.

evancz commented 3 years ago

If the proposed fix is to call percentEncode on all segments, a user from Russia or Denmark might prefer the current behavior. For example:

UB.absolute ["искать", "книги"] []
-- current:  "/искать/книги"
-- proposed: "/%D0%B8%D1%81%D0%BA%D0%B0%D1%82%D1%8C/%D0%BA%D0%BD%D0%B8%D0%B3%D0%B8"

UB.absolute ["søg", "bøger"] []
-- current:  "/søg/bøger"
-- proposed: "/s%C3%B8g/b%C3%B8ger"

So the current defaults assume that wanting UTF-8 chars in a path is more common than having path segments that contain / characters. I marked this issue as "breaking" and as a "request" since so many people could have very different opinions on the defaults than the people participating in this thread.

The meta issue #42 attempts to summarize the options and solicit examples and evidence that could inform any future change.

pravdomil commented 3 years ago

Thought about this: Encode char if char code is lower then 128?

["søg", "bø/ger"].map(a => a.split("").map(v => v.charCodeAt(0) < 128 ? encodeURIComponent(v) : v).join("")).join("/")

Produces:

"søg/bø%2Fger"