Mercurial > minori
annotate src/core/http.cc @ 198:bc1ae1810855
dep/animia: switch from using classes to global functions
the old idea was ok, but sort of hackish; this method doesn't use classes
at all, and this way (especially important!) we can do wayland stuff AND x11
at the same time, which wasn't really possible without stupid workarounds in
the other method
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 24 Dec 2023 02:59:42 -0500 |
| parents | 9b10175be389 |
| children | 53211cb1e7f5 |
| rev | line source |
|---|---|
| 75 | 1 #include "core/http.h" |
| 2 #include "core/session.h" | |
| 3 #include <QByteArray> | |
| 4 #include <curl/curl.h> | |
| 5 #include <string> | |
| 6 #include <vector> | |
| 175 | 7 #include <iostream> |
| 75 | 8 |
| 9 namespace HTTP { | |
| 10 | |
| 77 | 11 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { |
| 75 | 12 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); |
| 13 return size * nmemb; | |
| 14 } | |
| 15 | |
|
100
f5940a575d83
track/constants: add many more video formats
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
16 QByteArray Get(const std::string& url, const std::vector<std::string>& headers) { |
| 75 | 17 struct curl_slist* list = NULL; |
| 18 QByteArray userdata; | |
| 19 | |
| 20 CURL* curl = curl_easy_init(); | |
| 21 if (curl) { | |
| 77 | 22 for (const std::string& h : headers) { |
| 75 | 23 list = curl_slist_append(list, h.c_str()); |
| 24 } | |
| 77 | 25 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 26 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | |
| 75 | 27 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); |
| 28 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); | |
| 29 /* Use system certs... useful on Windows. */ | |
| 30 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | |
| 31 CURLcode res = curl_easy_perform(curl); | |
| 32 session.IncrementRequests(); | |
| 33 curl_easy_cleanup(curl); | |
| 175 | 34 if (res != CURLE_OK) |
| 35 std::cerr << "curl_easy_perform(curl) failed!: " << curl_easy_strerror(res) << std::endl; | |
| 75 | 36 } |
| 77 | 37 return userdata; |
| 75 | 38 } |
| 39 | |
|
100
f5940a575d83
track/constants: add many more video formats
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
40 QByteArray Post(const std::string& url, const std::string& data, const std::vector<std::string>& headers) { |
| 77 | 41 struct curl_slist* list = NULL; |
| 42 QByteArray userdata; | |
| 75 | 43 |
| 44 CURL* curl = curl_easy_init(); | |
| 45 if (curl) { | |
| 46 for (const std::string& h : headers) { | |
| 47 list = curl_slist_append(list, h.c_str()); | |
| 48 } | |
| 49 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | |
| 50 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | |
| 51 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | |
| 52 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | |
| 77 | 53 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); |
| 75 | 54 /* Use system certs... useful on Windows. */ |
| 55 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | |
| 56 CURLcode res = curl_easy_perform(curl); | |
| 57 session.IncrementRequests(); | |
| 58 curl_easy_cleanup(curl); | |
| 175 | 59 if (res != CURLE_OK) |
| 60 std::cerr << "curl_easy_perform(curl) failed!: " << curl_easy_strerror(res) << std::endl; | |
| 75 | 61 } |
| 77 | 62 return userdata; |
| 75 | 63 } |
| 64 | |
| 76 | 65 } // namespace HTTP |
