drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
Utilities.h File Reference
#include <drogon/exports.h>
#include <trantor/utils/Date.h>
#include <trantor/utils/Funcs.h>
#include <trantor/utils/Utilities.h>
#include <trantor/utils/LogStream.h>
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <limits>
#include <sstream>
#include <algorithm>
#include <filesystem>
#include <string_view>
#include <unordered_map>
#include <type_traits>
Include dependency graph for Utilities.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  drogon::internal::CanConvertFromStringStream< T, typename >
struct  drogon::internal::CanConvertFromStringStream< T, std::void_t< decltype(std::declval< std::stringstream & >() > > std::declval< T & >())> >
struct  drogon::internal::CanConstructFromString< T >
struct  drogon::internal::CanConvertFromString< T >
struct  drogon::utils::internal::SafeStringHash

Namespaces

namespace  drogon
 Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn't handle async programs well. Drogon Test's syntax is inspired by both Google Test and Catch2.

Typedefs

template<typename T>
using drogon::SafeStringMap

Functions

DROGON_EXPORT const std::string_view & drogon::statusCodeToString (int code)
 Get the HTTP messages corresponding to the HTTP status codes.
DROGON_EXPORT bool drogon::utils::isInteger (std::string_view str)
 Determine if the string is an integer.
DROGON_EXPORT bool drogon::utils::isBase64 (std::string_view str)
 Determine if the string is base64 encoded.
DROGON_EXPORT std::string drogon::utils::genRandomString (int length)
 Generate random a string.
DROGON_EXPORT std::string drogon::utils::binaryStringToHex (const unsigned char *ptr, size_t length, bool lowerCase=false)
 Convert a binary string to hex format.
DROGON_EXPORT std::string drogon::utils::hexToBinaryString (const char *ptr, size_t length)
 Get a binary string from hexadecimal format.
DROGON_EXPORT std::vector< char > drogon::utils::hexToBinaryVector (const char *ptr, size_t length)
 Get a binary vector from hexadecimal format.
DROGON_EXPORT void drogon::utils::binaryStringToHex (const char *ptr, size_t length, char *out, bool lowerCase=false)
std::vector< std::string > drogon::utils::splitString (const std::string &str, const std::string &separator, bool acceptEmptyString=false)
 Split the string into multiple separated strings.
DROGON_EXPORT std::set< std::string > drogon::utils::splitStringToSet (const std::string &str, const std::string &separator)
bool drogon::utils::ci_equals (std::string_view str1, std::string_view str2)
 Compare two string_views for equality, ignoring case.
std::string_view & drogon::utils::trim_inplace (std::string_view &str)
std::string_view drogon::utils::trim (std::string_view str)
 Trim leading and trailing spaces and tabs from a string_view.
std::string drogon::utils::trim (std::string &&str)
 Trim leading and trailing spaces and tabs from a rvalue string.
std::vector< std::string_view > drogon::utils::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.
std::set< std::string_view > drogon::utils::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.
std::string drogon::utils::joinStringViews (const std::vector< std::string_view > &strs, std::string_view separator)
 Join a vector of string_view into a string.
std::string drogon::utils::joinStringViews (const std::set< std::string_view > &strs, std::string_view separator)
 Join a set of string_view into a string.
DROGON_EXPORT std::string drogon::utils::getUuid (bool lowercase=true)
 Get UUID string.
constexpr size_t drogon::utils::base64EncodedLength (size_t in_len, bool padded=true)
 Get the encoded length of base64.
DROGON_EXPORT void drogon::utils::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 drogon::utils::base64Encode (const unsigned char *bytesToEncode, size_t inLen, bool urlSafe=false, bool padded=true)
 Encode the string to base64 format.
std::string drogon::utils::base64Encode (std::string_view data, bool urlSafe=false, bool padded=true)
 Encode the string to base64 format.
void drogon::utils::base64EncodeUnpadded (const unsigned char *bytesToEncode, size_t inLen, unsigned char *outputBuffer, bool urlSafe=false)
 Encode the string to base64 format with no padding.
std::string drogon::utils::base64EncodeUnpadded (const unsigned char *bytesToEncode, size_t inLen, bool urlSafe=false)
 Encode the string to base64 format with no padding.
std::string drogon::utils::base64EncodeUnpadded (std::string_view data, bool urlSafe=false)
 Encode the string to base64 format with no padding.
constexpr size_t drogon::utils::base64DecodedLength (size_t inLen)
 Get the decoded length of base64.
DROGON_EXPORT size_t drogon::utils::base64Decode (const char *encodedString, size_t inLen, unsigned char *outputBuffer)
std::string drogon::utils::base64Decode (std::string_view encodedString)
 Decode the base64 format string.
DROGON_EXPORT std::vector< char > drogon::utils::base64DecodeToVector (std::string_view encodedString)
DROGON_EXPORT bool drogon::utils::needUrlDecoding (const char *begin, const char *end)
 Check if the string need decoding.
DROGON_EXPORT std::string drogon::utils::urlDecode (const char *begin, const char *end)
 Decode from or encode to the URL format string.
std::string drogon::utils::urlDecode (const std::string &szToDecode)
std::string drogon::utils::urlDecode (const std::string_view &szToDecode)
DROGON_EXPORT std::string drogon::utils::urlEncode (const std::string &)
DROGON_EXPORT std::string drogon::utils::urlEncodeComponent (const std::string &)
DROGON_EXPORT std::string drogon::utils::getMd5 (const char *data, const size_t dataLen)
 Get the MD5 digest of a string.
std::string drogon::utils::getMd5 (const std::string &originalString)
DROGON_EXPORT std::string drogon::utils::getSha1 (const char *data, const size_t dataLen)
std::string drogon::utils::getSha1 (const std::string &originalString)
DROGON_EXPORT std::string drogon::utils::getSha256 (const char *data, const size_t dataLen)
std::string drogon::utils::getSha256 (const std::string &originalString)
DROGON_EXPORT std::string drogon::utils::getSha3 (const char *data, const size_t dataLen)
std::string drogon::utils::getSha3 (const std::string &originalString)
DROGON_EXPORT std::string drogon::utils::getBlake2b (const char *data, const size_t dataLen)
std::string drogon::utils::getBlake2b (const std::string &originalString)
DROGON_EXPORT std::string drogon::utils::gzipCompress (const char *data, const size_t ndata)
 Compress or decompress data using gzip lib.
DROGON_EXPORT std::string drogon::utils::gzipDecompress (const char *data, const size_t ndata)
DROGON_EXPORT std::string drogon::utils::brotliCompress (const char *data, const size_t ndata)
 Compress or decompress data using brotli lib.
DROGON_EXPORT std::string drogon::utils::brotliDecompress (const char *data, const size_t ndata)
DROGON_EXPORT char * drogon::utils::getHttpFullDate (const trantor::Date &date=trantor::Date::now())
 Get the http full date string.
DROGON_EXPORT const std::string & drogon::utils::getHttpFullDateStr (const trantor::Date &date=trantor::Date::now())
DROGON_EXPORT void drogon::utils::dateToCustomFormattedString (const std::string &fmtStr, std::string &str, const trantor::Date &date)
DROGON_EXPORT trantor::Date drogon::utils::getHttpDate (const std::string &httpFullDateString)
 Get the trantor::Date object according to the http full date string.
DROGON_EXPORT std::string drogon::utils::formattedString (const char *format,...)
 Get a formatted string.
DROGON_EXPORT int drogon::utils::createPath (const std::string &path)
 Recursively create a file system path.
std::string drogon::utils::fromWidePath (const std::wstring &strPath)
std::wstring drogon::utils::toWidePath (const std::string &strUtf8Path)
const std::string & drogon::utils::toNativePath (const std::string &strPath)
 Convert a generic (UTF-8) path with to an OS native path.
std::string drogon::utils::toNativePath (const std::wstring &strPath)
const std::string & drogon::utils::fromNativePath (const std::string &strPath)
 Convert a OS native path (wide string on Windows) to a generic UTF-8 path.
std::string drogon::utils::fromNativePath (const std::wstring &strPath)
DROGON_EXPORT void drogon::utils::replaceAll (std::string &s, const std::string &from, const std::string &to)
 Replace all occurrences of from to to inplace.
DROGON_EXPORT bool drogon::utils::secureRandomBytes (void *ptr, size_t size)
 Generates cryptographically secure random bytes.
DROGON_EXPORT std::string drogon::utils::secureRandomString (size_t size)
 Generates cryptographically secure random string.
template<typename T>
drogon::utils::fromString (const std::string &p) noexcept(false)
template<>
std::string drogon::utils::fromString< std::string > (const std::string &p) noexcept(false)
template<>
bool drogon::utils::fromString< bool > (const std::string &p) noexcept(false)
DROGON_EXPORT bool drogon::utils::supportsTls () noexcept
LogStream & trantor::operator<< (LogStream &ls, const std::string_view &v)
LogStream & trantor::operator<< (LogStream &ls, const std::filesystem::path &p)

Variables

DROGON_EXPORT const size_t drogon::utils::internal::fixedRandomNumber

Detailed Description

Author
An Tao

Copyright 2018, An Tao. All rights reserved. https://github.com/an-tao/drogon Use of this source code is governed by a MIT license that can be found in the License file.

Drogon

Function Documentation

◆ base64Decode()

DROGON_EXPORT size_t drogon::utils::base64Decode ( const char * encodedString,
size_t inLen,
unsigned char * outputBuffer )

Decode the base64 format string. Return the number of bytes written.

◆ brotliCompress()

DROGON_EXPORT std::string drogon::utils::brotliCompress ( const char * data,
const size_t ndata )

Compress or decompress data using brotli lib.

Parameters
datathe input data
ndatathe input data length

◆ ci_equals()

bool drogon::utils::ci_equals ( std::string_view str1,
std::string_view str2 )
inline

Compare two string_views for equality, ignoring case.

Warning
This is locale dependent
Parameters
[in]str1The first string_view.
[in]str2The second string_view.
Returns
true if the string_views are equal, ignoring case; false otherwise.

◆ createPath()

DROGON_EXPORT int drogon::utils::createPath ( const std::string & path)

Recursively create a file system path.

Return 0 or -1 on success or failure.

◆ fromNativePath()

const std::string & drogon::utils::fromNativePath ( const std::string & strPath)
inline

Convert a OS native path (wide string on Windows) to a generic UTF-8 path.

This is a helper, mainly for Windows and multi-platform projects.

On Windows, backslash directory separators are converted to slash, and a a UTF-8 string is returned, suitable for libraries that supports UTF-8 paths like OpenSSL or drogon.

On other OSes, returns an UTF-8 string without altering the directory separators (backslashes are NOT replaced with slashes, since they are valid characters for directory/file names).

Parameters
strPathWide string or UTF-8 path.
Returns
A generic path.

◆ fromWidePath()

std::string drogon::utils::fromWidePath ( const std::wstring & strPath)
inline

Convert a wide string path with arbitrary directory separators to a UTF-8 portable path for use with trantor.

This is a helper, mainly for Windows and multi-platform projects.

Note
On Windows, backslash directory separators are converted to slash to keep portable paths.
Remarks
On other OSes, backslashes are not converted to slash, since they are valid characters for directory/file names.
Parameters
strPathWide string path.
Returns
std::string UTF-8 path, with slash directory separator.

◆ genRandomString()

DROGON_EXPORT std::string drogon::utils::genRandomString ( int length)

Generate random a string.

Parameters
lengthThe string length The returned string consists of uppercase and lowercase letters and numbers

◆ getHttpDate()

DROGON_EXPORT trantor::Date drogon::utils::getHttpDate ( const std::string & httpFullDateString)

Get the trantor::Date object according to the http full date string.

Returns trantor::Date(std::numeric_limits<int64_t>::max()) upon failure.

◆ getHttpFullDate()

DROGON_EXPORT char * drogon::utils::getHttpFullDate ( const trantor::Date & date = trantor::Date::now())

Get the http full date string.

rfc2616-3.3.1 Full Date format(RFC 822) like this:

Sun, 06 Nov 1994 08:49:37 GMT
Wed, 12 Sep 2018 09:22:40 GMT

◆ gzipCompress()

DROGON_EXPORT std::string drogon::utils::gzipCompress ( const char * data,
const size_t ndata )

Compress or decompress data using gzip lib.

Parameters
datathe input data
ndatathe input data length

◆ joinStringViews() [1/2]

std::string drogon::utils::joinStringViews ( const std::set< std::string_view > & strs,
std::string_view separator )
inline

Join a set of string_view into a string.

Parameters
[in]strsThe set of string_views to join.
[in]separatorThe separator to use between string_views.
Returns
A single string obtained by joining the input string_views with the specified separator.
Note
Empty values are skipped.

◆ joinStringViews() [2/2]

std::string drogon::utils::joinStringViews ( const std::vector< std::string_view > & strs,
std::string_view separator )
inline

Join a vector of string_view into a string.

Parameters
[in]strsThe vector of string_views to join.
[in]separatorThe separator to use between string_views.
Returns
A single string obtained by joining the input string_views with the specified separator.
Note
Empty values are skipped.

◆ replaceAll()

DROGON_EXPORT void drogon::utils::replaceAll ( std::string & s,
const std::string & from,
const std::string & to )

Replace all occurrences of from to to inplace.

Parameters
sstring to alter
fromstring to replace
tostring to replace with

◆ secureRandomBytes()

DROGON_EXPORT bool drogon::utils::secureRandomBytes ( void * ptr,
size_t size )

Generates cryptographically secure random bytes.

Parameters
ptrthe pointer which the random bytes are stored to
sizenumber of bytes to generate
Returns
true if generation is successful. False otherwise

◆ secureRandomString()

DROGON_EXPORT std::string drogon::utils::secureRandomString ( size_t size)

Generates cryptographically secure random string.

Parameters
sizenumber of characters to generate
Returns
the random string

◆ splitString()

std::vector< std::string > drogon::utils::splitString ( const std::string & str,
const std::string & separator,
bool acceptEmptyString = false )
inline

Split the string into multiple separated strings.

Parameters
strstring to split
separatorelement separator
acceptEmptyStringif true, empty strings are accepted in the result, for example, splitting the ",1,2,,3," by "," produces ["","1","2","","3",""]

◆ splitStringView()

std::vector< std::string_view > drogon::utils::splitStringView ( std::string_view str,
std::string_view separator,
bool trimValues = true,
bool acceptEmptyString = false )
inline

Split a string_view into a vector of string_views.

Parameters
[in]strThe string_view to split.
[in]separatorThe separator to use for splitting.
[in]trimValuesWhether to trim whitespace from the resulting string_views.
[in]acceptEmptyStringWhether to include empty strings in the result.
Returns
A vector of string_views obtained by splitting the input string_view.

◆ splitStringViewToSet()

std::set< std::string_view > drogon::utils::splitStringViewToSet ( std::string_view str,
std::string_view separator,
bool trimValues = true,
bool acceptEmptyString = false )
inline

Split a string_view into a set of string_views. \copyparams splitStringView.

Returns
A set of (unique) string_views obtained by splitting the input string_view.
Note
Uniqueness is case-sensitive: "A" and "a" are considered different values.

◆ toNativePath()

const std::string & drogon::utils::toNativePath ( const std::string & strPath)
inline

Convert a generic (UTF-8) path with to an OS native path.

This is a helper, mainly for Windows and multi-platform projects.

On Windows, slash directory separators are converted to backslash, and a wide string is returned.

On other OSes, returns an UTF-8 string without altering the directory separators.

Parameters
strPathWide string or UTF-8 path.
Returns
An OS path, suitable for use with the OS API.

◆ toWidePath()

std::wstring drogon::utils::toWidePath ( const std::string & strUtf8Path)
inline

Convert a UTF-8 path with arbitrary directory separator to a wide string path.

This is a helper, mainly for Windows and multi-platform projects.

Note
On Windows, slash directory separators are converted to backslash. Although it accepts both slash and backslash as directory separator in its API, it is better to stick to its standard.
Remarks
On other OSes, slashes are not converted to backslashes, since they are not interpreted as directory separators and are valid characters for directory/file names.
Parameters
strUtf8PathAscii path considered as being UTF-8.
Returns
std::wstring path with, on windows, standard backslash directory separator to stick to its standard.

◆ trim() [1/2]

std::string drogon::utils::trim ( std::string && str)
inline

Trim leading and trailing spaces and tabs from a rvalue string.

Parameters
[in]strThe string to trim.
Returns
The string with leading and trailing spaces and tabs removed.

◆ trim() [2/2]

std::string_view drogon::utils::trim ( std::string_view str)
inline

Trim leading and trailing spaces and tabs from a string_view.

Parameters
[in]strThe string_view to trim.
Returns
A string_view with leading and trailing spaces and tabs removed.

◆ trim_inplace()

std::string_view & drogon::utils::trim_inplace ( std::string_view & str)
inline

Trim leading and trailing spaces and tabs from a string_view, modifying it.

Parameters
[in,out]strThe string_view to trim.
Returns
The trimmed string_view.