-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp_server.cpp
500 lines (421 loc) · 14.6 KB
/
http_server.cpp
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
#include "http_server.hpp"
#include "event_dispatcher.hpp"
#include "settings.hpp"
#include "state.hpp"
#include "utils/encoding.hpp"
#include "utils/env.hpp"
#include "utils/md_helpers.hpp"
#include "utils/serialization.hpp"
#include "version.hpp"
#include "watcher.hpp"
#include <duckdb/common/serializer/binary_serializer.hpp>
#include <duckdb/common/serializer/memory_stream.hpp>
#include <duckdb/main/attached_database.hpp>
#include <duckdb/parser/parser.hpp>
namespace duckdb {
namespace ui {
unique_ptr<HttpServer> HttpServer::server_instance;
HttpServer *HttpServer::GetInstance(ClientContext &context) {
if (server_instance) {
// We already have an instance, make sure we're running on the right DB
server_instance->UpdateDatabaseInstance(context.db);
} else {
server_instance = make_uniq<HttpServer>(context.db);
std::atexit(HttpServer::StopInstance);
}
return server_instance.get();
}
void HttpServer::UpdateDatabaseInstanceIfRunning(
shared_ptr<DatabaseInstance> db) {
if (server_instance) {
server_instance->UpdateDatabaseInstance(db);
}
}
void HttpServer::UpdateDatabaseInstance(
shared_ptr<DatabaseInstance> context_db) {
const auto current_db = server_instance->LockDatabaseInstance();
if (current_db == context_db) {
return;
}
bool has_watcher = !!server_instance->watcher;
if (has_watcher) {
server_instance->watcher->Stop();
server_instance->watcher = nullptr;
}
server_instance->ddb_instance = context_db;
if (has_watcher) {
server_instance->watcher = make_uniq<Watcher>(*this);
server_instance->watcher->Start();
}
}
bool HttpServer::IsRunningOnMachine(ClientContext &context) {
if (Started()) {
return true;
}
const auto local_port = GetLocalPort(context);
auto local_url = StringUtil::Format("http://localhost:%d", local_port);
httplib::Client client(local_url);
return client.Get("/info");
}
bool HttpServer::Started() {
return server_instance && server_instance->main_thread;
}
void HttpServer::StopInstance() {
if (Started()) {
server_instance->DoStop();
}
}
const HttpServer &HttpServer::Start(ClientContext &context, bool *was_started) {
if (Started()) {
if (was_started) {
*was_started = true;
}
return *GetInstance(context);
}
if (was_started) {
*was_started = false;
}
const auto remote_url = GetRemoteUrl(context);
const auto port = GetLocalPort(context);
auto server = GetInstance(context);
server->DoStart(port, remote_url);
return *server;
}
void HttpServer::DoStart(const uint16_t _local_port,
const std::string &_remote_url) {
if (Started()) {
throw std::runtime_error("HttpServer already started");
}
local_port = _local_port;
local_url = StringUtil::Format("http://localhost:%d", local_port);
remote_url = _remote_url;
user_agent =
StringUtil::Format("duckdb-ui/%s-%s(%s)", DuckDB::LibraryVersion(),
UI_EXTENSION_VERSION, DuckDB::Platform());
event_dispatcher = make_uniq<EventDispatcher>();
main_thread = make_uniq<std::thread>(&HttpServer::Run, this);
watcher = make_uniq<Watcher>(*this);
watcher->Start();
}
bool HttpServer::Stop() {
if (!Started()) {
return false;
}
server_instance->DoStop();
return true;
}
void HttpServer::DoStop() {
if (event_dispatcher) {
event_dispatcher->Close();
event_dispatcher = nullptr;
}
server.stop();
if (watcher) {
watcher->Stop();
watcher = nullptr;
}
if (main_thread) {
main_thread->join();
main_thread.reset();
}
ddb_instance.reset();
remote_url = "";
local_port = 0;
}
std::string HttpServer::LocalUrl() const {
return StringUtil::Format("http://localhost:%d/", local_port);
}
shared_ptr<DatabaseInstance> HttpServer::LockDatabaseInstance() {
return ddb_instance.lock();
}
void HttpServer::Run() {
server.Get("/info", [&](const httplib::Request &req, httplib::Response &res) {
HandleGetInfo(req, res);
});
server.Get("/localEvents",
[&](const httplib::Request &req, httplib::Response &res) {
HandleGetLocalEvents(req, res);
});
server.Get("/localToken",
[&](const httplib::Request &req, httplib::Response &res) {
HandleGetLocalToken(req, res);
});
server.Get("/.*", [&](const httplib::Request &req, httplib::Response &res) {
HandleGet(req, res);
});
server.Post("/ddb/interrupt",
[&](const httplib::Request &req, httplib::Response &res) {
HandleInterrupt(req, res);
});
server.Post("/ddb/run",
[&](const httplib::Request &req, httplib::Response &res,
const httplib::ContentReader &content_reader) {
HandleRun(req, res, content_reader);
});
server.Post("/ddb/tokenize",
[&](const httplib::Request &req, httplib::Response &res,
const httplib::ContentReader &content_reader) {
HandleTokenize(req, res, content_reader);
});
server.listen("localhost", local_port);
}
void HttpServer::HandleGetInfo(const httplib::Request &req,
httplib::Response &res) {
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("X-DuckDB-Version", DuckDB::LibraryVersion());
res.set_header("X-DuckDB-Platform", DuckDB::Platform());
res.set_header("X-DuckDB-UI-Extension-Version", UI_EXTENSION_VERSION);
res.set_content("", "text/plain");
}
void HttpServer::HandleGetLocalEvents(const httplib::Request &req,
httplib::Response &res) {
res.set_chunked_content_provider(
"text/event-stream", [&](size_t /*offset*/, httplib::DataSink &sink) {
if (event_dispatcher->WaitEvent(&sink)) {
return true;
}
sink.done();
return false;
});
}
void HttpServer::HandleGetLocalToken(const httplib::Request &req,
httplib::Response &res) {
// GET requests don't include Origin, so use Referer instead.
// Referer includes the path, so only compare the start.
auto referer = req.get_header_value("Referer");
if (referer.compare(0, local_url.size(), local_url) != 0) {
res.status = 401;
return;
}
auto db = ddb_instance.lock();
if (!db) {
res.status = 500;
res.set_content("Database was invalidated, UI needs to be restarted",
"text/plain");
return;
}
Connection connection{*db};
try {
auto token = GetMDToken(connection);
res.status = 200;
res.set_content(token, "text/plain");
} catch (std::exception &ex) {
res.status = 500;
res.set_content("Could not get token: " + std::string(ex.what()),
"text/plain");
}
}
void HttpServer::HandleGet(const httplib::Request &req,
httplib::Response &res) {
// Create HTTP client to remote URL
// TODO: Can this be created once and shared?
httplib::Client client(remote_url);
client.set_keep_alive(true);
if (IsEnvEnabled("ui_disable_server_certificate_verification")) {
client.enable_server_certificate_verification(false);
}
// forward GET to remote URL
auto result = client.Get(req.path, req.params, {{"User-Agent", user_agent}});
if (!result) {
res.status = 500;
return;
}
// Repond with result of forwarded GET
res = result.value();
// If this is the config request, return additional information.
if (req.path == "/config") {
res.set_header("X-DuckDB-Version", DuckDB::LibraryVersion());
res.set_header("X-DuckDB-Platform", DuckDB::Platform());
// The UI looks for this to select the appropriate DuckDB mode (HTTP or
// Wasm).
res.set_header("X-DuckDB-UI-Extension-Version", UI_EXTENSION_VERSION);
}
}
void HttpServer::HandleInterrupt(const httplib::Request &req,
httplib::Response &res) {
auto origin = req.get_header_value("Origin");
if (origin != local_url) {
res.status = 401;
return;
}
auto description = req.get_header_value("X-DuckDB-UI-Request-Description");
auto connection_name = req.get_header_value("X-DuckDB-UI-Connection-Name");
auto db = ddb_instance.lock();
if (!db) {
res.status = 404;
return;
}
auto connection =
UIStorageExtensionInfo::GetState(*db).FindConnection(connection_name);
if (!connection) {
res.status = 404;
return;
}
connection->Interrupt();
SetResponseEmptyResult(res);
}
void HttpServer::HandleRun(const httplib::Request &req, httplib::Response &res,
const httplib::ContentReader &content_reader) {
try {
DoHandleRun(req, res, content_reader);
} catch (const std::exception &ex) {
SetResponseErrorResult(res, ex.what());
}
}
void HttpServer::DoHandleRun(const httplib::Request &req,
httplib::Response &res,
const httplib::ContentReader &content_reader) {
auto origin = req.get_header_value("Origin");
if (origin != local_url) {
res.status = 401;
return;
}
auto description = req.get_header_value("X-DuckDB-UI-Request-Description");
auto connection_name = req.get_header_value("X-DuckDB-UI-Connection-Name");
auto database_name =
DecodeBase64(req.get_header_value("X-DuckDB-UI-Database-Name"));
std::vector<std::string> parameter_values;
auto parameter_count_string =
req.get_header_value("X-DuckDB-UI-Parameter-Count");
if (!parameter_count_string.empty()) {
auto parameter_count = std::stoi(parameter_count_string);
for (idx_t i = 0; i < parameter_count; ++i) {
auto parameter_value = DecodeBase64(req.get_header_value(
StringUtil::Format("X-DuckDB-UI-Parameter-Value-%d", i)));
parameter_values.push_back(parameter_value);
}
}
std::string content = ReadContent(content_reader);
auto db = ddb_instance.lock();
if (!db) {
SetResponseErrorResult(
res, "Database was invalidated, UI needs to be restarted");
return;
}
auto connection =
UIStorageExtensionInfo::GetState(*db).FindOrCreateConnection(
*db, connection_name);
// Set current database if optional header is provided.
if (!database_name.empty()) {
auto &context = *connection->context;
context.RunFunctionInTransaction([&] {
auto &manager = context.db->GetDatabaseManager();
manager.SetDefaultDatabase(context, database_name);
});
}
// We use a pending query so we can execute tasks and fetch chunks
// incrementally. This enables cancellation.
unique_ptr<PendingQueryResult> pending;
// Create pending query, with request content as SQL.
if (parameter_values.size() > 0) {
auto prepared = connection->Prepare(content);
if (prepared->HasError()) {
SetResponseErrorResult(res, prepared->GetError());
return;
}
vector<Value> values;
for (auto ¶meter_value : parameter_values) {
// TODO: support non-string parameters?
values.push_back(Value(parameter_value));
}
pending = prepared->PendingQuery(values, true);
} else {
pending = connection->PendingQuery(content, true);
}
if (pending->HasError()) {
SetResponseErrorResult(res, pending->GetError());
return;
}
// Execute tasks until result is ready (or there's an error).
auto exec_result = PendingExecutionResult::RESULT_NOT_READY;
while (!PendingQueryResult::IsResultReady(exec_result)) {
exec_result = pending->ExecuteTask();
if (exec_result == PendingExecutionResult::BLOCKED ||
exec_result == PendingExecutionResult::NO_TASKS_AVAILABLE) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
switch (exec_result) {
case PendingExecutionResult::EXECUTION_ERROR:
SetResponseErrorResult(res, pending->GetError());
break;
case PendingExecutionResult::EXECUTION_FINISHED:
case PendingExecutionResult::RESULT_READY: {
// Get the result. This should be quick because it's ready.
auto result = pending->Execute();
// Fetch the chunks and serialize the result.
SuccessResult success_result;
success_result.column_names_and_types = {std::move(result->names),
std::move(result->types)};
// TODO: support limiting the number of chunks fetched
auto chunk = result->Fetch();
while (chunk) {
success_result.chunks.push_back(
{static_cast<uint16_t>(chunk->size()), std::move(chunk->data)});
chunk = result->Fetch();
}
MemoryStream success_response_content;
BinarySerializer::Serialize(success_result, success_response_content);
SetResponseContent(res, success_response_content);
break;
}
default:
SetResponseErrorResult(res, "Unexpected PendingExecutionResult");
break;
}
}
void HttpServer::HandleTokenize(const httplib::Request &req,
httplib::Response &res,
const httplib::ContentReader &content_reader) {
auto origin = req.get_header_value("Origin");
if (origin != local_url) {
res.status = 401;
return;
}
auto description = req.get_header_value("X-DuckDB-UI-Request-Description");
std::string content = ReadContent(content_reader);
auto tokens = Parser::Tokenize(content);
// Read and serialize result
TokenizeResult result;
result.offsets.reserve(tokens.size());
result.types.reserve(tokens.size());
for (auto token : tokens) {
result.offsets.push_back(token.start);
result.types.push_back(token.type);
}
MemoryStream response_content;
BinarySerializer::Serialize(result, response_content);
SetResponseContent(res, response_content);
}
std::string
HttpServer::ReadContent(const httplib::ContentReader &content_reader) {
std::ostringstream oss;
content_reader([&](const char *data, size_t data_length) {
oss.write(data, data_length);
return true;
});
return oss.str();
}
void HttpServer::SetResponseContent(httplib::Response &res,
const MemoryStream &content) {
auto data = content.GetData();
auto length = content.GetPosition();
res.set_content(reinterpret_cast<const char *>(data), length,
"application/octet-stream");
}
void HttpServer::SetResponseEmptyResult(httplib::Response &res) {
EmptyResult empty_result;
MemoryStream response_content;
BinarySerializer::Serialize(empty_result, response_content);
SetResponseContent(res, response_content);
}
void HttpServer::SetResponseErrorResult(httplib::Response &res,
const std::string &error) {
ErrorResult error_result;
error_result.error = error;
MemoryStream response_content;
BinarySerializer::Serialize(error_result, response_content);
SetResponseContent(res, response_content);
}
} // namespace ui
} // namespace duckdb