Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apps] Added url percent decoding to url query string keys and values. #2015

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion apps/uriparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,41 @@ string UriParser::queryValue(const string& strKey) const
return m_mapQuery.at(strKey);
}

// NOTE: handles percent encoded single byte ASCII / Latin-1 characters but not unicode characters and encodings

static string url_decode(const string& str)
{
string ret;
size_t prev_idx;
size_t idx;

for (prev_idx = 0; string::npos != (idx = str.find('%', prev_idx)); prev_idx = idx + 3)
{
char tmp[3];
unsigned hex;

if (idx + 2 >= str.size()) // bad percent encoding
break;

tmp[0] = str[idx + 1];
tmp[1] = str[idx + 2];
tmp[2] = '\0';

if (!isxdigit((unsigned char) tmp[0]) || // bad percent encoding
!isxdigit((unsigned char) tmp[1]) ||
1 != sscanf(tmp, "%x", &hex) ||
0 == hex)
break;

ret += str.substr(prev_idx, idx - prev_idx);
ret += (char) hex;
}

ret += str.substr(prev_idx, str.size() - prev_idx);

return ret;
}

void UriParser::Parse(const string& strUrl, DefaultExpect exp)
{
int iQueryStart = -1;
Expand Down Expand Up @@ -305,7 +340,7 @@ void UriParser::Parse(const string& strUrl, DefaultExpect exp)
idx = strQueryPair.find("=");
if (idx != string::npos)
{
m_mapQuery[strQueryPair.substr(0, idx)] = strQueryPair.substr(idx + 1, strQueryPair.size() - (idx + 1));
m_mapQuery[url_decode(strQueryPair.substr(0, idx))] = url_decode(strQueryPair.substr(idx + 1, strQueryPair.size() - (idx + 1)));
}
}

Expand Down