From d4f6dfa0b51907dd18022f118c0351c24b2d49cd Mon Sep 17 00:00:00 2001 From: End Date: Fri, 14 Aug 2020 18:54:43 +0200 Subject: [PATCH] wip --- httpflow.h | 6 +++-- session.cpp | 63 ++++++++++++++++++++++++++++++++++++++--------------- session.h | 8 ++++--- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/httpflow.h b/httpflow.h index a0a64bd..bbce0bc 100644 --- a/httpflow.h +++ b/httpflow.h @@ -97,7 +97,6 @@ namespace http { struct Request { - std::string server_conn_id; std::string server_ip_address; bool tls; @@ -128,6 +127,7 @@ struct Response { }; struct Flow { + std::string uid; Request request; Response response; }; @@ -139,8 +139,10 @@ inline void from_json(const json& j, Flow& flow) { if(!j.contains("flow")) { return; } + + j.at("flow").at("id").get_to(flow.id); + if(j.at("flow").contains("server_conn")) { - j.at("flow").at("server_conn").at("id").get_to(flow.request.server_conn_id); j.at("flow").at("server_conn").at("tls_established").get_to(flow.request.tls); } if(j.at("flow").contains("request")) { diff --git a/session.cpp b/session.cpp index 4756ecc..420a978 100644 --- a/session.cpp +++ b/session.cpp @@ -116,30 +116,57 @@ void Session::bind_text(sqlite3_stmt* stmt, int id, std::string text) { sqlite3_bind_text(stmt, id, text.c_str(), -1, SQLITE_TRANSIENT); } -void Session::saveRequest(http::Flow flow) { - bind_text(stmt_insert_request, 1, flow.request.server_conn_id); - bind_text(stmt_insert_request, 2, flow.request.server_ip_address); - sqlite3_bind_int(stmt_insert_request, 3, flow.request.tls); - bind_text(stmt_insert_request, 4, flow.request.content); - bind_text(stmt_insert_request, 5, flow.request.scheme); - bind_text(stmt_insert_request, 6, flow.request.method); - bind_text(stmt_insert_request, 7, flow.request.host); - bind_text(stmt_insert_request, 8, flow.request.address); - sqlite3_bind_int(stmt_insert_request, 9, flow.request.port); - bind_text(stmt_insert_request, 10, flow.request.http_version); - bind_text(stmt_insert_request, 11, flow.request.path); - sqlite3_bind_double(stmt_insert_request, 12, flow.request.timestamp_start); - sqlite3_bind_double(stmt_insert_request, 13, flow.request.timestamp_end); - bind_text(stmt_insert_request, 14, flow.request.error); +int Session::get_last_id() { + auto rc = sqlite3_step(stmt_insert_request); + sqlite3_reset(stmt_insert_request); + if(rc != SQLITE_DONE) { + qDebug() << "error getting last id" << rc; + qDebug() << sqlite3_errmsg(db); + return -1; + } + return -1; +} + +int Session::saveRequest(http::Flow flow) { + int j = 1; + bind_text(stmt_insert_request, j++, flow.request.server_ip_address); + + sqlite3_bind_int(stmt_insert_request, j++, flow.request.tls); + + bind_text(stmt_insert_request, j++, flow.request.content); + bind_text(stmt_insert_request, j++, flow.request.scheme); + bind_text(stmt_insert_request, j++, flow.request.method); + bind_text(stmt_insert_request, j++, flow.request.host); + bind_text(stmt_insert_request, j++, flow.request.address); + + sqlite3_bind_int(stmt_insert_request, j++, flow.request.port); + + bind_text(stmt_insert_request, j++, flow.request.http_version); + bind_text(stmt_insert_request, j++, flow.request.path); + + sqlite3_bind_double(stmt_insert_request, j++, flow.request.timestamp_start); + sqlite3_bind_double(stmt_insert_request, j++, flow.request.timestamp_end); + + bind_text(stmt_insert_request, j++, flow.request.error); + auto rc = sqlite3_step(stmt_insert_request); sqlite3_reset(stmt_insert_request); if(rc != SQLITE_DONE) { qDebug() << "inserting request failed" << rc; qDebug() << sqlite3_errmsg(db); - throw; + return -1; } + + } -void Session::saveResponse(http::Flow flow) { - throw; +int Session::saveResponse(http::Flow flow) { + + auto rc = sqlite3_step(stmt_insert_response); + sqlite3_reset(stmt_insert_response); + if(rc != SQLITE_DONE) { + qDebug() << "inserting response failed" << rc; + qDebug() << sqlite3_errmsg(db); + return -1; + } } diff --git a/session.h b/session.h index ff8ae38..a1d9379 100644 --- a/session.h +++ b/session.h @@ -50,6 +50,7 @@ private: ); )rstr"; const char* create_flow_tbl = R"rstr( CREATE TABLE flow( id INTEGER PRIMARY KEY AUTOINCREMENT, + uid TEXT UNIQUE NOT NULL, request_id INTEGER, response_id INTEGER, FOREIGN KEY(request_id) REFERENCES request(id) ON UPDATE CASCADE ON DELETE CASCADE, @@ -62,7 +63,7 @@ private: const char* last_inserted_id = R"rstr( SELECT last_insert_rowid(); )rstr"; const char* check_tbl = R"rstr( SELECT name FROM sqlite_master WHERE type='table' AND name=?; )rstr"; const char* check_session_info = R"rstr( SELECT value FROM session WHERE key=?; )rstr"; - const char* insert_request = R"rstr( INSERT INTO request (server_conn_id, server_ip_address, tls, content, scheme, method, host, address, port, http_version, path, timestamp_start, timestamp_end, error) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?); )rstr"; + const char* insert_request = R"rstr( INSERT INTO request (server_ip_address, tls, content, scheme, method, host, address, port, http_version, path, timestamp_start, timestamp_end, error) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?); )rstr"; const char* insert_request_header = R"rstr( INSERT INTO request_header (key, value, request_id) VALUES (?,?,?); )rstr"; const char* insert_response = R"rstr( INSERT INTO response (status_code, http_version, reason, content, timestamp_start, timestamp_end) VALUES (?,?,?,?,?,?); )rstr"; const char* insert_response_header = R"rstr( INSERT INTO response_header (key, value, response_id) VALUES (?,?,?); )rstr"; @@ -85,6 +86,7 @@ private: void prepare(const char* query, sqlite3_stmt** stmt); bool prepare_and_exec(const char* query); void bind_text(sqlite3_stmt* stmt, int id, std::string text); + int get_last_id(); public: explicit Session(QObject *parent = nullptr); ~Session(); @@ -93,8 +95,8 @@ public: void unload(); bool isLoaded(); public slots: - void saveRequest(http::Flow flow); - void saveResponse(http::Flow flow); + int saveRequest(http::Flow flow); + int saveResponse(http::Flow flow); signals: };