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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
#include "web.h"
#include "app.h"
#include "html.h"
#include "mytime.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <microhttpd.h>
#include <cjson/cJSON.h>
#include <fcntl.h>
#include <crypt.h>
#define PAGE "<html><head><title>libmicrohttpd demo</title>"\
"</head><body>libmicrohttpd demo</body></html>"
static int generate_secret(char *out) {
unsigned char bytes[16];
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) return -1;
ssize_t n = read(fd, bytes, 16);
close(fd);
if (n != 16) return -1;
static const char hex[] = "0123456789abcdef";
for (int i = 0; i < 16; i++) {
out[i*2] = hex[(bytes[i] >> 4) & 0x0f];
out[i*2+1] = hex[bytes[i] & 0x0f];
}
out[32] = '\0';
return 0;
}
static char *generate_session_token(struct App *app, const char *username, const char *password) {
char combined[128];
snprintf(combined, sizeof(combined), "%s:%s", username, password);
return crypt(combined, app->session_secret);
}
static int validate_credentials(struct App *app, const char *username, const char *password) {
return strcmp(username, app->admin_user) == 0 && strcmp(password, app->admin_pass) == 0;
}
static int is_authenticated(struct App *app, struct MHD_Connection *connection) {
const char *cookie = MHD_lookup_connection_value(connection, MHD_COOKIE_KIND, "session");
if (cookie == NULL) return 0;
char *expected = generate_session_token(app, app->admin_user, app->admin_pass);
if (expected == NULL) return 0;
return strcmp(cookie, expected) == 0;
}
static enum MHD_Result create_post_collector(struct PostCollector *c) {
c->data = malloc(512);
c->size = 0;
c->capacity = 512;
return MHD_YES;
}
static enum MHD_Result collect_post(struct PostCollector *c, const char *data, size_t *data_len) {
size_t newSize = c->size + *data_len;
if(newSize > c->capacity) {
while(newSize > c->capacity) c->capacity *= 2;
c->data = realloc(c->data, c->capacity);
}
memcpy(c->data + c->size, data, *data_len);
c->size = newSize;
return MHD_YES;
}
static enum MHD_Result destroy_post_collector(struct PostCollector *c) {
if(c->capacity != 0) {
free(c->data);
c->capacity = 0;
}
return MHD_YES;
}
static enum MHD_Result
form_iterator(void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *filename,
const char *content_type,
const char *transfer_encoding,
const char *data,
uint64_t off,
size_t size)
{
struct Request *request = cls;
if (strcmp(key, "name") == 0 && off + size < CLIENT_NAME_MAX) {
memcpy(request->form.name + off, data, size);
request->form.name[off + size] = '\0';
request->form.name_set = 1;
} else if (strcmp(key, "client_id") == 0 && off == 0) {
char buf[32] = {0};
size_t copy_size = size < 31 ? size : 31;
memcpy(buf, data, copy_size);
request->form.client_id = strtoll(buf, NULL, 10);
request->form.client_id_set = 1;
} else if (strcmp(key, "username") == 0 && off + size < 32) {
memcpy(request->form.username + off, data, size);
request->form.username[off + size] = '\0';
request->form.username_set = 1;
} else if (strcmp(key, "password") == 0 && off + size < 64) {
memcpy(request->form.password + off, data, size);
request->form.password[off + size] = '\0';
request->form.password_set = 1;
}
return MHD_YES;
}
static enum HandlerStatus handle_logout(
struct MHD_Connection *connection,
struct Request *request
) {
if (strcmp(request->url, "/logout") != 0)
return HANDLER_DECLINE;
if (strcmp(request->method, "POST") != 0)
return HANDLER_DECLINE;
struct MHD_Response *response = MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Set-Cookie", "session=; Path=/; HttpOnly; Max-Age=0");
MHD_add_response_header(response, "Location", "/login");
enum MHD_Result ret = MHD_queue_response(connection, MHD_HTTP_SEE_OTHER, response);
MHD_destroy_response(response);
return ret == MHD_YES ? HANDLER_ACCEPTED : HANDLER_ERROR;
}
static enum HandlerStatus handle_login(
struct MHD_Connection *connection,
struct Request *request
) {
if (strcmp(request->url, "/login") != 0)
return HANDLER_DECLINE;
struct Server *server = request->server;
enum MHD_Result ret;
if (strcmp(request->method, "POST") == 0) {
if (request->post_proc == NULL && !request->form.username_set) {
request->post_proc = MHD_create_post_processor(
connection, 1024, &form_iterator, request);
if (request->post_proc == NULL)
return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
MHD_destroy_post_processor(request->post_proc);
request->post_proc = NULL;
if (!request->form.username_set || !request->form.password_set) {
size_t html_len;
char *html = html_render_login_page("Invalid credentials", &html_len);
if (!html) return HANDLER_ERROR;
struct MHD_Response *response = MHD_create_response_from_buffer(
html_len, html, MHD_RESPMEM_MUST_FREE);
ret = MHD_add_response_header(response, "Content-Type", "text/html");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
if (!validate_credentials(server->app, request->form.username, request->form.password)) {
size_t html_len;
char *html = html_render_login_page("Invalid credentials", &html_len);
if (!html) return HANDLER_ERROR;
struct MHD_Response *response = MHD_create_response_from_buffer(
html_len, html, MHD_RESPMEM_MUST_FREE);
ret = MHD_add_response_header(response, "Content-Type", "text/html");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
char *token = generate_session_token(server->app, request->form.username, request->form.password);
if (token == NULL) return HANDLER_ERROR;
char cookie_header[128];
snprintf(cookie_header, sizeof(cookie_header), "session=%s; Path=/; HttpOnly", token);
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Set-Cookie", cookie_header);
MHD_add_response_header(response, "Location", "/clients");
ret = MHD_queue_response(connection, MHD_HTTP_SEE_OTHER, response);
MHD_destroy_response(response);
return ret == MHD_YES ? HANDLER_ACCEPTED : HANDLER_ERROR;
}
if (strcmp(request->method, "GET") == 0) {
size_t html_len;
char *html = html_render_login_page(NULL, &html_len);
if (!html) return HANDLER_ERROR;
struct MHD_Response *response = MHD_create_response_from_buffer(
html_len, html, MHD_RESPMEM_MUST_FREE);
ret = MHD_add_response_header(response, "Content-Type", "text/html");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
return HANDLER_DECLINE;
}
static enum HandlerStatus handle_clients(
struct MHD_Connection *connection,
struct Request *request
) {
if (strcmp(request->url, "/clients") != 0)
return HANDLER_DECLINE;
struct Server *server = request->server;
enum MHD_Result ret;
if (strcmp(request->method, "POST") == 0) {
if (request->post_proc == NULL && !request->form.name_set) {
request->post_proc = MHD_create_post_processor(
connection, 1024, &form_iterator, request);
if (request->post_proc == NULL)
return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
MHD_destroy_post_processor(request->post_proc);
request->post_proc = NULL;
if (!is_authenticated(server->app, connection)) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Location", "/login");
ret = MHD_queue_response(connection, MHD_HTTP_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
if (!request->form.name_set || request->form.name[0] == '\0') {
return HANDLER_ERROR;
}
struct CreateClientResult result;
ret = create_client(server->app, request->form.name, &result);
if(ret != 0) return HANDLER_ERROR;
// Post-Redirect-Get: redirect back to GET /clients
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
ret = MHD_add_response_header(response, "Location", "/clients");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(connection, MHD_HTTP_SEE_OTHER, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
if (strcmp(request->method, "GET") == 0) {
if (!is_authenticated(server->app, connection)) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Location", "/login");
ret = MHD_queue_response(connection, MHD_HTTP_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
struct ListClientResult *clients = malloc(sizeof(struct ListClientResult) + sizeof(struct ListClientResultClient) * 16);
ret = list_clients(server->app, clients);
if(ret != 0) {
free(clients);
return HANDLER_ERROR;
}
size_t html_len;
char *html = html_render_clients_page(request, clients, &html_len);
free(clients);
if (!html) {
return HANDLER_ERROR;
}
struct MHD_Response *response = MHD_create_response_from_buffer(
html_len,
html,
MHD_RESPMEM_MUST_FREE
);
ret = MHD_add_response_header(response, "Content-Type", "text/html");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(
connection,
MHD_HTTP_OK,
response
);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
return HANDLER_DECLINE;
}
static enum HandlerStatus handle_clients_delete(
struct MHD_Connection *connection,
struct Request *request
) {
if (strcmp(request->url, "/clients/delete") != 0)
return HANDLER_DECLINE;
if (strcmp(request->method, "POST") != 0)
return HANDLER_DECLINE;
struct Server *server = request->server;
enum MHD_Result ret;
if (request->post_proc == NULL && !request->form.client_id_set) {
request->post_proc = MHD_create_post_processor(
connection, 1024, &form_iterator, request);
if (request->post_proc == NULL)
return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
MHD_destroy_post_processor(request->post_proc);
request->post_proc = NULL;
if (!is_authenticated(server->app, connection)) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Location", "/login");
ret = MHD_queue_response(connection, MHD_HTTP_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
if (!request->form.client_id_set || request->form.client_id <= 0) {
return HANDLER_ERROR;
}
ret = delete_client(server->app, request->form.client_id);
if(ret != 0) return HANDLER_ERROR;
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
ret = MHD_add_response_header(response, "Location", "/clients");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(connection, MHD_HTTP_SEE_OTHER, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
static enum HandlerStatus handle_api_report(
struct MHD_Connection *connection,
struct Request *request
) {
if (strcmp(request->url, "/api/report") != 0)
return HANDLER_DECLINE;
if (strcmp(request->method, "POST") != 0)
return HANDLER_DECLINE;
struct Server *server = request->server;
enum MHD_Result ret;
if (request->pp.capacity == 0) {
create_post_collector(&request->pp);
return HANDLER_ACCEPTED;
}
cJSON *rootJson = cJSON_ParseWithLength(request->pp.data, request->pp.size);
cJSON *clientJson = cJSON_GetObjectItemCaseSensitive(rootJson, "client");
if(!cJSON_IsString(clientJson) || clientJson->valuestring == NULL) {
abort();
}
cJSON *secretJson = cJSON_GetObjectItemCaseSensitive(rootJson, "secret");
if(!cJSON_IsString(secretJson) || secretJson->valuestring == NULL) {
abort();
}
cJSON *eventJson = cJSON_GetObjectItemCaseSensitive(rootJson, "event");
if(!cJSON_IsString(eventJson) || eventJson->valuestring == NULL) {
abort();
}
int is_finish;
if(strcmp(eventJson->valuestring, "begin_backup") == 0) {
is_finish = 0;
} else if(strcmp(eventJson->valuestring, "end_backup") == 0) {
is_finish = 1;
} else {
abort();
}
ret = submit_report(server->app, clientJson->valuestring, secretJson->valuestring, is_finish);
if(ret == -2) {
cJSON_Delete(rootJson);
destroy_post_collector(&request->pp);
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT
);
ret = MHD_add_response_header(response, "Content-Type", "application/json");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(
connection,
MHD_HTTP_UNAUTHORIZED,
response
);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
if(ret != 0) {
return HANDLER_ERROR;
}
cJSON_Delete(rootJson);
destroy_post_collector(&request->pp);
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT
);
ret = MHD_add_response_header(response, "Content-Type", "application/json");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(
connection,
MHD_HTTP_NO_CONTENT,
response
);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
static enum HandlerStatus handle_client_detail(
struct MHD_Connection *connection,
struct Request *request
) {
if (strncmp(request->url, "/client/", 8) != 0)
return HANDLER_DECLINE;
if (strcmp(request->method, "GET") != 0)
return HANDLER_DECLINE;
struct Server *server = request->server;
enum MHD_Result ret;
if (!is_authenticated(server->app, connection)) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Location", "/login");
ret = MHD_queue_response(connection, MHD_HTTP_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
int64_t client_id;
if (sscanf(request->url + 8, "%ld", &client_id) != 1 || client_id <= 0) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
struct GetClientOnlyResult client_result;
ret = get_client(server->app, client_id, &client_result);
if(ret != 0) {
return HANDLER_ERROR;
}
if(!client_result.found) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT
);
ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
struct GetBackupsResult *backups_result = malloc(sizeof(struct GetBackupsResult) + sizeof(struct BackupRecord) * MAX_BACKUPS_DISPLAY);
backups_result->backups_len = MAX_BACKUPS_DISPLAY;
ret = get_client_backups(server->app, client_id, backups_result);
if(ret != 0) {
free(backups_result);
return HANDLER_ERROR;
}
size_t html_len;
char *html = html_render_client_detail_page(request, &client_result, backups_result, &html_len);
free(backups_result);
if (!html) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT
);
ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
struct MHD_Response *response = MHD_create_response_from_buffer(
html_len,
html,
MHD_RESPMEM_MUST_FREE
);
ret = MHD_add_response_header(response, "Content-Type", "text/html");
if(ret != MHD_YES) return HANDLER_ERROR;
ret = MHD_queue_response(
connection,
MHD_HTTP_OK,
response
);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
static enum HandlerStatus handle_root(
struct MHD_Connection *connection,
struct Request *request
) {
if (strcmp(request->url, "/") != 0)
return HANDLER_DECLINE;
if (strcmp(request->method, "GET") != 0)
return HANDLER_DECLINE;
struct Server *server = request->server;
enum MHD_Result ret;
if (!is_authenticated(server->app, connection)) {
struct MHD_Response *response = MHD_create_response_from_buffer(
0, "", MHD_RESPMEM_PERSISTENT
);
MHD_add_response_header(response, "Location", "/login");
ret = MHD_queue_response(connection, MHD_HTTP_FOUND, response);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
struct MHD_Response *response = MHD_create_response_from_buffer(
strlen(server->page),
(void *)server->page,
MHD_RESPMEM_PERSISTENT
);
ret = MHD_queue_response(
connection,
MHD_HTTP_OK,
response
);
MHD_destroy_response(response);
if(ret != MHD_YES) return HANDLER_ERROR;
return HANDLER_ACCEPTED;
}
static enum HandlerStatus handle_not_found(
struct MHD_Connection *connection,
struct Request *request
) {
// This handler MUST accept the request since it will be the last one in
// the chain
struct MHD_Response *response = MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT);
MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response(response);
return HANDLER_ACCEPTED;
}
static RouteHandler handlers[] = {
handle_logout,
handle_login,
handle_clients,
handle_clients_delete,
handle_api_report,
handle_client_detail,
handle_root,
handle_not_found, // Catch all
};
#define HANDLER_COUNT (sizeof(handlers) / sizeof(handlers[0]))
static enum MHD_Result handler(
void *cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **ptr
) {
struct Server *server = cls;
struct Request *request = *ptr;
if (request == NULL) {
request = calloc(1, sizeof(struct Request));
if(request == NULL) {
return MHD_NO;
}
*ptr = request;
request->server = server;
request->url = url;
request->method = method;
// @COMPL: The proxy might rewrite this, and we should support that
request->host = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Host");
// @COMPL: There are other headers we should look into
request->proto = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "X-Forwarded-Proto");
// Fall through - handlers will set up POST processing if needed
}
if (*upload_data_size != 0) {
if (request->post_proc != NULL) {
MHD_post_process(request->post_proc, upload_data, *upload_data_size);
} else if (request->pp.capacity > 0) {
collect_post(&request->pp, upload_data, upload_data_size);
}
*upload_data_size = 0;
return MHD_YES;
}
for (size_t i = 0; i < HANDLER_COUNT; i++) {
enum HandlerStatus status = handlers[i](connection, request);
if (status == HANDLER_ACCEPTED) return MHD_YES;
if (status == HANDLER_ERROR) return MHD_NO;
// HANDLER_DECLINE: try next
}
// Should never reach here - handle_not_found always accepts
abort();
}
static void request_completed_callback (
void *cls,
struct MHD_Connection *connection,
void **con_cls,
enum MHD_RequestTerminationCode toe
) {
struct Request *request = *con_cls;
if (request->post_proc != NULL) {
MHD_destroy_post_processor(request->post_proc);
}
destroy_post_collector(&request->pp);
free(request);
}
static struct MHD_Daemon *d = NULL;
int web_begin(struct Server *server, int port, const char *admin_user, const char *admin_pass) {
assert(d == NULL);
server->page = PAGE;
server->app->admin_user = admin_user;
server->app->admin_pass = admin_pass;
if (generate_secret(server->app->session_secret) != 0) {
return 1;
}
d = MHD_start_daemon(
MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DUAL_STACK,
port,
NULL, NULL,
&handler, server,
MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
MHD_OPTION_END
);
if (d == NULL) return 1;
return 0;
}
int web_join(struct Server *server) {
assert(d != NULL);
MHD_stop_daemon(d);
d = NULL;
return 0;
}
|