annotate src/core/http.cc @ 408:9323153786dc default tip

http: fix build fail under non-mac
author Paper <paper@tflc.us>
date Wed, 21 Jan 2026 11:35:32 -0500
parents 2ae34a90f8d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
1 #include "core/http.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
2 #include "core/session.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
3 #include <QByteArray>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
4 #include <curl/curl.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
5 #include <iostream>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
6 #include <string>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
7 #include <vector>
408
9323153786dc http: fix build fail under non-mac
Paper <paper@tflc.us>
parents: 407
diff changeset
8 #include <cstring>
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
9
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
10 namespace HTTP {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
11
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
12 std::string UrlEncode(const std::string &data)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
13 {
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
14 std::string res;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
15 std::size_t sz = data.size();
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
16 std::size_t i;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
17
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
18 // output string will always be at least data.size()
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
19 // so reserve that much space beforehand
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
20 res.reserve(sz);
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
21
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
22 for (i = 0; i < sz; i++) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
23 // This works correctly for UTF-8 because of the way
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
24 // the data is laid out.
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
25 static const char lut[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~*'()";
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
26 unsigned char c = data[i];
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
27
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
28 if (std::memchr(lut, c, sizeof(lut) - 1)) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
29 res.push_back(c);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
30 } else {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
31 static const char lut[] = "0123456789ABCDEF";
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
32 res.push_back('%');
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
33 res.push_back(lut[c >> 4]);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
34 res.push_back(lut[c & 15]);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
35 }
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
36 }
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
37
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
38 return res;
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
39 }
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
40
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
41 // NOTE: This function is not guaranteed to return
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
42 // UTF-8, or even text at all.
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
43 std::string UrlDecode(const std::string &data)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
44 {
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
45 std::string res;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
46
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
47 const char *ptr = data.data();
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
48 std::size_t len = data.size();
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
49
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
50 // reserve space beforehand
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
51 res.reserve(len);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
52
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
53 while (len > 0) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
54 // find the next percent character
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
55 // there's probably a better way to do this!
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
56 const char *next = reinterpret_cast<const char *>(std::memchr(reinterpret_cast<const void *>(ptr), '%', len));
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
57
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
58 if (next) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
59 res.insert(res.end(), ptr, next);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
60 len = (next - ptr);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
61 ptr = next;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
62
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
63 // now process the two hex chars
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
64 if (len >= 3) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
65 unsigned char hi, lo;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
66
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
67 auto hex_char_to_value = [](unsigned char x, unsigned char &v) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
68 if (x >= 'A' && x <= 'F') {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
69 v = x - 'A' + 0xA;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
70 return true;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
71 }
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
72
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
73 if (x >= 'a' && x <= 'f') {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
74 v = x - 'a' + 0xA;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
75 return true;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
76 }
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
77
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
78 if (x >= '0' && x <= '9') {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
79 v = x - '0';
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
80 return true;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
81 }
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
82
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
83 return false;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
84 };
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
85
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
86 if (hex_char_to_value(ptr[1], hi) && hex_char_to_value(ptr[2], lo)) {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
87 ptr += 3;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
88 len -= 3;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
89 } else {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
90 len = 0;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
91 }
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
92 } else {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
93 // uh oh
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
94 len = 0;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
95 }
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
96 } else {
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
97 res.insert(res.end(), ptr, ptr + len);
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
98 //not needed: ptr += len;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
99 len = 0;
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
100 }
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
101 }
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
102
407
2ae34a90f8d4 http: roll our own URL encode/decode functions
Paper <paper@tflc.us>
parents: 397
diff changeset
103 return res;
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
104 }
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
105
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
106 std::string EncodeParamsList(std::string base, const std::map<std::string, std::string> &params)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
107 {
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
108 std::size_t count = 0;
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
109 for (const auto &param : params) {
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
110 base += (!count ? "?" : "&");
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
111 base += UrlEncode(param.first);
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
112 base += "=";
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
113 base += UrlEncode(param.second);
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
114
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
115 count++;
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
116 }
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
117
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
118 return base;
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
119 }
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
120
393
963047512d34 *: clang-format
Paper <paper@tflc.us>
parents: 390
diff changeset
121 static void SetCurlOpts(CURL *curl, const std::string &url, const std::vector<std::string> &headers,
963047512d34 *: clang-format
Paper <paper@tflc.us>
parents: 390
diff changeset
122 const std::string &data, Type type)
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
123 {
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
124 struct curl_slist *list;
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
125
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
126 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
127
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
128 list = NULL;
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
129 for (const std::string &h : headers)
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
130 list = curl_slist_append(list, h.c_str());
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
131 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
132
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
133 switch (type) {
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
134 case Type::Patch:
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
135 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
136 [[fallthrough]];
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
137 case Type::Post:
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
138 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
139 break;
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
140 case Type::Get:
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
141 break;
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
142 }
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
143
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
144 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
145 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); // threading
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
146 }
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
147
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
148 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userdata)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
149 {
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
150 reinterpret_cast<QByteArray *>(userdata)->append(reinterpret_cast<char *>(contents), size * nmemb);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
151 return size * nmemb;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
152 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
153
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
154 QByteArray Request(const std::string &url, const std::vector<std::string> &headers, const std::string &data, Type type)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
155 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
156 QByteArray userdata;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
157
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
158 CURL *curl = curl_easy_init();
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
159 if (curl) {
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
160 SetCurlOpts(curl, url, headers, data, type);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
161
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
162 /* Use our specific userdata & write callback
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
163 * TODO can this just be a lambda instead */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
164 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
165 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
166
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
167 CURLcode res = curl_easy_perform(curl);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
168 session.IncrementRequests();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
169 curl_easy_cleanup(curl);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
170 if (res != CURLE_OK)
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
171 session.SetStatusBar(std::string("curl_easy_perform(curl) failed!: ") + curl_easy_strerror(res));
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
172 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
173 return userdata;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
174 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
175
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
176 /* this function is static */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
177 size_t RequestThread::WriteCallback(void *contents, size_t size, size_t nmemb, void *userdata)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
178 {
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
179 RequestThread *thread = reinterpret_cast<RequestThread *>(userdata);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
180
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
181 if (thread->cancelled_)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
182 return CURL_WRITEFUNC_ERROR;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
183
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
184 /* else, continue on as normal */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
185 thread->array_.append(reinterpret_cast<char *>(contents), size * nmemb);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
186 return size * nmemb;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
187 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
188
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
189 RequestThread::RequestThread(Type type, QObject *parent) : QThread(parent)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
190 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
191 SetType(type);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
192 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
193
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
194 RequestThread::RequestThread(const std::string &url, const std::vector<std::string> &headers, const std::string &data,
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
195 Type type, QObject *parent)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
196 : QThread(parent)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
197 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
198 SetUrl(url);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
199 SetData(data);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
200 SetHeaders(headers);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
201 SetType(type);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
202 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
203
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
204 RequestThread::~RequestThread()
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
205 {
319
d928ec7b6a0d services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents: 317
diff changeset
206 /* block until the function can safely exit */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
207 Stop();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
208 wait();
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
209
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
210 /* Kill off any curl thing we made */
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
211 if (curl_)
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
212 curl_easy_cleanup(reinterpret_cast<CURL *>(curl_));
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
213 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
214
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
215 void RequestThread::SetUrl(const std::string &url)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
216 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
217 url_ = url;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
218 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
219
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
220 void RequestThread::SetHeaders(const std::vector<std::string> &headers)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
221 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
222 headers_ = headers;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
223 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
224
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
225 void RequestThread::SetData(const std::string &data)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
226 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
227 data_ = data;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
228 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
229
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
230 void RequestThread::SetType(Type type)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
231 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
232 type_ = type;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
233 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
234
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
235 void RequestThread::run()
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
236 {
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
237 /* If we don't have a curl object, create one */
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
238 if (!curl_) {
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
239 curl_ = reinterpret_cast<void *>(curl_easy_init());
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
240 }
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
241
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
242 CURL *curl = reinterpret_cast<CURL *>(curl_);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
243
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
244 if (curl) {
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
245 SetCurlOpts(curl, url_, headers_, data_, type_);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
246
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
247 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
248 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &RequestThread::WriteCallback);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
249
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
250 CURLcode res = curl_easy_perform(curl);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
251 session.IncrementRequests();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
252
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
253 if (res != CURLE_OK && !(res == CURLE_WRITE_ERROR && cancelled_))
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
254 session.SetStatusBar(std::string("curl_easy_perform(curl) failed!: ") + curl_easy_strerror(res));
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
255 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
256
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
257 emit ReceivedData(array_);
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
258 /* Clear it out for any subsequent runs */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
259 array_.clear();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
260 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
261
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
262 void RequestThread::Stop()
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 319
diff changeset
263 {
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
264 cancelled_ = true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
265 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
266
397
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
267 void Init()
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
268 {
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
269 curl_global_init(CURL_GLOBAL_ALL);
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
270 }
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
271
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
272 void Quit()
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
273 {
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
274 curl_global_cleanup();
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
275 }
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 396
diff changeset
276
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
277 } // namespace HTTP