NAWA 0.8
Web Application Framework for C++
utils.h
Go to the documentation of this file.
1
6/*
7 * Copyright (C) 2019-2021 Tobias Flaig.
8 *
9 * This file is part of nawa.
10 *
11 * nawa is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License,
13 * version 3, as published by the Free Software Foundation.
14 *
15 * nawa is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22 */
23
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
96 std::string makeHttpTime(time_t time);
97
104 time_t readHttpTime(std::string const& httpTime);
105
112 std::string makeSmtpTime(time_t time);
113
120 time_t readSmtpTime(std::string const& smtpTime);
121
129 std::vector<std::string> splitString(std::string str, char delimiter, bool ignoreEmpty = false);
130
137 std::string mergePath(std::vector<std::string> const& path);
138
145 std::vector<std::string> splitPath(std::string const& pathString);
146
153 std::string convertLineEndings(std::string const& in, std::string const& ending);
154
161 std::string getFileContents(std::string const& path);
162
169 std::string stringReplace(std::string input, std::unordered_map<char, char> const& patterns);
170
177 std::string stringReplace(std::string input, std::unordered_map<std::string, std::string> const& patterns);
178
184 std::unordered_multimap<std::string, std::string> splitQueryString(std::string const& queryString);
185
191 std::unordered_map<std::string, std::string> parseHeaders(std::string rawHeaders);
192
198 std::unordered_multimap<std::string, std::string> parseCookies(std::string const& rawCookies);
199
209 template<typename KeyType, typename ValueType, template<typename, typename, typename...> class MapType, typename... Args>
210 std::unordered_multimap<KeyType, ValueType> toUnorderedMultimap(MapType<KeyType, ValueType, Args...> inputMap) {
211 std::unordered_multimap<KeyType, ValueType> ret;
212 for (auto const& [k, v] : inputMap) {
213 ret.insert({k, v});
214 }
215 return ret;
216 }
217}// namespace nawa::utils
218
219#endif//NAWA_UTILS_H
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)
Definition: utils.cpp:247
std::string getFileContents(std::string const &path)
Definition: utils.cpp:463
std::unordered_multimap< KeyType, ValueType > toUnorderedMultimap(MapType< KeyType, ValueType, Args... > inputMap)
Definition: utils.h:210
time_t readSmtpTime(std::string const &smtpTime)
Definition: utils.cpp:388
std::unordered_multimap< std::string, std::string > splitQueryString(std::string const &queryString)
Definition: utils.cpp:501
std::string convertLineEndings(std::string const &in, std::string const &ending)
Definition: utils.cpp:452
time_t readHttpTime(std::string const &httpTime)
Definition: utils.cpp:369
std::unordered_multimap< std::string, std::string > parseCookies(std::string const &rawCookies)
Definition: utils.cpp:539
std::string stringReplace(std::string input, std::unordered_map< char, char > const &patterns)
Definition: utils.cpp:484
std::string generateErrorPage(unsigned int httpStatus)
Definition: utils.cpp:266
std::vector< std::string > splitPath(std::string const &pathString)
Definition: utils.cpp:446
std::string toLowercase(std::string s)
Definition: utils.cpp:256
std::string contentTypeByExtension(std::string extension)
Definition: utils.cpp:351
std::string toUppercase(std::string s)
Definition: utils.cpp:261
std::string getFileExtension(std::string const &filename)
Definition: utils.cpp:343
std::string makeHttpTime(time_t time)
Definition: utils.cpp:359
std::vector< std::string > splitString(std::string str, char delimiter, bool ignoreEmpty=false)
Definition: utils.cpp:418
std::string makeSmtpTime(time_t time)
Definition: utils.cpp:378
std::string mergePath(std::vector< std::string > const &path)
Definition: utils.cpp:435
std::unordered_map< std::string, std::string > parseHeaders(std::string rawHeaders)
Definition: utils.cpp:520