comparison src/core/http.cpp @ 75:d3e9310598b1

*: refactor some stuff text: "TextParagraph"s are now called sections, because that's the actual word for it :P text: new classes: Line and OneLineSection, solves many problems with paragraphs that are only one line long (ex. going out of bounds) http: reworked http stuff to allow threaded get requests, also moved it to its own file to (hopefully) remove clutter eventually I'll make a threaded post request method and use that in the "basic" function
author Paper <mrpapersonic@gmail.com>
date Wed, 04 Oct 2023 01:42:30 -0400
parents
children 3364fadc8a36
comparison
equal deleted inserted replaced
74:5ccb99bfa605 75:d3e9310598b1
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
84 }
85
86 #include "core/moc_http.cpp"