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

URI encoding/decoding fixes #192

Merged
merged 6 commits into from
Feb 8, 2019
Merged

URI encoding/decoding fixes #192

merged 6 commits into from
Feb 8, 2019

Conversation

wch
Copy link
Collaborator

@wch wch commented Feb 5, 2019

This is a follow-up to #185, and it does what @hadley suggested: mark the encoding of characters in C++ instead of R.

It also ensures that vector inputs and NAs are handled properly.

@wch wch changed the title Fix uri URI encoding/decoding fixes Feb 5, 2019
@wch wch requested a review from jcheng5 February 5, 2019 20:53
src/httpuv.cpp Outdated
if (value[i] == NA_STRING) {
out[i] = NA_STRING;
} else {
const char* s = doEncodeURI(Rcpp::as<std::string>(value[i]), false).c_str();
Copy link
Member

Choose a reason for hiding this comment

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

Don't you have an encoding problem here too? What does doEncodeURI assume about the input? You might want a helper like this:

inline const char* string_utf8(SEXP x, int i) {
  return Rf_translateCharUTF8(STRING_ELT(x, i));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's true. Our current documentation for encodeURI says the following, but it's not generally useful behavior:

If conformant non-ASCII behavior is important, ensure that your input vector is UTF-8 encoded before calling encodeURI or encodeURIComponent.

This differs from the behavior of base::URLencode. I agree that we should change it.

utf8_str <- "\ue5"   # "å", in UTF-8
latin1_str <- iconv(utf8_str, "UTF-8", "latin1")

utf8_str
#> [1] "å"
latin1_str
#> [1] "å"

# Look at raw bytes
charToRaw(utf8_str)
#> [1] c3 a5
charToRaw(latin1_str)
#> [1] e5

# base::URLencode
URLencode(utf8_str)
#> [1] "%C3%A5"
URLencode(latin1_str)
#> [1] "%C3%A5"

# httpuv::encodeURI
httpuv::encodeURI(utf8_str)
#> [1] "%C3%A5"
httpuv::encodeURI(latin1_str)
#> [1] "%E5"

src/httpuv.cpp Outdated
*it = doEncodeURI(*it, false);
for (int i = 0; i < value.size(); i++) {
if (value[i] == NA_STRING) {
out[i] = NA_STRING;
Copy link
Member

Choose a reason for hiding this comment

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

You might want to see if the CharacterVector is already filled with NAs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like CharacterVector(n) will return a vector filled with "", but CharacterVector(n, NA_STRING) returns a string filled with NAs.

if (value[i] != NA_STRING) {
const char* s = Rf_translateCharUTF8(value[i]);
s = doEncodeURI(s, false).c_str();
out[i] = Rf_mkCharCE(s, CE_UTF8);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think this needs a PROTECT, but if someone else could confirm, I'd appreciate it.

Copy link
Member

Choose a reason for hiding this comment

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

It should not because you're immediately assigning into an object that Rcpp should be PROTECTing.

@wch wch merged commit e59dbe8 into master Feb 8, 2019
@wch wch deleted the fix-uri branch February 8, 2019 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants