X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fhttp%2Futils.cpp;h=41fabeff80cb83c83e1ffd430b328073a27e1eb0;hb=HEAD;hp=5a5dc0487c0f1bf4dbd9132143e14567d7c30574;hpb=cf8d2e48581eeb8f1b83e8c48321a0bc2ffa6d83;p=libs%2Fnet.git diff --git a/source/http/utils.cpp b/source/http/utils.cpp index 5a5dc04..7e1037e 100644 --- a/source/http/utils.cpp +++ b/source/http/utils.cpp @@ -1,8 +1,8 @@ +#include "utils.h" #include -#include +#include #include #include -#include "utils.h" using namespace std; @@ -31,12 +31,13 @@ namespace Http { string urlencode(const string &str, EncodeLevel level) { string result; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + result.reserve(str.size()); + for(char c: str) { - if(is_reserved(*i, level)) - result += format("%%%02X", *i); + if(is_reserved(c, level)) + result += format("%%%02X", c); else - result += *i; + result += c; } return result; } @@ -44,14 +45,15 @@ string urlencode(const string &str, EncodeLevel level) string urlencode_plus(const string &str, EncodeLevel level) { string result; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + result.reserve(str.size()); + for(char c: str) { - if(*i==' ') + if(c==' ') result += '+'; - else if(is_reserved(*i, level)) - result += format("%%%02X", *i); + else if(is_reserved(c, level)) + result += format("%%%02X", c); else - result += *i; + result += c; } return result; } @@ -79,7 +81,7 @@ string urldecode(const string &str) Url parse_url(const string &str) { - static Regex r_url("(([a-z]+)://)?([a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(:[0-9])?)?(/[^?#]*)?(\\?([^#]+))?(#(.*))?"); + static Regex r_url("^(([a-z]+)://)?([a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(:[0-9]+)?)?(/[^?#]*)?(\\?([^#]*))?(#(.*))?$"); if(RegMatch m = r_url.match(str)) { Url url; @@ -104,29 +106,20 @@ string build_url(const Url &url) str += url.scheme+"://"; str += url.host; str += urlencode(url.path); - if(!url.query.empty()) - { - str += '?'; - str += url.query; - } - if(!url.fragment.empty()) - { - str += '#'; - str += url.fragment; - } + append(str, "?", url.query); + append(str, "#", url.fragment); return str; } Query parse_query(const std::string &str) { - vector parts = split(str, '&'); Query query; - for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) + for(const string &p: split(str, '&')) { - unsigned equals = i->find('='); - string &value = query[urldecode(i->substr(0, equals))]; + string::size_type equals = p.find('='); + string &value = query[urldecode(p.substr(0, equals))]; if(equals!=string::npos) - value = urldecode(i->substr(equals+1)); + value = urldecode(p.substr(equals+1)); } return query; } @@ -134,13 +127,11 @@ Query parse_query(const std::string &str) string build_query(const Query &query) { string str; - for(Query::const_iterator i=query.begin(); i!=query.end(); ++i) + for(const auto &kvp: query) { - if(i!=query.begin()) - str += '&'; - str += urlencode_plus(i->first); + append(str, "&", urlencode_plus(kvp.first)); str += '='; - str += urlencode_plus(i->second); + str += urlencode_plus(kvp.second); } return str; }