From d8cb75d3878919b84e4323fecf1cc791fb2338ae Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Sat, 13 Apr 2024 20:36:15 +0800 Subject: [PATCH] Improve url encode and decode code add user pass encode decode methond (#3468) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 去除url转义的冗余代码,添加用户名与密码的转义 --- src/Common/Parser.cpp | 4 +-- src/Common/strCoding.cpp | 60 +++++++++++++++++++++++++--------------- src/Common/strCoding.h | 2 ++ 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index 7e7de860..47f3c2d8 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -294,8 +294,8 @@ void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const st splitUrl(ip, ip, port); _url = std::move(url); - _user = strCoding::UrlDecodeComponent(user); - _passwd = strCoding::UrlDecodeComponent(passwd); + _user = strCoding::UrlDecodeUserOrPass(user); + _passwd = strCoding::UrlDecodeUserOrPass(passwd); _host = std::move(ip); _port = port; _is_ssl = is_ssl; diff --git a/src/Common/strCoding.cpp b/src/Common/strCoding.cpp index 1a0f0236..49a345ef 100644 --- a/src/Common/strCoding.cpp +++ b/src/Common/strCoding.cpp @@ -52,9 +52,7 @@ char HexStrToBin(const char *str) { } return (high << 4) | low; } - -string strCoding::UrlEncodePath(const string &str) { - const char *dont_escape = "!#&'*+:=?@/._-$,;~()"; +static string UrlEncodeCommon(const string &str,const char* dont_escape){ string out; size_t len = str.size(); for (size_t i = 0; i < len; ++i) { @@ -69,26 +67,7 @@ string strCoding::UrlEncodePath(const string &str) { } return out; } - -string strCoding::UrlEncodeComponent(const string &str) { - const char *dont_escape = "!'()*-._~"; - string out; - size_t len = str.size(); - for (size_t i = 0; i < len; ++i) { - char ch = str[i]; - if (isalnum((uint8_t) ch) || strchr(dont_escape, (uint8_t) ch) != NULL) { - out.push_back(ch); - } else { - char buf[4]; - snprintf(buf, 4, "%%%X%X", (uint8_t) ch >> 4, (uint8_t) ch & 0x0F); - out.append(buf); - } - } - return out; -} - -string strCoding::UrlDecodePath(const string &str) { - const char *dont_unescape = "#$&+,/:;=?@"; +static string UrlDecodeCommon(const string &str,const char* dont_unescape){ string output; size_t i = 0, len = str.length(); while (i < len) { @@ -114,6 +93,36 @@ string strCoding::UrlDecodePath(const string &str) { return output; } +string strCoding::UrlEncodePath(const string &str) { + const char *dont_escape = "!#&'*+:=?@/._-$,;~()"; + return UrlEncodeCommon(str,dont_escape); +} + +string strCoding::UrlEncodeComponent(const string &str) { + const char *dont_escape = "!'()*-._~"; + return UrlEncodeCommon(str,dont_escape); +} + +std::string strCoding::UrlEncodeUserOrPass(const std::string &str) { + // from rfc https://datatracker.ietf.org/doc/html/rfc3986 + // §2.3 Unreserved characters (mark) + //'-', '_', '.', '~' + // §2.2 Reserved characters (reserved) + // '$', '&', '+', ',', '/', ':', ';', '=', '?', '@', + // §3.2.1 + // The RFC allows ';', ':', '&', '=', '+', '$', and ',' in + // userinfo, so we must escape only '@', '/', and '?'. + // The parsing of userinfo treats ':' as special so we must escape + // that too. + const char *dont_escape = "$&+,;=-._~"; + return UrlEncodeCommon(str,dont_escape); +} + +string strCoding::UrlDecodePath(const string &str) { + const char *dont_unescape = "#$&+,/:;=?@"; + return UrlDecodeCommon(str,dont_unescape); +} + std::string strCoding::UrlDecodeComponent(const std::string &str) { string output; size_t i = 0, len = str.length(); @@ -143,6 +152,11 @@ std::string strCoding::UrlDecodeComponent(const std::string &str) { return output; } + +std::string strCoding::UrlDecodeUserOrPass(const std::string &str) { + const char *dont_unescape = ""; + return UrlDecodeCommon(str,dont_unescape); +} ///////////////////////////////windows专用/////////////////////////////////// #if defined(_WIN32) void UnicodeToGB2312(char* pOut, wchar_t uData) diff --git a/src/Common/strCoding.h b/src/Common/strCoding.h index e715e74d..bfddf7ff 100644 --- a/src/Common/strCoding.h +++ b/src/Common/strCoding.h @@ -22,6 +22,8 @@ public: static std::string UrlEncodeComponent(const std::string &str); // url参数 utf8编码 static std::string UrlDecodePath(const std::string &str); //url路径 utf8解码 static std::string UrlDecodeComponent(const std::string &str); // url参数 utf8解码 + static std::string UrlEncodeUserOrPass(const std::string &str); // url中用户名与密码编码 + static std::string UrlDecodeUserOrPass(const std::string &str); // url中用户名与密码解码 #if defined(_WIN32) static std::string UTF8ToGB2312(const std::string &str);//utf_8转为gb2312 static std::string GB2312ToUTF8(const std::string &str); //gb2312 转utf_8