From 4f59dc84bfa4be6e5fcea98b5f8775ddf7764dfa Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 11 Nov 2007 04:25:56 +0000 Subject: 2007-11-11 Tatsuhiro Tsujikawa urlencode the given url inside Request::parseUrl(...) * src/Request.{h, cc} * src/Util.{h, cc} * test/RequestTest.cc Removed #!metalink3! notation support because it is deleted from the metalink specification. * src/Request.{h, cc} * test/RequestTest.cc --- src/Request.cc | 40 ++++++++++++++++++++++++++++++---------- src/Request.h | 7 +++++-- src/Util.cc | 26 +++++++++++++++++--------- src/Util.h | 2 ++ 4 files changed, 54 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/Request.cc b/src/Request.cc index c519c501..69cf025e 100644 --- a/src/Request.cc +++ b/src/Request.cc @@ -66,14 +66,9 @@ bool Request::parseUrl(const string& url) { string tempUrl; string::size_type sharpIndex = url.find("#"); if(sharpIndex != string::npos) { - if(FeatureConfig::getInstance()->isSupported("metalink") && - url.find(METALINK_MARK) == sharpIndex) { - tempUrl = url.substr(sharpIndex+strlen(METALINK_MARK)); - } else { - tempUrl = url.substr(0, sharpIndex); - } + tempUrl = urlencode(url.substr(0, sharpIndex)); } else { - tempUrl = url; + tempUrl = urlencode(url); } currentUrl = tempUrl; string query; @@ -83,9 +78,6 @@ bool Request::parseUrl(const string& url) { file = ""; _username = ""; _password = ""; - if(tempUrl.find_first_not_of(SAFE_CHARS) != string::npos) { - return false; - } // find query part string::size_type startQueryIndex = tempUrl.find("?"); if(startQueryIndex != string::npos) { @@ -152,3 +144,31 @@ bool Request::parseUrl(const string& url) { file += query; return true; } + +bool Request::isHexNumber(const char c) const +{ + return '0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f'; +} + +string Request::urlencode(const string& src) const +{ + int32_t lastIndex = src.size()-1; + string result = src+" "; + for(int32_t index = lastIndex; index >= 0; --index) { + const char c = result[index]; + // '/' is not urlencoded because src is expected to be a path. + if(Util::shouldUrlencode(c)) { + if(c == '%') { + if(!isHexNumber(result[index+1]) || !isHexNumber(result[index+2])) { + result.replace(index, 1, "%25"); + } + } else { + char temp[4]; + sprintf(temp, "%%%02x", c); + temp[3] = '\0'; + result.replace(index, 1, temp); + } + } + } + return result.substr(0, result.size()-2); +} diff --git a/src/Request.h b/src/Request.h index 3dc26c35..facbc022 100644 --- a/src/Request.h +++ b/src/Request.h @@ -46,8 +46,6 @@ "%"\ "#" -#define METALINK_MARK "#!metalink3!" - class Request { public: enum TRACKER_EVENT { @@ -83,6 +81,11 @@ private: string _password; bool parseUrl(const string& url); + + bool isHexNumber(const char c) const; + + string urlencode(const string& src) const; + public: CookieBoxHandle cookieBox; public: diff --git a/src/Util.cc b/src/Util.cc index 9035766f..00f84399 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -247,18 +247,26 @@ string Util::replace(const string& target, const string& oldstr, const string& n return result; } +bool Util::shouldUrlencode(const char c) +{ + return !(// ALPHA + 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || + // DIGIT + '0' <= c && c <= '9' || + // safe + '$' == c || '-' == c || '_' == c || '.' == c || + // extra + '!' == c || '*' == c || '\'' == c ||'(' == c || + ')' == c || ',' == c || + // reserved + ';' == c || '/' == c || '?' == c || ':' == c || + '@' == c || '&' == c || '=' == c || '+' == c); +} + string Util::urlencode(const unsigned char* target, int32_t len) { string dest; for(int32_t i = 0; i < len; i++) { - if(!('0' <= target[i] && target[i] <= '9' || - 'A' <= target[i] && target[i] <= 'Z' || - 'a' <= target[i] && target[i] <= 'z' || - '$' == target[i] || '-' == target[i] || - '_' == target[i] || '.' == target[i] || - '+' == target[i] || '!' == target[i] || - '*' == target[i] || '\'' == target[i] || - '(' == target[i] || ')' == target[i] || - ',' == target[i])) { + if(shouldUrlencode(target[i])) { char temp[4]; sprintf(temp, "%%%02x", target[i]); temp[sizeof(temp)-1] = '\0'; diff --git a/src/Util.h b/src/Util.h index 1873566b..f49219fa 100644 --- a/src/Util.h +++ b/src/Util.h @@ -89,6 +89,8 @@ public: return urlencode((const unsigned char*)target.c_str(), target.size()); } + static bool shouldUrlencode(const char c); + static string urldecode(const string& target); static string torrentUrlencode(const unsigned char* target, int32_t len); -- cgit