-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneralcurling.cpp
80 lines (75 loc) · 2.96 KB
/
generalcurling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef curling
#define curling
#include <curl/curl.h>
#include <QUrl>
#include <QtNetwork/QNetworkRequest>
#include <iostream>
#include <sstream>
#include <regex>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <string>
#include <QEventLoop>
#include <QSslError>
#include <QString>
#include "generalcurling.h"
std::string chatmedata="";
void qwrite_callback(const std::string &str, const std::string methodtype) {
if (methodtype == "PUT") return;
if (methodtype == "GET") {
chatmedata = str;
}
return;
}
void qurling::finishedqurling() {
std::cout << "FINISHED QURLING" << std::endl;
}
void qurling::qurlstuff(const std::string& port, const std::string& pass, const std::string& location, const std::string method, const short status, std::string* postdata) {
QList<QSslError> ignoreErrors;
ignoreErrors << QSslError(QSslError::SelfSignedCertificate) << QSslError(QSslError::CertificateUntrusted);
QUrl clientURL(QString("https://127.0.0.1:%1%2").arg(QString::fromStdString(port), QString::fromStdString(location)));
QNetworkRequest request(clientURL);
request.setRawHeader("Authorization", "Basic " + QString::fromStdString(pass).toLocal8Bit());
QNetworkAccessManager networkManager;
QNetworkReply *reply;
if (method == "GET") {
reply = networkManager.get(request);
} else if (method == "PUT") {
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
switch (status) {
case ONLINE:
if (chatmedata.find(":\"online") != std::string::npos) {
return;
}
chatmedata = std::regex_replace(chatmedata, std::regex("(.*)\"availability\":\".*?\"(.*)"), "$01\"availability\":\"chat\"$02");
break;
case OFFLINE:
if (chatmedata.find("offline") != std::string::npos) {
return;
}
chatmedata = std::regex_replace(chatmedata, std::regex("(.*)\"availability\":\".*?\"(.*)"), "$01\"availability\":\"offline\"$02");
break;
}
reply = networkManager.put(request, chatmedata.c_str());
} else {
reply = networkManager.post(request, QByteArrayLiteral(""));
}
reply->ignoreSslErrors();
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
qwrite_callback(reply->readAll().toStdString(), method);
/*if (reply->error() == QNetworkReply::NoError) {
qwrite_callback(reply->readAll().toStdString());
std::cout << reply->readAll().toStdString() << std::endl;
} else {
std::cout << reply->errorString().toStdString() << std::endl;
}*/
return;
}
void curlstuff(const std::string& port, const std::string& pass, const std::string& location, const std::string method, const short status, std::string* postdata) {
qurling qurlstuff;
qurlstuff.qurlstuff(port, pass, location, method, status, postdata);
return;
}
#endif