2020-08-08 11:44:16 +00:00
# pragma once
# include <QObject>
2020-08-09 12:07:14 +00:00
# include <includes.h>
2020-08-13 03:35:22 +00:00
# include <httpflow.h>
2020-08-26 18:58:36 +00:00
# include <historymodel.h>
2020-08-18 15:14:48 +00:00
# include <optional>
2020-08-08 11:44:16 +00:00
class Session : public QObject
{
Q_OBJECT
private :
2020-08-13 21:25:04 +00:00
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 ,
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 ,
2020-09-01 20:14:49 +00:00
FOREIGN KEY ( request_id ) REFERENCES request ( id ) ON UPDATE CASCADE ON DELETE CASCADE
2020-08-13 21:25:04 +00:00
) ; ) 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 ,
2020-09-01 20:14:49 +00:00
FOREIGN KEY ( response_id ) REFERENCES response ( id ) ON UPDATE CASCADE ON DELETE CASCADE
2020-08-13 21:25:04 +00:00
) ; ) rstr " ;
const char * create_flow_tbl = R " rstr( CREATE TABLE flow(
id INTEGER PRIMARY KEY AUTOINCREMENT ,
2020-08-14 16:54:43 +00:00
uid TEXT UNIQUE NOT NULL ,
2020-08-13 21:25:04 +00:00
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 " ;
2020-08-18 15:14:48 +00:00
const char * create_setting_tbl = R " rstr( CREATE TABLE setting(
2020-08-13 21:25:04 +00:00
key TEXT UNIQUE NOT NULL PRIMARY KEY ,
value TEXT
) ; ) rstr " ;
2020-08-18 15:14:48 +00:00
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 " ;
2020-09-02 22:33:17 +00:00
const char * get_request_headers = R " rstr( SELECT key,value FROM response_header WHERE response_id=?; )rstr " ;
const char * get_response_headers = R " rstr( SELECT key,value FROM request_header WHERE request_id=?; )rstr " ;
2020-09-02 22:23:37 +00:00
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.http_version,req.content,res.http_version,res.content FROM flow f JOIN request req ON f.request_id=req.id JOIN response res ON f.response_id=res.id; )rstr " ;
2020-08-18 15:14:48 +00:00
2020-08-19 00:15:08 +00:00
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 " ;
2020-08-13 21:25:04 +00:00
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 " ;
2020-08-18 13:13:45 +00:00
const char * insert_flow = R " rstr( INSERT INTO flow (uid, request_id, response_id) VALUES (?,?,?); )rstr " ;
2020-08-18 15:14:48 +00:00
const char * insert_setting = R " rstr( INSERT INTO setting (key, value) VALUES (?,?); )rstr " ;
const char * update_setting = R " rstr( UPDATE setting SET value=? WHERE key=?; )rstr " ;
2020-08-13 21:25:04 +00:00
2020-08-09 12:07:14 +00:00
sqlite3 * db = nullptr ;
bool loaded = false ;
2020-08-13 21:25:04 +00:00
2020-08-18 15:14:48 +00:00
sqlite3_stmt * stmt_get_setting = nullptr ;
2020-09-01 20:14:49 +00:00
sqlite3_stmt * stmt_get_request_headers = nullptr ;
sqlite3_stmt * stmt_get_response_headers = nullptr ;
2020-08-19 00:15:08 +00:00
sqlite3_stmt * stmt_get_all_history = nullptr ;
2020-08-18 15:14:48 +00:00
2020-08-13 21:25:04 +00:00
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 ;
2020-08-18 15:14:48 +00:00
sqlite3_stmt * stmt_insert_setting = nullptr ;
sqlite3_stmt * stmt_update_setting = nullptr ;
2020-08-13 21:25:04 +00:00
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 ) ;
2020-08-18 15:14:48 +00:00
bool table_exists ( std : : string name ) ;
2020-09-01 20:14:49 +00:00
bool reset ( sqlite3_stmt * stmt ) ;
2020-09-02 12:13:03 +00:00
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 ) ;
2020-08-08 11:44:16 +00:00
public :
explicit Session ( QObject * parent = nullptr ) ;
2020-08-09 12:07:14 +00:00
~ Session ( ) ;
2020-08-13 21:25:04 +00:00
2020-08-09 12:07:14 +00:00
void load ( std : : filesystem : : path path ) ;
void unload ( ) ;
bool isLoaded ( ) ;
2020-08-18 15:14:48 +00:00
std : : optional < std : : string > getSetting ( std : : string key ) ;
2020-08-26 18:58:36 +00:00
std : : vector < HistoryItem > * getHistoryItems ( ) ;
2020-08-09 12:07:14 +00:00
public slots :
2020-08-14 16:54:43 +00:00
int saveRequest ( http : : Flow flow ) ;
2020-08-18 13:13:45 +00:00
int saveRequestHeader ( std : : string key , std : : string value , int id ) ;
2020-08-14 16:54:43 +00:00
int saveResponse ( http : : Flow flow ) ;
2020-08-18 13:13:45 +00:00
int saveResponseHeader ( std : : string key , std : : string value , int id ) ;
int saveFlow ( http : : Flow flow ) ;
2020-08-18 15:14:48 +00:00
bool saveSetting ( std : : string key , std : : string value ) ;
2020-08-08 11:44:16 +00:00
signals :
} ;