Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/rexyai/RestRserve into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
artemklevtsov committed Dec 25, 2019
2 parents 850c6a5 + e456765 commit ce266ab
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion inst/tinytest/test-url-encoding.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
url_encode = RestRserve:::url_encode
url_decode = RestRserve:::url_decode

text = c("Hello, World", "Hello Günter")
text = c("Hello, World", "Hello G\u00fcnter")

# Test empty input
expect_error(url_encode(NULL))
Expand Down
3 changes: 1 addition & 2 deletions src/url_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ static inline char from_hex(char ch) {
}

std::string url_decode_one(const std::string& value) {
char h;
std::ostringstream escaped;
escaped.fill('0');
for (auto cur = value.begin(), end = value.end(); cur != end; ++cur) {
std::string::value_type c = (*cur);
if (c == '%') {
if (cur[1] && cur[2]) {
h = from_hex(cur[1]) << 4 | from_hex(cur[2]);
char h = from_hex(cur[1]) << 4 | from_hex(cur[2]);
escaped << h;
cur += 2;
}
Expand Down
9 changes: 5 additions & 4 deletions src/url_encode.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#include <cctype>
#include <locale>
#include <iomanip>
#include <string>
#include <sstream>
#include <Rcpp.h>

std::string url_encode_one(const std::string& value) {
std::locale loc("C");
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (auto cur = value.begin(), end = value.end(); cur != end; ++cur) {
std::string::value_type c = (*cur);
// Keep alphanumeric and other accepted characters intact
// Keep alphanumeric and other accepted characters
// See: https://tools.ietf.org/html/rfc3986#section-2.3
if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
if (std::isalnum(c, loc) || c == '-' || c == '.' || c == '_' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << std::uppercase;
escaped << '%' << std::setw(2);
escaped << static_cast<unsigned int>(static_cast<unsigned char>(c));
escaped << static_cast<int>(static_cast<unsigned char>(c));
escaped << std::nouppercase;
}

Expand Down

0 comments on commit ce266ab

Please sign in to comment.