-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathWebUIRequestHandler.cpp
More file actions
433 lines (360 loc) · 15.9 KB
/
Copy pathWebUIRequestHandler.cpp
File metadata and controls
433 lines (360 loc) · 15.9 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
#include <Server/WebUIRequestHandler.h>
#include <Server/HTTP/HTTPResponseHelpers.h>
#include <Server/HTTPResponseHeaderWriter.h>
#include <Common/re2.h>
#include <Core/ServerSettings.h>
#include <IO/HTTPCommon.h>
#include <IO/Operators.h>
#include <Interpreters/Context.h>
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
#include <ClickStackResources.generated.h>
#include <SQLConsoleResources.generated.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/URI.h>
#include <Poco/Util/LayeredConfiguration.h>
/// Embedded HTML pages
constexpr unsigned char resource_play_html[] =
{
#embed "../../programs/server/play.html"
};
constexpr unsigned char resource_dashboard_html[] =
{
#embed "../../programs/server/dashboard.html"
};
constexpr unsigned char resource_uplot_js[] =
{
#embed "../../programs/server/js/uplot.js"
};
constexpr unsigned char resource_lz_string_js[] =
{
#embed "../../programs/server/js/lz-string.js"
};
constexpr unsigned char resource_xterm_js[] =
{
#embed "../../programs/server/js/xterm.min.js"
};
constexpr unsigned char resource_xterm_css[] =
{
#embed "../../programs/server/js/xterm.min.css"
};
constexpr unsigned char resource_addon_fit_js[] =
{
#embed "../../programs/server/js/addon-fit.min.js"
};
constexpr unsigned char resource_addon_web_links_js[] =
{
#embed "../../programs/server/js/addon-web-links.min.js"
};
constexpr unsigned char resource_viz_standalone_js[] =
{
#embed "../../programs/server/js/viz-standalone.js"
};
constexpr unsigned char resource_binary_html[] =
{
#embed "../../programs/server/binary.html"
};
constexpr unsigned char resource_merges_html[] =
{
#embed "../../programs/server/merges.html"
};
constexpr unsigned char resource_jemalloc_html[] =
{
#embed "../../programs/server/jemalloc.html"
};
constexpr unsigned char resource_schema_html[] =
{
#embed "../../programs/server/schema.html"
};
constexpr unsigned char resource_processors_profile_html[] =
{
#embed "../../programs/server/processors_profile.html"
};
constexpr unsigned char resource_docs_html[] =
{
#embed "../../programs/server/docs.html"
};
constexpr unsigned char resource_marked_js[] =
{
#embed "../../programs/server/js/marked.min.js"
};
constexpr unsigned char resource_katex_js[] =
{
#embed "../../programs/server/js/katex.min.js"
};
constexpr unsigned char resource_katex_css[] =
{
#embed "../../programs/server/js/katex.min.css"
};
namespace DB
{
static void handle(HTTPServerRequest & request, HTTPServerResponse & response, std::string_view html,
std::unordered_map<String, String> http_response_headers_override = {})
{
applyHTTPResponseHeaders(response, http_response_headers_override);
if (response.getContentType().empty())
response.setContentType("text/html; charset=UTF-8");
if (request.getVersion() == HTTPServerRequest::HTTP_1_1)
response.setChunkedTransferEncoding(true);
setResponseDefaultHeaders(response);
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_OK);
auto buf = responseWriteBuffer(request, response);
buf.get()->write(html.data(), html.size());
buf.get()->finalize();
}
template <typename EmbeddedResources>
const typename EmbeddedResources::value_type * findEmbeddedResource(const EmbeddedResources & resources, const std::string & resource_path)
{
auto it = std::lower_bound(
resources.begin(),
resources.end(),
resource_path,
[](const auto & resource, const std::string & path)
{
return resource.path < path;
});
if (it == resources.end() || it->path != resource_path)
return nullptr;
return &*it;
}
template <typename EmbeddedResource>
void handleCompressedEmbeddedResource(
HTTPServerRequest & request,
HTTPServerResponse & response,
const EmbeddedResource & resource,
const std::unordered_map<String, String> & http_response_headers_override)
{
response.setContentType(std::string(resource.mime_type));
auto headers_with_encoding = http_response_headers_override;
headers_with_encoding["Content-Encoding"] = "gzip";
handle(request, response, {reinterpret_cast<const char *>(resource.data), resource.size}, headers_with_encoding);
}
void PlayWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
handle(request, response, {reinterpret_cast<const char *>(resource_play_html), std::size(resource_play_html)}, http_response_headers_override);
}
void DashboardWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
std::string html(reinterpret_cast<const char *>(resource_dashboard_html), std::size(resource_dashboard_html));
/// Replace a link to external JavaScript file to embedded file.
/// This allows to open the HTML without running a server and to host it on server.
/// Note: we can embed the JavaScript file inline to the HTML,
/// but we don't do it to keep the "view-source" perfectly readable.
static re2::RE2 uplot_url = R"(https://[^\s"'`]+u[Pp]lot[^\s"'`]*\.js)";
RE2::Replace(&html, uplot_url, "/js/uplot.js");
static re2::RE2 lz_string_url = R"(https://[^\s"'`]+lz-string[^\s"'`]*\.js)";
RE2::Replace(&html, lz_string_url, "/js/lz-string.js");
handle(request, response, html, http_response_headers_override);
}
void BinaryWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
handle(request, response, {reinterpret_cast<const char *>(resource_binary_html), std::size(resource_binary_html)}, http_response_headers_override);
}
void MergesWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
handle(request, response, {reinterpret_cast<const char *>(resource_merges_html), std::size(resource_merges_html)}, http_response_headers_override);
}
void JavaScriptWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
struct Resource { const char * path; const unsigned char * data; size_t size; const char * content_type; };
const Resource resources[] = {
{"/js/uplot.js", resource_uplot_js, std::size(resource_uplot_js), "application/javascript; charset=UTF-8"},
{"/js/lz-string.js", resource_lz_string_js, std::size(resource_lz_string_js), "application/javascript; charset=UTF-8"},
{"/js/xterm.min.js", resource_xterm_js, std::size(resource_xterm_js), "application/javascript; charset=UTF-8"},
{"/js/xterm.min.css", resource_xterm_css, std::size(resource_xterm_css), "text/css; charset=UTF-8"},
{"/js/addon-fit.min.js", resource_addon_fit_js, std::size(resource_addon_fit_js), "application/javascript; charset=UTF-8"},
{"/js/addon-web-links.min.js", resource_addon_web_links_js, std::size(resource_addon_web_links_js), "application/javascript; charset=UTF-8"},
{"/js/viz-standalone.js", resource_viz_standalone_js, std::size(resource_viz_standalone_js), "application/javascript; charset=UTF-8"},
{"/js/marked.min.js", resource_marked_js, std::size(resource_marked_js), "application/javascript; charset=UTF-8"},
{"/js/katex.min.js", resource_katex_js, std::size(resource_katex_js), "application/javascript; charset=UTF-8"},
{"/js/katex.min.css", resource_katex_css, std::size(resource_katex_css), "text/css; charset=UTF-8"},
};
for (const auto & resource : resources)
{
if (request.getURI() == resource.path)
{
auto headers = http_response_headers_override;
if (resource.content_type)
headers["Content-Type"] = resource.content_type;
handle(request, response, {reinterpret_cast<const char *>(resource.data), resource.size}, headers);
return;
}
}
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
*response.send() << "Not found.\n";
}
void JemallocWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
handle(request, response, {reinterpret_cast<const char *>(resource_jemalloc_html), std::size(resource_jemalloc_html)}, http_response_headers_override);
}
void SchemaWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
handle(request, response, {reinterpret_cast<const char *>(resource_schema_html), std::size(resource_schema_html)}, http_response_headers_override);
}
void ProcessorsProfileWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
handle(request, response, {reinterpret_cast<const char *>(resource_processors_profile_html), std::size(resource_processors_profile_html)}, http_response_headers_override);
}
void DocsWebUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
std::string html(reinterpret_cast<const char *>(resource_docs_html), std::size(resource_docs_html));
/// Replace links to external JavaScript/CSS files (the Marked Markdown renderer and the
/// KaTeX math renderer) with embedded files served from the same origin.
/// This keeps the page self-contained and, more importantly, avoids executing third-party
/// network code in the ClickHouse HTTP origin, which handles user credentials.
/// The original CDN links are kept in the source so the page also works when opened as a
/// local file (file://) against a remote server, mirroring the `dashboard.html` handling.
static re2::RE2 marked_url = R"(https://[^\s"'`]+marked[^\s"'`]*\.js)";
RE2::Replace(&html, marked_url, "/js/marked.min.js");
static re2::RE2 katex_js_url = R"(https://[^\s"'`]+katex[^\s"'`]*\.js)";
RE2::Replace(&html, katex_js_url, "/js/katex.min.js");
static re2::RE2 katex_css_url = R"(https://[^\s"'`]+katex[^\s"'`]*\.css)";
RE2::Replace(&html, katex_css_url, "/js/katex.min.css");
handle(request, response, html, http_response_headers_override);
}
std::optional<std::string> ClickStackUIRequestHandler::getResourcePath(const std::string & uri) const
{
std::string_view path = uri;
if (path.starts_with("/clickstack"))
path.remove_prefix(11); // length of "/clickstack"
if (!path.empty() && path[0] == '/')
path.remove_prefix(1);
// Remove query parameters and fragments
auto query_pos = path.find('?');
if (query_pos != std::string_view::npos)
path = path.substr(0, query_pos);
auto fragment_pos = path.find('#');
if (fragment_pos != std::string_view::npos)
path = path.substr(0, fragment_pos);
// Remove trailing slash
if (!path.empty() && path.back() == '/')
path.remove_suffix(1);
/// `Poco::URI::decode` throws `Poco::URISyntaxException` on malformed
/// percent-encoding (e.g. lone `%`, `%X`, `%ZZ`). Without this catch the
/// exception would unwind into the server error handler and produce a 500
/// for any client that sends a malformed URI; returning std::nullopt lets
/// the request handler answer with a deterministic 400.
std::string decoded;
try
{
Poco::URI::decode(std::string(path), decoded);
}
catch (const Poco::URISyntaxException &)
{
return std::nullopt;
}
// Handle clean URLs - map page routes to .html files
// If path is empty or just "/", serve index.html
if (decoded.empty())
return std::string("index.html");
if (decoded.contains('.'))
return decoded;
// assuming a path with no "." is an html page
return decoded + ".html";
}
namespace
{
/// Resolve a page request against Next.js-style dynamic routes.
/// Example: /trace/abc -> /trace/[traceId].html
const ClickStack::EmbeddedResource * resolveDynamicRoute(const std::string & resource_path)
{
static constexpr std::string_view html_suffix = ".html";
static constexpr std::string_view dynamic_tail_suffix = "].html";
if (!std::string_view{resource_path}.ends_with(html_suffix))
return nullptr;
size_t last_slash = resource_path.rfind('/');
std::string_view prefix = last_slash == std::string::npos
? std::string_view{}
: std::string_view{resource_path}.substr(0, last_slash + 1);
for (const auto & candidate : ClickStack::embedded_resources)
{
std::string_view candidate_path{candidate.path};
if (!candidate_path.starts_with(prefix))
continue;
std::string_view tail = candidate_path.substr(prefix.size());
if (tail.empty() || tail.front() != '[')
continue;
if (!tail.ends_with(dynamic_tail_suffix))
continue;
if (tail.contains('/'))
continue;
return &candidate;
}
return nullptr;
}
}
void ClickStackUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
auto resource_path_opt = getResourcePath(request.getURI());
if (!resource_path_opt)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
*response.send() << "Malformed URL.\n";
return;
}
const std::string & resource_path = *resource_path_opt;
const auto * resource = findEmbeddedResource(ClickStack::embedded_resources, resource_path);
/// If the literal lookup missed and the request is for a page, try to
/// resolve it against a Next.js dynamic-route export (`foo/[id].html`).
/// This is what makes URLs like `/clickstack/trace/<trace-id>` serve the
/// `trace/[traceId].html` redirect page instead of 404ing.
if (!resource)
resource = resolveDynamicRoute(resource_path);
// Check if resource was found
if (!resource)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
*response.send() << "Not found.\n";
return;
}
handleCompressedEmbeddedResource(request, response, *resource, http_response_headers_override);
}
std::string SQLConsoleUIRequestHandler::getResourcePath(const std::string & uri) const
{
constexpr std::string_view sql_console_path = "/ui";
std::string_view path = uri;
if (path.starts_with(sql_console_path))
path.remove_prefix(sql_console_path.size());
if (!path.empty() && path[0] == '/')
path.remove_prefix(1);
auto query_pos = path.find('?');
if (query_pos != std::string_view::npos)
path = path.substr(0, query_pos);
auto fragment_pos = path.find('#');
if (fragment_pos != std::string_view::npos)
path = path.substr(0, fragment_pos);
if (!path.empty() && path.back() == '/')
path.remove_suffix(1);
if (path.empty())
return "index.html";
return std::string(path);
}
namespace
{
/// Missing hashed/root assets must 404; other unmatched paths are SPA routes (quoted identifiers may contain '.').
bool isSQLConsoleStaticAsset(std::string_view resource_path)
{
if (resource_path.starts_with("assets/") || resource_path.starts_with("monacoeditorwork/"))
return true;
return resource_path == "env.js" || resource_path == "index.html";
}
}
void SQLConsoleUIRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event &)
{
std::string resource_path = getResourcePath(request.getURI());
if (const auto * resource = findEmbeddedResource(SQLConsole::embedded_resources, resource_path))
{
handleCompressedEmbeddedResource(request, response, *resource, http_response_headers_override);
return;
}
if (!isSQLConsoleStaticAsset(resource_path))
{
if (const auto * resource = findEmbeddedResource(SQLConsole::embedded_resources, "index.html"))
{
handleCompressedEmbeddedResource(request, response, *resource, http_response_headers_override);
return;
}
}
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
*response.send() << "Not found.\n";
}
}