annotate src/core/http.cpp @ 76:3364fadc8a36

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