rstudio / httpuv

HTTP and WebSocket server package for R
Other
227 stars 86 forks source link

Feature request: RFC 1738 URL encoding #225

Closed antoine-sachet closed 5 years ago

antoine-sachet commented 5 years ago

Sometimes, I need to interface with servers working with RFC 1738 URL encoded strings (PHP outdated default for http_build_query) instead of the more recent RFC 3986. The main difference is that spaces are encoded as + instead of %20.

More context on StackOverflow.

It would be quite amazing if encode/decodeURI and encode/decodeURIComponent could take an optional argument encoding which would be "rfc3986" by default but could be set to "rfc1738".

Similar to the enc_type argument of the PHP function http_build_query.

wch commented 5 years ago

From my reading of RFC 1738, it doesn't say that spaces should be encoded as +.

I believe that + means space when the mime type is application/x-www-form-urlencoded. See here: https://stackoverflow.com/a/2678602/412655

The intent of encode/decodeURIComponent is that they behave like their JavaScript counterparts, so we're unlikely to add this functionality. Can you just wrap it in your own function that replaces + with ?

antoine-sachet commented 5 years ago

You're right, I had misunderstood the PHP documentation, the plus signs are because of the mime type, not RFC1738.

If enc_type is PHP_QUERY_RFC1738, then encoding is performed per » RFC 1738 and the application/x-www-form-urlencoded media type, which implies that spaces are encoded as plus (+) signs.

Re your suggestion, this is exactly what I am doing.

url_decode <- function(str) {
    str <- gsub(str, pattern = "+", replacement = "%20", fixed = TRUE)
    httpuv::decodeURIComponent(str)
}

Thanks