From 1ae7ec6058962df3099f6b309e5bdb08136cf683 Mon Sep 17 00:00:00 2001 From: antao Date: Mon, 7 Jan 2019 14:43:06 +0800 Subject: [PATCH 1/2] Optimize the urlDecode() method --- lib/inc/drogon/utils/Utilities.h | 1 + lib/src/HttpRequestImpl.h | 2 +- lib/src/Utilities.cc | 45 +++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h index c2c078eb..f0c7dc28 100755 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -51,6 +51,7 @@ std::string base64Decode(std::string const &encoded_string); /// Decode the URL format string send by web browser. std::string urlDecode(const std::string &szToDecode); +std::string urlDecode(const char *begin, const char *end); /// Commpress or decompress data using gzip lib. /** diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 7ec9de17..0a6d33f4 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -135,7 +135,7 @@ class HttpRequestImpl : public HttpRequest void setPath(const char *start, const char *end) { - _path = urlDecode(std::string(start, end)); + _path = urlDecode(start, end); } virtual void setPath(const std::string &path) override { diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 93a4eaf0..fa143812 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -267,22 +267,53 @@ std::string base64Decode(std::string const &encoded_string) } std::string urlDecode(const std::string &szToDecode) +{ + return urlDecode(szToDecode.c_str(), szToDecode.c_str() + szToDecode.length()); +} + +std::string urlDecode(const char *begin, const char *end) { std::string result; - result.reserve(szToDecode.length()); + auto len = end - begin; + result.reserve(len); int hex = 0; - for (size_t i = 0; i < szToDecode.length(); ++i) + for (size_t i = 0; i < len; ++i) { - switch (szToDecode[i]) + switch (begin[i]) { case '+': result += ' '; break; case '%': - if (isxdigit(szToDecode[i + 1]) && isxdigit(szToDecode[i + 2])) + if ((i + 2) < len && isxdigit(begin[i + 1]) && isxdigit(begin[i + 2])) { - std::string hexStr = szToDecode.substr(i + 1, 2); - hex = strtol(hexStr.c_str(), 0, 16); + uint x1 = begin[i + 1]; + if (x1 >= '0' && x1 <= '9') + { + x1 -= '0'; + } + else if (x1 >= 'a' && x1 <= 'f') + { + x1 = x1 - 'a' + 10; + } + else if (x1 >= 'A' && x1 <= 'F') + { + x1 = x1 - 'A' + 10; + } + uint x2 = begin[i + 2]; + if (x2 >= '0' && x2 <= '9') + { + x2 -= '0'; + } + else if (x2 >= 'a' && x2 <= 'f') + { + x2 = x2 - 'a' + 10; + } + else if (x2 >= 'A' && x2 <= 'F') + { + x2 = x2 - 'A' + 10; + } + hex = x1 * 16 + x2; //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@] //可以不经过编码直接用于URL if (!((hex >= 48 && hex <= 57) || //0-9 @@ -303,7 +334,7 @@ std::string urlDecode(const std::string &szToDecode) } break; default: - result += szToDecode[i]; + result += begin[i]; break; } } From 8cfdd0424fb13a15a5c99aa3a88872937534fe12 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Mon, 7 Jan 2019 15:25:29 +0800 Subject: [PATCH 2/2] Fix a compiler warning --- lib/src/Utilities.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index fa143812..7a182a91 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -274,7 +274,7 @@ std::string urlDecode(const std::string &szToDecode) std::string urlDecode(const char *begin, const char *end) { std::string result; - auto len = end - begin; + size_t len = end - begin; result.reserve(len); int hex = 0; for (size_t i = 0; i < len; ++i)