Comparer les révisions

...

10 Révisions

10 fichiers modifiés avec 41 ajouts et 26 suppressions

Voir le fichier

@ -42,7 +42,12 @@ GTEST_OBJECTS=$(filter-out qswiki.o, $(WIKIOBJECTS))
.DEFAULT_GOAL := qswiki .DEFAULT_GOAL := qswiki
release: CXXFLAGS=$(RELEASE_CXXFLAGS) release: CXXFLAGS=$(RELEASE_CXXFLAGS)
profile: CXXFLAGS=$(RELEASE_CXXFLAGS) -pg
profile: LDFLAGS+= -pg
release: qswiki release: qswiki
profile: qswiki
qswiki: $(WIKIOBJECTS) qswiki: $(WIKIOBJECTS)
$(CXX) $(WIKIOBJECTS) ${LDFLAGS} ${INCLUDEFLAGS} -o qswiki $(CXX) $(WIKIOBJECTS) ${LDFLAGS} ${INCLUDEFLAGS} -o qswiki
@ -53,10 +58,10 @@ gtest: $(GTESTS_TESTDIR)/*.cpp $(GTEST_OBJECTS)
$(CXX) -o gtest $(GTESTS_TESTDIR)/*.cpp $(GTEST_OBJECTS) $(GTEST_CXXFLAGS) $(GTEST_DIR)/src/gtest_main.cc $(GTEST_DIR)/src/gtest-all.cc $(GTEST_LDFLAGS) $(CXX) -o gtest $(GTESTS_TESTDIR)/*.cpp $(GTEST_OBJECTS) $(GTEST_CXXFLAGS) $(GTEST_DIR)/src/gtest_main.cc $(GTEST_DIR)/src/gtest-all.cc $(GTEST_LDFLAGS)
%.o:%.cpp %.o:%.cpp
$(CXX) ${CXXFLAGS} ${LDFLAGS} ${INCLUDEFLAGS} -c -o $@ $< $(CXX) ${CXXFLAGS} ${INCLUDEFLAGS} -c -o $@ $<
version.o:version.cpp version.o:version.cpp
$(CXX) ${CXXFLAGS} ${LDFLAGS} ${INCLUDEFLAGS} -DGITCOMMIT=\"$(shell git rev-parse --short HEAD)\" -c -o $@ $< $(CXX) ${CXXFLAGS} ${INCLUDEFLAGS} -DGITCOMMIT=\"$(shell git rev-parse --short HEAD)\" -c -o $@ $<
clean: clean:
rm -f $(OBJECTS) $(DEPENDS) rm -f $(OBJECTS) $(DEPENDS)

Voir le fichier

@ -104,7 +104,17 @@ void CLIConsole::startInteractive()
auto pair = CLIHandler::splitCommand(input); auto pair = CLIHandler::splitCommand(input);
if(pair.first == "exit") if(pair.first == "exit")
{ {
std::cout << "Exiting CLI"; if(attached)
{
std::cout << "You are attached. Quit attached instance too (y) or only this one(n)" << std::endl;
char response;
std::cin >> response;
if(response == 'y')
{
this->send("exit");
}
}
std::cout << "Exiting CLI" << std::endl;
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if(pair.first == "attach") if(pair.first == "attach")

Voir le fichier

@ -48,7 +48,7 @@ std::optional<User> UserDaoSqlite::find(std::string username)
stmt >> std::tie(user.login, user.password, user.salt, perms, user.enabled); stmt >> std::tie(user.login, user.password, user.salt, perms, user.enabled);
user.permissions = Permissions{perms}; user.permissions = Permissions{perms};
return std::move(user); return user;
} }
catch(const sqlite::errors::no_rows &e) catch(const sqlite::errors::no_rows &e)
{ {
@ -71,7 +71,7 @@ std::optional<User> UserDaoSqlite::find(int id)
stmt >> std::tie(user.login, user.password, user.salt, perms, user.enabled); stmt >> std::tie(user.login, user.password, user.salt, perms, user.enabled);
user.permissions = Permissions{perms}; user.permissions = Permissions{perms};
return std::move(user); return user;
} }
catch(const sqlite::errors::no_rows &e) catch(const sqlite::errors::no_rows &e)
{ {

Voir le fichier

@ -41,7 +41,7 @@ Request HttpGateway::convertRequest(httplib::Request request)
// TODO: this eats resources, where perhaps it does not need to. move it to request? // TODO: this eats resources, where perhaps it does not need to. move it to request?
for(auto &it : request.params) for(auto &it : request.params)
{ {
it.second = utils::html_xss(std::string{it.second}); it.second = utils::html_xss(it.second);
} }
if(request.method == "GET") if(request.method == "GET")
{ {
@ -83,7 +83,8 @@ void HttpGateway::work(RequestWorker &worker)
{ {
httplib::Server server; httplib::Server server;
server.set_payload_max_length(this->maxPayloadLength); server.set_payload_max_length(this->maxPayloadLength);
auto handler = [&](const httplib::Request &req, httplib::Response &res) { auto handler = [&](const httplib::Request &req, httplib::Response &res)
{
Request wikiRequest = convertRequest(req); Request wikiRequest = convertRequest(req);
Logger::debug() << "httpgateway: received request " << wikiRequest; Logger::debug() << "httpgateway: received request " << wikiRequest;
Response wikiresponse = worker.processRequest(wikiRequest); Response wikiresponse = worker.processRequest(wikiRequest);

Voir le fichier

@ -32,12 +32,12 @@ Response::Response(int http_status_code, std::string html)
this->html = std::move(html); this->html = std::move(html);
} }
void Response::addHeader(const std::string &key, const std::string &value) void Response::addHeader(std::string key, std::string value)
{ {
this->responseHeaders.insert(std::make_pair(key, value)); this->responseHeaders.insert(std::make_pair(key, value));
} }
Response Response::redirectTemporarily(const std::string &url) Response Response::redirectTemporarily(std::string url)
{ {
Response result; Response result;
result.addHeader("Location", url); result.addHeader("Location", url);

Voir le fichier

@ -27,8 +27,8 @@ class Response
return this->html; return this->html;
} }
void addHeader(const std::string &key, const std::string &value); void addHeader(std::string key, std::string value);
static Response redirectTemporarily(const std::string &url); static Response redirectTemporarily(std::string url);
void setStatus(int status) void setStatus(int status)
{ {

Voir le fichier

@ -67,7 +67,7 @@ std::string Template::resolveIncludes(std::string_view content)
return replacer.parse(content); return replacer.parse(content);
} }
TemplatePage Template::createPage(std::string name) TemplatePage Template::createPage(std::string_view name)
{ {
std::string content = loadResolvedPart(name); std::string content = loadResolvedPart(name);
Varreplacer replacer(this->templateprefix); Varreplacer replacer(this->templateprefix);

Voir le fichier

@ -24,7 +24,7 @@ class Template
std::string getPartPath(std::string_view partname); std::string getPartPath(std::string_view partname);
std::string loadResolvedPart(std::string_view partname); std::string loadResolvedPart(std::string_view partname);
std::string loadPartContent(std::string_view partname); std::string loadPartContent(std::string_view partname);
TemplatePage createPage(std::string name); TemplatePage createPage(std::string_view name);
public: public:
Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls, Template(std::string templateprefix, std::string templatepath, ConfigUrls &configUrls,

Voir le fichier

@ -84,7 +84,7 @@ std::string utils::urldecode(std::string_view str)
return result; return result;
} }
std::vector<std::string> utils::split(const std::string &str, char delim) std::vector<std::string> utils::split(std::string str, char delim)
{ {
std::vector<std::string> result; std::vector<std::string> result;
std::stringstream stream(str); std::stringstream stream(str);
@ -97,7 +97,7 @@ std::vector<std::string> utils::split(const std::string &str, char delim)
} }
// TODO: can easily break if we pass a regex here // TODO: can easily break if we pass a regex here
std::vector<std::string> utils::split(const std::string &str, const std::string &delim) std::vector<std::string> utils::split(std::string str, const std::string &delim)
{ {
std::regex regex{delim + "+"}; std::regex regex{delim + "+"};
return split(str, regex); return split(str, regex);
@ -112,7 +112,7 @@ std::vector<std::string> utils::split(const std::string &str, std::regex &regex)
return result; return result;
} }
std::string utils::strreplace(const std::string &str, const std::string &search, const std::string &replace) std::string utils::strreplace(std::string str, const std::string &search, const std::string &replace)
{ {
std::string result = str; std::string result = str;
auto searchlength = search.length(); auto searchlength = search.length();
@ -182,10 +182,9 @@ std::string utils::toISODate(time_t t)
return std::string{result}; return std::string{result};
} }
std::string utils::trim(const std::string &str) std::string utils::trim(std::string_view view)
{ {
std::string_view chars = " \t\n\r"; std::string_view chars = " \t\n\r";
std::string_view view = str;
auto n = view.find_first_not_of(chars); auto n = view.find_first_not_of(chars);
if(n != std::string_view::npos) if(n != std::string_view::npos)
{ {

16
utils.h
Voir le fichier

@ -11,22 +11,22 @@
namespace utils namespace utils
{ {
std::vector<std::string> split(const std::string &str, char delim); std::vector<std::string> split(std::string str, char delim);
std::vector<std::string> split(const std::string &str, const std::string &delim); std::vector<std::string> split(std::string str, const std::string &delim);
std::vector<std::string> split(const std::string &str, std::regex &regex); std::vector<std::string> split(const std::string &str, std::regex &regex);
std::string urldecode(std::string_view str); std::string urldecode(std::string_view str);
std::string strreplace(const std::string &str, const std::string &search, const std::string &replace); std::string strreplace(std::string str, const std::string &search, const std::string &replace);
std::string html_xss(std::string_view str); std::string html_xss(std::string_view str);
std::string getenv(const std::string &key); std::string getenv(const std::string &key);
template <class T, class U> bool hasKey(const std::map<T, U> &map, T key) template <class T, class U> bool hasKey(const std::map<T, U> &map, const T &key)
{ {
auto k = map.find(key); auto k = map.find(key);
return k != map.end(); return k != map.end();
} }
template <class T, class U> U getKeyOrEmpty(const std::map<T, U> &map, T key) template <class T, class U> U getKeyOrEmpty(const std::map<T, U> &map, const T &key)
{ {
auto k = map.find(key); auto k = map.find(key);
if(k != map.end()) if(k != map.end())
@ -36,7 +36,7 @@ template <class T, class U> U getKeyOrEmpty(const std::map<T, U> &map, T key)
return U(); return U();
} }
template <class T, class U> U getKeyOrEmpty(std::multimap<T, U> map, T key) template <class T, class U> U getKeyOrEmpty(const std::multimap<T, U> &map, const T &key)
{ {
auto k = map.find(key); auto k = map.find(key);
if(k != map.end()) if(k != map.end())
@ -46,7 +46,7 @@ template <class T, class U> U getKeyOrEmpty(std::multimap<T, U> map, T key)
return U(); return U();
} }
template <class T, class U> std::vector<U> getAll(std::multimap<T, U> map, T key) template <class T, class U> std::vector<U> getAll(const std::multimap<T, U> &map, const T &key)
{ {
std::vector<U> result; std::vector<U> result;
auto range = map.equal_range(key); auto range = map.equal_range(key);
@ -93,7 +93,7 @@ template <class T> inline std::string toString(const T &v)
return std::string(v.begin(), v.end()); return std::string(v.begin(), v.end());
} }
std::string trim(const std::string &str); std::string trim(std::string_view view);
} // namespace utils } // namespace utils
#endif #endif