From 793ee5ecbe6142d5bc03853400e158e416a3e94a Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 16 Aug 2023 20:15:34 -0400 Subject: [PATCH] push --- CMakeLists.txt | 1 + crow_test/webcontent/home.html | 21 ----------------- crow_test/webcontent/index.html | 21 ++++++++++------- include/crowsite/site/auth.h | 4 +++- include/crowsite/utility.h | 2 ++ src/crowsite/requests/jellyfin.cpp | 4 ++-- src/crowsite/utility.cpp | 5 ++++ src/main.cpp | 38 ++++++++++++++++++++++++------ 8 files changed, 57 insertions(+), 39 deletions(-) delete mode 100644 crow_test/webcontent/home.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e490ab..135981a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ option(ENABLE_TSAN "Enable the thread data race sanitizer" OFF) set(CMAKE_CXX_STANDARD 20) set(CROW_FEATURES compression) +cmake_policy(SET CMP0057 NEW) find_package(Crow) find_package(CURL) find_package(OpenSSL) diff --git a/crow_test/webcontent/home.html b/crow_test/webcontent/home.html deleted file mode 100644 index 0a95363..0000000 --- a/crow_test/webcontent/home.html +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - {{$SITE_TITLE}} - - -
-
-
- - -
-
- HAXsdsad -
-
-
- - diff --git a/crow_test/webcontent/index.html b/crow_test/webcontent/index.html index 04d6416..0a95363 100644 --- a/crow_test/webcontent/index.html +++ b/crow_test/webcontent/index.html @@ -1,16 +1,21 @@ + + {{$SITE_TITLE}} -

Hello {{person}}!

-
- -

- -

- -
+
+
+
+ + +
+
+ HAXsdsad +
+
+
diff --git a/include/crowsite/site/auth.h b/include/crowsite/site/auth.h index a2677ac..5a6ebe8 100644 --- a/include/crowsite/site/auth.h +++ b/include/crowsite/site/auth.h @@ -5,9 +5,11 @@ #ifndef CROWSITE_AUTH_H #define CROWSITE_AUTH_H +#include "crowsite/utility.h" + namespace cs { - + void handleLoginPost(cs::parser::Post postData); } diff --git a/include/crowsite/utility.h b/include/crowsite/utility.h index 929f65d..fa9a745 100644 --- a/include/crowsite/utility.h +++ b/include/crowsite/utility.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace cs { @@ -18,6 +19,7 @@ namespace cs { public: explicit Post(const std::string& input); + bool hasKey(const std::string& key); const std::string& operator[](const std::string& key); std::string dump(); diff --git a/src/crowsite/requests/jellyfin.cpp b/src/crowsite/requests/jellyfin.cpp index 0c1cb47..a4981a2 100644 --- a/src/crowsite/requests/jellyfin.cpp +++ b/src/crowsite/requests/jellyfin.cpp @@ -30,8 +30,8 @@ namespace cs::jellyfin for (const auto& user : json) { - auto username = user["Name"].s(); - auto userid = user["Id"].s(); + auto username = std::string(user["Name"].s()); + auto userid = std::string(user["Id"].s()); //BLT_TRACE("Processing %s = %s", username.operator std::string().c_str(), userid.operator std::string().c_str()); GLOBALS.user_ids[username] = userid; } diff --git a/src/crowsite/utility.cpp b/src/crowsite/utility.cpp index b1f1768..5a700bb 100644 --- a/src/crowsite/utility.cpp +++ b/src/crowsite/utility.cpp @@ -32,6 +32,11 @@ namespace cs { } return out; } + + bool Post::hasKey(const std::string& key) + { + return m_Values.find(key) != m_Values.end(); + } } namespace fs { diff --git a/src/main.cpp b/src/main.cpp index 061fa84..f64d676 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include class BLT_CrowLogger : public crow::ILogHandler { @@ -17,7 +18,8 @@ class BLT_CrowLogger : public crow::ILogHandler void log(std::string message, crow::LogLevel crow_level) final { blt::logging::log_level blt_level; - switch (crow_level){ + switch (crow_level) + { case crow::LogLevel::DEBUG: blt_level = blt::logging::log_level::DEBUG; break; @@ -47,6 +49,7 @@ int main(int argc, const char** argv) cs::requests::init(); blt::arg_parse parser; + parser.addArgument(blt::arg_builder("--tests").setAction(blt::arg_action_t::STORE_TRUE).build()); parser.addArgument(blt::arg_builder("token").build()); parser.addArgument(blt::arg_builder("user").build()); parser.addArgument(blt::arg_builder("pass").build()); @@ -64,7 +67,7 @@ int main(int argc, const char** argv) BLT_INFO("Init Crow with compression and logging enabled!"); crow::SimpleApp app; app.use_compression(crow::compression::GZIP); - app.loglevel(crow::LogLevel::INFO); + app.loglevel(crow::LogLevel::WARNING); BLT_INFO("Creating static context"); @@ -103,13 +106,19 @@ int main(int argc, const char** argv) // for (const auto& v : req.url_params.keys()) // BLT_TRACE("URL: %s = %s", v.c_str(), req.url_params.get(v)); if (name.ends_with(".html")) - return {engine.fetch(name)}; + { + crow::mustache::context ctx; + // we don't want to pass all get parameters to the context to prevent leaking + auto referer = req.url_params.get("referer"); + if (referer) + ctx["referer"] = referer; + auto page = crow::mustache::compile(engine.fetch(name)); + return page.render(ctx); + } crow::mustache::context ctx({{"person", name}}); auto user_page = crow::mustache::compile(engine.fetch("index.html")); - //BLT_TRACE(page); - return user_page.render(ctx); } ); @@ -118,13 +127,15 @@ int main(int argc, const char** argv) [](const crow::request& req) { cs::parser::Post pp(req.body); - return "Portabella Mushrooms! " + pp.dump(); + crow::response res(303); + res.set_header("Location", pp.hasKey("referer") ? pp["referer"] : "/"); + return res; } ); CROW_ROUTE(app, "/")( [&engine]() { - return engine.fetch("home.html"); + return engine.fetch("index.html"); } ); @@ -134,6 +145,19 @@ int main(int argc, const char** argv) } ); + app.tick( + std::chrono::seconds(1), [&app, &args]() -> void { + if (!args.contains("tests")) + return; + static uint64_t timer = 0; + const uint64_t wait = 10; + timer++; + if (timer > wait) + { + app.stop(); + } + } + ); app.port(8080).multithreaded().run(); cs::requests::cleanup();