Extracted isHarmlessUriChar()

This commit is contained in:
Veloman Yunkan 2023-01-17 19:41:33 +04:00
parent 822fb3748a
commit c5ccbd37e2
1 changed files with 22 additions and 7 deletions

View File

@ -161,6 +161,9 @@ std::string kiwix::encodeDiples(const std::string& str)
return result; return result;
} }
namespace
{
/* urlEncode() based on javascript encodeURI() & /* urlEncode() based on javascript encodeURI() &
encodeURIComponent(). Mostly code from rstudio/httpuv (GPLv3) */ encodeURIComponent(). Mostly code from rstudio/httpuv (GPLv3) */
@ -184,16 +187,15 @@ bool isReservedUrlChar(char c)
} }
} }
bool needsEscape(char c, bool encodeReserved) bool isHarmlessUriChar(char c)
{ {
if (c >= 'a' && c <= 'z') if (c >= 'a' && c <= 'z')
return false; return true;
if (c >= 'A' && c <= 'Z') if (c >= 'A' && c <= 'Z')
return false; return true;
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
return false; return true;
if (isReservedUrlChar(c))
return encodeReserved;
switch (c) { switch (c) {
case '-': case '-':
case '_': case '_':
@ -204,8 +206,19 @@ bool needsEscape(char c, bool encodeReserved)
case '\'': case '\'':
case '(': case '(':
case ')': case ')':
return false; return true;
} }
return false;
}
bool needsEscape(char c, bool encodeReserved)
{
if (isHarmlessUriChar(c))
return false;
if (isReservedUrlChar(c))
return encodeReserved;
return true; return true;
} }
@ -231,6 +244,8 @@ int hexToInt(char c) {
} }
} }
} // unnamed namespace
std::string kiwix::urlEncode(const std::string& value, bool encodeReserved) std::string kiwix::urlEncode(const std::string& value, bool encodeReserved)
{ {
std::ostringstream os; std::ostringstream os;