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

proposal: net/url: provide URIEscape method #41922

Closed
kpym opened this issue Oct 11, 2020 · 9 comments
Closed

proposal: net/url: provide URIEscape method #41922

kpym opened this issue Oct 11, 2020 · 9 comments

Comments

@kpym
Copy link

kpym commented Oct 11, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

Irrelevant

What did you do?

I want to encode data for Data URI as specified in rfc3986 section 2.4.

For example if I want to use Data URI for svg image like data:image/svg+xml,... the svg code should be escaped. And I don't want to use base64 because this makes the data more than 30% bigger.

What did you expect to see?

I would like to have a function URIEscape (that should be very close to the internal escape(s, encodeZone ) I think).

What did you see instead?

There is PathEscape that can be used to escape the data but such encoded data is bigger than needed because some symbols that do not need escape are percent escaped.

Notes :

  • In JavaScript there exists encodeURI which keeps more unescaped symbols than any of the options in shouldEscape (for example # is unescaped).
  • It seems that the spaces can be preserved even if this is not standard, as explained here.
@rsc rsc changed the title Proposal : [net/url] Provide URIEscape method proposal: net/url: provide URIEscape method Oct 12, 2020
@gopherbot gopherbot added this to the Proposal milestone Oct 12, 2020
@rsc
Copy link
Contributor

rsc commented Oct 12, 2020

Note that if you want an escaped form of a URL, the usual approach is to fill out a url.URL struct and then call its String method. Then each piece is properly escaped, which is not something a single function can do.

@rsc
Copy link
Contributor

rsc commented Oct 12, 2020

There is also PathEscape and QueryEscape. I am confused why you brought up escapeZone.
For a data: URL, I would expect PathEscape to be appropriate.

@kpym
Copy link
Author

kpym commented Oct 12, 2020

@rsc It depends what you meen by 'appropriate'. PathEscape works but is not optimal because it apply percent encoding to some character that do not need to be escaped.

I don't think that you can put the data URI in url.URL and hope to be parsed correctly because it is not an URL. The data URI can be composed for example of a single svg file, that is not an URL. And if you want to put a text that looks like an url in a data URI it will be badly escaped, for example the query part will use + for spaces that is not allowed in data URI.

As I said in my question, the data URI allows more characters to be non escaped.

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Oct 13, 2020
@rsc
Copy link
Contributor

rsc commented Jun 28, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jul 5, 2023

What is the specific escaping to be applied? For the body of a data: URL I would expect # to be invalid (and need escaping) since even scheme:opaque URL forms can have #fragment following them, at least according to the url.URL docs. Similarly ? since they can have queries too. So that sounds like PathEscape. The text above says "some symbols" are unnecessarily escaped by PathEscape (at least for use in a data URL). Which symbols specifically?

@kpym
Copy link
Author

kpym commented Jul 5, 2023

@rsc The javascript encodeURI preserves the following symbols that are encoded by PathEscape:

!#'()*,/;?

But using encodeURI to encode the <data> part of the dataURI is probably not the right thing to do. The problem is that browsers accept "less encoded" data than the standard requires.

I don't know anymore if having the equivalent of encodeURI is a good idea, sorry.

@rsc
Copy link
Contributor

rsc commented Jul 12, 2023

This is probably too specific for the standard library. svg is probably the only case where very few bytes have to be escaped. If we were escaping a png, for example, base64 is probably better than %-encoding all the binary in the png. So any function here would have to let you pick the encoding or just be very niche.

@rsc
Copy link
Contributor

rsc commented Jul 19, 2023

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jul 26, 2023

No change in consensus, so declined.
— rsc for the proposal review group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Declined
Development

No branches or pull requests

3 participants