-
Notifications
You must be signed in to change notification settings - Fork 32
/
request.c
1841 lines (1619 loc) · 64.8 KB
/
request.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) 2004-2008 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include "debug.h"
#include "request.h"
#include "request_util.h"
#include "service.h"
#include "access.h"
#include "acl.h"
#include "util.h"
#include "cfg_param.h"
#include "stats.h"
#include "body.h"
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
extern int TIMEOUT;
extern int KEEPALIVE_TIMEOUT;
extern const char *DEFAULT_SERVICE;
extern int PIPELINING;
extern int CHECK_FOR_BUGGY_CLIENT;
extern int ALLOW204_AS_200OK_ZERO_ENCAPS;
extern int FAKE_ALLOW204;
/*This variable defined in mpm_server.c and become 1 when the child must
halt imediatelly:*/
extern int CHILD_HALT;
#define FORBITTEN_STR "ICAP/1.0 403 Forbidden\r\n\r\n"
#define ci_method_supported(METHOD, METHOD_DEF) (METHOD & METHOD_DEF)
static ci_thread_mutex_t STAT_MTX;
static ci_stat_memblock_t *STATS = NULL;
static int STAT_REQUESTS = -1;
static int STAT_FAILED_REQUESTS = -1;
static int STAT_BYTES_IN = -1;
static int STAT_BYTES_OUT = -1;
static int STAT_HTTP_BYTES_IN = -1;
static int STAT_HTTP_BYTES_OUT = -1;
static int STAT_BODY_BYTES_IN = -1;
static int STAT_BODY_BYTES_OUT = -1;
static int STAT_REQMODS = -1;
static int STAT_RESPMODS = -1;
static int STAT_OPTIONS = -1;
static int STAT_ALLOW204 = -1;
static int STAT_ALLOW206 = -1;
static int STAT_TIME_PER_REQUESTS = -1;
static int STAT_PROC_TIME_PER_REQUESTS = -1;
static struct timestats {
time_t indx;
uint64_t accumulated_time;
uint64_t accumulated_proc_time;
int requests;
} STAT_TIME = {0, 0, 0};
static int request_stat_entry_register(const char *name, int type, const char *gname)
{
int stat = ci_stat_entry_register(name, type, gname);
assert(stat >= 0);
return stat;
}
void request_stats_init()
{
STAT_REQUESTS = request_stat_entry_register("REQUESTS", CI_STAT_INT64_T, "General");
STAT_REQMODS = request_stat_entry_register("REQMODS", CI_STAT_INT64_T, "General");
STAT_RESPMODS = request_stat_entry_register("RESPMODS", CI_STAT_INT64_T, "General");
STAT_OPTIONS = request_stat_entry_register("OPTIONS", CI_STAT_INT64_T, "General");
STAT_FAILED_REQUESTS = request_stat_entry_register("FAILED REQUESTS", CI_STAT_INT64_T, "General");
STAT_ALLOW204 = request_stat_entry_register("ALLOW 204", CI_STAT_INT64_T, "General");
STAT_ALLOW206 = request_stat_entry_register("ALLOW 206", CI_STAT_INT64_T, "General");
STAT_TIME_PER_REQUESTS = request_stat_entry_register("TIME PER REQUEST", CI_STAT_TIME_US_T, "General");
STAT_PROC_TIME_PER_REQUESTS = request_stat_entry_register("PROCESSING TIME PER REQUEST", CI_STAT_TIME_US_T, "General");
STAT_BYTES_IN = request_stat_entry_register("BYTES IN", CI_STAT_KBS_T, "General");
STAT_BYTES_OUT = request_stat_entry_register("BYTES OUT", CI_STAT_KBS_T, "General");
STAT_HTTP_BYTES_IN = request_stat_entry_register("HTTP BYTES IN", CI_STAT_KBS_T, "General");
STAT_HTTP_BYTES_OUT = request_stat_entry_register("HTTP BYTES OUT", CI_STAT_KBS_T, "General");
STAT_BODY_BYTES_IN = request_stat_entry_register("BODY BYTES IN", CI_STAT_KBS_T, "General");
STAT_BODY_BYTES_OUT = request_stat_entry_register("BODY BYTES OUT", CI_STAT_KBS_T, "General");
/*
Use a threads mutex lock to update request statistics. They are updated
only in one place in this file (function do_request), so this is should
work well.
*/
ci_thread_mutex_init(&STAT_MTX);
}
static int wait_for_data(ci_connection_t *conn, int secs, int what_wait)
{
int wait_status;
/*if we are going down do not wait....*/
if (CHILD_HALT)
return -1;
do {
wait_status = ci_connection_wait(conn, secs, what_wait);
if (wait_status < 0)
return -1;
if (wait_status == 0 && CHILD_HALT) /*abort*/
return -1;
} while (wait_status & ci_wait_should_retry);
if (wait_status == 0) /* timeout */
return -1;
return wait_status;
}
ci_request_t *server_request_alloc()
{
ci_connection_t *conn;
conn = (ci_connection_t *) calloc(1, sizeof(ci_connection_t));
assert(conn);
return ci_request_alloc(conn);
}
int server_request_use_connection(ci_request_t * req, ci_connection_t * connection, int protocol)
{
int access;
int len;
ci_request_reset(req);
ci_copy_connection(req->connection, connection);
if ((access = access_check_client(req)) == CI_ACCESS_DENY) { /*Check for client access */
len = strlen(FORBITTEN_STR);
ci_clock_time_get(&req->start_w_t);
ci_connection_write(connection, FORBITTEN_STR, len, TIMEOUT);
ci_clock_time_get(&req->stop_w_t);
return 0; /*Or something that means authentication error */
}
req->access_type = access;
req->protocol = protocol;
return 1;
}
int keepalive_request(ci_request_t *req)
{
/* Preserve extra read bytes*/
char *pstrblock = req->pstrblock_read;
int pstrblock_len = req->pstrblock_read_len;
/*Preserve protocol:*/
int protocol = req->protocol;
ci_request_reset(req);
req->protocol = protocol;
if (PIPELINING) {
req->pstrblock_read = pstrblock;
req->pstrblock_read_len = pstrblock_len;
}
if (req->pstrblock_read && req->pstrblock_read_len > 0)
return 1;
return wait_for_data(req->connection, KEEPALIVE_TIMEOUT, ci_wait_for_read);
}
/*Here we want to read in small blocks icap header becouse in most cases
it will not bigger than 512-1024 bytes.
So we are going to do small reads and small increments in icap headers size,
to save some space and keep small the number of over-read bytes
*/
#define ICAP_HEADER_READSIZE 512
/*this function check if there is enough space in buffer buf ....*/
static int icap_header_check_realloc(char **buf, int *size, int used, int mustadded)
{
char *newbuf;
int len;
if (*size - used < mustadded) {
len = *size + ICAP_HEADER_READSIZE;
newbuf = realloc(*buf, len);
if (!newbuf) {
return EC_500;
}
*buf = newbuf;
*size = *size + ICAP_HEADER_READSIZE;
}
return EC_100;
}
int ci_read_icap_header(ci_request_t * req, ci_headers_list_t * h, int timeout)
{
int bytes, request_status = EC_100, i, eoh = 0, startsearch = 0, readed = 0;
int wait_status = 0;
char *buf_end;
int dataPrefetch = 0;
buf_end = h->buf;
readed = 0;
bytes = 0;
if (PIPELINING && req->pstrblock_read && req->pstrblock_read_len > 0) {
if ((request_status =
icap_header_check_realloc(&(h->buf), &(h->bufsize), req->pstrblock_read_len,
ICAP_HEADER_READSIZE)) != EC_100)
return request_status;
memmove(h->buf, req->pstrblock_read, req->pstrblock_read_len);
readed = req->pstrblock_read_len;
buf_end = h->buf;
bytes = readed;
dataPrefetch = 1;
req->pstrblock_read = NULL;
req->pstrblock_read_len = 0;
ci_debug_printf(5, "Get data from previous request read.\n");
}
do {
if (!dataPrefetch) {
if ((wait_status = wait_for_data(req->connection, timeout, ci_wait_for_read)) < 0)
return EC_408;
bytes = ci_connection_read_nonblock(req->connection, buf_end, ICAP_HEADER_READSIZE);
if (bytes < 0)
return EC_408;
if (bytes == 0) /*NOP? should retry?*/
continue;
readed += bytes;
req->bytes_in += bytes;
} else
dataPrefetch = 0;
for (i = startsearch; i < bytes - 3; i++) { /*search for end of header.... */
if (strncmp(buf_end + i, "\r\n\r\n", 4) == 0) {
buf_end = buf_end + i + 2;
eoh = 1;
break;
}
}
if (eoh)
break;
if ((request_status =
icap_header_check_realloc(&(h->buf), &(h->bufsize), readed,
ICAP_HEADER_READSIZE)) != EC_100)
break;
buf_end = h->buf + readed;
if (startsearch > -3)
startsearch = (readed > 3 ? -3 : -readed); /*Including the last 3 char ellements ....... */
} while (1);
h->bufused = buf_end - h->buf; /* -1 ; */
req->pstrblock_read = buf_end + 2; /*after the \r\n\r\n. We keep the first \r\n and the other dropped.... */
req->pstrblock_read_len = readed - h->bufused - 2; /*the 2 of the 4 characters \r\n\r\n and the '\0' character */
req->request_bytes_in = h->bufused + 2; /*This is include the "\r\n\r\n" sequence*/
return request_status;
}
static int read_encaps_header(ci_request_t * req, ci_headers_list_t * h, int size)
{
int bytes = 0, remains, readed = 0;
char *buf_end = NULL;
if (!ci_headers_setsize(h, size + (CHECK_FOR_BUGGY_CLIENT != 0 ? 2 : 0)))
return EC_500;
buf_end = h->buf;
if (req->pstrblock_read_len > 0) {
readed =
(size > req->pstrblock_read_len ? req->pstrblock_read_len : size);
memcpy(h->buf, req->pstrblock_read, readed);
buf_end = h->buf + readed;
if (size <= req->pstrblock_read_len) { /*We have readed all this header....... */
req->pstrblock_read = (req->pstrblock_read) + readed;
req->pstrblock_read_len = (req->pstrblock_read_len) - readed;
} else {
req->pstrblock_read = NULL;
req->pstrblock_read_len = 0;
}
}
remains = size - readed;
while (remains > 0) {
if (wait_for_data(req->connection, TIMEOUT, ci_wait_for_read) < 0)
return CI_ERROR;
if ((bytes = ci_connection_read_nonblock(req->connection, buf_end, remains)) < 0)
return CI_ERROR;
remains -= bytes;
buf_end += bytes;
req->bytes_in += bytes;
}
h->bufused = buf_end - h->buf; // -1 ;
if (strncmp(buf_end - 4, "\r\n\r\n", 4) == 0) {
h->bufused -= 2; /*eat the last 2 bytes of "\r\n\r\n" */
} else if (CHECK_FOR_BUGGY_CLIENT && strncmp(buf_end - 2, "\r\n", 2) != 0) {
// Some icap clients missing the "\r\n\r\n" after end of headers
// when null-body is present.
*buf_end = '\r';
*(buf_end + 1) = '\n';
h->bufused += 2;
}
/*Currently we are counting only successfull http headers read.*/
req->http_bytes_in += size;
req->request_bytes_in += size;
return EC_100;
}
static int get_method(char *buf, char **end)
{
if (!strncmp(buf, "OPTIONS", 7)) {
*end = buf + 7;
return ICAP_OPTIONS;
} else if (!strncmp(buf, "REQMOD", 6)) {
*end = buf + 6;
return ICAP_REQMOD;
} else if (!strncmp(buf, "RESPMOD", 7)) {
*end = buf + 7;
return ICAP_RESPMOD;
} else {
*end = buf;
return -1;
}
}
static int parse_request(ci_request_t * req, char *buf)
{
char *start, *end;
int servnamelen, len, args_len;
int vmajor, vminor;
ci_service_module_t *service = NULL;
service_alias_t *salias = NULL;
if ((req->type = get_method(buf, &end)) < 0)
return EC_400;
while (*end == ' ') end++;
start = end;
if (strncasecmp(start, "icap://", 7) == 0)
start = start + 7;
else if (strncasecmp(start, "icaps://", 8) == 0)
start = start + 8;
else
return EC_400;
len = strcspn(start, "/ ");
end = start + len;
servnamelen =
(CI_MAXHOSTNAMELEN > len ? len : CI_MAXHOSTNAMELEN);
memcpy(req->req_server, start, servnamelen);
req->req_server[servnamelen] = '\0';
if (*end == '/') { /*we are expecting service name*/
start = ++end;
while (*end && *end != ' ' && *end != '?')
end++;
len = end - start;
len =
(len < MAX_SERVICE_NAME ? len : MAX_SERVICE_NAME);
if (len) {
strncpy(req->service, start, len);
req->service[len] = '\0';
}
if (*end == '?') { /*args */
start = ++end;
if ((end = strchr(start, ' ')) != NULL) {
args_len = strlen(req->args);
len = end - start;
if (args_len && len) {
req->args[args_len] = '&';
args_len++;
}
len = (len < (MAX_SERVICE_ARGS - args_len) ?
len : (MAX_SERVICE_ARGS - args_len));
strncpy(req->args + args_len, start, len);
req->args[args_len + len] = '\0';
} else
return EC_400;
} /*end of parsing args */
}
while (*end == ' ')
end++;
start = end;
vminor = vmajor = -1;
if (strncmp(start, "ICAP/", 5) == 0) {
start += 5;
vmajor = strtol(start, &end, 10);
if (vmajor > 0 && *end == '.') {
start = end + 1;
vminor = strtol(start, &end, 10);
if (end == start) /*no chars parsed*/
vminor = -1;
}
}
if (vminor == -1 || vmajor < 1)
return EC_400;
if (req->service[0] == '\0' && DEFAULT_SERVICE) { /*No service name defined*/
strncpy(req->service, DEFAULT_SERVICE, MAX_SERVICE_NAME);
}
if (req->service[0] != '\0') {
if (!(service = find_service(req->service))) { /*else search for an alias */
if ((salias = find_service_alias(req->service))) {
service = salias->service;
if (salias->args[0] != '\0') {
strncpy(req->args, salias->args, MAX_SERVICE_ARGS);
req->args[MAX_SERVICE_ARGS] = '\0';
}
}
}
}
req->current_service_mod = service;
if (!req->current_service_mod)
return EC_404; /*Service not found*/
if (!ci_method_supported(req->current_service_mod->mod_type, req->type)
&& req->type != ICAP_OPTIONS) {
return EC_405; /* Method not allowed for service. */
}
return EC_100;
}
static int check_request(ci_request_t *req)
{
/*Check encapsulated header*/
if (req->entities[0] == NULL && req->type != ICAP_OPTIONS) /*No encapsulated header*/
return EC_400;
ci_debug_printf(6, "\n type:%d Entities: %d %d %d %d \n",
req->type,
req->entities[0] ? req->entities[0]->type : -1,
req->entities[1] ? req->entities[1]->type : -1,
req->entities[2] ? req->entities[2]->type : -1,
req->entities[3] ? req->entities[3]->type : -1
);
if (req->type == ICAP_REQMOD) {
if (req->entities[2] != NULL)
return EC_400;
else if (req->entities[1] != NULL) {
if (req->entities[0]->type != ICAP_REQ_HDR)
return EC_400;
if (req->entities[1]->type != ICAP_REQ_BODY && req->entities[1]->type != ICAP_NULL_BODY)
return EC_400;
} else {
/*If it has only one encapsulated object it must be body data*/
if (req->entities[0]->type != ICAP_REQ_BODY)
return EC_400;
}
} else if (req->type == ICAP_RESPMOD) {
if (req->entities[3] != NULL)
return EC_400;
else if (req->entities[2] != NULL) {
assert(req->entities[0]);
assert(req->entities[1]);
if (req->entities[0]->type != ICAP_REQ_HDR)
return EC_400;
if (req->entities[1]->type != ICAP_RES_HDR)
return EC_400;
if (req->entities[2]->type != ICAP_RES_BODY && req->entities[2]->type != ICAP_NULL_BODY)
return EC_400;
} else if (req->entities[1] != NULL) {
if (req->entities[0]->type != ICAP_RES_HDR && req->entities[0]->type != ICAP_REQ_HDR)
return EC_400;
if (req->entities[1]->type != ICAP_RES_BODY && req->entities[1]->type != ICAP_NULL_BODY)
return EC_400;
} else {
/*If it has only one encapsulated object it must be body data*/
if (req->entities[0]->type != ICAP_RES_BODY)
return EC_400;
}
}
return EC_100;
}
static int parse_header(ci_request_t * req)
{
int i, request_status = EC_100, result;
ci_headers_list_t *h;
char *val;
h = req->request_header;
if ((request_status = ci_read_icap_header(req, h, TIMEOUT)) != EC_100)
return request_status;
if ((request_status = ci_headers_unpack(h)) != EC_100)
return request_status;
if ((request_status = parse_request(req, h->headers[0])) != EC_100)
return request_status;
for (i = 1; i < h->used && request_status == EC_100; i++) {
if (strncasecmp("Preview:", h->headers[i], 8) == 0) {
val = h->headers[i] + 8;
for (; isspace((int)*val) && *val != '\0'; ++val);
errno = 0;
result = strtol(val, NULL, 10);
if (errno != EINVAL && errno != ERANGE) {
req->preview = result;
if (result >= 0)
ci_buf_reset_and_resize(&(req->preview_data), result + 64);
}
} else if (strncasecmp("Encapsulated:", h->headers[i], 13) == 0)
request_status = process_encapsulated(req, h->headers[i]);
else if (strncasecmp("Connection:", h->headers[i], 11) == 0) {
val = h->headers[i] + 11;
for (; isspace((int)*val) && *val != '\0'; ++val);
/* if(strncasecmp(val,"keep-alive",10)==0)*/
if (strncasecmp(val, "close", 5) == 0)
req->keepalive = 0;
/*else the default behaviour of keepalive ..... */
} else if (strncasecmp("Allow:", h->headers[i], 6) == 0) {
if (strstr(h->headers[i]+6, "204"))
req->allow204 = 1;
if (strstr(h->headers[i]+6, "206"))
req->allow206 = 1;
}
}
if (request_status != EC_100)
return request_status;
return check_request(req);
}
static int parse_encaps_headers(ci_request_t * req)
{
int size, i, request_status = 0;
ci_encaps_entity_t *e = NULL;
for (i = 0; (e = req->entities[i]) != NULL; i++) {
if (e->type > ICAP_RES_HDR) //res_body,req_body or opt_body so the end of the headers.....process_encapsulated
return EC_100;
if (req->entities[i + 1] == NULL)
return EC_400;
size = req->entities[i + 1]->start - e->start;
if ((request_status =
read_encaps_header(req, (ci_headers_list_t *) e->entity,
size)) != EC_100)
return request_status;
if ((request_status =
ci_headers_unpack((ci_headers_list_t *) e->entity)) != EC_100)
return request_status;
}
return EC_100;
}
/*
In read_preview_data I must check if readed data are more than
those client said in preview header
*/
static int read_preview_data(ci_request_t * req)
{
int ret;
char *wdata;
req->current_chunk_len = 0;
req->chunk_bytes_read = 0;
req->write_to_module_pending = 0;
if (req->pstrblock_read_len == 0) {
if (wait_for_data(req->connection, TIMEOUT, ci_wait_for_read) < 0)
return CI_ERROR;
if (net_data_read(req) == CI_ERROR)
return CI_ERROR;
}
do {
do {
if ((ret = parse_chunk_data(req, &wdata)) == CI_ERROR) {
ci_debug_printf(1,
"Error parsing chunks, current chunk len: %d readed:%d, str:%s\n",
req->current_chunk_len,
req->chunk_bytes_read, req->pstrblock_read);
return CI_ERROR;
}
if (ci_buf_write
(&(req->preview_data), wdata,
req->write_to_module_pending) < 0)
return CI_ERROR;
req->write_to_module_pending = 0;
if (ret == CI_EOF) {
req->pstrblock_read = NULL;
req->pstrblock_read_len = 0;
if (req->eof_received)
return CI_EOF;
return CI_OK;
}
} while (ret != CI_NEEDS_MORE);
if (wait_for_data(req->connection, TIMEOUT, ci_wait_for_read) < 0)
return CI_ERROR;
if (net_data_read(req) == CI_ERROR)
return CI_ERROR;
} while (1);
return CI_ERROR;
}
static void ec_responce_simple(ci_request_t * req, int ec)
{
char buf[256];
int len;
snprintf(buf, sizeof(buf), "ICAP/1.0 %d %s\r\n\r\n",
ci_status_code(ec), ci_status_code_string(ec));
len = strlen(buf);
ci_clock_time_get(&req->start_w_t);
ci_connection_write(req->connection, buf, len, TIMEOUT);
ci_clock_time_get(&req->stop_w_t);
req->bytes_out += len;
req->return_code = ec;
}
static int ec_responce(ci_request_t * req, int ec)
{
char buf[256];
ci_service_xdata_t *srv_xdata = NULL;
int len, allow204to200OK = 0;
if (req->current_service_mod)
srv_xdata = service_data(req->current_service_mod);
ci_headers_reset(req->response_header);
if (ec == EC_204 && ALLOW204_AS_200OK_ZERO_ENCAPS) {
allow204to200OK = 1;
ec = EC_200;
}
snprintf(buf, sizeof(buf), "ICAP/1.0 %d %s",
ci_status_code(ec), ci_status_code_string(ec));
ci_headers_add(req->response_header, buf);
ci_headers_add(req->response_header, "Server: C-ICAP/" VERSION);
if (req->keepalive)
ci_headers_add(req->response_header, "Connection: keep-alive");
else
ci_headers_add(req->response_header, "Connection: close");
if (srv_xdata) {
ci_service_data_read_lock(srv_xdata);
ci_headers_add(req->response_header, srv_xdata->ISTag);
ci_service_data_read_unlock(srv_xdata);
}
if (!ci_headers_is_empty(req->xheaders)) {
ci_headers_addheaders(req->response_header, req->xheaders);
}
if (allow204to200OK) {
if (req->type == ICAP_REQMOD)
ci_headers_add(req->response_header, "Encapsulated: req-hdr=0, null-body=0");
else
ci_headers_add(req->response_header, "Encapsulated: res-hdr=0, null-body=0");
}
/*
TODO: Release req->entities (ci_request_release_entity())
*/
ci_headers_pack(req->response_header);
req->return_code = ec;
ci_clock_time_get(&req->start_w_t);
len = ci_connection_write(req->connection,
req->response_header->buf, req->response_header->bufused,
TIMEOUT);
ci_clock_time_get(&req->stop_w_t);
/*We are finishing sending*/
req->status = SEND_EOF;
if (len < 0)
return -1;
req->bytes_out += len;
return len;
}
extern char MY_HOSTNAME[];
static int mk_responce_header(ci_request_t * req)
{
ci_headers_list_t *head;
ci_encaps_entity_t **e_list;
ci_service_xdata_t *srv_xdata;
char buf[512];
srv_xdata = service_data(req->current_service_mod);
ci_headers_reset(req->response_header);
head = req->response_header;
assert(req->return_code >= EC_100 && req->return_code < EC_MAX);
snprintf(buf, sizeof(buf), "ICAP/1.0 %d %s",
ci_status_code(req->return_code), ci_status_code_string(req->return_code));
ci_headers_add(head, buf);
ci_headers_add(head, "Server: C-ICAP/" VERSION);
if (req->keepalive)
ci_headers_add(head, "Connection: keep-alive");
else
ci_headers_add(head, "Connection: close");
ci_service_data_read_lock(srv_xdata);
ci_headers_add(head, srv_xdata->ISTag);
ci_service_data_read_unlock(srv_xdata);
if (!ci_headers_is_empty(req->xheaders)) {
ci_headers_addheaders(head, req->xheaders);
}
e_list = req->entities;
if (req->type == ICAP_RESPMOD) {
if (e_list[0]->type == ICAP_REQ_HDR) {
ci_request_release_entity(req, 0);
e_list[0] = e_list[1];
e_list[1] = e_list[2];
e_list[2] = NULL;
}
}
snprintf(buf, sizeof(buf), "Via: ICAP/1.0 %s (C-ICAP/" VERSION " %s )",
MY_HOSTNAME,
(req->current_service_mod->mod_short_descr ? req->
current_service_mod->mod_short_descr : req->current_service_mod->
mod_name));
/*Here we must append it to an existsing Via header not just add a new header */
if (req->type == ICAP_RESPMOD) {
ci_http_response_add_header(req, buf);
} else if (req->type == ICAP_REQMOD) {
ci_http_request_add_header(req, buf);
}
ci_response_pack(req);
return 1;
}
/****************************************************************/
/* New functions to send responce */
const char *eol_str = "\r\n";
const char *eof_str = "0\r\n\r\n";
static int send_current_block_data(ci_request_t * req)
{
int bytes;
if (req->remain_send_block_bytes == 0)
return 0;
if ((bytes =
ci_connection_write_nonblock(req->connection, req->pstrblock_responce,
req->remain_send_block_bytes)) < 0) {
ci_debug_printf(5, "Error writing to socket (errno:%d, bytes:%d. string:\"%s\")", errno, req->remain_send_block_bytes, req->pstrblock_responce);
return CI_ERROR;
}
ci_clock_time_get(&req->stop_w_t);
/*
if (bytes == 0) {
ci_debug_printf(5, "Can not write to the client. Is the connection closed?");
return CI_ERROR;
}
*/
req->pstrblock_responce += bytes;
req->remain_send_block_bytes -= bytes;
req->bytes_out += bytes;
if (req->status >= SEND_HEAD1 && req->status <= SEND_HEAD3)
req->http_bytes_out +=bytes;
return req->remain_send_block_bytes;
}
static int format_body_chunk(ci_request_t * req)
{
int def_bytes;
char *wbuf = NULL;
char tmpbuf[EXTRA_CHUNK_SIZE];
static const size_t REQ_WBUF_SIZE = sizeof(((ci_request_t *)0)->wbuf);
if (!req->responce_hasbody)
return CI_EOF;
if (req->remain_send_block_bytes > 0) {
assert(req->remain_send_block_bytes <= MAX_CHUNK_SIZE);
/*The data are not written yet but I hope there is not any problem.
It is difficult to compute data sent */
req->http_bytes_out += req->remain_send_block_bytes;
req->body_bytes_out += req->remain_send_block_bytes;
wbuf = req->wbuf + EXTRA_CHUNK_SIZE + req->remain_send_block_bytes;
/*Put the "\r\n" sequence at the end of chunk */
*(wbuf++) = '\r';
*wbuf = '\n';
def_bytes =
snprintf(tmpbuf, sizeof(tmpbuf), "%x\r\n",
req->remain_send_block_bytes);
assert(def_bytes < EXTRA_CHUNK_SIZE);
wbuf = req->wbuf + EXTRA_CHUNK_SIZE - def_bytes; /*Copy the chunk define in the beggining of chunk ..... */
memcpy(wbuf, tmpbuf, def_bytes);
req->pstrblock_responce = wbuf;
req->remain_send_block_bytes += def_bytes + 2;
} else if (req->remain_send_block_bytes == CI_EOF) {
if (req->return_code == EC_206 && req->i206_use_original_body >= 0) {
def_bytes = snprintf(req->wbuf, REQ_WBUF_SIZE,
"0; use-original-body=%" PRId64 "\r\n\r\n",
req->i206_use_original_body );
req->pstrblock_responce = req->wbuf;
req->remain_send_block_bytes = def_bytes;
} else {
def_bytes = snprintf(req->wbuf, REQ_WBUF_SIZE, "0\r\n\r\n");
req->pstrblock_responce = req->wbuf;
req->remain_send_block_bytes = def_bytes;
}
return CI_EOF;
}
return CI_OK;
}
static int resp_check_body(ci_request_t * req)
{
int i;
ci_encaps_entity_t **e = req->entities;
for (i = 0; e[i] != NULL; i++)
if (e[i]->type == ICAP_NULL_BODY)
return 0;
return 1;
}
/*
The
if((ret=send_current_block_data(req))!=0)
return ret;
must called after this function....
*/
static int update_send_status(ci_request_t * req)
{
int i, status;
ci_encaps_entity_t *e;
if (req->status == SEND_NOTHING) { //If nothing has send start sending....
if (!mk_responce_header(req)) {
ci_debug_printf(1, "Error constructing the responce headers!\n");
return CI_ERROR;
}
req->responce_hasbody = resp_check_body(req);
req->pstrblock_responce = req->response_header->buf;
req->remain_send_block_bytes = req->response_header->bufused;
req->status = SEND_RESPHEAD;
ci_clock_time_get(&req->start_w_t);
ci_debug_printf(9, "Going to send response headers\n");
return CI_OK;
}
if (req->status == SEND_EOF) {
ci_debug_printf(9, "The req->status is EOF (remain to send bytes:%d)\n",
req->remain_send_block_bytes);
if (req->remain_send_block_bytes == 0)
return CI_EOF;
else
return CI_OK;
}
if (req->status == SEND_BODY) {
ci_debug_printf(9, "Send status is SEND_BODY return\n");
return CI_OK;
}
if ((status = req->status) < SEND_HEAD3) {
status++;
}
if (status > SEND_RESPHEAD && status < SEND_BODY) { /*status is SEND_HEAD1 SEND_HEAD2 or SEND_HEAD3 */
i = status - SEND_HEAD1; /*We have to send next headers block .... */
if ((e = req->entities[i]) != NULL
&& (e->type == ICAP_REQ_HDR || e->type == ICAP_RES_HDR)) {
req->pstrblock_responce = ((ci_headers_list_t *) e->entity)->buf;
req->remain_send_block_bytes =
((ci_headers_list_t *) e->entity)->bufused;
req->status = status;
ci_debug_printf(9, "Going to send http headers on entity :%d\n", i);
return CI_OK;
} else if (req->responce_hasbody) { /*end of headers, going to send body now.A body always follows the res_hdr or req_hdr..... */
req->status = SEND_BODY;
return CI_OK;
} else {
req->status = SEND_EOF;
req->pstrblock_responce = (char *) NULL;
req->remain_send_block_bytes = 0;
return CI_EOF;
}
}
return CI_ERROR; /*Can not be reached (I thing)...... */
}
static int mod_null_io(char *rbuf, int *rlen, char *wbuf, int *wlen, int iseof,
ci_request_t *req)
{
if (iseof)
*rlen = CI_EOF;
else
*rlen = 0;
return CI_OK;
}
static int mod_echo_io(char *wbuf, int *wlen, char *rbuf, int *rlen, int iseof,
ci_request_t *req)
{
if (!req->echo_body)
return CI_ERROR;
if (rlen && rbuf) {
*rlen = ci_ring_buf_write(req->echo_body, rbuf, *rlen);
if (*rlen < 0)
return CI_ERROR;
}
if (wbuf && wlen) {
*wlen = ci_ring_buf_read(req->echo_body, wbuf, *wlen);
if (*wlen == 0 && req->eof_received)
*wlen = CI_EOF;
}
return CI_OK;
}
static int get_send_body(ci_request_t * req, int parse_only)
{
char *wchunkdata = NULL, *rchunkdata = NULL;
int parse_chunk_ret, has_formated_data = 0;
int (*service_io) (char *rbuf, int *rlen, char *wbuf, int *wlen, int iseof,
ci_request_t *);
int action = 0, rchunk_has_space = 0, service_eof = 0, wbytes, rbytes;
int lock_status;
int no_io;
if (parse_only)
service_io = mod_null_io;
else if (req->echo_body)
service_io = mod_echo_io;
else
service_io = req->current_service_mod->mod_service_io;
if (!service_io)
return CI_ERROR;
req->status = SEND_NOTHING;
/*in the case we did not have preview data and body is small maybe
the c-icap already read the body with the headers so do not read
if there are unparsed bytes in pstrblock buffer
*/
if (req->pstrblock_read_len == 0)
action = ci_wait_for_read;
do {
if (action) {
int ret;
ci_debug_printf(9, "Going to %s/%s data\n",
(action & ci_wait_for_read ? "Read" : "-"),
(action & ci_wait_for_write ? "Write" : "-")
);
if ((ret =
wait_for_data(req->connection, TIMEOUT,
action)) < 0)
break;
if (ret & ci_wait_for_read) {
if (net_data_read(req) == CI_ERROR)
return CI_ERROR;
}
if (ret & ci_wait_for_write) {
if (!req->data_locked && req->status == SEND_NOTHING) {
update_send_status(req);
}
if (send_current_block_data(req) == CI_ERROR)
return CI_ERROR;
}
ci_debug_printf(9,
"OK done reading/writing going to process\n");
}