From 90f8289bcd734e2e0201a0f1e4f64f4c6c214cf6 Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Sun, 25 Aug 2024 15:27:48 +0300 Subject: [PATCH] Hot fix: now bad cookies do not ruin everything --- .gitignore | 2 ++ building/main.cpp | 2 ++ .../http_structures/cookies.cpp | 33 ++++++++++++------- .../backend_logic/server_data_interact.h | 2 ++ .../when_internalapi_getchatinfo.cpp | 5 +++ .../backend_logic/when_list_rooms.cpp | 18 ++++++++++ src/web_chat/iu9_ca_web_chat_lib/run.cpp | 8 ++--- 7 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp create mode 100644 src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp diff --git a/.gitignore b/.gitignore index d8d4e0e..fb75514 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ local.sh iu9-ca-web-chat.db log/ +core + diff --git a/building/main.cpp b/building/main.cpp index 7f7cfc5..a956e9f 100644 --- a/building/main.cpp +++ b/building/main.cpp @@ -149,8 +149,10 @@ struct CAWebChat { "login_cookie.cpp", "backend_logic/server_data_interact.cpp", "backend_logic/when_login.cpp", + "backend_logic/when_list_rooms.cpp", "backend_logic/when_internalapi_pollevents.cpp", "backend_logic/when_internalapi_getchatlist.cpp", + "backend_logic/when_internalapi_getchatinfo.cpp", }; for (std::string& u: T.units) u = "web_chat/iu9_ca_web_chat_lib/" + u; diff --git a/src/http_server/engine_engine_number_9/http_structures/cookies.cpp b/src/http_server/engine_engine_number_9/http_structures/cookies.cpp index 30ee695..85ef6b9 100644 --- a/src/http_server/engine_engine_number_9/http_structures/cookies.cpp +++ b/src/http_server/engine_engine_number_9/http_structures/cookies.cpp @@ -55,26 +55,31 @@ namespace een9 { }; skip_ows(); while (pos < hv.size()) { + if (!result.empty()) { + if (!isThis(';')) + THROW("Incorrect Cookie header line, missing ;"); + pos++; + skip_ows(); + } std::string name_of_pechenye = read_to_space_or_eq(); - ASSERT(isCookieName(name_of_pechenye), "Incorrect Cookie name"); + // ASSERT(isCookieName(name_of_pechenye), "Incorrect Cookie name"); skip_ows(); - ASSERT(isThis('='), "Incorrect Cookie header line, missing ="); + if (!isThis('=')) + THROW("Incorrect Cookie header line, missing ="); pos++; skip_ows(); std::string value_of_pechenye; if (isThis('"')) { pos++; value_of_pechenye = read_to_space_or_dq_or_semc(); - ASSERT(isThis('"'), "Incorrect Cookie header line, missing \""); + if (!isThis('"')) + THROW("Incorrect Cookie header line, missing \""); pos++; } else { value_of_pechenye = read_to_space_or_dq_or_semc(); } - ASSERT(isCookieValue(value_of_pechenye), "Incorrect Cookie value"); - if (result.empty()) - result.emplace_back(); - result.back().first = std::move(name_of_pechenye); - result.back().second = std::move(value_of_pechenye); + // ASSERT(isCookieValue(value_of_pechenye), "Incorrect Cookie value"); + result.emplace_back(name_of_pechenye, value_of_pechenye); skip_ows(); } return result; @@ -84,10 +89,14 @@ namespace een9 { findAllClientCookies(const std::vector>& header) { std::vector> result; for (const std::pair& line: header) { - if (line.first == "Cookie") { - std::vector> new_cookies = parseCookieHeader(line.second); - result.reserve(result.size() + new_cookies.size()); - result.insert(result.end(), new_cookies.begin(), new_cookies.end()); + try { + if (line.first == "Cookie") { + std::vector> new_cookies = parseCookieHeader(line.second); + result.reserve(result.size() + new_cookies.size()); + result.insert(result.end(), new_cookies.begin(), new_cookies.end()); + } + } catch (const std::exception& e) { + printf("!!!findAllClientCookies failure\n"); } } return result; diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h index e6361ac..f39a58f 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/server_data_interact.h @@ -63,6 +63,8 @@ namespace iu9cawebchat { std::string when_page_login(WorkerGuestData& wgd, const json::JSON& config_presentation, const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo); + std::string when_page_list_rooms(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo); json::JSON internalapi_pollEvents(SqliteConnection& conn, int64_t uid, const json::JSON& Sent); diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp new file mode 100644 index 0000000..23556d7 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_internalapi_getchatinfo.cpp @@ -0,0 +1,5 @@ +#include "server_data_interact.h" + +namespace iu9cawebchat { + // todo +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp new file mode 100644 index 0000000..c3b4673 --- /dev/null +++ b/src/web_chat/iu9_ca_web_chat_lib/backend_logic/when_list_rooms.cpp @@ -0,0 +1,18 @@ +#include "server_data_interact.h" + +namespace iu9cawebchat { + std::string when_page_list_rooms(WorkerGuestData& wgd, const json::JSON& config_presentation, + const een9::ClientRequest& req, const std::vector& login_cookies, const json::JSON& userinfo) { + if (userinfo.isNull()) { + printf("Somebody entered /list-room with %s without being logged in\n", req.method.c_str()); + if (!login_cookies.empty()) { + printf("Login cookies: \n"); + for (auto& c: login_cookies) { + printf("%s as %s\n", c.nickname.c_str(), c.password.c_str()); + } + } + return een9::form_http_server_response_307("/login"); + } + return RTEE("list-rooms", config_presentation, wgd, userinfo); + } +} diff --git a/src/web_chat/iu9_ca_web_chat_lib/run.cpp b/src/web_chat/iu9_ca_web_chat_lib/run.cpp index 3cff03a..3d4d2ab 100644 --- a/src/web_chat/iu9_ca_web_chat_lib/run.cpp +++ b/src/web_chat/iu9_ca_web_chat_lib/run.cpp @@ -82,20 +82,18 @@ namespace iu9cawebchat { int64_t logged_in_user = -1; initial_extraction_of_all_the_useful_info_from_cookies(*wgd.db, req, cookies, login_cookies, userinfo, logged_in_user); - std::string result; - if (req.uri_path == "/" || req.uri_path == "/list-rooms") { - if (logged_in_user < 0) - result = een9::form_http_server_response_307("/login"); - return RTEE("list-rooms", config_presentation, wgd, userinfo); + return when_page_list_rooms(wgd, config_presentation, req, login_cookies, userinfo); } if (req.uri_path == "/login") { return when_page_login(wgd, config_presentation, req, login_cookies, userinfo); } if (req.uri_path == "/chat") { + // todo: write it actually return RTEE("chat", config_presentation, wgd, userinfo); } if (req.uri_path == "/profile") { + // todo: write it actually return RTEE("profile", config_presentation, wgd, userinfo); } // if (req.uri_path == "/registration") {