Repository navigation
Expand file tree
/
Copy pathserverGETConnections.cpp
More file actions
404 lines (352 loc) · 17.2 KB
/
Copy pathserverGETConnections.cpp
File metadata and controls
404 lines (352 loc) · 17.2 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
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include "content.hh"
#include "htmlPages.hh"
#include "match.hh"
#include "process.hh"
#include "server.hh"
extern int gVerbosity;
/**
* Handler used to generate a 404 reply.
*
* @param cls a ’const char* ’ with the HTML webpage to return
* @param mime mime type to use
* @param session session handle
* @param connection connection to use
*/
extern bool gDebugAPI; // serves the diagnostic routes (see main.cpp)
static int page_not_found(struct MHD_Connection* connection, const char* page, int length,
const char* mimetype)
{
struct MHD_Response* response =
MHD_create_response_from_buffer(length, (void*)page, MHD_RESPMEM_MUST_COPY);
int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_ENCODING, mimetype);
MHD_destroy_response(response);
return ret;
}
//------------------------------------------------------------------
// Actual callback method called every time a GET or POST request is received.
// by the server.
int FaustServer::dispatchGETConnections(struct MHD_Connection* connection, const std::string& url)
{
// TArgs args;
// MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, get_params, &args);
if (gVerbosity >= 2) {
std::cerr << "ANSWER GET CONNECTION " << url << std::endl;
}
if (matchExtension(url, ".php") ||
(url.length() > 100 &&
url.find("/webapp/") == std::string::npos) /*matchExtension(url, ".js")*/) {
return page_not_found(connection, "/favicon.ico", 12, "image/x-icon");
} else if (matchURL(url, "/")) {
std::stringstream ss;
ss << askpage_head << nr_of_uploading_clients << askpage_tail;
return send_page(connection, ss.str().c_str(), ss.str().size(), MHD_HTTP_OK, "text/html");
} else if (matchURL(url, "/targets")) {
return send_page(connection, fTargets.c_str(), fTargets.size(), MHD_HTTP_OK,
"application/json");
} else if (matchURL(url, "/version")) {
// Get Faust version from the first line of faust --version
std::string version = "2.81.2"; // fallback
std::string output;
captureProgram({"faust", "--version"}, output, true);
std::string first_line = output.substr(0, output.find('\n'));
std::smatch number;
if (std::regex_search(first_line, number, std::regex(R"([0-9]+\.[0-9]+\.[0-9]+)"))) {
version = number.str(0);
}
return send_page(connection, version.c_str(), version.size(), MHD_HTTP_OK, "text/plain");
} else if (gDebugAPI && matchURL(url, "/app")) {
return serveAppInterface(connection);
} else if (gDebugAPI && matchURL(url, "/verbosity0")) {
gVerbosity = 0;
std::stringstream ss;
ss << "Verbosity " << gVerbosity;
return send_page(connection, ss.str().c_str(), ss.str().size(), MHD_HTTP_OK, "text/html");
} else if (gDebugAPI && matchURL(url, "/verbosity1")) {
gVerbosity = 1;
std::stringstream ss;
ss << "Verbosity " << gVerbosity;
return send_page(connection, ss.str().c_str(), ss.str().size(), MHD_HTTP_OK, "text/html");
} else if (gDebugAPI && matchURL(url, "/verbosity2")) {
gVerbosity = 2;
std::stringstream ss;
ss << "Verbosity " << gVerbosity;
return send_page(connection, ss.str().c_str(), ss.str().size(), MHD_HTTP_OK, "text/html");
/*
} else if (matchURL(url, "/crash1")) {
// simulate crash -- to be removed in production
exit(-1);
} else if (matchURL(url, "/crash2")) {
// simulate crash -- to be removed in production
int* p = 0;
*p = 5 / (*p);
return page_not_found(connection, "/crash2", 7, "image/x-icon");
*/
} else if (matchURL(url, "/*/*/*/installer.sh")) {
return makeAndSendResourceFile(connection, url);
} else if (matchURL(url, "/*/*/*/binary.zip")) {
return makeAndSendResourceFile(connection, url);
} else if (matchURL(url, "/*/*/*/precompile")) {
return makeAndSendResourceFile(connection, url);
} else if (matchURL(url, "/*/*/*/binary.apk")) {
return makeAndSendResourceFile(connection, url);
} else if (matchURL(url, "/*/diagram/*") && matchExtension(url, ".svg")) {
// Legacy redirection: /sha/diagram/foo.svg -> /sha/svg/foo.svg
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 4) {
// URL format: /sha1/diagram/filename.svg -> serve from /sha1/svg/filename.svg
fs::path filepath = fDirectory / U[1] / "svg" / U[3];
if (fs::exists(filepath)) {
return send_file(connection, filepath, "image/svg+xml");
}
}
std::string error_msg = "SVG diagram not found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"image/svg+xml");
} else if (matchURL(url, "/*/generated.cpp")) {
// Serve the generated C++ file from validate_faust
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 2) {
fs::path filepath = fDirectory / U[1] / "generated.cpp";
if (fs::exists(filepath)) {
return send_file(connection, filepath, "text/x-c");
}
}
std::string error_msg = "File not found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (matchURL(url, "/*/errors.log")) {
// Serve the compilation errors log file from validate_faust
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 2) {
fs::path filepath = fDirectory / U[1] / "errors.log";
if (fs::exists(filepath)) {
return send_file(connection, filepath, "text/plain");
}
}
std::string error_msg = "No errors.log file found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (matchURL(url, "/*/filename")) {
// Serve the original filename for drag-and-drop functionality
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 2) {
fs::path filepath = fDirectory / U[1] / "filename.txt";
if (fs::exists(filepath)) {
return send_file(connection, filepath, "text/plain");
}
}
std::string error_msg = "No filename information found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (matchURL(url, "/*/svg.zip")) {
// Create and serve a ZIP file containing all SVG diagrams
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 2) {
fs::path svg_dir = fDirectory / U[1] / "svg";
if (fs::exists(svg_dir) && fs::is_directory(svg_dir)) {
// Create temporary ZIP file
fs::path temp_zip = fDirectory / U[1] / "temp_svg.zip";
if (create_svg_zip(svg_dir, temp_zip)) {
// Send the ZIP file and then delete it
int result = send_file(connection, temp_zip, "application/zip");
try {
fs::remove(temp_zip);
} catch (const fs::filesystem_error& e) {
if (gVerbosity >= 1) {
std::cerr
<< "Warning: Could not remove temp ZIP: " << e.code().message()
<< std::endl;
}
}
return result;
}
}
}
std::string error_msg = "No SVG diagrams found or could not create ZIP";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (matchURL(url, "/*/webapp.zip")) {
// Create and serve a ZIP file containing all webapp files
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 2) {
std::string sha1 = U[1];
std::string error_msg;
// Ensure webapp exists, generate if needed
if (!ensure_webapp_exists(sha1, error_msg)) {
int status_code = (error_msg.find("Session not found") != std::string::npos ||
error_msg.find("No DSP file found") != std::string::npos)
? MHD_HTTP_NOT_FOUND
: MHD_HTTP_INTERNAL_SERVER_ERROR;
return send_page(connection, error_msg.c_str(), error_msg.size(), status_code,
"text/plain");
}
fs::path webapp_dir = fDirectory / sha1 / "webapp";
if (fs::exists(webapp_dir) && fs::is_directory(webapp_dir)) {
// Create temporary ZIP file
fs::path temp_zip = fDirectory / sha1 / "temp_webapp.zip";
if (create_webapp_zip(webapp_dir, temp_zip)) {
// Send the ZIP file and then delete it
int result = send_file(connection, temp_zip, "application/zip");
try {
fs::remove(temp_zip);
} catch (const fs::filesystem_error& e) {
if (gVerbosity >= 1) {
std::cerr << "Warning: Could not remove temp webapp ZIP: "
<< e.code().message() << std::endl;
}
}
return result;
}
}
}
std::string error_msg = "No webapp files found or could not create ZIP";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (matchURL(url, "/*/svg/*") && matchExtension(url, ".svg")) {
// Serve SVG files from the svg/ directory created by validate_faust
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 4) {
// URL format: /<sha1>/svg/<filename.svg>
// U[0] is empty string, U[1] is sha1, U[2] is "svg", U[3] is filename
fs::path filepath = fDirectory / U[1] / U[2] / U[3];
if (fs::exists(filepath)) {
return send_file(connection, filepath, "image/svg+xml");
}
}
std::string error_msg = "SVG file not found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"image/svg+xml");
} else if (matchBeginURL(url, "/*/web/pwa") || matchBeginURL(url, "/*/web/pwa-poly")) {
return makeAndSendResourceFile(connection, url);
} else if (matchURL(url, "/*/signals.svg")) {
return serveSignalsSvg(connection, url);
} else if (matchURL(url, "/*/tasks.svg")) {
return serveTaskSvg(connection, url);
} else if (matchURL(url, "/*/webapp")) {
// Extract SHA from URL: /sha1/webapp -> sha1
std::string sha1 = url.substr(1, url.find('/', 1) - 1);
return generate_webapp_view(connection, sha1);
} else if (url.find("/webapp/") != std::string::npos && url.size() > 40) {
// Handle webapp assets using string matching instead of pattern matching
// Expected format: /<sha>/webapp/path/to/file.ext
if (gVerbosity >= 2) {
std::cerr << "DEBUG: Processing webapp asset URL: " << url << std::endl;
}
// Find the position of "/webapp/"
size_t webapp_pos = url.find("/webapp/");
if (webapp_pos != std::string::npos) {
// Extract SHA (everything before /webapp/)
std::string sha1 = url.substr(1, webapp_pos - 1); // Skip leading '/'
// Extract asset path (everything after /webapp/)
std::string asset_path = url.substr(webapp_pos + 8); // Skip "/webapp/"
if (!sha1.empty() && !asset_path.empty()) {
auto session_dir = fDirectory / sha1;
auto webapp_file = session_dir / "webapp" / asset_path;
if (fs::exists(webapp_file)) {
// Determine MIME type based on file extension
std::string ext = webapp_file.extension().string();
const char* mime_type = "application/octet-stream";
if (ext == ".html") {
mime_type = "text/html";
} else if (ext == ".js") {
mime_type = "application/javascript";
} else if (ext == ".css") {
mime_type = "text/css";
} else if (ext == ".wasm") {
mime_type = "application/wasm";
} else if (ext == ".json") {
mime_type = "application/json";
} else if (ext == ".png") {
mime_type = "image/png";
} else if (ext == ".svg") {
mime_type = "image/svg+xml";
}
return send_file(connection, webapp_file, mime_type);
}
}
}
if (gVerbosity >= 2) {
std::cerr << "DEBUG: Webapp asset not found for URL: " << url << std::endl;
}
std::string error_msg = "Webapp asset not found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (url.find(".js") != std::string::npos || url.find(".wasm") != std::string::npos ||
url.find(".css") != std::string::npos || url.find(".json") != std::string::npos ||
url.find(".png") != std::string::npos) {
// Handle webapp asset requests that come as direct paths (for iframe compatibility)
// Expected patterns: /<sha>/index.js, /<sha>/faust-ui/index.css,
// /<sha>/faustwasm/create-node.js, etc.
std::vector<std::string> parts = decomposeURL(url);
if (parts.size() >= 2) {
std::string sha1 = parts[1];
// Reconstruct the full asset path (everything after /<sha>/)
std::string asset_path = "";
for (size_t i = 2; i < parts.size(); i++) {
if (!asset_path.empty()) {
asset_path += "/";
}
asset_path += parts[i];
}
auto session_dir = fDirectory / sha1;
auto webapp_file = session_dir / "webapp" / asset_path;
if (fs::exists(webapp_file)) {
// Determine MIME type based on file extension
std::string ext = webapp_file.extension().string();
const char* mime_type = "application/octet-stream";
if (ext == ".html") {
mime_type = "text/html";
} else if (ext == ".js") {
mime_type = "application/javascript";
} else if (ext == ".css") {
mime_type = "text/css";
} else if (ext == ".wasm") {
mime_type = "application/wasm";
} else if (ext == ".json") {
mime_type = "application/json";
} else if (ext == ".png") {
mime_type = "image/png";
} else if (ext == ".svg") {
mime_type = "image/svg+xml";
}
if (gVerbosity >= 2) {
std::cerr << "Serving webapp asset: " << webapp_file << " as " << mime_type
<< std::endl;
}
return send_file(connection, webapp_file, mime_type);
}
}
// If not found as webapp asset, return 404
std::string error_msg = "Webapp asset not found: " + url;
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (gDebugAPI && matchURL(url, "/sessions/list")) {
return serveSessionsList(connection);
} else if (matchURL(url, "/*/user_code.dsp")) {
// Serve the original DSP code from a session
std::vector<std::string> U = decomposeURL(url);
if (U.size() >= 2) {
fs::path filepath = fDirectory / U[1] / "user_code.dsp";
if (fs::exists(filepath)) {
return send_file(connection, filepath, "text/plain");
}
}
std::string error_msg = "DSP file not found";
return send_page(connection, error_msg.c_str(), error_msg.size(), MHD_HTTP_NOT_FOUND,
"text/plain");
} else if (matchURL(url, "/favicon.ico")) {
return page_not_found(connection, "/favicon.ico", 12, "image/x-icon");
} else {
if (gVerbosity >= 1) {
std::cerr << "WARNING: INVALID URL " << url << std::endl;
}
return page_not_found(connection, "/favicon.ico", 12, "image/x-icon");
}
}