From d871f113b3a650c41135e6ec976f27d7954de98b Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Tue, 6 Aug 2024 11:53:33 +0300 Subject: [PATCH] Added query splitting --- building/main.cpp | 2 + .../form_data_structure/urlencoded_query.cpp | 41 +++++++++++++++++++ .../form_data_structure/urlencoded_query.h | 12 ++++++ src/web_chat/main.cpp | 7 +++- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.cpp create mode 100644 src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.h diff --git a/building/main.cpp b/building/main.cpp index 98de247..241d527 100644 --- a/building/main.cpp +++ b/building/main.cpp @@ -85,6 +85,7 @@ struct CAWebChat { "http_structures/response_gen.cpp", "connecting_assets/static_asset_manager.cpp", "running_mainloop.cpp", + "form_data_structure/urlencoded_query.cpp", }; for (std::string& u: T.units) u = "http_server/engine_engine_number_9/" + u; @@ -99,6 +100,7 @@ struct CAWebChat { "http_structures/client_request.h", "http_structures/response_gen.h", "running_mainloop.h", + "form_data_structure/urlencoded_query.h", }; for (std::string& u: T.exported_headers) u = "engine_engine_number_9/" + u; diff --git a/src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.cpp b/src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.cpp new file mode 100644 index 0000000..15c0782 --- /dev/null +++ b/src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.cpp @@ -0,0 +1,41 @@ +#include "urlencoded_query.h" +#include + +namespace een9 { + std::vector> split_html_query(const std::string &query) { + std::vector> result; + if (query.empty()) + return result; + result = {{"", ""}}; + bool filling_second = false; + auto fref = [&]() -> std::string& { return filling_second ? result.back().second : result.back().first; }; + for (size_t i = 0; i < query.size();) { + if (query[i] == '&') { + result.emplace_back("", ""); + filling_second = false; + } else if (query[i] == '=') { + filling_second = true; + } else if (query[i] == '+') { + fref() += ' '; + } else if (query[i] == '%') { + if (i + 3 > query.size()) + return {}; + auto readhex = [&](char ch) -> uint8_t { + if ('0' <= ch && ch <= '9') + return ch - '0'; + if ('a' <= ch && ch <= 'h') + return ch - 'a' + 10; + if ('A' <= ch && ch <= 'H') + return ch - 'A' + 10; + return 10; + }; + fref() += (char)((readhex(query[i + 1]) << 4) | readhex(query[i + 2])); + i += 2; + } else { + fref() += query[i]; + } + i++; + } + return result; + } +} diff --git a/src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.h b/src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.h new file mode 100644 index 0000000..9c69187 --- /dev/null +++ b/src/http_server/engine_engine_number_9/form_data_structure/urlencoded_query.h @@ -0,0 +1,12 @@ +#ifndef ENGINE_ENGINE_NUMBER_9_FORM_DATA_STRUCTURE_URLENCODED_QUERY_H +#define ENGINE_ENGINE_NUMBER_9_FORM_DATA_STRUCTURE_URLENCODED_QUERY_H + +#include +#include + +namespace een9{ + /* application/x-www-form-urlencoded */ + std::vector> split_html_query(const std::string& query); +} + +#endif diff --git a/src/web_chat/main.cpp b/src/web_chat/main.cpp index 012f0e2..df3f943 100644 --- a/src/web_chat/main.cpp +++ b/src/web_chat/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include bool termination = false; @@ -62,12 +63,16 @@ int main(int argc, char** argv){ params.guest_core = [&samI](const een9::SlaveTask& task, const een9::ClientRequest& req) -> std::string { een9::StaticAsset sa; int ret; - printf("%s", unsafe_client_request_stringification(req).c_str()); + // printf("%s", unsafe_client_request_stringification(req).c_str()); if (req.uri_path == "/output") { std::string text = unsafe_client_request_stringification(req); return een9::form_http_server_response_200("text/plain", text); } if (req.uri_path == "/" || req.uri_path == "/index.html") { + for (auto& p: een9::split_html_query(req.uri_query)) { + printf("Query: %s = %s\n", p.first.c_str(), p.second.c_str()); + } + printf(""); ret = samI.get_asset("/assets/html/test.html", sa); een9_ASSERT_pl(ret == 0); return een9::form_http_server_response_200(sa.type, sa.content);