-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_http_client.cpp
More file actions
429 lines (383 loc) · 9.89 KB
/
Copy pathsync_http_client.cpp
File metadata and controls
429 lines (383 loc) · 9.89 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
#include "tools/sync_http_client.hpp"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <sstream>
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include <winsock2.h>
#include <winhttp.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winhttp.lib")
using NativeSocket = SOCKET;
constexpr NativeSocket kInvalidNativeSocket = INVALID_SOCKET;
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
using NativeSocket = int;
constexpr NativeSocket kInvalidNativeSocket = -1;
#endif
namespace droidcli::tools {
namespace {
struct ParsedHttpUrl {
core::String host;
core::String path = "/";
int32_t port = 80;
bool is_https = false;
bool valid = false;
};
ParsedHttpUrl parse_http_url(const core::String& url)
{
ParsedHttpUrl parsed;
core::String scheme_prefix = "http://";
if (url.rfind("https://", 0) == 0)
{
scheme_prefix = "https://";
parsed.is_https = true;
parsed.port = 443;
}
else if (url.rfind(scheme_prefix, 0) != 0)
{
return parsed;
}
core::String remainder = url.substr(scheme_prefix.size());
const size_t path_index = remainder.find('/');
if (path_index != core::String::npos)
{
parsed.path = remainder.substr(path_index);
remainder = remainder.substr(0, path_index);
}
const size_t port_index = remainder.find(':');
if (port_index != core::String::npos)
{
parsed.host = remainder.substr(0, port_index);
parsed.port = std::max(1, std::atoi(remainder.substr(port_index + 1).c_str()));
}
else
{
parsed.host = remainder;
}
parsed.valid = !parsed.host.empty();
return parsed;
}
#if defined(_WIN32)
// ASCII-only widening: adequate for hostnames and already percent-encoded
// paths/query strings (the only things this function ever needs to widen).
std::wstring widen_ascii(const core::String& value)
{
std::wstring wide;
wide.reserve(value.size());
for (const char character : value)
{
wide.push_back(static_cast<wchar_t>(static_cast<unsigned char>(character)));
}
return wide;
}
// HTTPS transport via WinHTTP (handles the TLS handshake natively - no
// OpenSSL/Schannel code of our own). The plain-HTTP path below (raw sockets)
// is untouched and still used for local peers (Ollama, connectors on
// 127.0.0.1); this path exists for any https:// connector or external API
// with no plain-HTTP fallback.
bool https_request_winhttp(
const core::String& method,
const ParsedHttpUrl& parsed,
const core::String& body,
int32_t& status_code_out,
core::String& response_body_out,
const core::Array<core::String>& extra_headers)
{
bool ok = false;
const HINTERNET session = WinHttpOpen(
L"droidcli/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
if (!session)
{
return false;
}
const HINTERNET connect = WinHttpConnect(
session,
widen_ascii(parsed.host).c_str(),
static_cast<INTERNET_PORT>(parsed.port),
0);
if (!connect)
{
WinHttpCloseHandle(session);
return false;
}
const HINTERNET request = WinHttpOpenRequest(
connect,
widen_ascii(method).c_str(),
widen_ascii(parsed.path).c_str(),
nullptr,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
if (!request)
{
WinHttpCloseHandle(connect);
WinHttpCloseHandle(session);
return false;
}
std::wstring header_block;
if (method == "POST")
{
header_block += L"Content-Type: application/json\r\n";
}
for (const core::String& extra_header : extra_headers)
{
header_block += widen_ascii(extra_header) + L"\r\n";
}
const wchar_t* headers = header_block.empty() ? nullptr : header_block.c_str();
const BOOL sent = WinHttpSendRequest(
request,
headers,
headers ? static_cast<DWORD>(-1) : 0,
body.empty() ? WINHTTP_NO_REQUEST_DATA : const_cast<char*>(body.data()),
static_cast<DWORD>(body.size()),
static_cast<DWORD>(body.size()),
0);
if (sent && WinHttpReceiveResponse(request, nullptr))
{
DWORD status_code = 0;
DWORD status_size = sizeof(status_code);
WinHttpQueryHeaders(
request,
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX,
&status_code,
&status_size,
WINHTTP_NO_HEADER_INDEX);
status_code_out = static_cast<int32_t>(status_code);
core::String body_accum;
for (;;)
{
DWORD available = 0;
if (!WinHttpQueryDataAvailable(request, &available) || available == 0)
{
break;
}
core::Array<char> chunk(available);
DWORD read = 0;
if (!WinHttpReadData(request, chunk.data(), available, &read) || read == 0)
{
break;
}
body_accum.append(chunk.data(), static_cast<size_t>(read));
}
response_body_out = std::move(body_accum);
ok = true;
}
WinHttpCloseHandle(request);
WinHttpCloseHandle(connect);
WinHttpCloseHandle(session);
return ok;
}
#endif // _WIN32
bool ensure_socket_library()
{
#if defined(_WIN32)
static bool initialized = false;
if (!initialized)
{
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
{
return false;
}
initialized = true;
}
#endif
return true;
}
void close_socket(const NativeSocket socket_handle)
{
#if defined(_WIN32)
closesocket(socket_handle);
#else
close(socket_handle);
#endif
}
bool read_http_response(const NativeSocket socket_handle, int32_t& status_code_out, core::String& body_out)
{
core::String buffer;
char chunk[2048];
while (buffer.find("\r\n\r\n") == core::String::npos)
{
const int received = recv(socket_handle, chunk, sizeof(chunk), 0);
if (received <= 0)
{
return false;
}
buffer.append(chunk, static_cast<size_t>(received));
if (buffer.size() > 1024 * 1024)
{
return false;
}
}
const size_t header_end = buffer.find("\r\n\r\n");
const core::String header_block = buffer.substr(0, header_end);
body_out = buffer.substr(header_end + 4);
const size_t status_index = header_block.find(' ');
if (status_index != core::String::npos)
{
status_code_out = std::atoi(header_block.c_str() + static_cast<int>(status_index + 1));
}
int content_length = -1;
std::istringstream header_stream(header_block);
core::String line;
while (std::getline(header_stream, line))
{
if (!line.empty() && line.back() == '\r')
{
line.pop_back();
}
core::String lower = line;
for (char& character : lower)
{
character = static_cast<char>(std::tolower(static_cast<unsigned char>(character)));
}
if (lower.rfind("content-length:", 0) == 0)
{
content_length = std::atoi(line.substr(15).c_str());
}
}
while (content_length >= 0 && static_cast<int>(body_out.size()) < content_length)
{
const int received = recv(socket_handle, chunk, sizeof(chunk), 0);
if (received <= 0)
{
break;
}
body_out.append(chunk, static_cast<size_t>(received));
}
if (content_length >= 0)
{
body_out = body_out.substr(0, static_cast<size_t>(content_length));
}
return true;
}
} // namespace
bool sync_http_request(
const core::String& method,
const core::String& url,
const core::String& body,
int32_t& status_code_out,
core::String& response_body_out,
const core::Array<core::String>& extra_headers)
{
status_code_out = 0;
response_body_out.clear();
const ParsedHttpUrl parsed = parse_http_url(url);
if (!parsed.valid)
{
return false;
}
if (parsed.is_https)
{
#if defined(_WIN32)
return https_request_winhttp(method, parsed, body, status_code_out, response_body_out, extra_headers);
#else
// No TLS transport wired up on this platform yet - Ollama and every
// connector this codebase talks to today are plain HTTP on
// localhost. Add an OpenSSL (or similar) path here if a non-Windows
// host needs outbound HTTPS.
return false;
#endif
}
if (!ensure_socket_library())
{
return false;
}
const NativeSocket client_socket = ::socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == kInvalidNativeSocket)
{
return false;
}
sockaddr_in address {};
address.sin_family = AF_INET;
address.sin_port = htons(static_cast<uint16_t>(parsed.port));
if (parsed.host == "localhost" || parsed.host == "127.0.0.1")
{
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
else
{
hostent* host_entry = gethostbyname(parsed.host.c_str());
if (!host_entry)
{
close_socket(client_socket);
return false;
}
std::memcpy(&address.sin_addr, host_entry->h_addr_list[0], static_cast<size_t>(host_entry->h_length));
}
if (connect(client_socket, reinterpret_cast<sockaddr*>(&address), sizeof(address)) != 0)
{
close_socket(client_socket);
return false;
}
core::String request = method + " " + parsed.path + " HTTP/1.1\r\n";
request += "Host: " + parsed.host + "\r\n";
request += "Connection: close\r\n";
if (method == "POST")
{
request += "Content-Type: application/json\r\n";
request += "Content-Length: " + std::to_string(body.size()) + "\r\n";
}
for (const core::String& extra_header : extra_headers)
{
request += extra_header + "\r\n";
}
request += "\r\n";
if (method == "POST")
{
request += body;
}
const char* data = request.c_str();
size_t remaining = request.size();
while (remaining > 0)
{
const int sent = send(client_socket, data, static_cast<int>(remaining), 0);
if (sent <= 0)
{
close_socket(client_socket);
return false;
}
data += sent;
remaining -= static_cast<size_t>(sent);
}
const bool ok = read_http_response(client_socket, status_code_out, response_body_out);
close_socket(client_socket);
return ok;
}
bool sync_http_post_json(
const core::String& url,
const core::String& body,
int32_t& status_code_out,
core::String& response_body_out,
const core::Array<core::String>& extra_headers)
{
return sync_http_request("POST", url, body, status_code_out, response_body_out, extra_headers);
}
bool sync_http_get(
const core::String& url,
int32_t& status_code_out,
core::String& response_body_out)
{
return sync_http_request("GET", url, {}, status_code_out, response_body_out, {});
}
} // namespace droidcli::tools