NAWA 0.9
Web Application Framework for C++
nawa::utils Namespace Reference

Functions

void regexReplaceCallback (std::string &s, std::regex const &rgx, std::function< std::string(std::vector< std::string > const &)> const &fmt)
 
std::string hexDump (std::string const &in)
 
std::string toLowercase (std::string s)
 
std::string toUppercase (std::string s)
 
std::string generateErrorPage (unsigned int httpStatus)
 
std::string getFileExtension (std::string const &filename)
 
std::string contentTypeByExtension (std::string extension)
 
std::string makeHttpTime (time_t time)
 
time_t readHttpTime (std::string const &httpTime)
 
std::string makeSmtpTime (time_t time)
 
time_t readSmtpTime (std::string const &smtpTime)
 
std::vector< std::string > splitString (std::string str, char delimiter, bool ignoreEmpty=false)
 
std::string mergePath (std::vector< std::string > const &path)
 
std::vector< std::string > splitPath (std::string const &pathString)
 
std::string convertLineEndings (std::string const &in, std::string const &ending)
 
std::string getFileContents (std::string const &path)
 
std::string stringReplace (std::string input, std::unordered_map< char, char > const &patterns)
 
std::string stringReplace (std::string input, std::unordered_map< std::string, std::string > const &patterns)
 
std::unordered_multimap< std::string, std::string > splitQueryString (std::string const &queryString)
 
std::unordered_map< std::string, std::string > parseHeaders (std::string rawHeaders)
 
std::unordered_multimap< std::string, std::string > parseCookies (std::string const &rawCookies)
 
template<typename KeyType , typename ValueType , template< typename, typename, typename... > class MapType, typename... Args>
std::unordered_multimap< KeyType, ValueType > toUnorderedMultimap (MapType< KeyType, ValueType, Args... > inputMap)
 

Function Documentation

◆ regexReplaceCallback()

void nawa::utils::regexReplaceCallback ( std::string &  s,
std::regex const &  rgx,
std::function< std::string(std::vector< std::string > const &)> const &  fmt 
)

ASCII (byte-)string based regex_replace variant which takes a callback function as format parameter. This function may be used to generate individual replacements for each match.

Parameters
sString in which the replacements should take place.
rgxstd::regex object containing the regular expression to use for replacement.
fmtCallback function taking a reference to a string vector as a parameter and returning the replacement as a string. The vector contains the full match at index 0 and, if capturing groups are used in the expression, the submatches at the following indexes (capturing group 1 at index 1, ...)

◆ hexDump()

std::string nawa::utils::hexDump ( std::string const &  in)

Apply hex encoding to every byte in a string of bytes.

Parameters
inByte string that should be converted.
Returns
String containing the hexadecimal representation of every byte (thus, twice as long as in).

Definition at line 247 of file utils.cpp.

◆ toLowercase()

std::string nawa::utils::toLowercase ( std::string  s)

Convert every ASCII letter in the given string to lowercase.

Parameters
sString to convert.
Returns
Lowercase string.

Definition at line 256 of file utils.cpp.

◆ toUppercase()

std::string nawa::utils::toUppercase ( std::string  s)

Convert every ASCII letter in the given string to uppercase.

Parameters
sString to convert.
Returns
Uppercase string.

Definition at line 261 of file utils.cpp.

◆ generateErrorPage()

std::string nawa::utils::generateErrorPage ( unsigned int  httpStatus)

Generate a very basic HTML error page for a given HTTP status.

Parameters
httpStatusThe HTTP status. Currently supported: 400, 401, 403, 404, 405, 406, 408, 409, 410, 415, 418, 429, 451, 500, 501, 503.
Returns
An HTML document containing a simple error page. If the given status code is unknown, it will be titled "Unknown error".

Definition at line 266 of file utils.cpp.

◆ getFileExtension()

std::string nawa::utils::getFileExtension ( std::string const &  filename)

Get the extension (that is, everything after the last '.') out of a filename. Swallows out_of_range exceptions.

Parameters
filenameThe filename.
Returns
String containing the extension, or an empty string if no extension has been found.

Definition at line 343 of file utils.cpp.

◆ contentTypeByExtension()

std::string nawa::utils::contentTypeByExtension ( std::string  extension)

Get the MIME content type string for a particular file extension. This function checks the extension against a limited, incomplete set of file types (case-insensitive). If no match is found, "application/octet-stream" will be returned.

Parameters
extensionThe file extension (without '.').
Returns
A MIME content type string.

Definition at line 351 of file utils.cpp.

◆ makeHttpTime()

std::string nawa::utils::makeHttpTime ( time_t  time)

Convert a time_t value (UNIX timestamp) to a HTTP header compatible date/time string. May throw an Exception with error code 1 if interpretation of the UNIX timestamp fails.

Parameters
timeUNIX timestamp as a time_t value.
Returns
Time string in the format "<day-name(3)>, <day(2)> <month(3)> <year(4)> <hour(2)>:<minute(2)>:<second(2)> GMT".

Definition at line 359 of file utils.cpp.

◆ readHttpTime()

time_t nawa::utils::readHttpTime ( std::string const &  httpTime)

Create a time_t value (UNIX timestamp) from a HTTP header date/time string. May throw an Exception with error code 1 if parsing fails.

Parameters
httpTimeTime string in the format: "<day-name(3)>, <day(2)> <month(3)> <year(4)> <hour(2)>:<minute(2)>:<second(2)> GMT".
Returns
UNIX timestamp value (time_t).

Definition at line 373 of file utils.cpp.

◆ makeSmtpTime()

std::string nawa::utils::makeSmtpTime ( time_t  time)

Convert a time_t value (UNIX timestamp) to a SMTP header compatible date/time string. May throw an Exception with error code 1 if interpretation of the UNIX timestamp fails.

Parameters
timeUNIX timestamp as a time_t value.
Returns
Time string in the format "<day-name(3)>, <day(1*2)> <month(3)> <year(4)> <hour(2)>:<minute(2)>:<second(2)> <tzoffset(5)>".

Definition at line 392 of file utils.cpp.

◆ readSmtpTime()

time_t nawa::utils::readSmtpTime ( std::string const &  smtpTime)

Create a time_t value (UNIX timestamp) from a SMTP header date/time string. May throw an Exception with error code 1 if parsing fails.

Parameters
smtpTimeTime string in the format: "<day-name(3)>, <day(1*2)> <month(3)> <year(4)> <hour(2)>:<minute(2)>:<second(2)> <tzoffset(5)>".
Returns
UNIX timestamp value (time_t).

Definition at line 405 of file utils.cpp.

◆ splitString()

std::vector< std::string > nawa::utils::splitString ( std::string  str,
char  delimiter,
bool  ignoreEmpty = false 
)

Split a string using a character as a delimiter.

Parameters
strString to split.
delimiterDelimiter.
ignoreEmptyIgnore empty tokens, i.e., do not add them to the vector.
Returns
Vector containing the elements.

Definition at line 448 of file utils.cpp.

◆ mergePath()

std::string nawa::utils::mergePath ( std::vector< std::string > const &  path)

Convert a vector representation of a path ({"dir1", "dir2"}) to a string representation ("/dir1/dir2"). An empty vector will result in the string representation "/".

Parameters
pathVector representation of a path.
Returns
String representation of the given path.

Definition at line 465 of file utils.cpp.

◆ splitPath()

std::vector< std::string > nawa::utils::splitPath ( std::string const &  pathString)

Convert a string representation of a path ("/dir1/dir2") to a vector representation ({"dir1", "dir2"}). Query strings (starting with '?') are being ignored and will not be part of the returned vector.

Parameters
pathStringSting representation of the path.
Returns
Vector representation of the given path.

Definition at line 476 of file utils.cpp.

◆ convertLineEndings()

std::string nawa::utils::convertLineEndings ( std::string const &  in,
std::string const &  ending 
)

Convert line endings in a string between LF (Unix) and CRLF (Windows/Email).

Parameters
inInput string with CRLF or LF line endings
endingThe new line ending (e.g., "\\n" or "\\r\\n")
Returns
String with converted line endings.

Definition at line 482 of file utils.cpp.

◆ getFileContents()

std::string nawa::utils::getFileContents ( std::string const &  path)

Simple function to read a whole file into a string. Will throw a nawa::Exception with error code 1 if the file does not exist.

Parameters
pathPath and filename.
Returns
String containing the whole file.

Definition at line 493 of file utils.cpp.

◆ stringReplace() [1/2]

std::string nawa::utils::stringReplace ( std::string  input,
std::unordered_map< char, char > const &  patterns 
)

Replace all occurrences of pattern keys by the corresponding pattern values in the input string.

Parameters
inputString in which the patterns shall be replaced.
patternsMap of key characters (to be replaced in the input string) and value characters (the replacements).
Returns
String with replacements applied.

Definition at line 514 of file utils.cpp.

◆ stringReplace() [2/2]

std::string nawa::utils::stringReplace ( std::string  input,
std::unordered_map< std::string, std::string > const &  patterns 
)

Replace all occurrences of pattern keys by the corresponding pattern values in the input string.

Parameters
inputString in which the patterns shall be replaced.
patternsMap of key strings (to be replaced in the input string) and value strings (the replacements).
Returns
String with replacements applied.

Definition at line 521 of file utils.cpp.

◆ splitQueryString()

std::unordered_multimap< std::string, std::string > nawa::utils::splitQueryString ( std::string const &  queryString)

Generate a GET key => value multimap out of a query string (the ?k1=v1&k2=v2... part of a URL).

Parameters
queryStringQuery string or URL containing a query string.
Returns
Key => value map containing the GET variables.

Definition at line 531 of file utils.cpp.

◆ parseHeaders()

std::unordered_map< std::string, std::string > nawa::utils::parseHeaders ( std::string  rawHeaders)

Parse a block of headers into a map.

Parameters
rawHeadersThe raw block of headers.
Returns
Map with key => value mapping (keys will be transformed to lowercase).

Definition at line 550 of file utils.cpp.

◆ parseCookies()

std::unordered_multimap< std::string, std::string > nawa::utils::parseCookies ( std::string const &  rawCookies)

Parse cookies sent by the browser.

Parameters
rawCookiesThe content of the "Cookie" header.
Returns
A multimap of the cookies.

Definition at line 569 of file utils.cpp.

◆ toUnorderedMultimap()

template<typename KeyType , typename ValueType , template< typename, typename, typename... > class MapType, typename... Args>
std::unordered_multimap< KeyType, ValueType > nawa::utils::toUnorderedMultimap ( MapType< KeyType, ValueType, Args... >  inputMap)

Convert any iterable map to an unordered_multimap.

Template Parameters
KeyTypeKey type (automatically deduced).
ValueTypeValue type (automatically deduced).
MapTypeInput map type (automatically deduced).
ArgsFurther template arguments of map type (automatically deduced).
Parameters
inputMapThe input map to convert.
Returns
An unordered_multimap with the input map's content.

Definition at line 214 of file utils.h.