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 
31 namespace 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
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:493
time_t readSmtpTime(std::string const &smtpTime)
Definition: utils.cpp:405
std::unordered_multimap< KeyType, ValueType > toUnorderedMultimap(MapType< KeyType, ValueType, Args... > inputMap)
Definition: utils.h:214
std::unordered_multimap< std::string, std::string > splitQueryString(std::string const &queryString)
Definition: utils.cpp:531
std::string convertLineEndings(std::string const &in, std::string const &ending)
Definition: utils.cpp:482
time_t readHttpTime(std::string const &httpTime)
Definition: utils.cpp:373
std::unordered_multimap< std::string, std::string > parseCookies(std::string const &rawCookies)
Definition: utils.cpp:569
std::string stringReplace(std::string input, std::unordered_map< char, char > const &patterns)
Definition: utils.cpp:514
std::string generateErrorPage(unsigned int httpStatus)
Definition: utils.cpp:266
std::vector< std::string > splitPath(std::string const &pathString)
Definition: utils.cpp:476
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:448
std::string makeSmtpTime(time_t time)
Definition: utils.cpp:392
std::string mergePath(std::vector< std::string > const &path)
Definition: utils.cpp:465
std::unordered_map< std::string, std::string > parseHeaders(std::string rawHeaders)
Definition: utils.cpp:550