Files

69 lines
1.2 KiB
C++
Raw Permalink Normal View History

/* (c) ZeroTier, Inc.
* See LICENSE.txt in nonfree/
*/
2025-04-03 14:26:09 -07:00
#include "CtlUtil.hpp"
#ifdef ZT_CONTROLLER_USE_LIBPQ
#include <iomanip>
2025-07-03 11:26:23 -04:00
#include <sstream>
2025-04-03 14:26:09 -07:00
namespace ZeroTier {
2025-07-03 11:26:23 -04:00
const char* _timestr()
2025-04-03 14:26:09 -07:00
{
time_t t = time(0);
2025-07-03 11:26:23 -04:00
char* ts = ctime(&t);
char* p = ts;
if (! p)
2025-04-03 14:26:09 -07:00
return "";
while (*p) {
if (*p == '\n') {
*p = (char)0;
break;
}
++p;
}
return ts;
}
2025-07-03 11:26:23 -04:00
std::vector<std::string> split(std::string str, char delim)
{
2025-04-03 14:26:09 -07:00
std::istringstream iss(str);
std::vector<std::string> tokens;
std::string item;
2025-07-03 11:26:23 -04:00
while (std::getline(iss, item, delim)) {
2025-04-03 14:26:09 -07:00
tokens.push_back(item);
}
return tokens;
}
2025-07-03 11:26:23 -04:00
std::string url_encode(const std::string& value)
{
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
2025-04-03 14:26:09 -07:00
2025-07-03 11:26:23 -04:00
for (std::string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
std::string::value_type c = (*i);
2025-04-03 14:26:09 -07:00
2025-07-03 11:26:23 -04:00
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
2025-04-03 14:26:09 -07:00
2025-07-03 11:26:23 -04:00
// Any other characters are percent-encoded
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int((unsigned char)c);
escaped << std::nouppercase;
}
2025-04-03 14:26:09 -07:00
2025-07-03 11:26:23 -04:00
return escaped.str();
2025-04-03 14:26:09 -07:00
}
2025-07-03 11:26:23 -04:00
} // namespace ZeroTier
2025-04-03 14:26:09 -07:00
#endif