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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Remotes:
r-lib/later
Collate:
'RcppExports.R'
'decode_uri.R'
'httpuv.R'
'server.R'
'static_paths.R'
Expand Down
24 changes: 14 additions & 10 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ base64encode <- function(x) {
}

#' URI encoding/decoding
#'
#'
#' Encodes/decodes strings using URI encoding/decoding in the same way that web
#' browsers do. The precise behaviors of these functions can be found at
#' developer.mozilla.org:
#' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI}{encodeURI},
#' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent}{encodeURIComponent},
#' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI}{decodeURI},
#' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent}{decodeURIComponent}
#'
#'
#' Intended as a faster replacement for \code{\link[utils]{URLencode}} and
#' \code{\link[utils]{URLdecode}}.
#'
#'
#' encodeURI differs from encodeURIComponent in that the former will not encode
#' reserved characters: \code{;,/?:@@&=+$}
#'
#'
#' decodeURI differs from decodeURIComponent in that it will refuse to decode
#' encoded sequences that decode to a reserved character. (If in doubt, use
#' decodeURIComponent.)
#'
#'
#' The only way these functions differ from web browsers is in the encoding of
#' non-ASCII characters. All non-ASCII characters will be escaped byte-by-byte.
#' If conformant non-ASCII behavior is important, ensure that your input vector
#' is UTF-8 encoded before calling encodeURI or encodeURIComponent.
#'
#'
#' @param value Character vector to be encoded or decoded.
#' @return Encoded or decoded character vector of the same length as the
#' input value. \code{decodeURI} and \code{decodeURIComponent} will return
Expand All @@ -86,12 +86,16 @@ encodeURIComponent <- function(value) {
.Call('_httpuv_encodeURIComponent', PACKAGE = 'httpuv', value)
}

decodeURI_ <- function(value) {
.Call('_httpuv_decodeURI_', PACKAGE = 'httpuv', value)
#' @rdname encodeURI
#' @export
decodeURI <- function(value) {
.Call('_httpuv_decodeURI', PACKAGE = 'httpuv', value)
}

decodeURIComponent_ <- function(value) {
.Call('_httpuv_decodeURIComponent_', PACKAGE = 'httpuv', value)
#' @rdname encodeURI
#' @export
decodeURIComponent <- function(value) {
.Call('_httpuv_decodeURIComponent', PACKAGE = 'httpuv', value)
}

#' Check whether an address is IPv4 or IPv6
Expand Down
15 changes: 0 additions & 15 deletions R/decode_uri.R

This file was deleted.

2 changes: 1 addition & 1 deletion man/encodeURI.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/RcppExports-legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

using namespace Rcpp;

std::vector<std::string> decodeURIComponent_(std::vector<std::string> value);
Rcpp::CharacterVector decodeURIComponent(Rcpp::CharacterVector value);
RcppExport SEXP httpuv_decodeURIComponent(SEXP valueSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<std::string> >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(decodeURIComponent_(value));
Rcpp::CharacterVector value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(decodeURIComponent(value));
return rcpp_result_gen;
END_RCPP
}
34 changes: 17 additions & 17 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,46 @@ BEGIN_RCPP
END_RCPP
}
// encodeURI
std::vector<std::string> encodeURI(std::vector<std::string> value);
Rcpp::CharacterVector encodeURI(Rcpp::CharacterVector value);
RcppExport SEXP _httpuv_encodeURI(SEXP valueSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<std::string> >::type value(valueSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(encodeURI(value));
return rcpp_result_gen;
END_RCPP
}
// encodeURIComponent
std::vector<std::string> encodeURIComponent(std::vector<std::string> value);
Rcpp::CharacterVector encodeURIComponent(Rcpp::CharacterVector value);
RcppExport SEXP _httpuv_encodeURIComponent(SEXP valueSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<std::string> >::type value(valueSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(encodeURIComponent(value));
return rcpp_result_gen;
END_RCPP
}
// decodeURI_
std::vector<std::string> decodeURI_(std::vector<std::string> value);
RcppExport SEXP _httpuv_decodeURI_(SEXP valueSEXP) {
// decodeURI
Rcpp::CharacterVector decodeURI(Rcpp::CharacterVector value);
RcppExport SEXP _httpuv_decodeURI(SEXP valueSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<std::string> >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(decodeURI_(value));
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(decodeURI(value));
return rcpp_result_gen;
END_RCPP
}
// decodeURIComponent_
std::vector<std::string> decodeURIComponent_(std::vector<std::string> value);
RcppExport SEXP _httpuv_decodeURIComponent_(SEXP valueSEXP) {
// decodeURIComponent
Rcpp::CharacterVector decodeURIComponent(Rcpp::CharacterVector value);
RcppExport SEXP _httpuv_decodeURIComponent(SEXP valueSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<std::string> >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(decodeURIComponent_(value));
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type value(valueSEXP);
rcpp_result_gen = Rcpp::wrap(decodeURIComponent(value));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -251,13 +251,13 @@ static const R_CallMethodDef CallEntries[] = {
{"_httpuv_base64encode", (DL_FUNC) &_httpuv_base64encode, 1},
{"_httpuv_encodeURI", (DL_FUNC) &_httpuv_encodeURI, 1},
{"_httpuv_encodeURIComponent", (DL_FUNC) &_httpuv_encodeURIComponent, 1},
{"_httpuv_decodeURI_", (DL_FUNC) &_httpuv_decodeURI_, 1},
{"_httpuv_decodeURIComponent_", (DL_FUNC) &_httpuv_decodeURIComponent_, 1},
{"_httpuv_decodeURI", (DL_FUNC) &_httpuv_decodeURI, 1},
{"_httpuv_decodeURIComponent", (DL_FUNC) &_httpuv_decodeURIComponent, 1},
{"_httpuv_ipFamily", (DL_FUNC) &_httpuv_ipFamily, 1},
{"_httpuv_invokeCppCallback", (DL_FUNC) &_httpuv_invokeCppCallback, 2},
{"_httpuv_getRNGState", (DL_FUNC) &_httpuv_getRNGState, 0},
{"_httpuv_wsconn_address", (DL_FUNC) &_httpuv_wsconn_address, 1},
{"httpuv_decodeURIComponent", (DL_FUNC) &httpuv_decodeURIComponent, 1},
{"httpuv_decodeURIComponent", (DL_FUNC) &httpuv_decodeURIComponent, 1},
{NULL, NULL, 0}
};

Expand Down
95 changes: 57 additions & 38 deletions src/httpuv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ std::string doEncodeURI(std::string value, bool encodeReserved) {
for (std::string::const_iterator it = value.begin();
it != value.end();
it++) {

if (!needsEscape(*it, encodeReserved)) {
os << *it;
} else {
Expand All @@ -501,60 +501,66 @@ std::string doEncodeURI(std::string value, bool encodeReserved) {
}

//' URI encoding/decoding
//'
//'
//' Encodes/decodes strings using URI encoding/decoding in the same way that web
//' browsers do. The precise behaviors of these functions can be found at
//' developer.mozilla.org:
//' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI}{encodeURI},
//' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent}{encodeURIComponent},
//' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI}{decodeURI},
//' \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent}{decodeURIComponent}
//'
//'
//' Intended as a faster replacement for \code{\link[utils]{URLencode}} and
//' \code{\link[utils]{URLdecode}}.
//'
//'
//' encodeURI differs from encodeURIComponent in that the former will not encode
//' reserved characters: \code{;,/?:@@&=+$}
//'
//'
//' decodeURI differs from decodeURIComponent in that it will refuse to decode
//' encoded sequences that decode to a reserved character. (If in doubt, use
//' decodeURIComponent.)
//'
//'
//' The only way these functions differ from web browsers is in the encoding of
//' non-ASCII characters. All non-ASCII characters will be escaped byte-by-byte.
//' If conformant non-ASCII behavior is important, ensure that your input vector
//' is UTF-8 encoded before calling encodeURI or encodeURIComponent.
//'
//'
//' @param value Character vector to be encoded or decoded.
//' @return Encoded or decoded character vector of the same length as the
//' input value. \code{decodeURI} and \code{decodeURIComponent} will return
//' strings that are UTF-8 encoded.
//'
//' @export
// [[Rcpp::export]]
std::vector<std::string> encodeURI(std::vector<std::string> value) {
for (std::vector<std::string>::iterator it = value.begin();
it != value.end();
it++) {
Rcpp::CharacterVector encodeURI(Rcpp::CharacterVector value) {
Rcpp::CharacterVector out(value.size());

*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.

} 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"

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.

}
}

return value;
return out;
}

//' @rdname encodeURI
//' @export
// [[Rcpp::export]]
std::vector<std::string> encodeURIComponent(std::vector<std::string> value) {
for (std::vector<std::string>::iterator it = value.begin();
it != value.end();
it++) {
Rcpp::CharacterVector encodeURIComponent(Rcpp::CharacterVector value) {
Rcpp::CharacterVector out(value.size());

*it = doEncodeURI(*it, true);
for (int i = 0; i < value.size(); i++) {
if (value[i] == NA_STRING) {
out[i] = NA_STRING;
} else {
const char* s = doEncodeURI(Rcpp::as<std::string>(value[i]), true).c_str();
out[i] = Rf_mkCharCE(s, CE_UTF8);
}
}

return value;
return out;
}

int hexToInt(char c) {
Expand Down Expand Up @@ -584,14 +590,14 @@ std::string doDecodeURI(std::string value, bool component) {
for (std::string::const_iterator it = value.begin();
it != value.end();
it++) {

// If there aren't enough characters left for this to be a
// valid escape code, just use the character and move on
if (it > value.end() - 3) {
os << *it;
continue;
}

if (*it == '%') {
char hi = *(++it);
char lo = *(++it);
Expand All @@ -612,32 +618,45 @@ std::string doDecodeURI(std::string value, bool component) {
os << *it;
}
}

return os.str();
}


//' @rdname encodeURI
//' @export
// [[Rcpp::export]]
std::vector<std::string> decodeURI_(std::vector<std::string> value) {
for (std::vector<std::string>::iterator it = value.begin();
it != value.end();
it++) {
Rcpp::CharacterVector decodeURI(Rcpp::CharacterVector value) {
Rcpp::CharacterVector out(value.size());

*it = doDecodeURI(*it, false);
for (int i = 0; i < value.size(); i++) {
if (value[i] == NA_STRING) {
out[i] = NA_STRING;
} else {
const char* s = doDecodeURI(Rcpp::as<std::string>(value[i]), false).c_str();
out[i] = Rf_mkCharCE(s, CE_UTF8);
}
}
return value;

return out;
}

//' @rdname encodeURI
//' @export
// [[Rcpp::export]]
std::vector<std::string> decodeURIComponent_(std::vector<std::string> value) {
for (std::vector<std::string>::iterator it = value.begin();
it != value.end();
it++) {
Rcpp::CharacterVector decodeURIComponent(Rcpp::CharacterVector value) {
Rcpp::CharacterVector out(value.size());

*it = doDecodeURI(*it, true);
for (int i = 0; i < value.size(); i++) {
if (value[i] == NA_STRING) {
out[i] = NA_STRING;
} else {
const char* s = doDecodeURI(Rcpp::as<std::string>(value[i]), true).c_str();
out[i] = Rf_mkCharCE(s, CE_UTF8);
}
}
return value;

return out;
}

//' Check whether an address is IPv4 or IPv6
Expand Down
Loading