-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_server.c
1867 lines (1640 loc) · 52.9 KB
/
http_server.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
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) 2011-2024 Rozhuk Ivan <rozhuk.im@gmail.com>
* 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 REGENTS 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 REGENTS 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.
*
* Author: Rozhuk Ivan <rozhuk.im@gmail.com>
*
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <stdlib.h> /* malloc, exit */
#include <unistd.h> /* close, write, sysconf */
#include <string.h> /* memcpy, memmove, memset, strerror... */
#include <strings.h> /* explicit_bzero */
#include <stdio.h> /* snprintf, fprintf */
#include <time.h>
#include <errno.h>
#include "utils/macro.h"
#include "utils/mem_utils.h"
#include "utils/num2str.h"
#include "utils/str2num.h"
#include "proto/http.h"
#include "threadpool/threadpool_task.h"
#include "net/socket.h"
#include "net/socket_address.h"
#include "net/utils.h"
#include "utils/info.h"
#include "net/hostname_list.h"
#include "proto/http_server.h"
#ifdef HTTP_SRV_XML_CONFIG
# include "utils/buf_str.h"
# include "utils/xml.h"
#endif
#define HTTP_LIB_NAME "HTTP core server by Rozhuk Ivan"
#define HTTP_LIB_VER "1.7"
#define HTTP_LIB_NAME_VER HTTP_LIB_NAME"/"HTTP_LIB_VER
#define HTTP_SRV_ALLOC_CNT 8
typedef struct http_srv_bind_s {
tp_task_p *tptasks; /* Accept incomming task. */
size_t tptasks_cnt;
http_srv_p srv; /* HTTP server */
void *udata; /* Acceptor associated data. */
hostname_list_t hst_name_lst; /* List of host names on this bind. */
http_srv_bind_settings_t s; /* settings */
} http_srv_bind_t;
typedef struct http_srv_s {
tp_p tp;
http_srv_on_conn_cb on_conn; /* New client connected callback */
http_srv_cli_ccb_t ccb; /* Default client callbacks. */
void *udata; /* Server associated data. */
http_srv_stat_t stat;
size_t bind_count;
size_t bind_allocated;
http_srv_bind_p *bnd; /* Acceptors pointers array. */
hostname_list_t hst_name_lst; /* List of host names on this server. */
http_srv_settings_t s; /* settings */
} http_srv_t;
typedef struct http_srv_cli_s {
tp_task_p tptask; /* recv/send from/to client, and socket container. */
io_buf_p rcv_buf;/* Used for receive http request only. */
io_buf_p buf; /* Used for send http responce only. */
http_srv_bind_p bnd; /* */
http_srv_req_t req; /* Parsed request data. */
http_srv_resp_t resp; /* Responce data. */
http_srv_cli_ccb_t ccb; /* Custom client callbacks. */
void *udata; /* Client associated data. */
uint32_t flags; /* Flags: HTTP_SRV_CLI_F_*. */
uint32_t flags_int; /* Flags: HTTP_SRV_CLI_FI_*. */
sockaddr_storage_t addr; /* Client address. */
} http_srv_cli_t;
#define HTTP_SRV_CLI_FI_NEXT_BYTE_MASK ((uint32_t)0x000000ff)
#define HTTP_SRV_CLI_FI_NEXT_BYTE_SET (((uint32_t)1) << 9) /* Keep here byte value from next request. */
http_srv_cli_p http_srv_cli_alloc(http_srv_bind_p bnd, tpt_p tpt,
uintptr_t skt, http_srv_cli_ccb_p ccb, void *udata);
void http_srv_cli_free(http_srv_cli_p cli);
static int http_srv_new_conn_cb(tp_task_p tptask, int error, uintptr_t skt,
sockaddr_storage_p addr, void *arg);
static int http_srv_recv_done_cb(tp_task_p tptask, int error, io_buf_p buf,
uint32_t eof, size_t transfered_size, void *arg);
static int http_srv_send_responce(http_srv_cli_p cli, const uint8_t **delimiter);
static int http_srv_snd_done_cb(tp_task_p tptask, int error, io_buf_p buf,
uint32_t eof, size_t transfered_size, void *arg);
/*
* resp_p_flags - HTTP_SRV_RESP_P_F_*
*/
static int http_srv_snd(http_srv_cli_p cli);
void
http_srv_def_settings(int add_os_ver, const char *app_ver, int add_lib_ver,
http_srv_settings_p s_ret) {
size_t app_ver_size;
if (NULL == s_ret)
return;
/* Init. */
memset(s_ret, 0x00, sizeof(http_srv_settings_t));
skt_opts_init(HTTP_SRV_S_SKT_OPTS_INT_MASK,
HTTP_SRV_S_SKT_OPTS_INT_VALS, &s_ret->skt_opts);
s_ret->skt_opts.mask |= SO_F_NONBLOCK;
s_ret->skt_opts.bit_vals |= SO_F_NONBLOCK;
#ifdef SO_ACCEPTFILTER
memcpy(s_ret->skt_opts.tcp_acc_filter.af_name,
HTTP_SRV_S_SKT_OPTS_ACC_FILTER_NAME,
sizeof(HTTP_SRV_S_SKT_OPTS_ACC_FILTER_NAME));
#elif defined(TCP_DEFER_ACCEPT)
s_ret->skt_opts.tcp_acc_defer = HTTP_SRV_S_SKT_OPTS_ACC_FILTER_DEFER;
#endif
/* Default settings. */
s_ret->skt_opts.mask |= HTTP_SRV_S_DEF_SKT_OPTS_MASK;
s_ret->skt_opts.bit_vals |= HTTP_SRV_S_DEF_SKT_OPTS_VALS;
s_ret->skt_opts.rcv_timeout = HTTP_SRV_S_DEF_SKT_OPTS_RCVTIMEO;
s_ret->skt_opts.snd_timeout = HTTP_SRV_S_DEF_SKT_OPTS_SNDTIMEO;
s_ret->rcv_io_buf_init_size = HTTP_SRV_S_DEF_RCV_IO_BUF_INIT;
s_ret->rcv_io_buf_max_size = HTTP_SRV_S_DEF_RCV_IO_BUF_MAX;
s_ret->snd_io_buf_init_size = HTTP_SRV_S_DEF_SND_IO_BUF_INIT;
s_ret->hdrs_reserve_size = HTTP_SRV_S_DEF_HDRS_SIZE;
s_ret->req_p_flags = HTTP_SRV_S_DEF_RQ_P_FLAGS;
s_ret->resp_p_flags = HTTP_SRV_S_DEF_RESP_P_FLAGS;
/* 'OS/version UPnP/1.1 product/version' */
s_ret->http_server_size = 0;
if (0 != add_os_ver) {
if (0 != info_get_os_ver("/", 1, s_ret->http_server,
(sizeof(s_ret->http_server) - 1),
&s_ret->http_server_size)) {
memcpy(s_ret->http_server, "Generic OS/1.0", 15);
s_ret->http_server_size = 14;
}
}
if (NULL != app_ver &&
(sizeof(s_ret->http_server) - 2) > (app_ver_size = strlen(app_ver))) {
if (0 != s_ret->http_server_size) {
s_ret->http_server[s_ret->http_server_size ++] = ' ';
}
memcpy(s_ret->http_server, app_ver, app_ver_size);
s_ret->http_server_size = app_ver_size;
}
if (0 != add_lib_ver &&
(sizeof(s_ret->http_server) - 2) > sizeof(HTTP_LIB_NAME_VER)) {
if (0 != s_ret->http_server_size) {
s_ret->http_server[s_ret->http_server_size ++] = ' ';
}
memcpy(s_ret->http_server, HTTP_LIB_NAME_VER,
sizeof(HTTP_LIB_NAME_VER));
s_ret->http_server_size = (sizeof(HTTP_LIB_NAME_VER) - 1);
}
s_ret->http_server[s_ret->http_server_size] = 0;
}
void
http_srv_bind_def_settings(skt_opts_p skt_opts, http_srv_bind_settings_p s_ret) {
if (NULL == s_ret)
return;
memset(s_ret, 0x00, sizeof(http_srv_bind_settings_t));
/* default settings */
memcpy(&s_ret->skt_opts, skt_opts, sizeof(skt_opts_t));
}
#ifdef HTTP_SRV_XML_CONFIG
int
http_srv_xml_load_hostnames(const uint8_t *buf, size_t buf_size,
hostname_list_p hst_name_lst) {
int error;
const uint8_t *data, *cur_pos;
size_t data_size;
char strbuf[256];
if (NULL == buf || 0 == buf_size || NULL == hst_name_lst)
return (EINVAL);
/* Read hostnames. */
hostname_list_init(hst_name_lst);
cur_pos = NULL;
while (0 == xml_get_val_args(buf, buf_size, &cur_pos, NULL, NULL,
&data, &data_size, (const uint8_t*)"hostnameList", "hostname", NULL)) {
error = hostname_list_add(hst_name_lst, data, data_size);
data_size = MIN(data_size, (sizeof(strbuf) - 1));
memcpy(strbuf, data, data_size);
strbuf[data_size] = 0;
if (0 != error) {
SYSLOG_ERR(LOG_WARNING, error,
"hostname_list_add(%s).", strbuf);
continue;
}
syslog(LOG_INFO, "hostname: %s", strbuf);
}
return (0);
}
int
http_srv_xml_load_settings(const uint8_t *buf, size_t buf_size,
http_srv_settings_p s) {
const uint8_t *data;
size_t data_size;
if (NULL == buf || 0 == buf_size || NULL == s)
return (EINVAL);
/* Read from config. */
/* Socket options. */
if (0 == xml_get_val_args(buf, buf_size, NULL, NULL, NULL,
&data, &data_size, (const uint8_t*)"skt", NULL)) {
skt_opts_xml_load(data, data_size,
HTTP_SRV_S_SKT_OPTS_LOAD_MASK, &s->skt_opts);
}
xml_get_val_size_t_args(buf, buf_size, NULL, &s->rcv_io_buf_init_size,
(const uint8_t*)"ioBufInitSize", NULL);
xml_get_val_size_t_args(buf, buf_size, NULL, &s->rcv_io_buf_max_size,
(const uint8_t*)"ioBufMaxSize", NULL);
return (0);
}
int
http_srv_xml_load_bind(const uint8_t *buf, size_t buf_size,
http_srv_bind_settings_p s) {
int error;
const uint8_t *data;
size_t data_size;
uint16_t tm16;
char straddr[STR_ADDR_LEN];
if (NULL == buf || 0 == buf_size || NULL == s)
return (EINVAL);
/* address */
if (0 != xml_get_val_args(buf, buf_size, NULL, NULL, NULL,
&data, &data_size, (const uint8_t*)"address", NULL)) {
syslog(LOG_CRIT, "HTTP server: server addr not set.");
return (EINVAL);
}
if (0 != sa_addr_port_from_str(&s->addr, (const char*)data, data_size)) {
memcpy(straddr, data, MIN((sizeof(straddr) - 1), data_size));
straddr[MIN((sizeof(straddr) - 1), data_size)] = 0;
syslog(LOG_CRIT, "HTTP server: invalid addr: %s (len=%zu).",
straddr, data_size);
return (EINVAL);
}
if (0 == xml_get_val_args(buf, buf_size, NULL, NULL, NULL,
&data, &data_size, (const uint8_t*)"ifName", NULL)) {
tm16 = sa_port_get(&s->addr);
error = get_if_addr_by_name((const char*)data, data_size,
s->addr.ss_family, &s->addr);
if (0 != error) {
memcpy(straddr, data, MIN((sizeof(straddr) - 1), data_size));
straddr[MIN((sizeof(straddr) - 1), data_size)] = 0;
SYSLOG_ERR(LOG_CRIT, error,
"HTTP server: cant get addr for: %s.", straddr);
return (error);
}
sa_port_set(&s->addr, tm16);
}
/* Socket options. */
skt_opts_xml_load(buf, buf_size, HTTP_SRV_S_SKT_OPTS_LOAD_MASK, &s->skt_opts);
return (0);
}
int
http_srv_xml_load_start(const uint8_t *buf, size_t buf_size, tp_p tp,
http_srv_on_conn_cb on_conn, http_srv_cli_ccb_p ccb,
http_srv_settings_p srv_settings, void *udata,
http_srv_p *http_srv) {
int error;
const uint8_t *data, *cur_pos;
size_t data_size;
char straddr[STR_ADDR_LEN];
http_srv_settings_t srv_s;
http_srv_bind_settings_t bind_s;
hostname_list_t hst_name_lst;
if (NULL == buf || 0 == buf_size || NULL == tp ||
NULL == srv_settings || NULL == http_srv)
return (EINVAL);
/* HTTP server settings. */
if (0 == xml_calc_tag_count_args(buf, buf_size,
(const uint8_t*)"bindList", "bind", NULL))
goto no_http_svr;
/* Default settings. */
memcpy(&srv_s, srv_settings, sizeof(srv_s));
/* Read from config. */
http_srv_xml_load_settings(buf, buf_size, &srv_s);
/* Read hostnames. */
hostname_list_init(&hst_name_lst);
http_srv_xml_load_hostnames(buf, buf_size, &hst_name_lst);
error = http_srv_create(tp, on_conn, ccb, &hst_name_lst,
&srv_s, udata, http_srv);
if (0 != error) {
SYSLOG_ERR(LOG_CRIT, error, "http_srv_create().");
hostname_list_deinit(&hst_name_lst);
return (error);
}
/* Load and add servers. */
cur_pos = NULL;
while (0 == xml_get_val_args(buf, buf_size, &cur_pos, NULL, NULL,
&data, &data_size, (const uint8_t*)"bindList", "bind", NULL)) {
http_srv_bind_def_settings(&srv_s.skt_opts, &bind_s);
error = http_srv_xml_load_bind(data, data_size, &bind_s);
if (0 != error) {
SYSLOG_ERR(LOG_ERR, error, "http_srv_xml_load_bind().");
continue;
}
/* Read hostnames. */
hostname_list_init(&hst_name_lst);
http_srv_xml_load_hostnames(data, data_size, &hst_name_lst);
sa_addr_port_to_str(&bind_s.addr, straddr, sizeof(straddr), NULL);
/* Try bind... */
error = http_srv_bind_add((*http_srv), &bind_s, &hst_name_lst, NULL, NULL);
if (0 != error) {
SYSLOG_ERR(LOG_ERR, error, "http_srv_bind_add(): %s,"
" backlog = %i, tcp_cc = %s.",
straddr, bind_s.skt_opts.backlog, bind_s.skt_opts.tcp_cc);
continue;
}
syslog(LOG_INFO, "bind %s, backlog = %i, tcp_cc = %s.",
straddr, bind_s.skt_opts.backlog, bind_s.skt_opts.tcp_cc);
}
if (0 == http_srv_get_bind_count((*http_srv))) {
no_http_svr:
syslog(LOG_NOTICE, "no bind address specified, nothink to do...");
return (EINVAL);
}
return (0);
}
#endif /* HTTP_SRV_XML_CONFIG */
int
http_srv_create(tp_p tp, http_srv_on_conn_cb on_conn,
http_srv_cli_ccb_p ccb, hostname_list_p hst_name_lst,
http_srv_settings_p s, void *udata, http_srv_p *srv_ret) {
int error;
http_srv_p srv = NULL;
struct timespec ts;
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == srv_ret) {
error = EINVAL;
goto err_out;
}
if (NULL != s) { /* Validate settings. */
if ((0 != s->snd_io_buf_init_size &&
(s->hdrs_reserve_size + HTTP_SRV_S_DEF_HDRS_SIZE) > s->snd_io_buf_init_size) ||
sizeof(s->http_server) <= s->http_server_size) {
error = EINVAL;
goto err_out;
}
}
/* Create. */
srv = calloc(1, sizeof(http_srv_t));
if (NULL == srv) {
error = ENOMEM;
goto err_out;
}
srv->tp = tp;
srv->on_conn = on_conn;
if (NULL != ccb) {
srv->ccb = (*ccb); /* memcpy */
}
srv->udata = udata;
if (NULL != hst_name_lst) {
memcpy(&srv->hst_name_lst, hst_name_lst, sizeof(hostname_list_t));
}
if (NULL == s) { /* Apply default settings */
http_srv_def_settings(1, NULL, 1, &srv->s);
} else {
memcpy(&srv->s, s, sizeof(http_srv_settings_t));
}
/* kb -> bytes, sec -> msec */
skt_opts_cvt(SKT_OPTS_MULT_K, &srv->s.skt_opts);
srv->s.rcv_io_buf_init_size *= 1024;
srv->s.rcv_io_buf_max_size *= 1024;
srv->s.snd_io_buf_init_size *= 1024;
srv->s.hdrs_reserve_size *= 1024;
srv->s.http_server[srv->s.http_server_size] = 0;
clock_gettime(CLOCK_REALTIME_FAST, &ts);
srv->stat.start_time = ts.tv_sec;
clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
srv->stat.start_time_abs = ts.tv_sec;
(*srv_ret) = srv;
return (0);
err_out:
hostname_list_deinit(hst_name_lst);
if (NULL != srv) {
free(srv);
}
/* Error. */
SYSLOG_ERR_EX(LOG_ERR, error, "err_out.");
return (error);
}
void
http_srv_shutdown(http_srv_p srv) {
size_t i;
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == srv || NULL != srv->bnd)
return;
for (i = 0; i < srv->bind_count; i ++) {
http_srv_bind_shutdown(srv->bnd[i]);
}
}
void
http_srv_destroy(http_srv_p srv) {
size_t i;
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == srv)
return;
if (NULL != srv->bnd) {
for (i = 0; i < srv->bind_count; i ++) {
http_srv_bind_remove(srv->bnd[i]);
}
free(srv->bnd);
}
hostname_list_deinit(&srv->hst_name_lst);
free(srv);
}
size_t
http_srv_get_bind_count(http_srv_p srv) {
if (NULL == srv)
return (0);
return (srv->bind_count);
}
int
http_srv_stat_get(http_srv_p srv, http_srv_stat_p stat) {
if (NULL == srv || NULL == stat)
return (EINVAL);
memcpy(stat, &srv->stat, sizeof(http_srv_stat_t));
return (0);
}
tp_p
http_srv_tp_get(http_srv_p srv) {
if (NULL == srv)
return (NULL);
return (srv->tp);
}
int
http_srv_tp_set(http_srv_p srv, tp_p tp) {
if (NULL == srv)
return (EINVAL);
srv->tp = tp;
return (0);
}
int
http_srv_on_conn_cb_set(http_srv_p srv, http_srv_on_conn_cb on_conn) {
if (NULL == srv)
return (EINVAL);
srv->on_conn = on_conn;
return (0);
}
int
http_srv_ccb_get(http_srv_p srv, http_srv_cli_ccb_p ccb) {
if (NULL == srv || NULL == ccb)
return (EINVAL);
(*ccb) = srv->ccb;
return (0);
}
int
http_srv_ccb_set(http_srv_p srv, http_srv_cli_ccb_p ccb) {
if (NULL == srv || NULL == ccb)
return (EINVAL);
srv->ccb = (*ccb);
return (0);
}
int
http_srv_on_destroy_cb_set(http_srv_p srv, http_srv_on_destroy_cb on_destroy) {
if (NULL == srv)
return (EINVAL);
srv->ccb.on_destroy = on_destroy;
return (0);
}
int
http_srv_on_req_rcv_cb_set(http_srv_p srv, http_srv_on_req_rcv_cb on_req_rcv) {
if (NULL == srv)
return (EINVAL);
srv->ccb.on_req_rcv = on_req_rcv;
return (0);
}
int
http_srv_on_rep_snd_cb_set(http_srv_p srv, http_srv_on_resp_snd_cb on_rep_snd) {
if (NULL == srv)
return (EINVAL);
srv->ccb.on_rep_snd = on_rep_snd;
return (0);
}
void *
http_srv_get_udata(http_srv_p srv) {
if (NULL == srv)
return (NULL);
return (srv->udata);
}
int
http_srv_set_udata(http_srv_p srv, void *udata) {
if (NULL == srv)
return (EINVAL);
srv->udata = udata;
return (0);
}
/* HTTP Acceptor */
int
http_srv_bind_add(http_srv_p srv, http_srv_bind_settings_p s,
hostname_list_p hst_name_lst, void *udata, http_srv_bind_p *bind_ret) {
int error;
http_srv_bind_p bnd = NULL;
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == srv || NULL == s)
return (EINVAL);
bnd = calloc(1, sizeof(http_srv_bind_t));
if (NULL == bnd)
return (ENOMEM);
bnd->srv = srv;
memcpy(&bnd->s, s, sizeof(http_srv_bind_settings_t));
bnd->udata = udata;
if (NULL != hst_name_lst) {
memcpy(&bnd->hst_name_lst, hst_name_lst, sizeof(hostname_list_t));
}
/* kb -> bytes, sec -> msec */
skt_opts_cvt(SKT_OPTS_MULT_K, &bnd->s.skt_opts);
/* Create listen sockets per thread or on one on rand thread. */
error = tp_task_bind_accept_multi_create(srv->tp,
&bnd->s.addr, SOCK_STREAM, IPPROTO_TCP, &bnd->s.skt_opts,
TP_TASK_F_CLOSE_ON_DESTROY, 0, http_srv_new_conn_cb, bnd,
&bnd->tptasks_cnt, &bnd->tptasks);
if (0 != error)
goto err_out;
/* Link server and acceptor. */
error = realloc_items((void**)&srv->bnd, sizeof(http_srv_bind_p),
&srv->bind_allocated, HTTP_SRV_ALLOC_CNT, srv->bind_count);
if (0 != error) /* Realloc fail! */
goto err_out;
srv->bnd[srv->bind_count] = bnd;
srv->bind_count ++;
if (NULL != bind_ret) {
(*bind_ret) = bnd;
}
return (0);
err_out:
/* Error. */
bnd->srv = NULL;
http_srv_bind_remove(bnd);
SYSLOG_ERR_EX(LOG_ERR, error, "err_out.");
return (error);
}
void
http_srv_bind_shutdown(http_srv_bind_p bnd) {
size_t i;
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == bnd)
return;
for (i = 0; i < bnd->tptasks_cnt; i ++) {
tp_task_ident_close(bnd->tptasks[i]);
}
}
void
http_srv_bind_remove(http_srv_bind_p bnd) {
size_t i;
http_srv_p srv;
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == bnd)
return;
if (NULL != bnd->srv) {
srv = bnd->srv;
for (i = 0; i < srv->bind_count; i ++) {
if (srv->bnd[i] != bnd)
continue;
memmove(&srv->bnd[i], &srv->bnd[(i + 1)],
(sizeof(http_srv_bind_p) * (srv->bind_count - (i + 1))));
srv->bind_count --;
break;
}
}
for (i = 0; i < bnd->tptasks_cnt; i ++) {
tp_task_destroy(bnd->tptasks[i]);
bnd->tptasks[i] = NULL;
}
free(bnd->tptasks);
bnd->tptasks = NULL;
bnd->tptasks_cnt = 0;
hostname_list_deinit(&bnd->hst_name_lst);
free(bnd);
}
http_srv_p
http_srv_bind_get_srv(http_srv_bind_p bnd) {
if (NULL == bnd)
return (NULL);
return (bnd->srv);
}
void *
http_srv_bind_get_udata(http_srv_bind_p bnd) {
if (NULL == bnd)
return (NULL);
return (bnd->udata);
}
int
http_srv_bind_set_udata(http_srv_bind_p bnd, void *udata) {
if (NULL == bnd)
return (EINVAL);
bnd->udata = udata;
return (0);
}
int
http_srv_bind_get_addr(http_srv_bind_p bnd, sockaddr_storage_p addr) {
if (NULL == bnd || NULL == addr)
return (EINVAL);
sa_copy(&bnd->s.addr, addr);
return (0);
}
/* HTTP Client */
http_srv_cli_p
http_srv_cli_alloc(http_srv_bind_p bnd, tpt_p tpt, uintptr_t skt,
http_srv_cli_ccb_p ccb, void *udata) {
http_srv_cli_p cli;
SYSLOGD_EX(LOG_DEBUG, "...");
cli = calloc(1, sizeof(http_srv_cli_t));
if (NULL == cli)
return (cli);
bnd->srv->stat.connections ++;
cli->rcv_buf = io_buf_alloc(IO_BUF_FLAGS_STD, bnd->srv->s.rcv_io_buf_init_size);
if (NULL == cli->rcv_buf)
goto err_out;
cli->buf = cli->rcv_buf;
if (0 != tp_task_create(tpt, skt, tp_task_sr_handler,
(TP_TASK_F_CLOSE_ON_DESTROY | TP_TASK_F_CB_AFTER_EVERY_READ),
cli, &cli->tptask))
goto err_out;
cli->bnd = bnd;
cli->ccb = (*ccb); /* memcpy */
cli->udata = udata;
return (cli);
err_out:
http_srv_cli_free(cli);
return (NULL);
}
void
http_srv_cli_free(http_srv_cli_p cli) {
SYSLOGD_EX(LOG_DEBUG, "...");
if (NULL == cli)
return;
cli->bnd->srv->stat.connections --;
if (NULL != cli->ccb.on_destroy) { /* Call back handler. */
cli->ccb.on_destroy(cli, cli->udata, &cli->resp);
}
tp_task_destroy(cli->tptask);
io_buf_free(cli->rcv_buf);
if (cli->buf != cli->rcv_buf) {
io_buf_free(cli->buf);
}
free(cli);
}
static void
http_srv_cli_next_req(http_srv_cli_p cli) {
size_t tm = 0;
if (NULL == cli)
return;
/* Move data to buf start. */
if (NULL != cli->req.data) {
cli->req.data += cli->req.data_size; /* Move pointer to next request. */
tm = (size_t)(cli->rcv_buf->used - (size_t)(cli->req.data - cli->rcv_buf->data));
memmove(cli->rcv_buf->data, cli->req.data, tm);
/* Restore original value. */
if (0 != (HTTP_SRV_CLI_FI_NEXT_BYTE_SET & cli->flags_int)) {
cli->rcv_buf->data[0] = (HTTP_SRV_CLI_FI_NEXT_BYTE_MASK & cli->flags_int);
cli->flags_int &= ~(HTTP_SRV_CLI_FI_NEXT_BYTE_MASK | HTTP_SRV_CLI_FI_NEXT_BYTE_SET);
}
} else {
debugd_break();
}
/* Update used buf size. */
IO_BUF_BUSY_SIZE_SET(cli->rcv_buf, tm);
/* Re init client. */
explicit_bzero(&cli->req, sizeof(http_srv_req_t));
explicit_bzero(&cli->resp, sizeof(http_srv_resp_t));
}
tp_task_p
http_srv_cli_get_tptask(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->tptask);
}
tp_task_p
http_srv_cli_export_tptask(http_srv_cli_p cli) {
tp_task_p tptask;
if (NULL == cli)
return (NULL);
tptask = cli->tptask;
cli->tptask = NULL;
return (tptask);
}
int
http_srv_cli_import_tptask(http_srv_cli_p cli, tp_task_p tptask,
tpt_p tpt) {
if (NULL == cli || NULL == tptask)
return (EINVAL);
/* Convert to "ready to read notifier". */
tp_task_stop(tptask);
tp_task_udata_set(tptask, cli);
tp_task_tp_cb_func_set(tptask, tp_task_sr_handler);
tp_task_flags_set(tptask, (TP_TASK_F_CLOSE_ON_DESTROY | TP_TASK_F_CB_AFTER_EVERY_READ));
tp_task_tpt_set(tptask, tpt);
cli->tptask = tptask;
return (0);
}
io_buf_p
http_srv_cli_get_buf(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->buf);
}
int
http_srv_cli_buf_reset(http_srv_cli_p cli) {
if (NULL == cli || NULL == cli->buf)
return (EINVAL);
IO_BUF_BUSY_SIZE_SET(cli->buf, cli->bnd->srv->s.hdrs_reserve_size);
return (0);
}
int
http_srv_cli_buf_realloc(http_srv_cli_p cli, int allow_decrease, size_t new_size) {
if (NULL == cli || NULL == cli->buf)
return (EINVAL);
new_size += cli->bnd->srv->s.hdrs_reserve_size;
if (new_size > cli->buf->size || /* Need more space! */
(0 != allow_decrease &&
(new_size * 2) < cli->buf->size)) { /* Space too mach. */
return (io_buf_realloc(&cli->buf, 0, new_size));
}
return (0);
}
http_srv_bind_p
http_srv_cli_get_acc(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->bnd);
}
http_srv_p
http_srv_cli_get_srv(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
if (NULL == cli->bnd)
return (NULL);
return (cli->bnd->srv);
}
http_srv_req_p
http_srv_cli_get_req(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (&cli->req);
}
http_srv_resp_p
http_srv_cli_get_resp(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (&cli->resp);
}
int
http_srv_cli_ccb_get(http_srv_cli_p cli, http_srv_cli_ccb_p ccb) {
if (NULL == cli || NULL == ccb)
return (EINVAL);
(*ccb) = cli->ccb; /* memcpy */
return (0);
}
int
http_srv_cli_ccb_set(http_srv_cli_p cli, http_srv_cli_ccb_p ccb) {
if (NULL == cli || NULL == ccb)
return (EINVAL);
cli->ccb = (*ccb); /* memcpy */
return (0);
}
http_srv_on_req_rcv_cb
http_srv_cli_get_on_req_rcv(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->ccb.on_req_rcv);
}
int
http_srv_cli_set_on_req_rcv(http_srv_cli_p cli, http_srv_on_req_rcv_cb on_req_rcv) {
if (NULL == cli)
return (EINVAL);
cli->ccb.on_req_rcv = on_req_rcv;
return (0);
}
http_srv_on_resp_snd_cb
http_srv_cli_get_on_rep_snd(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->ccb.on_rep_snd);
}
int
http_srv_cli_set_on_rep_snd(http_srv_cli_p cli, http_srv_on_resp_snd_cb on_rep_snd) {
if (NULL == cli)
return (EINVAL);
cli->ccb.on_rep_snd = on_rep_snd;
return (0);
}
http_srv_on_destroy_cb
http_srv_cli_get_on_destroy(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->ccb.on_destroy);
}
int
http_srv_cli_set_on_destroy(http_srv_cli_p cli, http_srv_on_destroy_cb on_destroy) {
if (NULL == cli)
return (EINVAL);
cli->ccb.on_destroy = on_destroy;
return (0);
}
void *
http_srv_cli_get_udata(http_srv_cli_p cli) {
if (NULL == cli)
return (NULL);
return (cli->udata);
}
int
http_srv_cli_set_udata(http_srv_cli_p cli, void *udata) {
if (NULL == cli)
return (EINVAL);
cli->udata = udata;
return (0);
}
uint32_t
http_srv_cli_get_flags(http_srv_cli_p cli) {
if (NULL == cli)
return (0);
return (cli->flags);
}
int
http_srv_cli_get_addr(http_srv_cli_p cli, sockaddr_storage_p addr) {
if (NULL == cli || NULL == addr)
return (EINVAL);
sa_copy(&cli->addr, addr);
return (0);
}