annotate src/core/http.cc @ 407:2ae34a90f8d4

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