From 38d3a2ea78f1dd886e02c7a896713b3aa3788be9 Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Sat, 17 Aug 2024 15:49:28 +0300 Subject: [PATCH] Printing bad requests to log/req --- .gitignore | 1 + README.md | 4 +- .../http_structures/client_request_parse.cpp | 11 +++++ .../engine_engine_number_9/os_utils.cpp | 22 +++++++++ .../engine_engine_number_9/os_utils.h | 2 + src/web_chat/initialize.cpp | 48 ++++++++++++++++--- 6 files changed, 79 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 63e19b3..0314578 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ compile_commands.json local.sh iu9-ca-web-chat.db +log diff --git a/README.md b/README.md index 505236b..63688ef 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,7 @@ regexis024_build_system.sh Зачем писать комментарии в коде, если можно их вынести в отдельные пдф-ки? -- [API сервиса]( +- [Документация для разработчиков]( https://gitlab.yyyi.ru/collarbone-annihilation/iu9-ca-chat-api) -- [Доки New York Transit Line]( - https://gitlab.yyyi.ru/collarbone-annihilation/new_york_transit_line_documentation_rus) О том как работает всё остальное можно только догадываться. diff --git a/src/http_server/engine_engine_number_9/http_structures/client_request_parse.cpp b/src/http_server/engine_engine_number_9/http_structures/client_request_parse.cpp index bce7ce2..c9ec6b3 100644 --- a/src/http_server/engine_engine_number_9/http_structures/client_request_parse.cpp +++ b/src/http_server/engine_engine_number_9/http_structures/client_request_parse.cpp @@ -3,6 +3,13 @@ #include #include #include +// Used for debug + +#include "unistd.h" +#include +#include "sys/dir.h" +#include "../os_utils.h" + namespace een9 { ClientRequestParser_CommonPrograms::ClientRequestParser_CommonPrograms() { @@ -109,6 +116,10 @@ namespace een9 { } /* We either finish now or we finish later */ } else if (!vm.haveSurvivors()) { +#ifdef DEBUG_ALLOW_LOUD + mkdir("log", 0750); + writeFile("log/req", header); +#endif status = -1; THROW("bad request"); } diff --git a/src/http_server/engine_engine_number_9/os_utils.cpp b/src/http_server/engine_engine_number_9/os_utils.cpp index cc14d30..6ed7a2e 100644 --- a/src/http_server/engine_engine_number_9/os_utils.cpp +++ b/src/http_server/engine_engine_number_9/os_utils.cpp @@ -67,6 +67,28 @@ namespace een9 { readFromFileDescriptor(fdw(), result, "file \"" + path + "\""); } + + /* write(fd, text); close(fd); */ + void writeToFileDescriptor(int fd, const std::string& text, const std::string& description = "") { + size_t n = text.size(); + size_t i = 0; + while (i < n) { + size_t block = std::min(2048lu, n - i); + int ret = write(fd, &text[i], block); + ASSERT_on_iret(ret, "Writing to" + description); + i += ret; + } + close(fd); + } + + /* Truncational */ + void writeFile(const std::string& path, const std::string& text) { + int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0755); + ASSERT_on_iret(fd, "Opening \"" + path + "\""); + UniqueFdWrapper fdw(fd); + writeToFileDescriptor(fdw(), text, "file \"" + path + "\n"); + } + void configure_socket_rcvsndtimeo(int fd, timeval tv) { int ret; ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval)); diff --git a/src/http_server/engine_engine_number_9/os_utils.h b/src/http_server/engine_engine_number_9/os_utils.h index 22f21d8..636fd00 100644 --- a/src/http_server/engine_engine_number_9/os_utils.h +++ b/src/http_server/engine_engine_number_9/os_utils.h @@ -30,6 +30,8 @@ namespace een9 { void readFile(const std::string& path, std::string& result); + void writeFile(const std::string& path, const std::string& text); + void configure_socket_rcvsndtimeo(int fd, timeval tv); } diff --git a/src/web_chat/initialize.cpp b/src/web_chat/initialize.cpp index 687880a..f0a613f 100644 --- a/src/web_chat/initialize.cpp +++ b/src/web_chat/initialize.cpp @@ -4,11 +4,14 @@ #include #include #include +#include +#include void sqlite_single_statement(sqlite3* db_hand, const std::string& req_statement) { sqlite3_stmt* stmt_obj = NULL; - int ret = sqlite3_prepare16_v2(db_hand, req_statement.c_str(), -1, &stmt_obj, NULL); + int ret = sqlite3_prepare_v2(db_hand, req_statement.c_str(), -1, &stmt_obj, NULL); een9_ASSERT(ret == 0, "Can't compile request expression"); + assert(sqlite3_errcode(db_hand) == SQLITE_OK); struct Guard1{sqlite3_stmt*& r; ~Guard1(){if (sqlite3_finalize(r) != 0) {abort();}}} guard1{stmt_obj}; while (true) { ret = sqlite3_step(stmt_obj); @@ -34,6 +37,29 @@ void sqlite_single_statement(sqlite3* db_hand, const std::string& req_statement) ccase(NULL) case SQLITE3_TEXT: printf(" TEXT |"); break; + default: + een9_THROW("AAAAAA"); + } + } + printf("\n"); + printf("Values: | "); + for (int i = 0; i < cc; i++) { + if (types[i] == SQLITE_INTEGER) { + printf("%ld | ", sqlite3_column_int64(stmt_obj, i)); + } else if (types[i] == SQLITE_FLOAT) { + printf("%lf | ", sqlite3_column_double(stmt_obj, i)); + } else if (types[i] == SQLITE_BLOB) { + const void* blob = sqlite3_column_blob(stmt_obj, i); + een9_ASSERT(sqlite3_errcode(db_hand) == SQLITE_OK, "oom in sqlite3_column_blob"); + size_t sz = sqlite3_column_bytes(stmt_obj, i); + printf("Blob of size %s | ", sz); + } else if (types[i] == SQLITE_NULL) { + printf("NULL | "); + } else { + const unsigned char* text = sqlite3_column_text(stmt_obj, i); + een9_ASSERT(sqlite3_errcode(db_hand) != SQLITE_NOMEM, "oom in sqlite3_column_text"); + printf("%s | ", (const char*)text); + // todo: THIS F. B.S. IS NOT SAFE } } printf("\n"); @@ -48,12 +74,22 @@ void initialize_website(const json::JSON& config, const std::string& root_pw) { int ret; ret = find_db_sqlite_file_path(config, db_path); een9_ASSERT(ret == 0, "Invalid settings[\"database\"] field"); + if (een9::isRegularFile(db_path)) { + // todo: plaese, don't do this + ret = unlink(db_path.c_str()); + een9_ASSERT_pl(ret == 0); + } een9_ASSERT(!een9::isRegularFile(db_path), "Database file exists prior to initialization. " "Can't preceed withut harming existing data"); - // sqlite3* db_hand = NULL; - // ret = sqlite3_open(db_path.c_str(), &db_hand); - // een9_ASSERT(ret == 0, "Can't open database"); - // struct Guard1{sqlite3*& dhp; ~Guard1(){if (sqlite3_close(dhp) != 0) {abort();}}} guard1{db_hand}; - // sqlite_single_statement(db_hand, "CREATE TABLE tb(a INT, b INT);"); + sqlite3* db_hand = NULL; + ret = sqlite3_open(db_path.c_str(), &db_hand); + een9_ASSERT(ret == 0, "Can't open database"); + assert(sqlite3_errcode(db_hand) == SQLITE_OK); + struct Guard1{sqlite3*& dhp; ~Guard1(){if (sqlite3_close(dhp) != 0) {abort();}}} guard1{db_hand}; + sqlite_single_statement(db_hand, "CREATE TABLE tb(a INT, b INT)"); + sqlite_single_statement(db_hand, "INSERT INTO tb(a) VALUES (111)"); + sqlite_single_statement(db_hand, "INSERT INTO tb(b) VALUES ('yaeyahiyagdohzghz5echp')"); + sqlite_single_statement(db_hand, "INSERT INTO tb(a, b) VALUES (1123123, 'string')"); + sqlite_single_statement(db_hand, "SELECT * FROM tb"); // todo: actually write something } \ No newline at end of file