-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
1334 lines (986 loc) · 40.2 KB
/
Copy pathserver.c
File metadata and controls
1334 lines (986 loc) · 40.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
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2017, Carl Binding, LI-9494 Schaan
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <ftw.h>
// set include-path!
#include <microhttpd.h>
#include "config.h"
#include "json.h"
#include "utils.h"
#include "nlkup.h"
#include "sessions.h"
#include "logger.h"
// file name of configs
#define CONFIG_FILE_NAME "configs.txt"
// HTTP port default value
#define HTTP_PORT 8888
// distinguish between POST requests...
#define POST_MULTI_PART 2
#define POST_URL_ENCODED 3
#define GET 0
#define POST 1
#define MAX_NBR_PARAMS 16
#define POST_BUFFER_SIZE 4096
#define POST_RESPONSE_BUFFER_SIZE 512
// expect url of form "host:port/nlkup?..."
#define SERVER_URL "/nlkup"
#define GUI_URL "/nlkup_gui"
#define SAVED_FILE_DIRECTORY "/tmp"
#define PATH_SEPARATOR "/"
// GET cmd=alias number=1234567890
// GET cmd=block number=1234567890
// GET cmd=range number=123456 range_postfix_length=4
// GET cmd=range_around number=1234567890 nbr_before=xxx nbr_after=xxx
// POST cmd=delete number=123456890
// POST cmd=insert number=1234567890 alias=1234567890
// POST cmd=dump_file file_name=....
// POST cmd=restore_file file_name=.... binary=true|false
// JSON values for return status
static const char *empty_json = "{}"; // empty JSON object
// generates an JSON empty response.
#define GEN_EMPTY_RESP() MHD_create_response_from_buffer( strlen( empty_json), (void*) empty_json, MHD_RESPMEM_PERSISTENT)
static struct MHD_Response *gen_response_status( const int status) {
char *msg = status_to_json( status, NULL);
return MHD_create_response_from_buffer( strlen( msg), msg, MHD_RESPMEM_MUST_FREE);
}
#define CHECK_ALL_DIGITS( nbr) if ( !all_digits( nbr)) { \
log_msg( ERR, "handle_get_request: badly formatted number %s\n", nbr); \
*http_status = MHD_HTTP_BAD_REQUEST; \
response = GEN_EMPTY_RESP(); \
goto out; \
}
// the range postfix combined with number must not exceed number length
#define CHECK_RANGE_POSTFIX_LENGTH( pfx_len_str, nbr) { \
if ( !all_digits( pfx_len_str)) { \
log_msg( ERR, "handle_get_request: badly formatted range postfix length %s\n", nbr); \
*http_status = MHD_HTTP_BAD_REQUEST; \
response = GEN_EMPTY_RESP(); \
goto out; \
} \
int pfx_len = atoi( (char *) pfx_len_str); \
if ( strlen( nbr) + pfx_len > MAX_NBR_LENGTH ) { \
log_msg( ERR, "handle_get_request: range postfix length too large %s %s\n", nbr, pfx_len_str); \
*http_status = MHD_HTTP_BAD_REQUEST; \
response = GEN_EMPTY_RESP(); \
goto out; \
} \
}
typedef struct {
unsigned char *key;
unsigned char *value;
} KV_KeyValue, *KV_KeyValuePtr;
// per request we create one of these structures. used to collect POST parameters
struct request_info_struct
{
int request_type; // POST or GET
struct MHD_PostProcessor *postprocessor; // to clean things up.
KV_KeyValuePtr key_values[MAX_NBR_PARAMS]; // POST parameters
int kv_len; // usage counter
int post_req_type; // multi-part vs url-encoded
Session session; // session for connection/request. based on cookie id.
};
static int check_logged_in( const Session session) {
if ( session == NULL)
return FALSE;
if ( session->logged_in == TRUE)
return TRUE;
return FALSE;
}
static struct MHD_Response *handle_get_request( struct MHD_Connection *connection,
int *http_status,
struct request_info_struct *req_info,
int is_gui_request) {
long start_time = get_time_micro();
long end_time = 0;
const char *cmd = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "cmd");
const char *nbr = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "number");
struct MHD_Response *response = NULL;
if ( cmd == NULL || nbr == NULL) {
log_msg( ERR, "handle_get_request: missing query parameters\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
if ( strlen( nbr) < PREFIX_LENGTH) {
log_msg( ERR, "handle_get_request: number too short: %s\n", nbr);
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
if ( is_gui_request && strcasecmp( cmd, "range_around") != 0) {
log_msg( ERR, "handle_get_request: disallowed GUI request %s\n", cmd);
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
if ( strcasecmp( cmd, "alias") == 0) { // lookup the alias of the given number
CHECK_ALL_DIGITS( nbr);
unsigned char *alias = NULL;
int status = nlkup_search_entry( nbr, &alias);
// generate some JSON
unsigned char buffer[1024];
memset( buffer, 0, sizeof( buffer));
snprintf( buffer, sizeof( buffer), "{ \"alias\" : \"%s\", \"status\" : %d }\n",
((alias!=NULL?((char *) alias):"")), status);
if ( alias != NULL) {
mem_free( alias); alias = NULL;
}
// set HTTP status and create response
*http_status = MHD_HTTP_OK;
response = MHD_create_response_from_buffer( strlen( buffer), (void *) buffer, MHD_RESPMEM_MUST_COPY);
goto out;
} else if ( strcasecmp( cmd, "block") == 0) {
CHECK_ALL_DIGITS( nbr);
LkupTblPtr table = NULL;
int status = nlkup_get_block( nbr, &table);
unsigned char *buffer = table_to_json( table, status, nbr);
*http_status = MHD_HTTP_OK;
response = MHD_create_response_from_buffer( strlen( buffer), (void *) buffer, MHD_RESPMEM_MUST_FREE);
if ( table != NULL) {
free_lkup_tbl( table); table = NULL;
}
goto out;
} else if ( strcasecmp( cmd, "range") == 0) {
CHECK_ALL_DIGITS( nbr);
const char *range_postfix_length = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "range_postfix_length");
CHECK_RANGE_POSTFIX_LENGTH( range_postfix_length, nbr);
LkupTblPtr table = NULL;
int status = nlkup_get_range( nbr, range_postfix_length, &table);
unsigned char *buffer = table_to_json( table, status, nbr);
*http_status = MHD_HTTP_OK;
response = MHD_create_response_from_buffer( strlen( buffer), (void *) buffer, MHD_RESPMEM_MUST_FREE);
if ( table != NULL) {
free_lkup_tbl( table); table = NULL;
}
goto out;
} else if ( strcasecmp( cmd, "range_around") == 0) {
// check that user is logged in...
if ( is_gui_request && !check_logged_in( req_info->session)) {
log_msg( ERR, "handle_get_request: GUI request %s: not logged in %s\n", cmd, req_info->session->username);
*http_status = MHD_HTTP_UNAUTHORIZED;
response = GEN_EMPTY_RESP();
goto out;
}
CHECK_ALL_DIGITS( nbr);
const char *nbr_before_str = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "nbr_before");
const char *nbr_after_str = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "nbr_after");
if ( IS_NULL( nbr_before_str) || IS_NULL( nbr_after_str)) {
log_msg( ERR, "handle_get_request: missing parameters in range_around\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
// log user's activity
if ( is_gui_request) {
log_msg( INFO, "GET range_around user: %s %s %s %s\n", req_info->session->username, nbr, nbr_before_str, nbr_after_str);
}
if ( !all_digits( nbr_before_str) || !all_digits( nbr_after_str)) {
log_msg( ERR, "handle_get_request: ill-formed parameters in range_around %s %s\n", nbr_before_str, nbr_after_str);
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
int nbr_before = atoi( nbr_before_str);
int nbr_after = atoi( nbr_after_str);
if ( nbr_before <= 0 || nbr_after <= 0) {
log_msg( ERR, "handle_get_request: negative or zero parameters in range_around %s %s\n", nbr_before_str, nbr_after_str);
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
int data_len = 0;
NumberAliasStruct *data = NULL;
int rc = 0;
if ((rc = nlkup_get_range_around( nbr, nbr_before, nbr_after, &data_len, &data)) < 0) {
if ( rc != NOT_ENOUGH_DATA) {
// internal error
log_msg( ERR, "handle_get_request: nlkup_get_range_around failed %s %s %s\n", nbr, nbr_before_str, nbr_after_str);
*http_status = MHD_HTTP_INTERNAL_SERVER_ERROR;
response = GEN_EMPTY_RESP();
goto out;
}
}
log_msg( DEBUG, "handle_get_request: range_around: %s %s %s %d\n", nbr, nbr_before_str, nbr_after_str, data_len);
if ( data_len == 0 || data == NULL) {
// no data
*http_status = MHD_HTTP_NO_CONTENT;
response = GEN_EMPTY_RESP();
goto out;
}
// note that we don't include a status here, because datatables/editor protocol doesn't include such info
// client would need to figure out that less data has been returned.
JSON_Buffer json = number_aliases_to_json( data_len, data);
free( data); data = NULL;
*http_status = MHD_HTTP_OK;
response = MHD_create_response_from_buffer( json_get_length( json), (void *) json_get( json), MHD_RESPMEM_MUST_FREE);
json_free( json, FALSE); json = NULL;
goto out;
} else {
log_msg( ERR, "handle_get_request: unsupported command %s\n", cmd);
*http_status = MHD_HTTP_BAD_REQUEST;
response = GEN_EMPTY_RESP();
goto out;
}
out:
end_time = get_time_micro();
log_msg( INFO, "get request: %s %ld [usec]\n", (cmd!=NULL?cmd:""), (long) (end_time-start_time));
return response;
}
static KV_KeyValuePtr alloc_key_value( const unsigned char *key, const char *value, const int val_len) {
assert( key != NULL && strlen( key) > 0);
assert( value != NULL && val_len > 0);
KV_KeyValuePtr kv = calloc( 1, sizeof( KV_KeyValue));
kv->key = strdup( key);
kv->value = strdup( value);
return kv;
}
static void free_key_value( KV_KeyValuePtr kv) {
if ( kv == NULL) {
return;
}
free( kv->key);
free( kv->value);
memset( kv, 0, sizeof( KV_KeyValue));
free( kv);
}
// searches array of key-values until key is found or returns NULL
static unsigned char *get_key_value( const unsigned char *key, const KV_KeyValuePtr kvs[], int kvs_len, const int strict) {
assert( key != NULL && strlen( key) > 0);
assert( kvs != NULL && kvs_len > 0);
int i = 0;
for ( i = 0; i < kvs_len; i++) {
if ( kvs[i] == NULL) { // end of list
return NULL;
}
if ( strict) { // exact match
if ( strcasecmp( key, kvs[i]->key) == 0) {
return kvs[i]->value;
}
} else { // partial match
const char *cp = strcasestr( kvs[i]->key, key);
if ( cp != NULL) { // key contained
return kvs[i]->value;
}
}
}
return NULL;
}
static int insert_key_value( struct request_info_struct *req_info,
const unsigned char *key,
const unsigned char *value,
const int val_len) {
if ( req_info->kv_len == MAX_NBR_PARAMS) {
log_msg( CRIT, "insert_key_value: out of heap space\n");
return -1;
}
req_info->key_values[req_info->kv_len++] = alloc_key_value( key, value, val_len);
return 0;
}
static unsigned char *get_key_value_from_req_info( struct request_info_struct *req_info,
const unsigned char *key) {
return get_key_value( key, req_info->key_values, req_info->kv_len, TRUE);
}
// we look for a key which is a substring in any of the detected parameter key-value pairs.
// assume that key is sufficiently unique. Ideally some reg-exp matching needed....
static unsigned char *get_matching_key_value_from_req_info( struct request_info_struct *req_info,
const unsigned char *key) {
return get_key_value( key, req_info->key_values, req_info->kv_len, FALSE);
}
static int save_posted_file( const unsigned char *input_elem_name_attr,
const char *file_name,
const char *data,
size_t data_size,
uint64_t data_offset) {
int s = 0;
log_msg( DEBUG, "save_posted_file \"%s\" \"%s\" %d %d\n", input_elem_name_attr, file_name==NULL?"":file_name, (int) data_size, (int) data_offset);
if ( IS_NULL( input_elem_name_attr)) { // value of HTML INPUT element attribute "name"
return -1;
}
if ( IS_NULL( file_name)) { // no file-name. nothing to be saved
// there is possibly some data present,
return 0;
}
if ( data_size <= 0) {
return 0;
}
const char *saved_file_directory = CFG_get_str( "saved_file_directory", SAVED_FILE_DIRECTORY);
char *fn = str_cat( saved_file_directory, PATH_SEPARATOR, file_name, NULL);
if ( fn == NULL) {
log_msg( CRIT, "save_posted_file: str_cat filename\n");
return -1;
}
log_msg( DEBUG, "save_posted_file: fn = %s\n", fn);
char *file_flags = NULL;
if ( data_offset == 0)
file_flags = "w";
else
file_flags = "a";
FILE *f = fopen( fn, file_flags);
if ( f == NULL) {
log_msg( ERR, "save_posted_file: fopen failure %s\n", fn);
s = -1;
goto out;
}
int n = 0;
if (( n = fwrite( data, sizeof( char), data_size, f)) != data_size) {
log_msg( WARN, "save_posted_file: not all data written %d %d\n", data_size, n);
s = -1;
}
if ( fclose( f) < 0) {
log_msg( WARN, "save_posted_file: fclose failure %s\n", fn);
}
out:
if ( fn != NULL) free( fn);
return s;
}
// is called multiple times for POST requests to figure our the key-value pairs contained in the request body
static int
iterate_post (void *coninfo_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_info_struct *req_info = coninfo_cls;
log_msg( DEBUG, "iterate_post \"%s\" \"%s\" \"%s\" %d\n", key, data, filename==NULL?"":filename, (int) size);
if ( req_info->request_type == POST && req_info->post_req_type == POST_MULTI_PART) {
// posting multi-part. each file is one part
// the "name" field in content-disposition becomes the key value....
// this is the "name" given in HTML form input element
// furthermore we should get a non-null file-name, plus the actual data, its size and an offset
if ( save_posted_file( key, filename, data, size, off) < 0) {
return MHD_NO;
}
return MHD_YES;
}
if ( size == 0 || IS_NULL( data)) {
return MHD_NO;
}
// record key value pair in request info
if ( insert_key_value( req_info, key, data, size) < 0) {
return MHD_NO;
}
return MHD_YES;
}
// upcall when HTTP request fully processed
static void request_completed (void *cls, struct MHD_Connection *connection,
void **con_cls, enum MHD_RequestTerminationCode toe)
{
struct request_info_struct *req_info = *con_cls;
log_msg( DEBUG, "request_completed\n");
if (NULL == req_info) {
return;
}
if (req_info->request_type == POST) {
// destroy postprocessor
MHD_destroy_post_processor (req_info->postprocessor);
req_info->postprocessor = NULL;
// free key-value pairs if any
int i = 0;
for ( i = 0; i < req_info->kv_len; i++) {
free_key_value( req_info->key_values[i]);
req_info->key_values[i] = NULL;
}
}
if ( NULL != req_info->session) {
// THREAD SAFETY ?
req_info->session->ref_cnt--;
}
// release memory
free (req_info);
*con_cls = NULL;
}
// allocates a request info structure which is mainly used for POST requests to collect various key-value pairs
static int alloc_request_info( struct MHD_Connection *connection,
const char *method,
void **con_cls,
int post_req_type) {
struct request_info_struct *req_info;
req_info = calloc (1, sizeof (struct request_info_struct));
if (NULL == req_info) {
return MHD_NO;
}
if (0 == strcasecmp (method, "POST")) {
req_info->postprocessor = MHD_create_post_processor (connection, POST_BUFFER_SIZE,
iterate_post, (void *) req_info);
if (NULL == req_info->postprocessor) {
log_msg( ERR, "failure to MHD_create_post_processor. content-encoding missing?\n");
free (req_info);
return MHD_NO;
}
req_info->request_type = POST;
req_info->post_req_type = post_req_type;
} else if ( 0 == strcasecmp( method, "GET")) {
req_info->request_type = GET;
} else {
log_msg( ERR, "unsupported method: %s\n", method);
free( req_info);
return MHD_NO;
}
*con_cls = (void *) req_info;
return MHD_YES;
}
// checks if a POST has a supported content-type.
static int check_post_has_content_type( struct MHD_Connection *connection,
const char *method) {
if ( strcasecmp( method, "POST") != 0) { // not a post
return 1;
}
const char *content_type = MHD_lookup_connection_value ( connection, MHD_HEADER_KIND, "content-type");
log_msg( DEBUG, "content-type: %s\n", content_type == NULL ? "null":content_type);
if ( content_type == NULL) { // no such header field...
return 0;
}
// microhttpd only supports some content-types for post-processor creation...
// content-type for multi-part encoding also includes boundary
if ( strncasecmp( content_type, MHD_HTTP_POST_ENCODING_FORM_URLENCODED, strlen( MHD_HTTP_POST_ENCODING_FORM_URLENCODED)) == 0) {
return POST_URL_ENCODED;
} else if ( strncasecmp( content_type, MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, strlen( MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)) == 0) {
return POST_MULTI_PART;
}
// wrong content type...
return 0;
}
static int gen_json_response( unsigned char buf[], int buf_sz, int status) {
unsigned char *cp = buf;
int cnt = 0;
cnt += snprintf( cp+cnt, buf_sz-cnt, "{ \"status\" : %d }\n", status);
return 0;
}
static int check_credentials( const char *username, const char *password) {
return SUCCESS;
}
static struct MHD_Response *handle_login( struct request_info_struct *req_info,
int *http_status,
const char *cmd) {
const unsigned char *username = get_key_value_from_req_info( req_info, "username");
const unsigned char *password = get_key_value_from_req_info( req_info, "password");
struct MHD_Response *response = NULL;
if ( IS_NULL( username) || IS_NULL( password) ) {
log_msg( WARN, "missing or empty credentials in POST login request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
int rc = check_credentials( username, password);
if ( rc == SUCCESS) {
*http_status = MHD_HTTP_OK;
response = gen_response_status( SUCCESS);
Session session = req_info->session;
session->logged_in = TRUE;
snprintf( session->username, sizeof( session->username), "%s", username);
log_msg( INFO, "user %s logged in, session_id %s\n", username, session->session_id);
goto out;
} else {
log_msg( WARN, "check_credentials failed in POST login request %s %s\n", username, password);
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( rc);
goto out;
}
out:
return response;
}
static struct MHD_Response *handle_edit( struct request_info_struct *req_info,
int *http_status,
const char *cmd) {
struct MHD_Response *response = NULL;
if ( !check_logged_in( req_info->session)) {
log_msg( ERR, "handle_edit: GUI request %s: not logged in %s\n", cmd, req_info->session->username);
*http_status = MHD_HTTP_UNAUTHORIZED;
return GEN_EMPTY_RESP();
}
return NULL;
}
static struct MHD_Response *handle_remove( struct request_info_struct *req_info,
int *http_status,
const char *cmd) {
struct MHD_Response *response = NULL;
if ( !check_logged_in( req_info->session)) {
log_msg( ERR, "handle_remove: GUI request %s: not logged in %s\n", cmd, req_info->session->username);
*http_status = MHD_HTTP_UNAUTHORIZED;
return GEN_EMPTY_RESP();
}
// for the datatables/editor protocol format see https://editor.datatables.net/manual/server
// "data[<key>][number]" = ... and "data[<key>][alias]" = ...
const char *number = get_matching_key_value_from_req_info( req_info, "[number]");
const char *alias = get_matching_key_value_from_req_info( req_info, "[alias]");
if ( IS_NULL( number) || IS_NULL( alias)) {
log_msg( ERR, "handle_remove: GUI request %s: null number or alias\n", cmd);
*http_status = MHD_HTTP_BAD_REQUEST;
return GEN_EMPTY_RESP();
}
log_msg( INFO, "handle_remove: user %s number %s alias %s\n", req_info->session->username, number, alias);
*http_status = MHD_HTTP_OK;
if ( nlkup_delete_entry( number) < 0) {
log_msg( ERR, "handle_remove: nlkup_delete_entry failure\n");
return gen_response_status( FAILURE);
}
return gen_response_status( SUCCESS);
}
static struct MHD_Response *handle_create( struct request_info_struct *req_info,
int *http_status,
const char *cmd) {
if ( !check_logged_in( req_info->session)) {
log_msg( ERR, "handle_create: GUI request %s: not logged in %s\n", cmd, req_info->session->username);
*http_status = MHD_HTTP_UNAUTHORIZED;
return GEN_EMPTY_RESP();
}
// for the datatables/editor protocol format see https://editor.datatables.net/manual/server
// "data[<key>][number]" = ... and "data[<key>][alias]" = ...
const char *number = get_matching_key_value_from_req_info( req_info, "[number]");
const char *alias = get_matching_key_value_from_req_info( req_info, "[alias]");
if ( IS_NULL( number) || IS_NULL( alias)) {
log_msg( ERR, "handle_create: GUI request %s: null number or alias\n", cmd);
*http_status = MHD_HTTP_BAD_REQUEST;
return GEN_EMPTY_RESP();
}
log_msg( INFO, "handle_create: user %s number %s alias %s\n", req_info->session->username, number, alias);
*http_status = MHD_HTTP_OK;
if ( nlkup_enter_entry( number, alias) < 0) {
log_msg( ERR, "handle_create: nlkup_enter_entry failure\n");
return gen_response_status( FAILURE);
}
return gen_response_status( SUCCESS);
}
static struct MHD_Response *handle_post_request_url_encoded( struct MHD_Connection *connection,
struct request_info_struct *req_info,
int *http_status,
const char *cmd,
int is_gui_request)
{
unsigned char *response_buffer = calloc( POST_RESPONSE_BUFFER_SIZE, sizeof( unsigned char));
int response_status = 0;
struct MHD_Response *response = NULL;
*http_status = MHD_HTTP_OK;
if ( IS_NULL( cmd)) {
log_msg( WARN, "no or empty command in POST request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
// GUI commands come from datatable/editor and thus have special formats.
// we use a different route for GUI commands "/nlkup_gui"
if ( is_gui_request) {
if ( strcasecmp( cmd, "edit") == 0) {
response = handle_edit( req_info, http_status, cmd);
} else if ( strcasecmp( cmd, "remove") == 0) {
response = handle_remove( req_info, http_status, cmd);
} else if ( strcasecmp( cmd, "create") == 0) {
response = handle_create( req_info, http_status, cmd);
} else if ( strcasecmp( cmd, "login") == 0) {
response = handle_login( req_info, http_status, cmd);
} else {
log_msg( WARN, "unhandled GUI command in POST request: %s\n", cmd);
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
}
return response;
}
// non GUI commands
if ( strcasecmp( cmd, "delete") == 0) {
const unsigned char *number = get_key_value_from_req_info( req_info, "number");
if ( IS_NULL( number)) {
log_msg( WARN, "missing or empty number in POST delete request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
if ( strlen( number) < PREFIX_LENGTH) {
log_msg( WARN, "number too short in POST delete request %s\n", number);
response_status = NBR_TOO_SHORT;
} else if ( !all_digits( number)) {
log_msg( WARN, "number not all digits in POST delete request %s\n", number);
response_status = ILLEGAL_NUMBER;
} else {
response_status = nlkup_delete_entry( number);
}
gen_json_response( response_buffer, POST_RESPONSE_BUFFER_SIZE, response_status);
response = MHD_create_response_from_buffer( strlen( response_buffer), response_buffer, MHD_RESPMEM_MUST_FREE);
goto out;
} else if ( strcasecmp( cmd, "insert") == 0) {
const unsigned char *number = get_key_value_from_req_info( req_info, "number");
const unsigned char *alias = get_key_value_from_req_info( req_info, "alias");
if ( IS_NULL( number) || IS_NULL( alias)) {
log_msg( WARN, "missing or empty number or alias in POST insert request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
if ( strlen( number) < PREFIX_LENGTH || strlen( alias) < PREFIX_LENGTH) {
log_msg( WARN, "number or alias too short in POST insert request %s %s\n", number, alias);
response_status = NBR_TOO_SHORT;
} else if ( !all_digits( number) || !all_digits( alias)) {
log_msg( WARN, "number or alias not all digits in POST insert request %s %s\n", number, alias);
response_status = ILLEGAL_NUMBER;
} else {
response_status = nlkup_enter_entry( number, alias);
}
gen_json_response( response_buffer, POST_RESPONSE_BUFFER_SIZE, response_status);
response = MHD_create_response_from_buffer( strlen( response_buffer), response_buffer, MHD_RESPMEM_MUST_FREE);
goto out;
} else if ( strcasecmp( cmd, "dump_file") == 0) {
const unsigned char *file_name = get_key_value_from_req_info( req_info, "file_name");
if ( IS_NULL( file_name)) {
log_msg( WARN, "missing or empty file_name in POST dump_file request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
gen_json_response( response_buffer, POST_RESPONSE_BUFFER_SIZE, response_status);
response = MHD_create_response_from_buffer( strlen( response_buffer), response_buffer, MHD_RESPMEM_MUST_FREE);
goto out;
} else if ( strcasecmp( cmd, "restore_file") == 0) {
const unsigned char *file_name = get_key_value_from_req_info( req_info, "file_name");
if ( IS_NULL( file_name) ) {
log_msg( WARN, "missing or empty file_name in POST restore_file request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
gen_json_response( response_buffer, POST_RESPONSE_BUFFER_SIZE, response_status);
response = MHD_create_response_from_buffer( strlen( response_buffer), response_buffer, MHD_RESPMEM_MUST_FREE);
goto out;
#if 0
} else if ( strcasecmp( cmd, "login") == 0) {
const unsigned char *username = get_key_value_from_req_info( req_info, "username");
const unsigned char *password = get_key_value_from_req_info( req_info, "password");
if ( IS_NULL( username) || IS_NULL( password) ) {
log_msg( WARN, "missing or empty credentials in POST login request\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
int rc = check_credentials( username, password);
if ( rc == SUCCESS) {
*http_status = MHD_HTTP_OK;
response = gen_response_status( SUCCESS);
Session session = req_info->session;
session->logged_in = TRUE;
snprintf( session->username, sizeof( session->username), "%s", username);
log_msg( INFO, "user %s logged in, session_id %s\n", username, session->session_id);
goto out;
} else {
log_msg( WARN, "check_credentials failed in POST login request %s %s\n", username, password);
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( rc);
goto out;
}
#endif
} else {
log_msg( WARN, "unknown command in POST request: %s\n", cmd);
*http_status = MHD_HTTP_BAD_REQUEST;
response = gen_response_status( FAILURE);
goto out;
}
out:
return response;
}
static struct MHD_Response *handle_post_request_multi_part( struct MHD_Connection *connection,
struct request_info_struct *req_info,
int *http_status,
int is_gui_request) {
const char *response_string = "{ \"status\" : 0 }";
struct MHD_Response *response = NULL;
// the processing of the request really happens in the post-processor callback....
*http_status = MHD_HTTP_BAD_REQUEST;
response = MHD_create_response_from_buffer( strlen( response_string), (void*) response_string, MHD_RESPMEM_PERSISTENT);
return response;
}
// after collecting all the parameters we handle the POST request here
static struct MHD_Response *handle_post_request( struct MHD_Connection *connection,
struct request_info_struct *req_info,
int *http_status,
int is_gui_request) {
const char *error_status = "{ \"status\": -1}"; // JSON status code
*http_status = MHD_HTTP_OK;
long start_time = get_time_micro();
long end_time = 0;
struct MHD_Response *response = NULL;
char *cmd = NULL;
if ( req_info->post_req_type == POST_MULTI_PART) {
cmd = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
response = handle_post_request_multi_part( connection, req_info, http_status, is_gui_request);
} else if ( req_info->post_req_type == POST_URL_ENCODED) {
cmd = get_key_value_from_req_info( req_info, "cmd");
// the GUI uses datatables/editor https://datatables.net/
// for delete, create, and edit the parameter is "action"...
if ( is_gui_request && cmd == NULL) {
cmd = get_key_value_from_req_info( req_info, "action");
}
response = handle_post_request_url_encoded( connection, req_info, http_status, cmd, is_gui_request);
} else {
log_msg( ERR, "unhandled post request type\n");
*http_status = MHD_HTTP_BAD_REQUEST;
response = MHD_create_response_from_buffer( strlen( error_status), (void*) error_status, MHD_RESPMEM_PERSISTENT);
}
end_time = get_time_micro();
log_msg( INFO, "post request: %s %ld [usec]\n", (cmd!=NULL?cmd:""), (long) (end_time-start_time));
return response;
}
int answer_to_request (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 **con_cls)
{
struct MHD_Response *response = NULL;