17#include <drogon/exports.h>
18#include <trantor/utils/Date.h>
19#include <trantor/utils/Funcs.h>
20#include <trantor/utils/Utilities.h>
21#include <trantor/utils/LogStream.h>
31#include <unordered_map>
35DROGON_EXPORT
char *strptime(
const char *s,
const char *f,
struct tm *tm);
36DROGON_EXPORT time_t timegm(
struct tm *tm);
42template <
typename T,
typename =
void>
50 std::void_t<decltype(std::declval<std::stringstream &>() >>
51 std::declval<T &>())>> : std::true_type
82DROGON_EXPORT
bool isBase64(std::string_view str);
94 bool lowerCase =
false);
106 bool lowerCase =
false);
116inline std::vector<std::string>
splitString(
const std::string &str,
117 const std::string &separator,
118 bool acceptEmptyString =
false)
120 return trantor::splitString(str, separator, acceptEmptyString);
123DROGON_EXPORT std::set<std::string> splitStringToSet(
124 const std::string &str,
125 const std::string &separator);
133inline bool ci_equals(std::string_view str1, std::string_view str2)
135 if (str1.size() != str2.size())
137 return std::equal(str1.begin(),
140 [](
unsigned char a,
unsigned char b) {
141 return std::tolower(a) == std::tolower(b);
152 auto pos = str.find_first_not_of(
" \t");
154 str.remove_prefix((std::min)(pos, str.size()));
157 pos = str.find_last_not_of(
" \t");
158 str.remove_suffix(str.size() - pos - 1);
166inline std::string_view
trim(std::string_view str)
175inline std::string
trim(std::string &&str)
177 auto pos = str.find_last_not_of(
" \t");
178 if (pos == std::string::npos)
181 pos = str.find_first_not_of(
" \t");
197 std::string_view str,
198 std::string_view separator,
199 bool trimValues =
true,
200 bool acceptEmptyString =
false)
202 std::vector<std::string_view> result;
203 if (separator.empty())
207 if (acceptEmptyString || !str.empty())
208 result.push_back(str);
213 while ((end = str.find(separator, start)) != std::string_view::npos)
215 auto token = str.substr(start, end - start);
218 if (acceptEmptyString || !token.empty())
219 result.push_back(token);
220 start = end + separator.size();
222 auto token = str.substr(start);
225 if (acceptEmptyString || !token.empty())
227 result.push_back(token);
240 std::string_view str,
241 std::string_view separator,
242 bool trimValues =
true,
243 bool acceptEmptyString =
false)
245 auto v =
splitStringView(str, separator, trimValues, acceptEmptyString);
246 return std::set<std::string_view>(v.begin(), v.end());
257 std::string_view separator)
260 for (std::string_view str : strs)
265 result.append(separator);
279 std::string_view separator)
287DROGON_EXPORT std::string
getUuid(
bool lowercase =
true);
292 return padded ? ((in_len + 3 - 1) / 3) * 4 : (in_len * 8 + 6 - 1) / 6;
298 unsigned char *outputBuffer,
299 bool urlSafe =
false,
305 bool urlSafe =
false,
311 bytesToEncode, inLen, (
unsigned char *)ret.data(), urlSafe, padded);
317 bool urlSafe =
false,
329 unsigned char *outputBuffer,
330 bool urlSafe =
false)
332 base64Encode(bytesToEncode, inLen, outputBuffer, urlSafe,
false);
338 bool urlSafe =
false)
340 return base64Encode(bytesToEncode, inLen, urlSafe,
false);
345 bool urlSafe =
false)
353 return (inLen * 3) / 4;
360 unsigned char *outputBuffer);
365 auto inLen = encodedString.size();
369 base64Decode(encodedString.data(), inLen, (
unsigned char *)ret.data()));
373DROGON_EXPORT std::vector<char> base64DecodeToVector(
374 std::string_view encodedString);
380DROGON_EXPORT std::string
urlDecode(
const char *begin,
const char *end);
382inline std::string
urlDecode(
const std::string &szToDecode)
384 auto begin = szToDecode.data();
385 return urlDecode(begin, begin + szToDecode.length());
388inline std::string urlDecode(
const std::string_view &szToDecode)
390 auto begin = szToDecode.data();
391 return urlDecode(begin, begin + szToDecode.length());
394DROGON_EXPORT std::string urlEncode(
const std::string &);
395DROGON_EXPORT std::string urlEncodeComponent(
const std::string &);
398DROGON_EXPORT std::string
getMd5(
const char *data,
const size_t dataLen);
400inline std::string
getMd5(
const std::string &originalString)
402 return getMd5(originalString.data(), originalString.length());
405DROGON_EXPORT std::string getSha1(
const char *data,
const size_t dataLen);
407inline std::string getSha1(
const std::string &originalString)
409 return getSha1(originalString.data(), originalString.length());
412DROGON_EXPORT std::string getSha256(
const char *data,
const size_t dataLen);
414inline std::string getSha256(
const std::string &originalString)
416 return getSha256(originalString.data(), originalString.length());
419DROGON_EXPORT std::string getSha3(
const char *data,
const size_t dataLen);
421inline std::string getSha3(
const std::string &originalString)
423 return getSha3(originalString.data(), originalString.length());
426DROGON_EXPORT std::string getBlake2b(
const char *data,
const size_t dataLen);
428inline std::string getBlake2b(
const std::string &originalString)
430 return getBlake2b(originalString.data(), originalString.length());
438DROGON_EXPORT std::string
gzipCompress(
const char *data,
const size_t ndata);
439DROGON_EXPORT std::string gzipDecompress(
const char *data,
const size_t ndata);
447DROGON_EXPORT std::string brotliDecompress(
const char *data,
461 const trantor::Date &date = trantor::Date::now());
463DROGON_EXPORT
const std::string &getHttpFullDateStr(
464 const trantor::Date &date = trantor::Date::now());
466DROGON_EXPORT
void dateToCustomFormattedString(
const std::string &fmtStr,
468 const trantor::Date &date);
473DROGON_EXPORT trantor::Date
getHttpDate(
const std::string &httpFullDateString);
502 return trantor::utils::fromWidePath(strPath);
524inline std::wstring
toWidePath(
const std::string &strUtf8Path)
526 return trantor::utils::toWidePath(strUtf8Path);
543#if defined(_WIN32) && !defined(__MINGW32__)
544inline std::wstring toNativePath(
const std::string &strPath)
546 return trantor::utils::toNativePath(strPath);
549inline const std::wstring &toNativePath(
const std::wstring &strPath)
551 return trantor::utils::toNativePath(strPath);
556 return trantor::utils::toNativePath(strPath);
559inline std::string toNativePath(
const std::wstring &strPath)
561 return trantor::utils::toNativePath(strPath);
583 return trantor::utils::fromNativePath(strPath);
587inline std::string fromNativePath(
const std::wstring &strPath)
589 return trantor::utils::fromNativePath(strPath);
599 const std::string &from,
600 const std::string &to);
622T fromString(
const std::string &p)
noexcept(
false)
624 if constexpr (std::is_integral<T>::value && std::is_signed<T>::value)
627 auto v = std::stoll(p, &pos);
631 throw std::invalid_argument(
"Invalid value");
632 if ((v <
static_cast<long long>((std::numeric_limits<T>::min)())) ||
633 (v >
static_cast<long long>((std::numeric_limits<T>::max)())))
634 throw std::out_of_range(
"Value out of range");
635 return static_cast<T
>(v);
637 else if constexpr (std::is_integral<T>::value &&
638 (!std::is_signed<T>::value))
641 auto v = std::stoull(p, &pos);
645 throw std::invalid_argument(
"Invalid value");
647 static_cast<unsigned long long>((std::numeric_limits<T>::max)()))
648 throw std::out_of_range(
"Value out of range");
649 return static_cast<T
>(v);
651 else if constexpr (std::is_floating_point<T>::value)
654 auto v = std::stold(p, &pos);
658 throw std::invalid_argument(
"Invalid value");
660 static_cast<long double>((std::numeric_limits<T>::lowest)())) ||
661 (v >
static_cast<long double>((std::numeric_limits<T>::max)())))
662 throw std::out_of_range(
"Value out of range");
663 return static_cast<T
>(v);
665 else if constexpr (internal::CanConvertFromStringStream<T>::value)
670 std::stringstream ss(p);
674 ss.exceptions(std::ios_base::failbit);
679 throw std::runtime_error(
"Bad type conversion");
685 throw std::runtime_error(
"Bad type conversion");
690inline std::string fromString<std::string>(
const std::string &p)
noexcept(
false)
696inline bool fromString<bool>(
const std::string &p)
noexcept(
false)
698 if (!p.empty() && std::all_of(p.begin(), p.end(), [](
unsigned char c) {
699 return std::isdigit(c);
701 return (std::stoll(p) != 0);
703 std::transform(p.begin(), p.end(), l.begin(), [](
unsigned char c) {
704 return (char)tolower(c);
710 else if (l ==
"false")
714 throw std::runtime_error(
"Can't convert from string '" + p +
"' to bool");
717DROGON_EXPORT
bool supportsTls() noexcept;
721DROGON_EXPORT
extern const size_t fixedRandomNumber;
725 size_t operator()(
const std::string &str)
const
727 const size_t A = 6665339;
728 const size_t B = 2534641;
729 size_t h = fixedRandomNumber;
731 h = (h * A) ^ (ch * B);
740 std::unordered_map<std::string, T, utils::internal::SafeStringHash>;
745inline LogStream &operator<<(LogStream &ls,
const std::string_view &v)
748 ls.append(v.data(), v.length());
752inline LogStream &operator<<(LogStream &ls,
const std::filesystem::path &p)
754 return ls << p.string();
std::wstring toWidePath(const std::string &strUtf8Path)
Definition Utilities.h:524
DROGON_EXPORT std::string genRandomString(int length)
Generate random a string.
void base64EncodeUnpadded(const unsigned char *bytesToEncode, size_t inLen, unsigned char *outputBuffer, bool urlSafe=false)
Encode the string to base64 format with no padding.
Definition Utilities.h:327
DROGON_EXPORT bool isInteger(std::string_view str)
Determine if the string is an integer.
std::string_view & trim_inplace(std::string_view &str)
Definition Utilities.h:150
DROGON_EXPORT void base64Encode(const unsigned char *bytesToEncode, size_t inLen, unsigned char *outputBuffer, bool urlSafe=false, bool padded=true)
Encode the string to base64 format.
std::string joinStringViews(const std::vector< std::string_view > &strs, std::string_view separator)
Join a vector of string_view into a string.
Definition Utilities.h:256
const std::string & fromNativePath(const std::string &strPath)
Convert a OS native path (wide string on Windows) to a generic UTF-8 path.
Definition Utilities.h:581
DROGON_EXPORT trantor::Date getHttpDate(const std::string &httpFullDateString)
Get the trantor::Date object according to the http full date string.
DROGON_EXPORT std::string brotliCompress(const char *data, const size_t ndata)
Compress or decompress data using brotli lib.
bool ci_equals(std::string_view str1, std::string_view str2)
Compare two string_views for equality, ignoring case.
Definition Utilities.h:133
constexpr size_t base64EncodedLength(size_t in_len, bool padded=true)
Get the encoded length of base64.
Definition Utilities.h:290
DROGON_EXPORT std::string binaryStringToHex(const unsigned char *ptr, size_t length, bool lowerCase=false)
Convert a binary string to hex format.
std::vector< std::string_view > splitStringView(std::string_view str, std::string_view separator, bool trimValues=true, bool acceptEmptyString=false)
Split a string_view into a vector of string_views.
Definition Utilities.h:196
DROGON_EXPORT std::string gzipCompress(const char *data, const size_t ndata)
Compress or decompress data using gzip lib.
DROGON_EXPORT std::string formattedString(const char *format,...)
Get a formatted string.
std::set< std::string_view > splitStringViewToSet(std::string_view str, std::string_view separator, bool trimValues=true, bool acceptEmptyString=false)
Split a string_view into a set of string_views. \copyparams splitStringView.
Definition Utilities.h:239
DROGON_EXPORT bool secureRandomBytes(void *ptr, size_t size)
Generates cryptographically secure random bytes.
constexpr size_t base64DecodedLength(size_t inLen)
Get the decoded length of base64.
Definition Utilities.h:351
DROGON_EXPORT std::string getUuid(bool lowercase=true)
Get UUID string.
DROGON_EXPORT bool isBase64(std::string_view str)
Determine if the string is base64 encoded.
DROGON_EXPORT std::string hexToBinaryString(const char *ptr, size_t length)
Get a binary string from hexadecimal format.
DROGON_EXPORT size_t base64Decode(const char *encodedString, size_t inLen, unsigned char *outputBuffer)
DROGON_EXPORT bool needUrlDecoding(const char *begin, const char *end)
Check if the string need decoding.
DROGON_EXPORT std::vector< char > hexToBinaryVector(const char *ptr, size_t length)
Get a binary vector from hexadecimal format.
DROGON_EXPORT std::string urlDecode(const char *begin, const char *end)
Decode from or encode to the URL format string.
std::vector< std::string > splitString(const std::string &str, const std::string &separator, bool acceptEmptyString=false)
Split the string into multiple separated strings.
Definition Utilities.h:116
std::string_view trim(std::string_view str)
Trim leading and trailing spaces and tabs from a string_view.
Definition Utilities.h:166
DROGON_EXPORT char * getHttpFullDate(const trantor::Date &date=trantor::Date::now())
Get the http full date string.
DROGON_EXPORT void replaceAll(std::string &s, const std::string &from, const std::string &to)
Replace all occurrences of from to to inplace.
DROGON_EXPORT int createPath(const std::string &path)
Recursively create a file system path.
DROGON_EXPORT std::string secureRandomString(size_t size)
Generates cryptographically secure random string.
std::string fromWidePath(const std::wstring &strPath)
Definition Utilities.h:500
DROGON_EXPORT std::string getMd5(const char *data, const size_t dataLen)
Get the MD5 digest of a string.
const std::string & toNativePath(const std::string &strPath)
Convert a generic (UTF-8) path with to an OS native path.
Definition Utilities.h:554
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
DROGON_EXPORT const std::string_view & statusCodeToString(int code)
Get the HTTP messages corresponding to the HTTP status codes.
Definition Utilities.h:57
Definition Utilities.h:44
Definition Utilities.h:62
Definition Utilities.h:724