NAWA 0.9
Web Application Framework for C++
utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24#ifndef NAWA_UTILS_H
25#define NAWA_UTILS_H
26
27#include <regex>
28#include <string>
29#include <unordered_map>
30
31namespace nawa::utils {
41 void regexReplaceCallback(std::string& s, std::regex const& rgx,
42 std::function<std::string(std::vector<std::string> const&)> const& fmt);
43
49 std::string hexDump(std::string const& in);
50
56 std::string toLowercase(std::string s);
57
63 std::string toUppercase(std::string s);
64
72 std::string generateErrorPage(unsigned int httpStatus);
73
79 std::string getFileExtension(std::string const& filename);
80
88 std::string contentTypeByExtension(std::string extension);
89
97 std::string makeHttpTime(time_t time);
98
106 time_t readHttpTime(std::string const& httpTime);
107
115 std::string makeSmtpTime(time_t time);
116
124 time_t readSmtpTime(std::string const& smtpTime);
125
133 std::vector<std::string> splitString(std::string str, char delimiter, bool ignoreEmpty = false);
134
141 std::string mergePath(std::vector<std::string> const& path);
142
149 std::vector<std::string> splitPath(std::string const& pathString);
150
157 std::string convertLineEndings(std::string const& in, std::string const& ending);
158
165 std::string getFileContents(std::string const& path);
166
173 std::string stringReplace(std::string input, std::unordered_map<char, char> const& patterns);
174
181 std::string stringReplace(std::string input, std::unordered_map<std::string, std::string> const& patterns);
182
188 std::unordered_multimap<std::string, std::string> splitQueryString(std::string const& queryString);
189
195 std::unordered_map<std::string, std::string> parseHeaders(std::string rawHeaders);
196
202 std::unordered_multimap<std::string, std::string> parseCookies(std::string const& rawCookies);
203
213 template<typename KeyType, typename ValueType, template<typename, typename, typename...> class MapType, typename... Args>
214 std::unordered_multimap<KeyType, ValueType> toUnorderedMultimap(MapType<KeyType, ValueType, Args...> inputMap) {
215 std::unordered_multimap<KeyType, ValueType> ret;
216 for (auto const& [k, v] : inputMap) {
217 ret.insert({k, v});
218 }
219 return ret;
220 }
221}// namespace nawa::utils
222
223#endif//NAWA_UTILS_H
std::string toLowercase(std::string s)
Definition: utils.cpp:256
std::vector< std::string > splitPath(std::string const &pathString)
Definition: utils.cpp:476
std::string mergePath(std::vector< std::string > const &path)
Definition: utils.cpp:465
void regexReplaceCallback(std::string &s, std::regex const &rgx, std::function< std::string(std::vector< std::string > const &)> const &fmt)
std::unordered_map< std::string, std::string > parseHeaders(std::string rawHeaders)
Definition: utils.cpp:550
std::string hexDump(std::string const &in)
Definition: utils.cpp:247
std::string getFileExtension(std::string const &filename)
Definition: utils.cpp:343
std::string convertLineEndings(std::string const &in, std::string const &ending)
Definition: utils.cpp:482
std::string generateErrorPage(unsigned int httpStatus)
Definition: utils.cpp:266
std::unordered_multimap< KeyType, ValueType > toUnorderedMultimap(MapType< KeyType, ValueType, Args... > inputMap)
Definition: utils.h:214
std::string makeHttpTime(time_t time)
Definition: utils.cpp:359
time_t readSmtpTime(std::string const &smtpTime)
Definition: utils.cpp:405
time_t readHttpTime(std::string const &httpTime)
Definition: utils.cpp:373
std::string contentTypeByExtension(std::string extension)
Definition: utils.cpp:351
std::string stringReplace(std::string input, std::unordered_map< char, char > const &patterns)
Definition: utils.cpp:514
std::unordered_multimap< std::string, std::string > splitQueryString(std::string const &queryString)
Definition: utils.cpp:531
std::string getFileContents(std::string const &path)
Definition: utils.cpp:493
std::vector< std::string > splitString(std::string str, char delimiter, bool ignoreEmpty=false)
Definition: utils.cpp:448
std::string makeSmtpTime(time_t time)
Definition: utils.cpp:392
std::string toUppercase(std::string s)
Definition: utils.cpp:261
std::unordered_multimap< std::string, std::string > parseCookies(std::string const &rawCookies)
Definition: utils.cpp:569