Added query splitting

This commit is contained in:
Андреев Григорий 2024-08-06 11:53:33 +03:00
parent 3e9bd7d1fa
commit d871f113b3
4 changed files with 61 additions and 1 deletions

View File

@ -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;

View File

@ -0,0 +1,41 @@
#include "urlencoded_query.h"
#include <stdint.h>
namespace een9 {
std::vector<std::pair<std::string, std::string>> split_html_query(const std::string &query) {
std::vector<std::pair<std::string, std::string>> 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;
}
}

View File

@ -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 <string>
#include <vector>
namespace een9{
/* application/x-www-form-urlencoded */
std::vector<std::pair<std::string, std::string>> split_html_query(const std::string& query);
}
#endif

View File

@ -7,6 +7,7 @@
#include <sqlite3.h>
#include <libjsonincpp/string_representation.h>
#include <libregexis024vm/vm_opcodes.h>
#include <engine_engine_number_9/form_data_structure/urlencoded_query.h>
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);