mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 10:40:05 +08:00
Improve url encode and decode code add user pass encode decode methond (#3468)
去除url转义的冗余代码,添加用户名与密码的转义
This commit is contained in:
parent
3dcd0ed463
commit
d8cb75d387
@ -294,8 +294,8 @@ void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const st
|
|||||||
splitUrl(ip, ip, port);
|
splitUrl(ip, ip, port);
|
||||||
|
|
||||||
_url = std::move(url);
|
_url = std::move(url);
|
||||||
_user = strCoding::UrlDecodeComponent(user);
|
_user = strCoding::UrlDecodeUserOrPass(user);
|
||||||
_passwd = strCoding::UrlDecodeComponent(passwd);
|
_passwd = strCoding::UrlDecodeUserOrPass(passwd);
|
||||||
_host = std::move(ip);
|
_host = std::move(ip);
|
||||||
_port = port;
|
_port = port;
|
||||||
_is_ssl = is_ssl;
|
_is_ssl = is_ssl;
|
||||||
|
@ -52,9 +52,7 @@ char HexStrToBin(const char *str) {
|
|||||||
}
|
}
|
||||||
return (high << 4) | low;
|
return (high << 4) | low;
|
||||||
}
|
}
|
||||||
|
static string UrlEncodeCommon(const string &str,const char* dont_escape){
|
||||||
string strCoding::UrlEncodePath(const string &str) {
|
|
||||||
const char *dont_escape = "!#&'*+:=?@/._-$,;~()";
|
|
||||||
string out;
|
string out;
|
||||||
size_t len = str.size();
|
size_t len = str.size();
|
||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
@ -69,26 +67,7 @@ string strCoding::UrlEncodePath(const string &str) {
|
|||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
static string UrlDecodeCommon(const string &str,const char* dont_unescape){
|
||||||
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 = "#$&+,/:;=?@";
|
|
||||||
string output;
|
string output;
|
||||||
size_t i = 0, len = str.length();
|
size_t i = 0, len = str.length();
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
@ -114,6 +93,36 @@ string strCoding::UrlDecodePath(const string &str) {
|
|||||||
return output;
|
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) {
|
std::string strCoding::UrlDecodeComponent(const std::string &str) {
|
||||||
string output;
|
string output;
|
||||||
size_t i = 0, len = str.length();
|
size_t i = 0, len = str.length();
|
||||||
@ -143,6 +152,11 @@ std::string strCoding::UrlDecodeComponent(const std::string &str) {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string strCoding::UrlDecodeUserOrPass(const std::string &str) {
|
||||||
|
const char *dont_unescape = "";
|
||||||
|
return UrlDecodeCommon(str,dont_unescape);
|
||||||
|
}
|
||||||
///////////////////////////////windows专用///////////////////////////////////
|
///////////////////////////////windows专用///////////////////////////////////
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
void UnicodeToGB2312(char* pOut, wchar_t uData)
|
void UnicodeToGB2312(char* pOut, wchar_t uData)
|
||||||
|
@ -22,6 +22,8 @@ public:
|
|||||||
static std::string UrlEncodeComponent(const std::string &str); // url参数 utf8编码
|
static std::string UrlEncodeComponent(const std::string &str); // url参数 utf8编码
|
||||||
static std::string UrlDecodePath(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 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)
|
#if defined(_WIN32)
|
||||||
static std::string UTF8ToGB2312(const std::string &str);//utf_8转为gb2312
|
static std::string UTF8ToGB2312(const std::string &str);//utf_8转为gb2312
|
||||||
static std::string GB2312ToUTF8(const std::string &str); //gb2312 转utf_8
|
static std::string GB2312ToUTF8(const std::string &str); //gb2312 转utf_8
|
||||||
|
Loading…
Reference in New Issue
Block a user