Merge pull request #192 from andrewgaul/special-characters

Simplify URL encoding
This commit is contained in:
Takeshi Nakatani 2015-06-20 11:47:22 +09:00
commit 4d49ace06b
2 changed files with 37 additions and 28 deletions

View File

@ -124,20 +124,20 @@ string urlEncode(const string &s)
{ {
string result; string result;
for (unsigned i = 0; i < s.length(); ++i) { for (unsigned i = 0; i < s.length(); ++i) {
if (s[i] == '/') { // Note- special case for fuse paths... char c = s[i];
result += s[i]; if (c == '/' // Note- special case for fuse paths...
} else if (isalnum(s[i])) { || c == '.'
result += s[i]; || c == '-'
} else if (s[i] == '.' || s[i] == '-' || s[i] == '*' || s[i] == '_') { || c == '_'
result += s[i]; || c == '~'
} else if (s[i] == ' ') { || (c >= 'a' && c <= 'z')
result += '%'; || (c >= 'A' && c <= 'Z')
result += '2'; || (c >= '0' && c <= '9')) {
result += '0'; result += c;
} else { } else {
result += "%"; result += "%";
result += hexAlphabet[static_cast<unsigned char>(s[i]) / 16]; result += hexAlphabet[static_cast<unsigned char>(c) / 16];
result += hexAlphabet[static_cast<unsigned char>(s[i]) % 16]; result += hexAlphabet[static_cast<unsigned char>(c) % 16];
} }
} }
return result; return result;
@ -152,24 +152,22 @@ string urlEncode2(const string &s)
{ {
string result; string result;
for (unsigned i = 0; i < s.length(); ++i) { for (unsigned i = 0; i < s.length(); ++i) {
if (s[i] == '=') { // Note- special case for fuse paths... char c = s[i];
result += s[i]; if (c == '=' // Note- special case for fuse paths...
}else if (s[i] == '&') { // Note- special case for s3... || c == '&' // Note- special case for s3...
result += s[i]; || c == '%'
}else if(s[i] == '%'){ || c == '.'
result += s[i]; || c == '-'
} else if (isalnum(s[i])) { || c == '_'
result += s[i]; || c == '~'
} else if (s[i] == '.' || s[i] == '-' || s[i] == '*' || s[i] == '_') { || (c >= 'a' && c <= 'z')
result += s[i]; || (c >= 'A' && c <= 'Z')
} else if (s[i] == ' ') { || (c >= '0' && c <= '9')) {
result += '%'; result += c;
result += '2';
result += '0';
} else { } else {
result += "%"; result += "%";
result += hexAlphabet[static_cast<unsigned char>(s[i]) / 16]; result += hexAlphabet[static_cast<unsigned char>(c) / 16];
result += hexAlphabet[static_cast<unsigned char>(s[i]) % 16]; result += hexAlphabet[static_cast<unsigned char>(c) % 16];
} }
} }
return result; return result;

View File

@ -303,6 +303,17 @@ fi
rm -f "/tmp/${BIG_FILE}" rm -f "/tmp/${BIG_FILE}"
rm -f "${BIG_FILE}" rm -f "${BIG_FILE}"
##########################################################
# Testing special characters
##########################################################
echo "Testing special characters ..."
ls 'special' 2>&1 | grep -q 'No such file or directory'
ls 'special?' 2>&1 | grep -q 'No such file or directory'
ls 'special*' 2>&1 | grep -q 'No such file or directory'
ls 'special~' 2>&1 | grep -q 'No such file or directory'
ls 'specialµ' 2>&1 | grep -q 'No such file or directory'
##################################################################### #####################################################################
# Tests are finished # Tests are finished
##################################################################### #####################################################################