2017-07-12 20:12:17 +02:00
|
|
|
#include "requestlogging.hpp"
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug>
|
2017-08-20 17:06:44 +02:00
|
|
|
#include <Qt>
|
2017-07-12 20:12:17 +02:00
|
|
|
#include <io/ioutils.hpp>
|
|
|
|
|
2017-08-20 17:06:44 +02:00
|
|
|
// $type $url $status $time
|
|
|
|
// $type = GET POST PATCH DELETE etc
|
|
|
|
// $url = request target
|
|
|
|
// $status = response code
|
|
|
|
// $time = time of request, file name for response: $SETTINGS_DIR/responses/$time
|
|
|
|
|
|
|
|
QDir responses(settings::dir().absoluteFilePath("responses"));
|
|
|
|
QString requestPath = settings::dir().absoluteFilePath("history");
|
|
|
|
|
2017-08-21 16:17:20 +02:00
|
|
|
|
2017-07-12 20:12:17 +02:00
|
|
|
void requestlogging::addEntry(RequestContext context) {
|
|
|
|
if (!responses.exists()) responses.mkpath(".");
|
2017-08-20 17:06:44 +02:00
|
|
|
QString timeNow = QDateTime::currentDateTime().toUTC().toString("yyyy-MM-dd HH-mm-ss-zzz");
|
|
|
|
QFile responseFile(responses.absoluteFilePath(timeNow));
|
|
|
|
QFile requestFile(requestPath);
|
|
|
|
|
2017-07-12 20:12:17 +02:00
|
|
|
if (!responseFile.open(QIODevice::WriteOnly)) {
|
|
|
|
qCritical().noquote() << "Could not save response! " + responseFile.errorString();
|
|
|
|
return;
|
|
|
|
}
|
2017-08-20 17:06:44 +02:00
|
|
|
|
|
|
|
if (!requestFile.open(QIODevice::Append)) {
|
|
|
|
qCritical().noquote() << "Could not append request! " + responseFile.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-21 16:17:20 +02:00
|
|
|
for (auto header : context.reply->rawHeaderList())
|
|
|
|
responseFile.write(header + ": " + context.reply->rawHeader(header) + "\n");
|
2017-07-12 20:12:17 +02:00
|
|
|
responseFile.write("\n\n" + context.response);
|
|
|
|
responseFile.close();
|
|
|
|
|
2017-08-21 16:17:20 +02:00
|
|
|
QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type
|
|
|
|
<< context.reply->url().toString() << " " // $url
|
|
|
|
<< context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << " " // $status
|
|
|
|
<< timeNow.replace(" ", "_"); // $time
|
2017-08-20 17:06:44 +02:00
|
|
|
requestFile.close();
|
2017-07-12 20:12:17 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 17:06:44 +02:00
|
|
|
using requestlogging::LoggedRequest;
|
|
|
|
|
|
|
|
QList<LoggedRequest> requestlogging::getRequests() {
|
|
|
|
QList<LoggedRequest> ret;
|
|
|
|
|
|
|
|
QFile requestFile(requestPath);
|
2017-08-21 16:17:20 +02:00
|
|
|
if (!requestFile.exists() || !requestFile.open(QIODevice::ReadOnly)) return ret;
|
2017-08-20 17:06:44 +02:00
|
|
|
|
|
|
|
QByteArray line;
|
|
|
|
while ((line = requestFile.readLine()).size() != 0) {
|
|
|
|
LoggedRequest r;
|
|
|
|
QTextStream stream(&line);
|
|
|
|
stream >> r.type;
|
|
|
|
stream >> r.url;
|
|
|
|
stream >> r.responseCode;
|
2017-08-21 16:17:20 +02:00
|
|
|
stream >> r.time;
|
|
|
|
r.time = r.time.replace("_", " ");
|
2017-08-20 17:06:44 +02:00
|
|
|
ret.append(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-07-12 20:12:17 +02:00
|
|
|
}
|