Mercurial > minori
comparison src/core/http.cpp @ 77:6f7385bd334c
*: update
formatted all source files, no more subclassing QThread... many other
changes :)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 06 Oct 2023 06:18:53 -0400 |
parents | 3364fadc8a36 |
children |
comparison
equal
deleted
inserted
replaced
76:3364fadc8a36 | 77:6f7385bd334c |
---|---|
1 #include "core/http.h" | 1 #include "core/http.h" |
2 #include "core/session.h" | 2 #include "core/session.h" |
3 #include <QByteArray> | 3 #include <QByteArray> |
4 #include <QMessageBox> | 4 #include <QMessageBox> |
5 #include <QThread> | |
6 #include <curl/curl.h> | 5 #include <curl/curl.h> |
7 #include <string> | 6 #include <string> |
8 #include <vector> | 7 #include <vector> |
9 | 8 |
10 namespace HTTP { | 9 namespace HTTP { |
11 | 10 |
12 HttpGetThread::HttpGetThread(std::string url, std::vector<std::string> headers, QObject* parent) : QThread(parent) { | 11 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { |
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); | 12 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); |
19 return size * nmemb; | 13 return size * nmemb; |
20 } | 14 } |
21 | 15 |
22 void HttpGetThread::run() { | 16 QByteArray Get(std::string url, std::vector<std::string> headers) { |
23 struct curl_slist* list = NULL; | 17 struct curl_slist* list = NULL; |
24 QByteArray userdata; | 18 QByteArray userdata; |
25 | 19 |
26 CURL* curl = curl_easy_init(); | 20 CURL* curl = curl_easy_init(); |
27 if (curl) { | 21 if (curl) { |
28 for (const std::string& h : _headers) { | 22 for (const std::string& h : headers) { |
29 list = curl_slist_append(list, h.c_str()); | 23 list = curl_slist_append(list, h.c_str()); |
30 } | 24 } |
31 curl_easy_setopt(curl, CURLOPT_URL, _url.c_str()); | 25 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
26 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | |
32 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | 27 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); |
33 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); | 28 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); |
34 /* Use system certs... useful on Windows. */ | 29 /* Use system certs... useful on Windows. */ |
35 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | 30 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); |
36 CURLcode res = curl_easy_perform(curl); | 31 CURLcode res = curl_easy_perform(curl); |
40 QMessageBox box(QMessageBox::Icon::Critical, "", | 35 QMessageBox box(QMessageBox::Icon::Critical, "", |
41 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); | 36 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); |
42 box.exec(); | 37 box.exec(); |
43 } | 38 } |
44 } | 39 } |
45 emit resultReady(userdata); | 40 return userdata; |
46 } | 41 } |
47 | 42 |
48 static size_t CurlWriteStringCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | 43 QByteArray Post(std::string url, std::string data, std::vector<std::string> headers) { |
49 reinterpret_cast<std::string*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); | 44 struct curl_slist* list = NULL; |
50 return size * nmemb; | 45 QByteArray userdata; |
51 } | |
52 | 46 |
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(); | 47 CURL* curl = curl_easy_init(); |
58 if (curl) { | 48 if (curl) { |
59 for (const std::string& h : headers) { | 49 for (const std::string& h : headers) { |
60 list = curl_slist_append(list, h.c_str()); | 50 list = curl_slist_append(list, h.c_str()); |
61 } | 51 } |
62 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | 52 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
63 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | 53 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); |
64 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | 54 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); |
65 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | 55 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); |
66 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteStringCallback); | 56 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); |
67 /* Use system certs... useful on Windows. */ | 57 /* Use system certs... useful on Windows. */ |
68 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | 58 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); |
69 CURLcode res = curl_easy_perform(curl); | 59 CURLcode res = curl_easy_perform(curl); |
70 session.IncrementRequests(); | 60 session.IncrementRequests(); |
71 curl_slist_free_all(list); | |
72 curl_easy_cleanup(curl); | 61 curl_easy_cleanup(curl); |
73 if (res != CURLE_OK) { | 62 if (res != CURLE_OK) { |
74 QMessageBox box(QMessageBox::Icon::Critical, "", | 63 QMessageBox box(QMessageBox::Icon::Critical, "", |
75 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); | 64 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); |
76 box.exec(); | 65 box.exec(); |
77 return ""; | |
78 } | 66 } |
79 return userdata; | |
80 } | 67 } |
81 return ""; | 68 return userdata; |
82 } | 69 } |
83 | 70 |
84 } // namespace HTTP | 71 } // namespace HTTP |
85 | |
86 #include "core/moc_http.cpp" |