103 lines
5.4 KiB
C++
103 lines
5.4 KiB
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <includes.h>
|
|
#include <httpflow.h>
|
|
|
|
class Session : public QObject
|
|
{
|
|
Q_OBJECT
|
|
private:
|
|
const char* create_request_tbl = R"rstr( CREATE TABLE request(
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
server_conn_id TEXT,
|
|
server_ip_address TEXT,
|
|
tls INTEGER,
|
|
content TEXT,
|
|
scheme TEXT,
|
|
method TEXT,
|
|
host TEXT,
|
|
address TEXT,
|
|
port INTEGER,
|
|
http_version TEXT,
|
|
path TEXT,
|
|
timestamp_start REAL,
|
|
timestamp_end REAL,
|
|
error TEXT
|
|
); )rstr";
|
|
const char* create_request_header_tbl = R"rstr( CREATE TABLE request_header(
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
key TEXT,
|
|
value TEXT,
|
|
request_id INTEGER,
|
|
FOREIGN KEY(request_id) REFERENCES request(referer_id) ON UPDATE CASCADE ON DELETE CASCADE
|
|
); )rstr";
|
|
const char* create_response_tbl = R"rstr( CREATE TABLE response(
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
status_code INTEGER,
|
|
http_version TEXT,
|
|
reason TEXT,
|
|
content TEXT,
|
|
timestamp_start REAL,
|
|
timestamp_end REAL
|
|
); )rstr";
|
|
const char* create_response_header_tbl = R"rstr( CREATE TABLE response_header(
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
key TEXT,
|
|
value TEXT,
|
|
response_id INTEGER,
|
|
FOREIGN KEY(response_id) REFERENCES response(referer_id) ON UPDATE CASCADE ON DELETE CASCADE
|
|
); )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,
|
|
FOREIGN KEY(response_id) REFERENCES response(id) ON UPDATE CASCADE ON DELETE CASCADE
|
|
); )rstr";
|
|
const char* create_session_tbl = R"rstr( CREATE TABLE session(
|
|
key TEXT UNIQUE NOT NULL PRIMARY KEY,
|
|
value TEXT
|
|
); )rstr";
|
|
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_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";
|
|
const char* insert_flow = R"rstr( INSERT INTO flow (request_id, response_id) VALUES (?,?); )rstr";
|
|
const char* insert_session = R"rstr( INSERT INTO session (key, value) VALUES (?,?); )rstr";
|
|
|
|
sqlite3* db = nullptr;
|
|
bool loaded = false;
|
|
|
|
sqlite3_stmt* stmt_last_inserted_id = nullptr;
|
|
sqlite3_stmt* stmt_insert_request = nullptr;
|
|
sqlite3_stmt* stmt_insert_request_header = nullptr;
|
|
sqlite3_stmt* stmt_insert_response = nullptr;
|
|
sqlite3_stmt* stmt_insert_response_header = nullptr;
|
|
sqlite3_stmt* stmt_insert_flow = nullptr;
|
|
sqlite3_stmt* stmt_insert_session = nullptr;
|
|
|
|
void open_db(std::filesystem::path path);
|
|
void prepare_tables();
|
|
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();
|
|
|
|
void load(std::filesystem::path path);
|
|
void unload();
|
|
bool isLoaded();
|
|
public slots:
|
|
int saveRequest(http::Flow flow);
|
|
int saveResponse(http::Flow flow);
|
|
signals:
|
|
|
|
};
|
|
|