75
|
1 #include "core/http.h"
|
|
2 #include "core/session.h"
|
|
3 #include <QByteArray>
|
|
4 #include <QMessageBox>
|
|
5 #include <QThread>
|
|
6 #include <curl/curl.h>
|
|
7 #include <string>
|
|
8 #include <vector>
|
|
9
|
|
10 namespace HTTP {
|
|
11
|
|
12 HttpGetThread::HttpGetThread(std::string url, std::vector<std::string> headers, QObject* parent) : QThread(parent) {
|
|
13 _url = url;
|
|
14 _headers = headers;
|
|
15 }
|
|
16
|
|
17 size_t HttpGetThread::WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
|
|
18 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb);
|
|
19 return size * nmemb;
|
|
20 }
|
|
21
|
|
22 void HttpGetThread::run() {
|
|
23 struct curl_slist* list = NULL;
|
|
24 QByteArray userdata;
|
|
25
|
|
26 CURL* curl = curl_easy_init();
|
|
27 if (curl) {
|
|
28 for (const std::string& h : _headers) {
|
|
29 list = curl_slist_append(list, h.c_str());
|
|
30 }
|
|
31 curl_easy_setopt(curl, CURLOPT_URL, _url.c_str());
|
|
32 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
|
|
33 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
|
|
34 /* Use system certs... useful on Windows. */
|
|
35 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
|
|
36 CURLcode res = curl_easy_perform(curl);
|
|
37 session.IncrementRequests();
|
|
38 curl_easy_cleanup(curl);
|
|
39 if (res != CURLE_OK) {
|
|
40 QMessageBox box(QMessageBox::Icon::Critical, "",
|
|
41 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
|
|
42 box.exec();
|
|
43 }
|
|
44 }
|
|
45 emit resultReady(userdata);
|
|
46 }
|
|
47
|
|
48 static size_t CurlWriteStringCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
|
|
49 reinterpret_cast<std::string*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb);
|
|
50 return size * nmemb;
|
|
51 }
|
|
52
|
|
53 /* Performs a basic (blocking) post request */
|
|
54 std::string PerformBasicPostRequest(std::string url, std::string data, std::vector<std::string> headers) {
|
|
55 struct curl_slist* list = NULL;
|
|
56 std::string userdata;
|
|
57 CURL* curl = curl_easy_init();
|
|
58 if (curl) {
|
|
59 for (const std::string& h : headers) {
|
|
60 list = curl_slist_append(list, h.c_str());
|
|
61 }
|
|
62 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
63 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
|
64 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
|
65 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
|
|
66 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteStringCallback);
|
|
67 /* Use system certs... useful on Windows. */
|
|
68 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
|
|
69 CURLcode res = curl_easy_perform(curl);
|
|
70 session.IncrementRequests();
|
|
71 curl_slist_free_all(list);
|
|
72 curl_easy_cleanup(curl);
|
|
73 if (res != CURLE_OK) {
|
|
74 QMessageBox box(QMessageBox::Icon::Critical, "",
|
|
75 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
|
|
76 box.exec();
|
|
77 return "";
|
|
78 }
|
|
79 return userdata;
|
|
80 }
|
|
81 return "";
|
|
82 }
|
|
83
|
76
|
84 } // namespace HTTP
|
75
|
85
|
|
86 #include "core/moc_http.cpp"
|