-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtoken_source_http.cpp
More file actions
261 lines (224 loc) · 9.41 KB
/
Copy pathtoken_source_http.cpp
File metadata and controls
261 lines (224 loc) · 9.41 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
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations.
*/
#include <algorithm>
#include <cctype>
#include <chrono>
#include <sstream>
#include <utility>
#include "token_source_internal.h"
#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <winhttp.h>
#else
#include <curl/curl.h>
#endif
namespace livekit {
namespace {
#if !defined(_WIN32)
size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* user_data) {
const size_t total_size = size * nmemb;
auto* response = static_cast<std::string*>(user_data);
response->append(contents, total_size);
return total_size;
}
#endif
std::string normalizeHttpMethod(std::string method) {
if (method.empty()) {
return "POST";
}
std::transform(method.begin(), method.end(), method.begin(),
[](unsigned char ch) { return static_cast<char>(std::toupper(ch)); });
return method;
}
// Coerce a caller-supplied timeout into a positive millisecond count. Both
// WinHTTP and libcurl treat 0 as "wait forever" and reject negatives, neither
// of which is sensible for a token fetch, so non-positive values fall back to
// the 30s default.
std::int64_t effectiveTimeoutMs(std::chrono::milliseconds timeout) {
constexpr std::int64_t kDefaultTimeoutMs = 30000;
const std::int64_t count = timeout.count();
return count > 0 ? count : kDefaultTimeoutMs;
}
#if defined(_WIN32)
std::wstring toWide(const std::string& value) {
if (value.empty()) {
return L"";
}
const int length = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), nullptr, 0);
if (length <= 0) {
return L"";
}
std::wstring wide(static_cast<std::size_t>(length), L'\0');
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), wide.data(), length);
return wide;
}
Result<std::string, std::string> winHttpRequest(const std::string& method, const std::string& url,
const std::map<std::string, std::string>& headers,
const std::string& json_body, std::chrono::milliseconds timeout) {
URL_COMPONENTS components{};
components.dwStructSize = sizeof(components);
components.dwSchemeLength = static_cast<DWORD>(-1);
components.dwHostNameLength = static_cast<DWORD>(-1);
components.dwUrlPathLength = static_cast<DWORD>(-1);
components.dwExtraInfoLength = static_cast<DWORD>(-1);
const std::wstring wide_url = toWide(url);
if (!WinHttpCrackUrl(wide_url.c_str(), 0, 0, &components)) {
return Result<std::string, std::string>::failure("failed to parse token server URL");
}
const std::wstring host(components.lpszHostName, components.dwHostNameLength);
// WinHttpCrackUrl splits the query string (and fragment) into lpszExtraInfo;
// lpszUrlPath alone drops it. Append it so endpoint URLs carrying query
// params (e.g. "/token?project=foo") reach the server, matching libcurl.
std::wstring object_name(components.lpszUrlPath, components.dwUrlPathLength);
if (components.lpszExtraInfo != nullptr && components.dwExtraInfoLength > 0) {
object_name.append(components.lpszExtraInfo, components.dwExtraInfoLength);
}
const std::wstring wide_method = toWide(normalizeHttpMethod(method));
HINTERNET session = WinHttpOpen(L"LiveKit-CPP/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
if (session == nullptr) {
return Result<std::string, std::string>::failure("WinHttpOpen failed");
}
const int timeout_ms = static_cast<int>(effectiveTimeoutMs(timeout));
WinHttpSetTimeouts(session, timeout_ms, timeout_ms, timeout_ms, timeout_ms);
HINTERNET connection = WinHttpConnect(session, host.c_str(), components.nPort, 0);
if (connection == nullptr) {
WinHttpCloseHandle(session);
return Result<std::string, std::string>::failure("WinHttpConnect failed");
}
const DWORD flags = (components.nScheme == INTERNET_SCHEME_HTTPS) ? WINHTTP_FLAG_SECURE : 0;
HINTERNET request = WinHttpOpenRequest(connection, wide_method.c_str(), object_name.c_str(), nullptr,
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, flags);
if (request == nullptr) {
WinHttpCloseHandle(connection);
WinHttpCloseHandle(session);
return Result<std::string, std::string>::failure("WinHttpOpenRequest failed");
}
std::wstring header_block = L"Content-Type: application/json\r\n";
for (const auto& [key, value] : headers) {
header_block += toWide(key);
header_block += L": ";
header_block += toWide(value);
header_block += L"\r\n";
}
const BOOL send_ok =
WinHttpSendRequest(request, header_block.c_str(), static_cast<DWORD>(-1L), const_cast<char*>(json_body.data()),
static_cast<DWORD>(json_body.size()), static_cast<DWORD>(json_body.size()), 0);
if (!send_ok) {
WinHttpCloseHandle(request);
WinHttpCloseHandle(connection);
WinHttpCloseHandle(session);
return Result<std::string, std::string>::failure("WinHttpSendRequest failed");
}
if (!WinHttpReceiveResponse(request, nullptr)) {
WinHttpCloseHandle(request);
WinHttpCloseHandle(connection);
WinHttpCloseHandle(session);
return Result<std::string, std::string>::failure("WinHttpReceiveResponse failed");
}
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);
std::string response_body;
DWORD available = 0;
do {
if (!WinHttpQueryDataAvailable(request, &available)) {
break;
}
if (available == 0) {
break;
}
std::string chunk(available, '\0');
DWORD read = 0;
if (!WinHttpReadData(request, chunk.data(), available, &read)) {
break;
}
chunk.resize(read);
response_body += chunk;
} while (available > 0);
WinHttpCloseHandle(request);
WinHttpCloseHandle(connection);
WinHttpCloseHandle(session);
if (status_code < 200 || status_code >= 300) {
std::ostringstream message;
message << "token server HTTP " << status_code << ": " << response_body;
return Result<std::string, std::string>::failure(message.str());
}
return Result<std::string, std::string>::success(std::move(response_body));
}
#endif
} // namespace
Result<std::string, std::string> tokenSourceHttpRequest(const std::string& method, const std::string& url,
const std::map<std::string, std::string>& headers,
const std::string& json_body,
std::chrono::milliseconds timeout) {
#if defined(_WIN32)
return winHttpRequest(method, url, headers, json_body, timeout);
#else
CURL* curl = curl_easy_init();
if (curl == nullptr) {
return Result<std::string, std::string>::failure("curl_easy_init failed");
}
const std::string normalized_method = normalizeHttpMethod(method);
std::string response_body;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, normalized_method.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_body.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(json_body.size()));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_body);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, static_cast<long>(effectiveTimeoutMs(timeout)));
curl_easy_setopt(curl, CURLOPT_USERAGENT, "LiveKit-CPP/1.0");
struct curl_slist* curl_headers = nullptr;
curl_headers = curl_slist_append(curl_headers, "Content-Type: application/json");
for (const auto& [key, value] : headers) {
std::string header;
header.reserve(key.size() + 2 + value.size());
header.append(key);
header.append(": ");
header.append(value);
curl_headers = curl_slist_append(curl_headers, header.c_str());
}
if (curl_headers != nullptr) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
}
const CURLcode perform_result = curl_easy_perform(curl);
long status_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);
if (curl_headers != nullptr) {
curl_slist_free_all(curl_headers);
}
curl_easy_cleanup(curl);
if (perform_result != CURLE_OK) {
return Result<std::string, std::string>::failure(curl_easy_strerror(perform_result));
}
if (status_code < 200 || status_code >= 300) {
std::ostringstream message;
message << "token server returned HTTP code " << status_code << ": ";
if (!response_body.empty()) {
message << response_body;
} else {
message << "<no response body>";
}
return Result<std::string, std::string>::failure(message.str());
}
return Result<std::string, std::string>::success(std::move(response_body));
#endif
}
} // namespace livekit