This commit is contained in:
Tim Blume 2020-09-02 14:13:03 +02:00
parent c42e9d07b5
commit 741d5d96b2
4 changed files with 57 additions and 36 deletions

View file

@ -46,7 +46,7 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const
case 1: { case 1: {
QDateTime timestamp; QDateTime timestamp;
timestamp.setTime_t(item.timestamp); timestamp.setTime_t(item.timestamp);
return timestamp.toString(Qt::SystemLocaleShortDate); return timestamp.toString("yyyy-mm-dd hh:mm:ss");
} }
case 2: { case 2: {
std::string url = item.scheme + "://" + item.host + ":" + std::to_string(item.port) + item.path; std::string url = item.scheme + "://" + item.host + ":" + std::to_string(item.port) + item.path;

View file

@ -16,6 +16,10 @@ struct HistoryItem {
std::string reason; std::string reason;
double rtt = 0.0; double rtt = 0.0;
size_t size = 0; size_t size = 0;
std::string request_content;
std::string response_content;
std::map<std::string, std::string> request_headers;
std::map<std::string, std::string> response_headers;
}; };
class HistoryModel : public QAbstractTableModel class HistoryModel : public QAbstractTableModel

View file

@ -57,8 +57,8 @@ bool Session::table_exists(std::string name) {
bool Session::reset(sqlite3_stmt *stmt) bool Session::reset(sqlite3_stmt *stmt)
{ {
auto res = sqlite3_reset(stmt); auto res = sqlite3_reset(stmt);
if(res != SQLITE_ROW || if(res != SQLITE_ROW &&
res != SQLITE_DONE || res != SQLITE_DONE &&
res != SQLITE_OK) { res != SQLITE_OK) {
qDebug() << "cannot reset statement"; qDebug() << "cannot reset statement";
qDebug() << sqlite3_errmsg(db); qDebug() << sqlite3_errmsg(db);
@ -69,6 +69,48 @@ bool Session::reset(sqlite3_stmt *stmt)
return true; return true;
} }
std::map<std::string, std::string> Session::getHeaders(int id, bool get)
{
auto stmt = stmt_get_request_headers;
if(!get) {
stmt = stmt_get_response_headers;
}
reset(stmt);
std::map<std::string, std::string> 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<std::string, std::string>();
}
return result;
}
std::map<std::string, std::string> Session::getRequestHeader(int id)
{
return getHeaders(id, true);
}
std::map<std::string, std::string> Session::getResponseHeader(int id)
{
return getHeaders(id, false);
}
void Session::prepare_tables() { void Session::prepare_tables() {
if(table_exists("setting")) { if(table_exists("setting")) {
prepare(get_setting, &stmt_get_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_all_history, &stmt_get_all_history);
prepare(get_request, &stmt_get_request);
prepare(get_request_headers, &stmt_get_request_headers); prepare(get_request_headers, &stmt_get_request_headers);
prepare(get_response, &stmt_get_response);
prepare(get_response_headers, &stmt_get_response_headers); prepare(get_response_headers, &stmt_get_response_headers);
prepare(update_setting, &stmt_update_setting); prepare(update_setting, &stmt_update_setting);
@ -153,8 +193,6 @@ std::optional<std::string> Session::getSetting(std::string key) {
} }
std::vector<HistoryItem>* Session::getHistoryItems() { std::vector<HistoryItem>* Session::getHistoryItems() {
qDebug() << "history items generated";
// todo check reset return code
reset(stmt_get_all_history); reset(stmt_get_all_history);
std::vector<HistoryItem>* result = new std::vector<HistoryItem>(); std::vector<HistoryItem>* result = new std::vector<HistoryItem>();
@ -174,6 +212,10 @@ std::vector<HistoryItem>* Session::getHistoryItems() {
i.reason = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++)); 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.rtt = sqlite3_column_double(stmt_get_all_history, j++);
i.size = sqlite3_column_int(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); result->push_back(i);
@ -189,26 +231,6 @@ std::vector<HistoryItem>* Session::getHistoryItems() {
return result; return result;
} }
std::optional<std::tuple<std::string, std::string>> Session::getRequest(int id)
{
}
std::optional<std::map<std::string, std::string>> Session::getRequestHeader(int id)
{
}
std::optional<std::tuple<std::string, std::string>> Session::getResponse(int id)
{
}
std::optional<std::map<std::string, std::string>> Session::getResponseHeader(int id)
{
}
void Session::bind_text(sqlite3_stmt* stmt, int id, std::string text) { void Session::bind_text(sqlite3_stmt* stmt, int id, std::string text) {
//qDebug() << id << " " << text.c_str() << " " << strlen(text.c_str()); //qDebug() << id << " " << text.c_str() << " " << strlen(text.c_str());
sqlite3_bind_text(stmt, id, text.c_str(), -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, id, text.c_str(), -1, SQLITE_TRANSIENT);

View file

@ -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_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_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_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_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 = 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"; 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; bool loaded = false;
sqlite3_stmt* stmt_get_setting = nullptr; 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_request_headers = nullptr;
sqlite3_stmt* stmt_get_response_headers = nullptr; sqlite3_stmt* stmt_get_response_headers = nullptr;
sqlite3_stmt* stmt_get_all_history = nullptr; sqlite3_stmt* stmt_get_all_history = nullptr;
@ -105,6 +101,9 @@ private:
void bind_text(sqlite3_stmt* stmt, int id, std::string text); void bind_text(sqlite3_stmt* stmt, int id, std::string text);
bool table_exists(std::string name); bool table_exists(std::string name);
bool reset(sqlite3_stmt* stmt); bool reset(sqlite3_stmt* stmt);
inline std::map<std::string, std::string> getHeaders(int id, bool get);
std::map<std::string, std::string> getRequestHeader(int id);
std::map<std::string, std::string> getResponseHeader(int id);
public: public:
explicit Session(QObject *parent = nullptr); explicit Session(QObject *parent = nullptr);
~Session(); ~Session();
@ -114,10 +113,6 @@ public:
bool isLoaded(); bool isLoaded();
std::optional<std::string> getSetting(std::string key); std::optional<std::string> getSetting(std::string key);
std::vector<HistoryItem>* getHistoryItems(); std::vector<HistoryItem>* getHistoryItems();
std::optional<std::tuple<std::string, std::string>> getRequest(int id);
std::optional<std::map<std::string, std::string>> getRequestHeader(int id);
std::optional<std::tuple<std::string, std::string>> getResponse(int id);
std::optional<std::map<std::string, std::string>> getResponseHeader(int id);
public slots: public slots:
int saveRequest(http::Flow flow); int saveRequest(http::Flow flow);
int saveRequestHeader(std::string key, std::string value, int id); int saveRequestHeader(std::string key, std::string value, int id);