From 741d5d96b2d917a6ac4b4923191e3f64733c7d1d Mon Sep 17 00:00:00 2001 From: Tim Blume Date: Wed, 2 Sep 2020 14:13:03 +0200 Subject: [PATCH] foo --- historymodel.cpp | 2 +- historymodel.h | 4 +++ session.cpp | 74 +++++++++++++++++++++++++++++++----------------- session.h | 13 +++------ 4 files changed, 57 insertions(+), 36 deletions(-) diff --git a/historymodel.cpp b/historymodel.cpp index 7ff7a7e..d672cb0 100644 --- a/historymodel.cpp +++ b/historymodel.cpp @@ -46,7 +46,7 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const case 1: { QDateTime timestamp; timestamp.setTime_t(item.timestamp); - return timestamp.toString(Qt::SystemLocaleShortDate); + return timestamp.toString("yyyy-mm-dd hh:mm:ss"); } case 2: { std::string url = item.scheme + "://" + item.host + ":" + std::to_string(item.port) + item.path; diff --git a/historymodel.h b/historymodel.h index 8cfc913..bebe447 100644 --- a/historymodel.h +++ b/historymodel.h @@ -16,6 +16,10 @@ struct HistoryItem { std::string reason; double rtt = 0.0; size_t size = 0; + std::string request_content; + std::string response_content; + std::map request_headers; + std::map response_headers; }; class HistoryModel : public QAbstractTableModel diff --git a/session.cpp b/session.cpp index 57bba7b..3649e1c 100644 --- a/session.cpp +++ b/session.cpp @@ -57,8 +57,8 @@ bool Session::table_exists(std::string name) { bool Session::reset(sqlite3_stmt *stmt) { auto res = sqlite3_reset(stmt); - if(res != SQLITE_ROW || - res != SQLITE_DONE || + if(res != SQLITE_ROW && + res != SQLITE_DONE && res != SQLITE_OK) { qDebug() << "cannot reset statement"; qDebug() << sqlite3_errmsg(db); @@ -69,6 +69,48 @@ bool Session::reset(sqlite3_stmt *stmt) return true; } +std::map Session::getHeaders(int id, bool get) +{ + auto stmt = stmt_get_request_headers; + if(!get) { + stmt = stmt_get_response_headers; + } + reset(stmt); + + std::map result; + int j = 1; + + sqlite3_bind_int(stmt, j++, id); + + auto rc = sqlite3_step(stmt); + while(rc == SQLITE_ROW) { + int j = 0; + std::string key = std::string((const char*)sqlite3_column_text(stmt, j++)); + std::string value = std::string((const char*)sqlite3_column_text(stmt, j++)); + + result.insert({key, value}); + + rc = sqlite3_step(stmt); + } + if(rc != SQLITE_DONE) { + qDebug() << "error getting history items" << rc; + qDebug() << sqlite3_errmsg(db); + return std::map(); + } + + return result; +} + +std::map Session::getRequestHeader(int id) +{ + return getHeaders(id, true); +} + +std::map Session::getResponseHeader(int id) +{ + return getHeaders(id, false); +} + void Session::prepare_tables() { if(table_exists("setting")) { prepare(get_setting, &stmt_get_setting); @@ -96,9 +138,7 @@ void Session::prepare_tables() { } prepare(get_all_history, &stmt_get_all_history); - prepare(get_request, &stmt_get_request); prepare(get_request_headers, &stmt_get_request_headers); - prepare(get_response, &stmt_get_response); prepare(get_response_headers, &stmt_get_response_headers); prepare(update_setting, &stmt_update_setting); @@ -153,8 +193,6 @@ std::optional Session::getSetting(std::string key) { } std::vector* Session::getHistoryItems() { - qDebug() << "history items generated"; - // todo check reset return code reset(stmt_get_all_history); std::vector* result = new std::vector(); @@ -174,6 +212,10 @@ std::vector* Session::getHistoryItems() { i.reason = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++)); i.rtt = sqlite3_column_double(stmt_get_all_history, j++); i.size = sqlite3_column_int(stmt_get_all_history, j++); + i.request_content = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++)); + i.response_content = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++)); + i.request_headers = getRequestHeader(i.id); + i.response_headers = getResponseHeader(i.id); result->push_back(i); @@ -189,26 +231,6 @@ std::vector* Session::getHistoryItems() { return result; } -std::optional> Session::getRequest(int id) -{ - -} - -std::optional> Session::getRequestHeader(int id) -{ - -} - -std::optional> Session::getResponse(int id) -{ - -} - -std::optional> Session::getResponseHeader(int id) -{ - -} - void Session::bind_text(sqlite3_stmt* stmt, int id, std::string text) { //qDebug() << id << " " << text.c_str() << " " << strlen(text.c_str()); sqlite3_bind_text(stmt, id, text.c_str(), -1, SQLITE_TRANSIENT); diff --git a/session.h b/session.h index de346cf..4d47c20 100644 --- a/session.h +++ b/session.h @@ -64,11 +64,9 @@ private: const char* get_tbl = R"rstr( SELECT name FROM sqlite_master WHERE type='table' AND name=?; )rstr"; const char* get_setting = R"rstr( SELECT value FROM setting WHERE key=?; )rstr"; - const char* get_request = R"rstr( SELECT req.content FROM flow f JOIN request req ON f.request_id=req.id WHERE f.id=?; )rstr"; - const char* get_response = R"rstr( SELECT res.content FROM flow f JOIN response res ON f.response_id=res.id WHERE f.id=?; )rstr"; const char* get_request_headers = R"rstr( SELECT * FROM response_header WHERE response_id=?; )rstr"; const char* get_response_headers = R"rstr( SELECT * FROM request_header WHERE request_id=?; )rstr"; - const char* get_all_history = R"rstr( SELECT f.id,req.timestamp_start,req.method,req.scheme,req.host,req.port,req.path,res.status_code,res.reason,res.timestamp_end-res.timestamp_start,length(res.content) FROM flow f JOIN request req ON f.request_id=req.id JOIN response res ON f.response_id=res.id; )rstr"; + const char* get_all_history = R"rstr( SELECT f.id,req.timestamp_start,req.method,req.scheme,req.host,req.port,req.path,res.status_code,res.reason,res.timestamp_end-res.timestamp_start,length(res.content),req.content,res.content FROM flow f JOIN request req ON f.request_id=req.id JOIN response res ON f.response_id=res.id; )rstr"; const char* insert_request = R"rstr( INSERT INTO request (server_ip_address, tls, content, scheme, method, host, 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"; @@ -83,8 +81,6 @@ private: bool loaded = false; sqlite3_stmt* stmt_get_setting = nullptr; - sqlite3_stmt* stmt_get_request = nullptr; - sqlite3_stmt* stmt_get_response = nullptr; sqlite3_stmt* stmt_get_request_headers = nullptr; sqlite3_stmt* stmt_get_response_headers = nullptr; sqlite3_stmt* stmt_get_all_history = nullptr; @@ -105,6 +101,9 @@ private: void bind_text(sqlite3_stmt* stmt, int id, std::string text); bool table_exists(std::string name); bool reset(sqlite3_stmt* stmt); + inline std::map getHeaders(int id, bool get); + std::map getRequestHeader(int id); + std::map getResponseHeader(int id); public: explicit Session(QObject *parent = nullptr); ~Session(); @@ -114,10 +113,6 @@ public: bool isLoaded(); std::optional getSetting(std::string key); std::vector* getHistoryItems(); - std::optional> getRequest(int id); - std::optional> getRequestHeader(int id); - std::optional> getResponse(int id); - std::optional> getResponseHeader(int id); public slots: int saveRequest(http::Flow flow); int saveRequestHeader(std::string key, std::string value, int id);