-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUrlRequest_Unix.cpp
More file actions
461 lines (408 loc) · 19 KB
/
Copy pathUrlRequest_Unix.cpp
File metadata and controls
461 lines (408 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "UrlRequest_Base.h"
#include <curl/curl.h>
#include <unistd.h>
#include <array>
#include <cstring>
#include <filesystem>
#include <cassert>
#include <sstream>
namespace
{
void curl_check(CURLcode code)
{
if (code != CURLE_OK)
{
throw std::runtime_error{(std::stringstream{} << "CURL call failed with code (" << code << ")").str()};
}
}
// Stable symbolic names for the CURLcodes most likely to be diagnostic in the field.
// Restricted to constants that have existed in libcurl for a long time so this compiles
// against older system curl headers; anything else gets a synthesized "CURLE_<n>" token.
std::string curl_error_symbol(CURLcode code)
{
switch (code)
{
case CURLE_UNSUPPORTED_PROTOCOL: return "CURLE_UNSUPPORTED_PROTOCOL";
case CURLE_URL_MALFORMAT: return "CURLE_URL_MALFORMAT";
case CURLE_COULDNT_RESOLVE_PROXY: return "CURLE_COULDNT_RESOLVE_PROXY";
case CURLE_COULDNT_RESOLVE_HOST: return "CURLE_COULDNT_RESOLVE_HOST";
case CURLE_COULDNT_CONNECT: return "CURLE_COULDNT_CONNECT";
case CURLE_REMOTE_ACCESS_DENIED: return "CURLE_REMOTE_ACCESS_DENIED";
case CURLE_PARTIAL_FILE: return "CURLE_PARTIAL_FILE";
case CURLE_HTTP_RETURNED_ERROR: return "CURLE_HTTP_RETURNED_ERROR";
case CURLE_WRITE_ERROR: return "CURLE_WRITE_ERROR";
case CURLE_UPLOAD_FAILED: return "CURLE_UPLOAD_FAILED";
case CURLE_READ_ERROR: return "CURLE_READ_ERROR";
case CURLE_OUT_OF_MEMORY: return "CURLE_OUT_OF_MEMORY";
case CURLE_OPERATION_TIMEDOUT: return "CURLE_OPERATION_TIMEDOUT";
case CURLE_RANGE_ERROR: return "CURLE_RANGE_ERROR";
case CURLE_HTTP_POST_ERROR: return "CURLE_HTTP_POST_ERROR";
case CURLE_SSL_CONNECT_ERROR: return "CURLE_SSL_CONNECT_ERROR";
case CURLE_BAD_DOWNLOAD_RESUME: return "CURLE_BAD_DOWNLOAD_RESUME";
case CURLE_FILE_COULDNT_READ_FILE: return "CURLE_FILE_COULDNT_READ_FILE";
case CURLE_TOO_MANY_REDIRECTS: return "CURLE_TOO_MANY_REDIRECTS";
case CURLE_GOT_NOTHING: return "CURLE_GOT_NOTHING";
case CURLE_SEND_ERROR: return "CURLE_SEND_ERROR";
case CURLE_RECV_ERROR: return "CURLE_RECV_ERROR";
case CURLE_SSL_CERTPROBLEM: return "CURLE_SSL_CERTPROBLEM";
case CURLE_SSL_CIPHER: return "CURLE_SSL_CIPHER";
case CURLE_PEER_FAILED_VERIFICATION: return "CURLE_PEER_FAILED_VERIFICATION";
case CURLE_BAD_CONTENT_ENCODING: return "CURLE_BAD_CONTENT_ENCODING";
case CURLE_SSL_CACERT_BADFILE: return "CURLE_SSL_CACERT_BADFILE";
case CURLE_REMOTE_FILE_NOT_FOUND: return "CURLE_REMOTE_FILE_NOT_FOUND";
case CURLE_ABORTED_BY_CALLBACK: return "CURLE_ABORTED_BY_CALLBACK";
default: return "CURLE_" + std::to_string(static_cast<int>(code));
}
}
void curl_check(CURLUcode code)
{
if (code != CURLUE_OK)
{
throw std::runtime_error{(std::stringstream{} << "CURLU call failed with code (" << code << ")").str()};
}
}
}
namespace UrlLib
{
class UrlRequest::Impl : public ImplBase
{
public:
~Impl()
{
Cleanup();
}
void Open(UrlMethod method, const std::string& url)
{
Cleanup();
ResetForOpen();
m_responseBuffer.clear();
m_method = method;
if (m_method == UrlMethod::Post)
{
// TODO: Implement POST
throw std::runtime_error{"Not implemented"};
}
m_curl = curl_easy_init();
if (m_curl)
{
m_curlu = curl_url();
if (!m_curlu)
{
throw std::runtime_error{"Out of memory"};
}
CURLUcode rc = curl_url_set(m_curlu, CURLUPART_URL, url.data(), CURLU_URLENCODE | CURLU_NON_SUPPORT_SCHEME | CURLU_ALLOW_SPACE);
if (rc != CURLUE_OK)
{
throw std::runtime_error{"Unable to build URL"};
}
char* scheme{};
auto schemeScope = gsl::finally([&scheme] { curl_free(scheme); });
if (curl_url_get(m_curlu, CURLUPART_SCHEME, &scheme, 0) == CURLUE_OK)
{
if (std::strcmp(scheme, "app") == 0)
{
curl_check(curl_url_set(m_curlu, CURLUPART_SCHEME, "file", 0));
char exe[1024];
int ret = readlink("/proc/self/exe", exe, std::size(exe) - 1);
if (ret == -1)
{
throw std::runtime_error{"Unable to get executable location"};
}
exe[ret] = 0;
char* host{};
auto hostScope = gsl::finally([&host] { curl_free(host); });
curl_check(curl_url_get(m_curlu, CURLUPART_HOST, &host, 0));
char* path{};
auto pathScope = gsl::finally([&path] { curl_free(path); });
curl_check(curl_url_get(m_curlu, CURLUPART_PATH, &path, 0));
auto newPath = std::filesystem::path{exe}.parent_path() / host / (path + 1);
curl_check(curl_url_set(m_curlu, CURLUPART_PATH, reinterpret_cast<const char*>(newPath.generic_u8string().data()), 0));
m_file = true;
}
else if (std::strcmp(scheme, "file") == 0)
{
m_file = true;
}
}
curl_check(curl_easy_setopt(m_curl, CURLOPT_CURLU, m_curlu));
curl_check(curl_easy_setopt(m_curl, CURLOPT_HEADERDATA, this));
curl_check(curl_easy_setopt(m_curl, CURLOPT_HEADERFUNCTION, HeaderCallback));
curl_check(curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, 1L));
// Request-specific failure detail (host/port/path specifics) lands here during
// the transfer; see the error handling in PerformAsync.
curl_check(curl_easy_setopt(m_curl, CURLOPT_ERRORBUFFER, m_curlErrorBuffer.data()));
// HTTP/2 and HTTP/3 are TLS-only, so only opt https:// transfers into a newer
// version; file://, app:// (rewritten to file above), and plain http:// keep
// libcurl's default. The version this build supports is detected at runtime so
// the same binary upgrades transparently with the system curl, and every value
// negotiates downward when the server or transport lacks support.
//
// The compile-time guards below key off LIBCURL_VERSION_NUM (a real macro from
// curlver.h) rather than the CURL_HTTP_VERSION_* constants: those are enum
// values, invisible to the preprocessor, so `#if defined(CURL_HTTP_VERSION_3)`
// would silently always be false. CURL_HTTP_VERSION_2TLS arrived in 7.47.0 and
// CURL_HTTP_VERSION_3 in 7.66.0.
if (scheme != nullptr && std::strcmp(scheme, "https") == 0)
{
const auto* versionInfo = curl_version_info(CURLVERSION_NOW);
long httpVersion = CURL_HTTP_VERSION_NONE;
#if LIBCURL_VERSION_NUM >= 0x074200 /* 7.66.0 */
// HTTP/3 with fallback. The runtime >= 8.0.0 gate matters because earlier
// curls treat CURL_HTTP_VERSION_3 as h3-or-fail instead of h3-with-fallback.
if ((versionInfo->features & CURL_VERSION_HTTP3) && versionInfo->version_num >= 0x080000)
{
httpVersion = CURL_HTTP_VERSION_3;
}
else
#endif
#if LIBCURL_VERSION_NUM >= 0x072F00 /* 7.47.0 */
if (versionInfo->features & CURL_VERSION_HTTP2)
{
// HTTP/2 over TLS via ALPN, HTTP/1.1 otherwise.
httpVersion = CURL_HTTP_VERSION_2TLS;
}
#endif
if (httpVersion != CURL_HTTP_VERSION_NONE)
{
curl_check(curl_easy_setopt(m_curl, CURLOPT_HTTP_VERSION, httpVersion));
}
}
}
}
arcana::task<void, std::exception_ptr> SendAsync()
{
switch (m_responseType)
{
case UrlResponseType::String:
{
return PerformAsync(m_responseString);
}
case UrlResponseType::Buffer:
{
return PerformAsync(m_responseBuffer);
}
}
throw std::runtime_error{"Invalid response type"};
}
gsl::span<const std::byte> ResponseBuffer() const
{
return m_responseBuffer;
}
private:
void Cleanup()
{
if (m_thread.has_value())
{
m_thread->join();
m_thread = {};
}
if (m_curlu)
{
curl_url_cleanup(m_curlu);
m_curlu = nullptr;
}
if (m_curl)
{
curl_easy_cleanup(m_curl);
m_curl = nullptr;
}
}
static void Append(std::string& string, char* buffer, size_t nitems)
{
string.insert(string.end(), buffer, buffer + nitems);
}
static void Append(std::vector<std::byte>& byteVector, char* buffer, size_t nitems)
{
auto bytes = reinterpret_cast<std::byte*>(buffer);
byteVector.insert(byteVector.end(), bytes, bytes + nitems);
}
// Drives the transfer through the libcurl *multi* interface rather than curl_easy_perform so
// that Abort() is observed promptly. curl_easy_perform blocks in an internal poll that, when
// a peer accepts the connection but sends nothing, can wait indefinitely without invoking any
// callback -- so a cancelled request would hang until the peer or OS gave up. Polling with a
// bounded timeout lets the loop re-check m_cancellationSource between waits, bounding abort
// latency to ~kPollTimeoutMs regardless of peer activity. Runs on the worker thread.
CURLcode PerformWithCancellation()
{
CURLM* multi = curl_multi_init();
if (multi == nullptr)
{
return CURLE_OUT_OF_MEMORY;
}
auto multiScope = gsl::finally([this, multi] {
curl_multi_remove_handle(multi, m_curl);
curl_multi_cleanup(multi);
});
if (curl_multi_add_handle(multi, m_curl) != CURLM_OK)
{
return CURLE_FAILED_INIT;
}
constexpr int kPollTimeoutMs = 100;
int runningHandles = 0;
do
{
if (m_cancellationSource.cancelled())
{
return CURLE_ABORTED_BY_CALLBACK;
}
if (curl_multi_perform(multi, &runningHandles) != CURLM_OK)
{
return CURLE_RECV_ERROR;
}
// Wait for socket activity but wake at least every kPollTimeoutMs so the cancellation
// check above runs even when the peer is idle (curl_multi_poll waits the full timeout
// even when there are no fds, so this never busy-loops during DNS resolution).
if (runningHandles != 0 && curl_multi_poll(multi, nullptr, 0, kPollTimeoutMs, nullptr) != CURLM_OK)
{
return CURLE_RECV_ERROR;
}
} while (runningHandles != 0);
// The transfer finished; surface the per-easy-handle result code.
CURLcode result = CURLE_OK;
int messagesInQueue = 0;
while (CURLMsg* message = curl_multi_info_read(multi, &messagesInQueue))
{
if (message->msg == CURLMSG_DONE && message->easy_handle == m_curl)
{
result = message->data.result;
}
}
return result;
}
template<typename DataT>
arcana::task<void, std::exception_ptr> PerformAsync(DataT& data)
{
data.clear();
curl_write_callback callback = [](char* buffer, size_t /*size*/, size_t nitems, void* userData) {
auto& data = *static_cast<DataT*>(userData);
Append(data, buffer, nitems);
return nitems;
};
curl_check(curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, callback));
curl_check(curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &data));
arcana::task_completion_source<void, std::exception_ptr> taskCompletionSource{};
m_thread.emplace([this, taskCompletionSource]() mutable
{
m_curlErrorBuffer[0] = '\0';
const CURLcode performResult = PerformWithCancellation();
if (performResult != CURLE_OK)
{
// Retain the default status code of 0 to indicate a client side error,
// matching the convention of UrlRequest_UWP.cpp's catch(winrt::hresult_error)
// and UrlRequest_Apple.mm's error branch -- but record what actually went
// wrong so consumers can surface it. Prefer libcurl's per-request
// CURLOPT_ERRORBUFFER detail (it includes host/port/path specifics, e.g.
// "Failed to connect to 127.0.0.1 port 47651 ...") over the generic
// curl_easy_strerror text.
const char* detail = m_curlErrorBuffer[0] != '\0' ? m_curlErrorBuffer.data() : curl_easy_strerror(performResult);
SetError("curl", curl_error_symbol(performResult), static_cast<int32_t>(performResult), detail);
}
else
{
try
{
long codep{};
curl_check(curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &codep));
if (codep == 0 && m_file)
{
// File scheme always returns 0
m_statusCode = UrlStatusCode::Ok;
}
else
{
m_statusCode = static_cast<UrlStatusCode>(codep);
}
}
catch (const std::exception& e)
{
// Keep status 0 and record why. Without this catch the exception would
// escape this std::thread and call std::terminate. "GetInfoFailed" is not
// a real CURLcode, so it deliberately omits the "CURLE_" prefix to avoid
// implying libcurl produced this code.
SetError("curl", "GetInfoFailed", -1, e.what());
}
}
taskCompletionSource.complete();
});
return taskCompletionSource.as_task();
}
static size_t HeaderCallback(char* buffer, size_t size, size_t nitems, void* userdata)
{
// libcurl passes the header bytes as `size` chunks of `nitems`; the valid length is
// their product. (curl currently always uses size == 1 for headers, but don't rely on it.)
const size_t length = size * nitems;
if (length > 0)
{
char* bufferEnd = buffer + length;
// Status line: "HTTP/<version> <code>[ <reason>]\r\n". HTTP/1.x carries a reason
// phrase; HTTP/2+ omits it. curl delivers a fresh status line for every response
// in the chain (e.g. each redirect hop), so reset and let the final one win.
if (length >= 5 && std::strncmp(buffer, "HTTP/", 5) == 0)
{
auto& impl = *static_cast<Impl*>(userdata);
impl.m_statusText.clear();
// Skip "HTTP/<version>" then the spaces and the numeric status code.
char* cursor = buffer;
for (; cursor < bufferEnd && *cursor != ' '; ++cursor) {} // end of version token
for (; cursor < bufferEnd && *cursor == ' '; ++cursor) {} // spaces before code
for (; cursor < bufferEnd && *cursor != ' '; ++cursor) {} // status code digits
for (; cursor < bufferEnd && *cursor == ' '; ++cursor) {} // spaces before reason
char* reasonEnd = bufferEnd;
for (; reasonEnd - 1 >= cursor; --reasonEnd)
{
auto ch = *(reasonEnd - 1);
if (ch != '\r' && ch != '\n' && ch != ' ')
{
break;
}
}
if (cursor < reasonEnd)
{
impl.m_statusText.assign(cursor, reasonEnd);
}
return nitems * size;
}
char* keyStart = buffer;
char* keyEnd = keyStart;
for (; keyEnd < bufferEnd; ++keyEnd)
{
if (*keyEnd == ':')
{
break;
}
}
if (keyEnd != bufferEnd)
{
char* valueStart = keyEnd + 1;
for (; valueStart < bufferEnd; ++valueStart)
{
if (*valueStart != ' ')
{
break;
}
}
char* valueEnd = bufferEnd;
for (; valueEnd - 1 >= valueStart; --valueEnd)
{
auto ch = *(valueEnd - 1);
if (ch != '\r' && ch != '\n' && ch != ' ')
{
break;
}
}
std::string key{keyStart, keyEnd};
std::string value{valueStart, valueEnd};
static_cast<Impl*>(userdata)->m_headers.insert({std::move(key), std::move(value)});
}
}
return nitems * size;
}
std::vector<std::byte> m_responseBuffer{};
CURL* m_curl{};
CURLU* m_curlu{};
bool m_file{};
std::array<char, CURL_ERROR_SIZE> m_curlErrorBuffer{};
std::optional<std::thread> m_thread{};
};
}
#include "UrlRequest_Shared.h"