-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
576 lines (464 loc) · 19 KB
/
Copy pathhttp_client.cpp
File metadata and controls
576 lines (464 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "shepherd.h"
#include "http_client.h"
#include <sstream>
#include <cstring>
#include <fstream>
#include <atomic>
// External cancellation flag - set by CLI/TUI when user presses escape
extern std::atomic<bool> g_generation_cancelled;
HttpClient::HttpClient() {
curl_ = curl_easy_init();
if (!curl_) {
std::cerr << "Failed to initialize CURL for HttpClient" << std::endl;
}
multi_handle_ = curl_multi_init();
if (!multi_handle_) {
std::cerr << "Failed to initialize CURL multi handle for HttpClient" << std::endl;
}
dout(1) << "HttpClient initialized" << std::endl;
}
HttpClient::~HttpClient() {
if (multi_handle_) {
curl_multi_cleanup(multi_handle_);
multi_handle_ = nullptr;
}
if (curl_) {
curl_easy_cleanup(curl_);
curl_ = nullptr;
}
dout(1) << "HttpClient destroyed" << std::endl;
}
void HttpClient::set_timeout(long timeout_seconds) {
timeout_seconds_ = timeout_seconds;
dout(1) << "HttpClient timeout set to " + std::to_string(timeout_seconds) + " seconds" << std::endl;
}
void HttpClient::set_ssl_verify(bool verify) {
ssl_verify_ = verify;
dout(1) << "HttpClient SSL verify: " + std::string(verify ? "enabled" : "disabled") << std::endl;
}
void HttpClient::set_ca_bundle(const std::string& ca_bundle_path) {
ca_bundle_path_ = ca_bundle_path;
dout(1) << "HttpClient CA bundle: " + ca_bundle_path << std::endl;
}
void HttpClient::set_verbose(bool verbose) {
verbose_ = verbose;
dout(1) << "HttpClient verbose: " + std::string(verbose ? "enabled" : "disabled") << std::endl;
}
#ifdef ENABLE_API_BACKENDS
void HttpClient::configure_curl() {
if (!curl_) return;
// Reset to clean state
curl_easy_reset(curl_);
// Set timeout
if (timeout_seconds_ > 0) {
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, timeout_seconds_);
// Also set connect timeout to avoid hanging on connection
curl_easy_setopt(curl_, CURLOPT_CONNECTTIMEOUT, 30L);
}
// SSL options
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, ssl_verify_ ? 1L : 0L);
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, ssl_verify_ ? 2L : 0L);
if (!ca_bundle_path_.empty()) {
curl_easy_setopt(curl_, CURLOPT_CAINFO, ca_bundle_path_.c_str());
}
// Verbose output for debugging
if (verbose_) {
curl_easy_setopt(curl_, CURLOPT_VERBOSE, 1L);
}
// Default to HTTP when no scheme is specified in the URL
curl_easy_setopt(curl_, CURLOPT_DEFAULT_PROTOCOL, "http");
// Follow redirects
curl_easy_setopt(curl_, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_, CURLOPT_MAXREDIRS, 5L);
}
struct curl_slist* HttpClient::set_headers(const std::map<std::string, std::string>& headers) {
struct curl_slist* header_list = nullptr;
for (const auto& [key, value] : headers) {
std::string header = key + ": " + value;
header_list = curl_slist_append(header_list, header.c_str());
}
return header_list;
}
size_t HttpClient::write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
size_t total_size = size * nmemb;
auto* response = static_cast<HttpResponse*>(userdata);
response->body.append(ptr, total_size);
return total_size;
}
size_t HttpClient::header_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
size_t total_size = size * nmemb;
auto* response = static_cast<HttpResponse*>(userdata);
std::string header_line(ptr, total_size);
// Parse header line (format: "Key: Value\r\n")
size_t colon_pos = header_line.find(':');
if (colon_pos != std::string::npos) {
std::string key = header_line.substr(0, colon_pos);
std::string value = header_line.substr(colon_pos + 1);
// Trim whitespace
size_t start = value.find_first_not_of(" \t\r\n");
size_t end = value.find_last_not_of(" \t\r\n");
if (start != std::string::npos && end != std::string::npos) {
value = value.substr(start, end - start + 1);
}
response->headers[key] = value;
}
return total_size;
}
size_t HttpClient::stream_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
size_t total_size = size * nmemb;
auto* data = static_cast<StreamCallbackData*>(userdata);
if (!data->continue_streaming) {
return 0; // Abort transfer
}
std::string chunk(ptr, total_size);
// Accumulate body for error response parsing
if (data->body) {
*data->body += chunk;
}
// Call user callback
if (data->callback) {
data->continue_streaming = data->callback(chunk, data->user_data);
}
return data->continue_streaming ? total_size : 0;
}
#endif
HttpResponse HttpClient::get(const std::string& url,
const std::map<std::string, std::string>& headers) {
HttpResponse response;
#ifdef ENABLE_API_BACKENDS
if (!curl_) {
response.error_message = "CURL not initialized";
return response;
}
dout(1) << "HTTP GET: " + url << std::endl;
configure_curl();
// Set URL
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
// Set callbacks
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl_, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl_, CURLOPT_HEADERDATA, &response);
// Set headers
struct curl_slist* header_list = set_headers(headers);
if (header_list) {
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_list);
}
// Perform request
CURLcode res = curl_easy_perform(curl_);
// Get status code
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &response.status_code);
// Clean up
if (header_list) {
curl_slist_free_all(header_list);
}
if (res != CURLE_OK) {
response.error_message = curl_easy_strerror(res);
std::cerr << "HTTP GET failed: " + response.error_message << std::endl;
} else {
dout(1) << "HTTP GET completed with status: " + std::to_string(response.status_code) << std::endl;
}
#else
response.error_message = "API backends not compiled in";
std::cerr << "HttpClient::get() called but API backends not compiled in" << std::endl;
#endif
return response;
}
HttpResponse HttpClient::get_stream(const std::string& url,
const std::map<std::string, std::string>& headers,
StreamCallback callback,
void* user_data) {
HttpResponse response;
#ifdef ENABLE_API_BACKENDS
if (!curl_) {
response.error_message = "CURL not initialized";
return response;
}
dout(1) << "HTTP GET (streaming): " + url << std::endl;
configure_curl();
// Force HTTP/1.1 for streaming - HTTP/2 proxies often buffer SSE responses
curl_easy_setopt(curl_, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// Set URL
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
// Set streaming callback
StreamCallbackData callback_data;
callback_data.callback = callback;
callback_data.user_data = user_data;
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, stream_callback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &callback_data);
curl_easy_setopt(curl_, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl_, CURLOPT_HEADERDATA, &response);
// Set headers
struct curl_slist* header_list = set_headers(headers);
if (header_list) {
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_list);
}
// No timeout for SSE connections
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 0L);
// Perform request
CURLcode res = curl_easy_perform(curl_);
// Get status code
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &response.status_code);
// Clean up
if (header_list) {
curl_slist_free_all(header_list);
}
if (res != CURLE_OK) {
response.error_message = curl_easy_strerror(res);
// CURLE_WRITE_ERROR (23) happens when callback returns false - intentional cancellation
if (res == CURLE_WRITE_ERROR) {
dout(1) << "HTTP GET (streaming) stopped by callback" << std::endl;
} else {
std::cerr << "HTTP GET (streaming) failed: " + response.error_message << std::endl;
}
} else {
dout(1) << "HTTP GET (streaming) completed with status: " + std::to_string(response.status_code) << std::endl;
}
#else
response.error_message = "API backends not compiled in";
std::cerr << "HttpClient::get_stream() called but API backends not compiled in" << std::endl;
#endif
return response;
}
HttpResponse HttpClient::post(const std::string& url,
const std::string& body,
const std::map<std::string, std::string>& headers) {
HttpResponse response;
#ifdef ENABLE_API_BACKENDS
if (!curl_) {
response.error_message = "CURL not initialized";
return response;
}
dout(1) << "HTTP POST: " + url << std::endl;
dout(1) << "POST body length: " + std::to_string(body.length()) << std::endl;
#ifdef _DEBUG
// Dump full request body at high debug level
extern int g_debug_level;
if (g_debug_level >= 5 && body.length() < 50000) {
dout(1) << "POST body:\n" + body << std::endl;
}
#endif
configure_curl();
// Set URL
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
// Set POST
curl_easy_setopt(curl_, CURLOPT_POST, 1L);
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, body.length());
// Set callbacks
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl_, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl_, CURLOPT_HEADERDATA, &response);
// Set headers
struct curl_slist* header_list = set_headers(headers);
if (header_list) {
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_list);
}
// Perform request
CURLcode res = curl_easy_perform(curl_);
// Get status code
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &response.status_code);
// Clean up
if (header_list) {
curl_slist_free_all(header_list);
}
if (res != CURLE_OK) {
response.error_message = curl_easy_strerror(res);
std::cerr << "HTTP POST failed: " + response.error_message << std::endl;
} else {
dout(1) << "HTTP POST completed with status: " + std::to_string(response.status_code) << std::endl;
if (response.body.length() > 500) {
dout(1) << "Response body (first 500 chars): " + response.body.substr(0, 500) << std::endl;
dout(2) << "Full response body:\n" + response.body << std::endl;
} else {
dout(1) << "Response body: " + response.body << std::endl;
}
}
#else
response.error_message = "API backends not compiled in";
std::cerr << "HttpClient::post() called but API backends not compiled in" << std::endl;
#endif
return response;
}
HttpResponse HttpClient::post_stream(const std::string& url,
const std::string& body,
const std::map<std::string, std::string>& headers,
StreamCallback callback,
void* user_data) {
HttpResponse response;
#ifdef ENABLE_API_BACKENDS
if (!curl_) {
response.error_message = "CURL not initialized";
return response;
}
dout(1) << "HTTP POST (streaming): " + url << std::endl;
dout(1) << "POST body length: " + std::to_string(body.length()) << std::endl;
#ifdef _DEBUG
// Dump full request body at high debug level
extern int g_debug_level;
if (g_debug_level >= 5 && body.length() < 50000) {
dout(1) << "POST body:\n" + body << std::endl;
}
#endif
configure_curl();
// Force HTTP/1.1 for streaming - HTTP/2 proxies often buffer SSE responses
curl_easy_setopt(curl_, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// Set URL
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
// Set POST
curl_easy_setopt(curl_, CURLOPT_POST, 1L);
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, body.length());
// Set streaming callback
StreamCallbackData callback_data;
callback_data.callback = callback;
callback_data.user_data = user_data;
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, stream_callback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &callback_data);
curl_easy_setopt(curl_, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl_, CURLOPT_HEADERDATA, &response);
// Set headers
struct curl_slist* header_list = set_headers(headers);
if (header_list) {
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_list);
}
// Perform request
CURLcode res = curl_easy_perform(curl_);
// Get status code
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &response.status_code);
// Clean up
if (header_list) {
curl_slist_free_all(header_list);
}
if (res != CURLE_OK) {
response.error_message = curl_easy_strerror(res);
std::cerr << "HTTP POST (streaming) failed: " + response.error_message << std::endl;
} else {
dout(1) << "HTTP POST (streaming) completed with status: " + std::to_string(response.status_code) << std::endl;
}
#else
response.error_message = "API backends not compiled in";
std::cerr << "HttpClient::post_stream() called but API backends not compiled in" << std::endl;
#endif
return response;
}
HttpResponse HttpClient::post_stream_cancellable(const std::string& url,
const std::string& body,
const std::map<std::string, std::string>& headers,
StreamCallback callback,
void* user_data) {
HttpResponse response;
#ifdef ENABLE_API_BACKENDS
if (!curl_ || !multi_handle_) {
response.error_message = "CURL not initialized";
return response;
}
dout(1) << "HTTP POST (streaming cancellable): " + url << std::endl;
dout(1) << "POST body length: " + std::to_string(body.length()) << std::endl;
#ifdef _DEBUG
// Dump full request body at high debug level
extern int g_debug_level;
if (g_debug_level >= 5 && body.length() < 50000) {
dout(1) << "POST body:\n" + body << std::endl;
}
#endif
configure_curl();
// Force HTTP/1.1 for streaming - HTTP/2 proxies often buffer the entire
// SSE response before forwarding, which defeats token-by-token streaming
curl_easy_setopt(curl_, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// Set URL
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
// Set POST
curl_easy_setopt(curl_, CURLOPT_POST, 1L);
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, body.length());
// Set streaming callback
StreamCallbackData callback_data;
callback_data.callback = callback;
callback_data.user_data = user_data;
callback_data.body = &response.body; // Accumulate body for error parsing
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, stream_callback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &callback_data);
curl_easy_setopt(curl_, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl_, CURLOPT_HEADERDATA, &response);
// Set headers
struct curl_slist* header_list = set_headers(headers);
if (header_list) {
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_list);
}
// Remove handle from multi if still attached from a previous call (defensive)
curl_multi_remove_handle(multi_handle_, curl_);
// Add handle to multi interface
CURLMcode mres = curl_multi_add_handle(multi_handle_, curl_);
if (mres != CURLM_OK) {
response.error_message = "Failed to add handle to multi interface";
std::cerr << "curl_multi_add_handle failed: " + std::string(curl_multi_strerror(mres)) << std::endl;
if (header_list) {
curl_slist_free_all(header_list);
}
return response;
}
// Event loop with escape key checking
int still_running = 0;
bool cancelled = false;
CURLcode curl_result = CURLE_OK;
do {
// Perform transfers
mres = curl_multi_perform(multi_handle_, &still_running);
if (mres != CURLM_OK) {
std::cerr << "curl_multi_perform failed: " + std::string(curl_multi_strerror(mres)) << std::endl;
break;
}
// Check for cancellation (escape key sets g_generation_cancelled)
if (g_generation_cancelled) {
cancelled = true;
dout(1) << "HTTP streaming cancelled by user" << std::endl;
break;
}
// Wait for activity with short timeout (100ms for responsive cancellation)
if (still_running) {
mres = curl_multi_poll(multi_handle_, nullptr, 0, 100, nullptr);
if (mres != CURLM_OK) {
std::cerr << "curl_multi_poll failed: " + std::string(curl_multi_strerror(mres)) << std::endl;
break;
}
}
} while (still_running);
// Check for completion messages
int msgs_left = 0;
CURLMsg* msg = nullptr;
while ((msg = curl_multi_info_read(multi_handle_, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
curl_result = msg->data.result;
break;
}
}
// Get status code
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &response.status_code);
// Remove handle from multi interface
curl_multi_remove_handle(multi_handle_, curl_);
// Clean up headers
if (header_list) {
curl_slist_free_all(header_list);
}
// Set error message if cancelled or failed
if (cancelled) {
response.error_message = "Request cancelled by user";
response.status_code = 0;
dout(1) << "HTTP POST (streaming cancellable) cancelled" << std::endl;
} else if (curl_result == CURLE_WRITE_ERROR && !callback_data.continue_streaming) {
// Callback requested abort - this is normal completion, not an error
dout(1) << "HTTP POST (streaming cancellable) completed (callback stopped)" << std::endl;
} else if (curl_result != CURLE_OK) {
response.error_message = curl_easy_strerror(curl_result);
std::cerr << "HTTP POST (streaming cancellable) failed: " + response.error_message << std::endl;
} else {
dout(1) << "HTTP POST (streaming cancellable) completed with status: " + std::to_string(response.status_code) << std::endl;
}
#else
response.error_message = "API backends not compiled in";
std::cerr << "HttpClient::post_stream_cancellable() called but API backends not compiled in" << std::endl;
#endif
return response;
}