-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwebapi.c
909 lines (794 loc) · 24.1 KB
/
webapi.c
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
/*
* Copyright (c) 2012, Toby Jaffey <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
// FIXME
//on unsubscribe, walk msgcache for all matching matching messages and remove them
#include <common/common.h>
#include <common/logging.h>
#include <common/util.h>
#include <common/cJSON.h>
#include "mqtthttpd.h"
#include "httpd.h"
#include "webapi.h"
#include "webapi_serve.h"
#include "midconnlist.h"
#include "streamlist.h"
#include "stream.h"
#include "sublist.h"
#include "msgcache.h"
#include "accesslog.h"
#define MAX_PATH_ELEMS 256
const int MAX_STREAMS = 256;
extern server_context_t g_srvctx;
static void inject_retained(const char *topic, const char *msg, void *userdata);
static char *json_escape(const char *in)
{
char *out;
size_t len = strlen(in);
char *outp;
if (NULL == (outp = out = malloc(len*2+3))) // worst case
return NULL;
*outp++ = '"';
while(*in)
{
switch(*in)
{
case '"':
*outp++ = '\\';
*outp++ = '"';
break;
case '\\':
*outp++ = '\\';
*outp++ = '\\';
break;
case '\r':
*outp++ = '\\';
*outp++ = 'r';
break;
case '\n':
*outp++ = '\\';
*outp++ = 'n';
break;
case '\t':
*outp++ = '\\';
*outp++ = 't';
break;
/*
case '\'':
*outp++ = '\\';
*outp++ = '\'';
break;
*/
/*
case '/':
*outp++ = '\\';
*outp++ = '/';
break;
*/
default:
*outp++ = *in;
}
in++;
}
*outp++ = '"';
*outp = 0;
return out;
}
static void stream_drainer(stream_t *stream, const char *str, bool first, void *userdata)
{
uint32_t connid = (uint32_t)(uintptr_t)userdata;
LOG_DEBUG("drainer: %s first=%d", str, first);
httpd_printf(connid, "%s%s", first ? "" : ",", str);
}
static bool strm_clr(idset_t *ids, uint32_t streamid, void *userdata)
{
stream_t *stream;
if (NULL == (stream = streamlist_find(streamid)))
{
LOG_ERROR("streamid %u not in list!", streamid);
return true;
}
stream_clear_connection(stream);
return true;
}
void httpd_request_complete(uint32_t connid, ebb_connection_info *conninfo)
{
LOG_DEBUG("req complete connid=%u", connid);
}
static void fail_conn(int mid, uint32_t connid, void *userdata)
{
ebb_connection_info *conninfo;
if (NULL != (conninfo = httpd_get_conninfo(connid)))
{
conninfo->http_status = 503;
httpd_close(connid);
}
}
void webapi_mqtt_failall_cb(void)
{
midconn_foreach(fail_conn, NULL);
}
void webapi_publish_cb(int mid)
{
uint32_t connid;
if (0 == midconn_find(mid, &connid))
{
httpd_close(connid);
}
else
{
LOG_ERROR("webapi_publish_cb mid not found %d", mid);
}
midconn_remove(mid);
}
struct topic_message
{
const char *topic;
const char *msg;
};
bool message_to_stream(uint32_t streamid, stream_t *stream, void *userdata)
{
struct topic_message *tm = (struct topic_message *)userdata;
char *s = NULL;
LOG_DEBUG("rx check: %s %s", stream->topic_patt, tm->topic);
if (topic_match_string(stream->topic_patt, tm->topic))
{
size_t len;
LOG_DEBUG("%s:%s -> stream %u", tm->topic, tm->msg, streamid);
len = strlen(tm->topic)*2 + strlen(tm->msg)*2 + 8; // {"":""} + null // FIXME
if (NULL == (s = (char *)malloc(len)))
LOG_ERROR("out of mem");
else
{
uint32_t connid;
ebb_connection_info *conninfo = NULL;
bool connected;
connected = stream_get_connection(stream, &connid);
if (connected)
{
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
return false;
}
if (conninfo->finished) // if conn is in process of closing,
connected = false;
}
if (conninfo && conninfo->rawmode)
{
// just the data
memcpy(s, tm->msg, strlen(tm->msg)+1);
}
else
{
// wrapped in JSON obj
cJSON *mj = NULL;
char *msgesc;
char *topicesc;
if (NULL == (topicesc = json_escape(tm->topic)))
{
conninfo->http_status = 503;
httpd_close(connid);
return false;
}
if (tm->msg[0] == 0)
{
snprintf(s, len, "{%s:\"\"}", topicesc);
}
else
if (0==strcmp(tm->msg, "inf"))
{
snprintf(s, len, "{%s:\"inf\"}", topicesc);
}
else
if (NULL != (mj = cJSON_Parse(tm->msg)))
{
snprintf(s, len, "{%s:%s}", topicesc, cJSON_PrintUnformatted(mj));
cJSON_Delete(mj);
}
else
{
if (NULL == (msgesc = json_escape(tm->msg)))
{
conninfo->http_status = 503;
httpd_close(connid);
free(topicesc);
return false;
}
else
{
snprintf(s, len, "{%s:%s}", topicesc, msgesc);
free(msgesc);
}
}
free(topicesc);
}
if (0 != stream_push(stream, s))
LOG_ERROR("stream_push %u failed", streamid);
else
{
if (connected)
{
if (!conninfo->rawmode)
httpd_printf(connid, "[");
stream_drain(stream, stream_drainer, (void *)(uintptr_t)connid);
if (!conninfo->rawmode)
httpd_printf(connid, "]");
stream_clear_connection(stream);
httpd_close(connid);
}
else
{
LOG_DEBUG("streamid %u not connected", stream->streamid);
}
}
}
}
if (NULL != s)
free(s);
return false;
}
void webapi_message_cb(int mid, char *topic, char *msg)
{
struct topic_message tm = {topic, msg};
LOG_DEBUG("webapi_message_cb %s %s", topic, msg);
msgcache_insert(topic, msg);
streamlist_foreach(message_to_stream, (void *)&tm);
// if (topic[0] == 0) // FIXME, for us, empty == null
// msgcache_remove(topic);
}
static stream_t *webapi_subscribe_response(ebb_connection_info *conninfo, const char *topic)
{
stream_t *stream = NULL;
if (NULL == (stream = stream_create(topic)))
{
LOG_ERROR("failed to create stream for topic patt %s", topic);
conninfo->http_status = 503;
}
else
{
char seckey_str[STREAM_SECKEY_SIZE * 2 + 1]; // hex print
int i;
if (0 != streamlist_insert(stream->streamid, stream))
{
LOG_ERROR("streamlist_insert failed");
conninfo->http_status = 503;
}
// print hex string
for (i=0;i<STREAM_SECKEY_SIZE;i++)
sprintf(seckey_str+i*2, "%02X", stream->seckey[i]);
if (!conninfo->rawmode)
{
httpd_printf(conninfo->connid, "{\"url\":\"/stream/%u\",\"id\":%u,\"seckey\":\"%s\"}", stream->streamid, stream->streamid, seckey_str);
}
else
{ // attach conn to stream
stream_set_connection(stream, conninfo->connid);
idset_insert(conninfo->streamids, stream->streamid);
stream_refresh_timer(stream); // stop from timing out
}
}
if (!conninfo->rawmode)
httpd_close(conninfo->connid);
return stream;
}
void webapi_subscribe_cb(int mid)
{
uint32_t connid;
ebb_connection_info *conninfo;
if (0 == midconn_find(mid, &connid))
{
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
return;
}
LOG_INFO("webapi_subscribe_cb: managed_str=%s", conninfo->managed_str);
webapi_subscribe_response(conninfo, conninfo->managed_str);
}
else
{
LOG_ERROR("webapi_subscribe_cb mid not found %d", mid);
}
midconn_remove(mid);
}
static int parse_query_qos(const char *key, const char *val, void *userdata)
{
int *qos = (int *)userdata;
if (0==strcmp(key, "qos"))
{
*qos = atoi(val);
return 0;
}
return 1;
}
static int json_fetch_keyval(const char *jsonstr, char **key, char **val)
{
cJSON *root = NULL;
cJSON *e;
*key = NULL;
*val = NULL;
if (NULL == (root = cJSON_Parse(jsonstr)))
goto fail;
e = root->child;
if (NULL == e || NULL == e->string || NULL == e->valuestring)
goto fail;
if (NULL == (*key = strdup(e->string)))
goto fail;
if (NULL == (*val = strdup(e->valuestring)))
goto fail;
if (NULL != root)
cJSON_Delete(root);
return 0;
fail:
if (NULL != root)
cJSON_Delete(root);
if (NULL != *key)
free(*key);
if (NULL != *val)
free(*val);
return 1;
}
static int json_fetch_key(const char *jsonstr, char **key)
{
cJSON *root = NULL;
cJSON *e;
*key = NULL;
if (NULL == (root = cJSON_Parse(jsonstr)))
goto fail;
e = root->child;
if (NULL == e || NULL == e->valuestring)
goto fail;
if (NULL == (*key = strdup(e->valuestring)))
goto fail;
if (NULL != root)
cJSON_Delete(root);
return 0;
fail:
if (NULL != root)
cJSON_Delete(root);
if (NULL != *key)
free(key);
return 1;
}
static int json_parse_array_uint32_seckey(const char *jsonstr, uint32_t *arr, uint8_t seckeys[MAX_STREAMS][STREAM_SECKEY_SIZE], size_t *len, size_t max_len)
{
cJSON *root = NULL;
cJSON *e;
size_t i = 0;
size_t hlen;
if (NULL == (root = cJSON_Parse(jsonstr)))
goto fail;
e = root->child;
if (NULL == e)
goto fail;
while(NULL != e)
{
if (i > max_len)
goto fail;
if (NULL == e->string)
{
LOG_INFO("no seckey");
goto fail;
}
arr[i] = atoi(e->string);
hlen = STREAM_SECKEY_SIZE;
if (0 != hexstring_parse(e->valuestring, seckeys[i], &hlen) || hlen != STREAM_SECKEY_SIZE)
{
LOG_INFO("bad seckey %s", e->valuestring);
goto fail;
}
i++;
e = e->next;
}
*len = i;
if (NULL != root)
cJSON_Delete(root);
return 0;
fail:
if (NULL != root)
cJSON_Delete(root);
return 1;
}
static int webapi_publish(uint32_t connid, uint32_t method, int argc, char **argv, const char *body, const char *reqpath, const char *uri_str, const char *auth)
{
ebb_connection_info *conninfo;
int mid;
int qos = 2;
char *topic = NULL;
char *msg = NULL;
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
return 1;
}
if (method != EBB_PUT && method != EBB_POST)
{
conninfo->http_status = 405; // method not allowed
return 1;
}
httpd_query_iterate(uri_str, parse_query_qos, (void *)&qos);
if (qos < 0 || qos > 2)
{
conninfo->http_status = 400;
return 1;
}
if (0 != json_fetch_keyval(body, &topic, &msg))
{
LOG_INFO("malformed json %s", body);
conninfo->http_status = 400;
return 1;
}
accesslog_write("%s publish %s", conninfo->ipaddr, topic);
if (mqtt_publish(&g_srvctx.mqttctx, topic, msg, qos, &mid) != 0)
{
conninfo->http_status = 503;
free(topic);
free(msg);
return 1;
}
else
{
#ifdef LOCAL_ECHO
// feed self
webapi_message_cb(mid, topic, msg);
#endif
free(topic);
free(msg);
if (qos == 0)
{
httpd_close(connid);
return 0;
}
else
{
LOG_DEBUG("midconn_insert mid=%d conn=%d", mid, connid);
midconn_insert(mid, connid);
conninfo->have_mid = true;
conninfo->mid = mid;
return 0;
}
}
return 1;
}
static int webapi_stream(uint32_t connid, uint32_t method, int argc, char **argv, const char *body, const char *reqpath, const char *uri_str, const char *auth)
{
ebb_connection_info *conninfo;
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
return 1;
}
if (method == EBB_POST && argc == 0)
{
uint32_t streamids[MAX_STREAMS];
stream_t *streams[MAX_STREAMS];
uint8_t seckeys[MAX_STREAMS][STREAM_SECKEY_SIZE];
size_t num_streams;
size_t i;
bool close_conn = false;
if (0 != json_parse_array_uint32_seckey(body, streamids, seckeys, &num_streams, MAX_STREAMS) || num_streams == 0)
{
conninfo->http_status = 400; // malformed
return 1;
}
for (i=0;i<num_streams;i++) // reject request if any of the streams don't exist or wrong seckey
{
//LOG_INFO("webapi_stream %u for connid=%u", streamids[i], connid);
if (NULL == (streams[i] = streamlist_find(streamids[i])))
{
LOG_DEBUG("no such stream %u", streamids[i]);
conninfo->http_status = 404; // no such
return 1;
}
else
{
if (0!=memcmp(seckeys[i], streams[i]->seckey, STREAM_SECKEY_SIZE))
{
LOG_INFO("invalid seckey for streamid %u", streamids[i]);
conninfo->http_status = 403; // verboten
return 1;
}
LOG_DEBUG("adding stream %u", streamids[i]);
}
}
for (i=0;i<num_streams;i++)
{
if (!stream_isempty(streams[i]))
{
if (close_conn)
httpd_printf(connid, "\r\n"); // FIXME, should this be a single JSON doc?
httpd_printf(connid, "[");
stream_drain(streams[i], stream_drainer, (void *)(uintptr_t)connid);
httpd_printf(connid, "]");
close_conn = true; // defer close till all available streams have been drained
}
else
{
uint32_t old_connid;
if (stream_get_connection(streams[i], &old_connid))
{
ebb_connection_info *old_conninfo;
LOG_DEBUG("closing old_connid %u replacing with %u for stream %u", old_connid, connid, streamids[i]);
if (NULL != (old_conninfo = httpd_get_conninfo(old_connid)))
idset_foreach(old_conninfo->streamids, strm_clr, NULL);
httpd_close(old_connid);
}
stream_set_connection(streams[i], connid);
// associate the stream to this connection
if (0 != idset_insert(conninfo->streamids, streamids[i]))
{
conninfo->http_status = 503;
return 1;
}
stream_refresh_timer(streams[i]); // stop from timing out
}
}
if (close_conn)
{
LOG_DEBUG("closing");
httpd_close(connid);
}
return 0;
}
else
{
conninfo->http_status = 405; // method not allowed
return 1;
}
return 0;
}
static void inject_retained(const char *topic, const char *msg, void *userdata)
{
stream_t *stream = (stream_t *)userdata;
struct topic_message tm = {topic, msg};
if (NULL == stream)
return;
if (topic_match_string(stream->topic_patt, topic))
{
LOG_INFO("inj %s %s", topic, msg);
message_to_stream(stream->streamid, stream, &tm);
}
}
static int webapi_subscribe(uint32_t connid, uint32_t method, int argc, char **argv, const char *body, const char *reqpath, const char *uri_str, const char *auth)
{
ebb_connection_info *conninfo;
int mid;
char *topic = NULL;
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
return 1;
}
if (method != EBB_PUT && method != EBB_POST)
{
conninfo->http_status = 405; // method not allowed
return 1;
}
if (0 != json_fetch_key(body, &topic))
{
LOG_INFO("malformed sub %s", body);
conninfo->http_status = 400;
return 1;
}
LOG_INFO("webapi_subscribe %s", topic);
accesslog_write("%s subscribe %s", conninfo->ipaddr, topic);
if (sublist_insert(topic))
{
if (mqtt_subscribe(&g_srvctx.mqttctx, topic, 2, &mid) != 0)
{
conninfo->http_status = 503;
free(topic);
return 1;
}
else
{
LOG_INFO("midconn_insert mid=%d conn=%d", mid, connid);
conninfo->managed_str = topic;
midconn_insert(mid, connid);
conninfo->have_mid = true;
conninfo->mid = mid;
return 0;
}
}
else
{
stream_t *stream;
// we're already subscribed, so no need to send mqtt subscribe req
stream = webapi_subscribe_response(conninfo, topic);
// for everything in msgcache, if matches topic, inject into conn
// FIXME, should walk topic tree
msgcache_foreach(inject_retained, stream);
free(topic);
return 0;
}
return 1;
}
static int webapi_raw(uint32_t connid, uint32_t method, int argc, char **argv, const char *body, const char *reqpath, const char *uri_str, const char *auth)
{
ebb_connection_info *conninfo;
int mid;
char *topic = NULL;
if (NULL == (topic = strdup(reqpath)))
{
LOG_ERROR("out of mem");
return 1;
}
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
free(topic);
return 1;
}
if (method != EBB_GET)
{
conninfo->http_status = 405; // method not allowed
free(topic);
return 1;
}
conninfo->rawmode = true;
strcpy(conninfo->mimetype, "text/html");
if (sublist_insert(topic))
{
if (mqtt_subscribe(&g_srvctx.mqttctx, topic, 2, &mid) != 0)
{
conninfo->http_status = 503;
free(topic);
return 1;
}
else
{
LOG_INFO("midconn_insert mid=%d conn=%d", mid, connid);
conninfo->managed_str = topic;
midconn_insert(mid, connid);
conninfo->have_mid = true;
conninfo->mid = mid;
return 0;
}
}
else
{
stream_t *stream;
// we're already subscribed, so no need to send mqtt subscribe req
stream = webapi_subscribe_response(conninfo, topic);
// for everything in msgcache, if matches topic, inject into conn
// FIXME, should walk topic tree
msgcache_foreach(inject_retained, stream);
free(topic);
return 0;
}
return 1;
}
static int webapi_command(uint32_t connid, uint32_t method, int argc, char **argv, const char *body, const char *reqpath, const char *uri_str, const char *auth, int vhost)
{
// if (vhost >=0 && 0 == strcmp(g_srvctx.vhttpd[vhost].name, "sensemote"))
{
if (argc > 0)
{
if (0 == strcmp(argv[0], "publish"))
return webapi_publish(connid, method, argc-1, argv+1, body, reqpath + strlen("/publish/"), uri_str, auth);
else
if (0 == strcmp(argv[0], "subscribe"))
return webapi_subscribe(connid, method, argc-1, argv+1, body, reqpath + strlen("/subscribe/"), uri_str, auth);
else
if (0 == strcmp(argv[0], "stream"))
return webapi_stream(connid, method, argc-1, argv+1, body, reqpath + strlen("/stream/"), uri_str, auth);
else
if (0 == strcmp(argv[0], "raw"))
return webapi_raw(connid, method, argc-1, argv+1, body, reqpath + strlen("/raw/"), uri_str, auth);
}
}
if (0 == webapi_serve(connid, method, argc, argv, body, reqpath, vhost))
return 0;
else
{
ebb_connection_info *conninfo;
if (NULL != (conninfo = httpd_get_conninfo(connid)))
{
conninfo->http_status = 404;
return 1;
}
}
return 1;
}
void httpd_open_cb(uint32_t connid)
{
LOG_DEBUG("HTTPDCONN OPEN %d", connid);
}
void httpd_close_cb(uint32_t connid)
{
ebb_connection_info *conninfo;
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid in httpd_close_cb!");
return;
}
LOG_DEBUG("HTTPDCONN CLOSE %u", connid);
if (conninfo->have_mid)
{
midconn_remove(conninfo->mid);
conninfo->have_mid = false;
}
idset_foreach(conninfo->streamids, strm_clr, NULL);
}
// path will always start with '/', may end with it
int httpd_request_cb(uint32_t connid, const char *host, uint32_t method, const char *uri_str, const char *path, const char *body_data, size_t body_len, const char *auth)
{
size_t i = 0;
char *pathstr = NULL;
char *bodystr = NULL;
char *path_argv[MAX_PATH_ELEMS];
int path_argc = 0;
int rc;
ebb_connection_info *conninfo;
int vhost;
LOG_DEBUG("httpd_request_cb host=%s uri_str=%s, path=%s, auth=%s", host, uri_str, path, auth);
if ((vhost = httpd_lookupvirt(host)) < 0)
{
LOG_DEBUG("VHOST DEFAULT");
}
else
{
LOG_DEBUG("VHOST = %s", g_srvctx.vhttpd[vhost].name);
}
if (NULL == (conninfo = httpd_get_conninfo(connid)))
{
LOG_ERROR("bad connid");
return 1;
}
// dissociate conn from streams, left from last req
idset_foreach(conninfo->streamids, strm_clr, NULL);
// FIXME, may be other inter-req cleanup to do here also
if (NULL == (pathstr = strdup(path)))
goto fail;
if (NULL == (bodystr = (char *)calloc(1, body_len+1)))
{
LOG_ERROR("out of mem for bodystr");
goto fail;
}
memcpy(bodystr, body_data, body_len);
while(i < strlen(path))
{
if (pathstr[i] == '/')
{
pathstr[i] = 0;
if (path_argc >= MAX_PATH_ELEMS)
{
LOG_ERROR("too many path elems");
goto fail;
}
path_argv[path_argc++] = pathstr + i + 1;
}
i++;
}
// chop off any empty / element
if (path_argc > 0)
{
if (*path_argv[path_argc-1] == 0 || *path_argv[path_argc-1] == '/')
path_argc--;
}
conninfo->rawmode = false;
rc = webapi_command(connid, method, path_argc, path_argv, bodystr, path, uri_str, auth, vhost);
conninfo->ifmodifiedsince = 0; // FIXME, need to reset this after, should be doing these things more cleanly
free(pathstr);
free(bodystr);
return rc;
fail:
if (NULL != pathstr)
free(pathstr);
if (NULL != bodystr)
free(bodystr);
return 1;
}