-
Notifications
You must be signed in to change notification settings - Fork 104
/
handler_proxy.c
1847 lines (1534 loc) · 45 KB
/
handler_proxy.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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2010 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "handler_proxy.h"
#include "connection-protected.h"
#include "util.h"
#include "thread.h"
#include "server-protected.h"
#include "source_interpreter.h"
#define ENTRIES "proxy"
#define DEFAULT_BUF_SIZE (64*1024) /* 64Kb */
#define DEFAULT_REUSE_MAX 16
/* Plug-in initialization
*/
PLUGIN_INFO_HANDLER_EASIEST_INIT (proxy, http_all_methods);
typedef struct {
cherokee_list_t listed;
cherokee_buffer_t key;
cherokee_buffer_t val;
} cherokee_header_add_t;
#define HEADER_ADD(h) ((cherokee_header_add_t *)(h))
static ret_t
header_add_new (cherokee_header_add_t **header)
{
CHEROKEE_NEW_STRUCT(n,header_add);
INIT_LIST_HEAD(&n->listed);
cherokee_buffer_init (&n->key);
cherokee_buffer_init (&n->val);
*header = n;
return ret_ok;
}
static ret_t
header_add_free (cherokee_header_add_t *header)
{
cherokee_buffer_mrproper (&header->key);
cherokee_buffer_mrproper (&header->val);
free (header);
return ret_ok;
}
static ret_t
props_free (cherokee_handler_proxy_props_t *props)
{
cherokee_list_t *i, *tmp;
cherokee_handler_proxy_hosts_mrproper (&props->hosts);
cherokee_avl_mrproper (&props->in_headers_hide, NULL);
cherokee_avl_mrproper (&props->out_headers_hide, NULL);
cherokee_regex_list_mrproper (&props->in_request_regexs);
cherokee_regex_list_mrproper (&props->out_request_regexs);
list_for_each_safe (i, tmp, &props->in_headers_add) {
cherokee_list_del (i);
header_add_free (HEADER_ADD(i));
}
list_for_each_safe (i, tmp, &props->out_headers_add) {
cherokee_list_del (i);
header_add_free (HEADER_ADD(i));
}
return cherokee_module_props_free_base (MODULE_PROPS(props));
}
ret_t
cherokee_handler_proxy_configure (cherokee_config_node_t *conf,
cherokee_server_t *srv,
cherokee_module_props_t **_props)
{
ret_t ret;
cherokee_list_t *i, *j;
cherokee_handler_proxy_props_t *props;
UNUSED(srv);
/* Instance a new property object
*/
if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, handler_proxy_props);
cherokee_module_props_init_base (MODULE_PROPS(n),
MODULE_PROPS_FREE(props_free));
n->balancer = NULL;
n->reuse_max = DEFAULT_REUSE_MAX;
n->in_allow_keepalive = true;
n->in_preserve_host = false;
n->out_preserve_server = false;
n->out_flexible_EOH = true;
n->vserver_errors = false;
INIT_LIST_HEAD (&n->in_request_regexs);
INIT_LIST_HEAD (&n->in_headers_add);
INIT_LIST_HEAD (&n->out_headers_add);
INIT_LIST_HEAD (&n->out_request_regexs);
cherokee_avl_init (&n->in_headers_hide);
cherokee_avl_set_case (&n->in_headers_hide, false);
cherokee_avl_init (&n->out_headers_hide);
cherokee_avl_set_case (&n->out_headers_hide, false);
*_props = MODULE_PROPS(n);
}
props = PROP_PROXY(*_props);
/* Parse the configuration tree
*/
cherokee_config_node_foreach (i, conf) {
cherokee_config_node_t *subconf = CONFIG_NODE(i);
if (equal_buf_str (&subconf->key, "balancer")) {
ret = cherokee_balancer_instance (&subconf->val, subconf,
srv, &props->balancer);
if (ret != ret_ok)
return ret;
} else if (equal_buf_str (&subconf->key, "reuse_max")) {
props->reuse_max = atoi (subconf->val.buf);
} else if (equal_buf_str (&subconf->key, "vserver_errors")) {
props->vserver_errors = !! atoi (subconf->val.buf);
} else if (equal_buf_str (&subconf->key, "in_allow_keepalive")) {
props->in_allow_keepalive = !! atoi (subconf->val.buf);
} else if (equal_buf_str (&subconf->key, "in_preserve_host")) {
props->in_preserve_host = !! atoi (subconf->val.buf);
} else if (equal_buf_str (&subconf->key, "out_preserve_server")) {
props->out_preserve_server = !! atoi (subconf->val.buf);
} else if (equal_buf_str (&subconf->key, "out_flexible_EOH")) {
props->out_flexible_EOH = !! atoi (subconf->val.buf);
} else if (equal_buf_str (&subconf->key, "in_header_hide")) {
cherokee_config_node_foreach (j, subconf) {
cherokee_avl_add (&props->in_headers_hide,
&CONFIG_NODE(j)->val, NULL);
}
} else if (equal_buf_str (&subconf->key, "out_header_hide")) {
cherokee_config_node_foreach (j, subconf) {
cherokee_avl_add (&props->out_headers_hide,
&CONFIG_NODE(j)->val, NULL);
}
} else if (equal_buf_str (&subconf->key, "in_header_add") ||
equal_buf_str (&subconf->key, "out_header_add")) {
cherokee_config_node_foreach (j, subconf) {
cherokee_header_add_t *header = NULL;
ret = header_add_new (&header);
if (ret != ret_ok)
return ret_error;
cherokee_buffer_add_buffer (&header->key, &CONFIG_NODE(j)->key);
cherokee_buffer_add_buffer (&header->val, &CONFIG_NODE(j)->val);
if (equal_buf_str (&subconf->key, "in_header_add"))
cherokee_list_add (&header->listed,
&props->in_headers_add);
else
cherokee_list_add (&header->listed,
&props->out_headers_add);
}
} else if (equal_buf_str (&subconf->key, "in_rewrite_request")) {
ret = cherokee_regex_list_configure (&props->in_request_regexs,
subconf, srv->regexs);
if (ret != ret_ok)
return ret;
/* Rewrite entries were fed in a decreasing
* order, but they should be evaluated from
* the lower to the highest: Revert them now.
*/
cherokee_list_invert (&props->in_request_regexs);
} else if (equal_buf_str (&subconf->key, "out_rewrite_request")) {
ret = cherokee_regex_list_configure (&props->out_request_regexs,
subconf, srv->regexs);
if (ret != ret_ok)
return ret;
cherokee_list_invert (&props->out_request_regexs);
}
}
/* Init properties
*/
cherokee_handler_proxy_hosts_init (&props->hosts);
/* Final checks
*/
if (props->balancer == NULL) {
LOG_CRITICAL_S (CHEROKEE_ERROR_HANDLER_NO_BALANCER);
return ret_error;
}
return ret_ok;
}
static int
replace_againt_regex_list (cherokee_buffer_t *in_buf,
cherokee_buffer_t *out_buf,
cherokee_list_t *regexs)
{
int re;
cherokee_list_t *i;
cherokee_regex_entry_t *regex_entry;
cint_t ovector[OVECTOR_LEN];
list_for_each (i, regexs) {
regex_entry = REGEX_ENTRY(i);
re = pcre_exec (regex_entry->re, NULL,
in_buf->buf, in_buf->len, 0, 0,
ovector, OVECTOR_LEN);
if (re == 0) {
LOG_ERROR_S (CHEROKEE_ERROR_HANDLER_REGEX_GROUPS);
}
if (re <= 0) {
continue;
}
/* Matched */
cherokee_regex_substitute (®ex_entry->subs, in_buf, out_buf, ovector, re, '$');
return 1;
}
return 0;
}
static ret_t
add_request (cherokee_handler_proxy_t *hdl,
cherokee_buffer_t *buf)
{
int re;
ret_t ret;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_buffer_t *tmp = &HANDLER_THREAD(hdl)->tmp_buf1;
cherokee_handler_proxy_props_t *props = HDL_PROXY_PROPS(hdl);
cherokee_buffer_clean (tmp);
/* Build the URL
*/
ret = cherokee_buffer_escape_uri (tmp, &conn->request);
if (ret != ret_ok) {
return ret_error;
}
if (! cherokee_buffer_is_empty (&conn->pathinfo)) {
ret = cherokee_buffer_escape_uri (tmp, &conn->pathinfo);
if (ret != ret_ok) {
return ret_error;
}
}
if (! cherokee_buffer_is_empty (&conn->query_string)) {
cherokee_buffer_add_char (tmp, '?');
ret = cherokee_buffer_add_buffer (tmp, &conn->query_string);
if (ret != ret_ok) {
return ret_error;
}
}
TRACE(ENTRIES, "Client request: '%s'\n", tmp->buf);
/* Check the regexs
*/
re = replace_againt_regex_list (tmp, buf, &props->in_request_regexs);
if (re == 0) {
/* Did not match any regex, use the raw URL
*/
cherokee_buffer_add_buffer (buf, tmp);
}
return ret_ok;
}
static void
add_header (cherokee_buffer_t *buf,
cherokee_buffer_t *key,
cherokee_buffer_t *val)
{
char *p;
char *end;
/* Remove the old header, if existed.
*/
p = buf->buf;
do {
p = strstr (p, key->buf);
if (p == NULL)
break;
if (p[key->len] != ':') {
p += key->len;
continue;
}
end = strchr (p + key->len, CHR_CR);
if (end) {
if (end[1] == CHR_LF)
end += 2;
else
end += 1;
} else {
end = strchr (p + key->len, CHR_LF);
}
if (unlikely (end == NULL))
return;
cherokee_buffer_remove_chunk (buf, p-buf->buf, end-p);
break;
} while (p);
/* Add the new header
*/
cherokee_buffer_add_buffer (buf, key);
cherokee_buffer_add_str (buf, ": ");
cherokee_buffer_add_buffer (buf, val);
cherokee_buffer_add_str (buf, CRLF);
}
static ret_t
build_request (cherokee_handler_proxy_t *hdl,
cherokee_buffer_t *buf)
{
ret_t ret;
cuint_t len;
const char *str;
char *begin;
char *colon;
char *end;
cuint_t header_len;
char *header_end;
cherokee_list_t *i;
char *ptr;
cuint_t ptr_len;
char *XFF = NULL;
cuint_t XFF_len = 0;
cherokee_boolean_t XFH = false;
cherokee_boolean_t is_keepalive = false;
cherokee_boolean_t is_close = false;
cherokee_boolean_t x_real_ip = false;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_handler_proxy_props_t *props = HDL_PROXY_PROPS(hdl);
cherokee_buffer_t *tmp = &HANDLER_THREAD(hdl)->tmp_buf1;
/* Method */
ret = cherokee_http_method_to_string (conn->header.method, &str, &len);
if (ret != ret_ok)
goto error;
cherokee_buffer_add (buf, str, len);
cherokee_buffer_add_char (buf, ' ');
/* The request */
ret = add_request (hdl, buf);
if (ret != ret_ok)
goto error;
/* HTTP version */
ret = cherokee_http_version_to_string (conn->header.version, &str, &len);
if (ret != ret_ok)
goto error;
cherokee_buffer_add_char (buf, ' ');
cherokee_buffer_add (buf, str, len);
cherokee_buffer_add_str (buf, CRLF);
/* Add header: "Host: " */
cherokee_buffer_add_str (buf, "Host: ");
if ((props->in_preserve_host) &&
(! cherokee_buffer_is_empty (&conn->host)))
{
cherokee_buffer_add_buffer (buf, &conn->host);
if (conn->host_port.len > 0) {
cherokee_buffer_add_str (buf, ":");
cherokee_buffer_add_buffer (buf, &conn->host_port);
}
}
else {
cherokee_buffer_add_buffer (buf, &hdl->src_ref->host);
if (! http_port_is_standard (hdl->src_ref->port, conn->socket.is_tls)) {
cherokee_buffer_add_char (buf, ':');
cherokee_buffer_add_ulong10 (buf, hdl->src_ref->port);
}
}
cherokee_buffer_add_str (buf, CRLF);
/* Add header: "Connection: " */
ret = cherokee_header_get_known (&conn->header, header_connection, &ptr, &ptr_len);
if (ret == ret_ok) {
if (! strncasecmp (ptr, "Keep-Alive", 10)) {
is_keepalive = true;
} else if (! strncasecmp (ptr, "Close", 5)){
is_close = true;
}
}
if ((props->in_allow_keepalive) &&
((is_keepalive) ||
((conn->header.version == http_version_11) && (! is_close))))
{
cherokee_buffer_add_str (buf, "Connection: Keep-Alive" CRLF);
hdl->pconn->keepalive_in = true;
ret = cherokee_header_get_known (&conn->header, header_keepalive, &ptr, &ptr_len);
if (ret == ret_ok) {
cherokee_buffer_add_str (buf, "Keep-Alive: ");
cherokee_buffer_add (buf, ptr, ptr_len);
cherokee_buffer_add_str (buf, CRLF);
}
} else {
cherokee_buffer_add_str (buf, "Connection: Close" CRLF);
hdl->pconn->keepalive_in = false;
}
/* Add header "Content-Length:" */
if (conn->post.has_info) {
cherokee_buffer_add_str (buf, "Content-Length: ");
cherokee_buffer_add_ullong10 (buf, conn->post.len);
cherokee_buffer_add_str (buf, CRLF);
}
/* Headers
*/
str = strchr (conn->incoming_header.buf, CHR_CR);
if (str == NULL)
goto error;
while ((*str == CHR_CR) || (*str == CHR_LF))
str++;
/* Add the client headers */
cherokee_header_get_length (&conn->header, &header_len);
begin = (char *)str;
header_end = conn->incoming_header.buf + (header_len - 2);
while (begin < header_end) {
char chr_end;
/* Where the line ends */
end = cherokee_header_get_next_line (begin);
if (end == NULL)
break;
chr_end = *end;
*end = '\0';
/* Check the header entry */
if ((! strncasecmp (begin, "Host:", 5)) ||
(! strncasecmp (begin, "Expect:", 7)) ||
(! strncasecmp (begin, "Connection:", 11)) ||
(! strncasecmp (begin, "Keep-Alive:", 11)) ||
(! strncasecmp (begin, "Content-Length:", 15)) ||
(! strncasecmp (begin, "Transfer-Encoding:", 18)))
{
goto next;
}
else if (! strncasecmp (begin, "X-Forwarded-For:", 16))
{
XFF = begin + 16;
while ((*XFF == ' ') && (XFF < end))
XFF++;
XFF_len = end - XFF;
goto next;
}
else if (! strncasecmp (begin, "X-Forwarded-Host:", 17))
{
XFH = true;
}
else if (! strncasecmp (begin, "X-Real-IP:", 10))
{
x_real_ip = true;
}
else {
colon = strchr (begin, ':');
if (unlikely (colon == NULL)) {
return ret_error;
}
*colon = '\0';
ret = cherokee_avl_get_ptr (&props->in_headers_hide, begin, NULL);
*colon = ':';
if (ret == ret_ok)
goto next;
}
cherokee_buffer_add (buf, begin, end-begin);
cherokee_buffer_add_str (buf, CRLF);
/* Prepare next iteration */
next:
*end = chr_end;
while ((*end == CHR_CR) || (*end == CHR_LF))
end++;
begin = end;
}
/* X-Forwarded-For */
cherokee_buffer_ensure_size (tmp, CHE_INET_ADDRSTRLEN+1);
cherokee_socket_ntop (&conn->socket, tmp->buf, tmp->size-1);
cherokee_buffer_add_str (buf, "X-Forwarded-For: ");
if (XFF != NULL) {
cherokee_buffer_add (buf, XFF, XFF_len);
cherokee_buffer_add_str (buf, ", ");
}
cherokee_buffer_add (buf, tmp->buf, strlen(tmp->buf));
cherokee_buffer_add_str (buf, CRLF);
/* X-Real-IP */
if (! x_real_ip) {
cherokee_buffer_add_str (buf, "X-Real-IP: ");
cherokee_buffer_add (buf, tmp->buf, strlen(tmp->buf));
cherokee_buffer_add_str (buf, CRLF);
}
/* X-Forwarded-Host */
if ((XFH == false) &&
(! cherokee_buffer_is_empty (&conn->host)))
{
cherokee_buffer_add_str (buf, "X-Forwarded-Host: ");
cherokee_buffer_add_buffer (buf, &conn->host);
if (! http_port_is_standard (conn->bind->port, conn->socket.is_tls))
{
cherokee_buffer_add_char (buf, ':');
cherokee_buffer_add_ulong10 (buf, conn->bind->port);
}
cherokee_buffer_add_str (buf, CRLF);
}
/* X-Forwarded-SSL */
cherokee_buffer_add_str (buf, "X-Forwarded-SSL: ");
if(conn->socket.is_tls) {
cherokee_buffer_add_str (buf, "on");
} else {
cherokee_buffer_add_str (buf, "off");
}
cherokee_buffer_add_str (buf, CRLF);
/* Additional headers */
list_for_each (i, &props->in_headers_add) {
add_header (buf, &HEADER_ADD(i)->key, &HEADER_ADD(i)->val);
}
/* End of Header */
cherokee_buffer_add_str (buf, CRLF);
return ret_ok;
error:
TRACE (ENTRIES",post", "Couldn't build request %s", "\n");
cherokee_buffer_clean (buf);
return ret_error;
}
static ret_t
do_connect (cherokee_handler_proxy_t *hdl)
{
ret_t ret;
ret = cherokee_socket_connect (&hdl->pconn->socket);
switch (ret) {
case ret_ok:
break;
case ret_deny:
case ret_error:
return ret;
case ret_eagain:
ret = cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl),
HANDLER_CONN(hdl),
hdl->pconn->socket.socket,
FDPOLL_MODE_WRITE, false);
if (ret != ret_ok) {
return ret_deny;
}
return ret_eagain;
default:
RET_UNKNOWN(ret);
return ret_error;
}
return ret_ok;
}
static ret_t
send_post_reset (cherokee_handler_proxy_t *hdl)
{
cherokee_connection_t *conn = HANDLER_CONN(hdl);
if (hdl->pconn == NULL) {
TRACE (ENTRIES, "Post reset: %s\n", "no pconn");
return ret_ok;
}
if (conn->post.send.read == 0) {
TRACE (ENTRIES, "Did not read POST. No need to %s\n", "reset");
return ret_ok;
}
if (! hdl->pconn->post.do_buf_sent) {
/* The post information is not buffered. Thus, there
* is no way to resend it to the back-end server.
*/
TRACE (ENTRIES, "POST could not be recovered: %s\n", "fail");
return ret_not_found;
}
/* The post has been buffered. It can be resend.
*/
TRACE (ENTRIES, "Recovering POST from buffer: %s\n", "ok");
hdl->pconn->post.sent = 0;
conn->post.send.read = 0;
return ret_ok;
}
static ret_t
send_post (cherokee_handler_proxy_t *hdl)
{
ret_t ret;
size_t written = 0;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_buffer_t *buffer = &hdl->pconn->post.buf_temp;
/* Send: buffered
*/
if (hdl->resending_post) {
TRACE (ENTRIES, "Sending post, type Resending. To be sent %d\n", buffer->len);
ret = cherokee_socket_bufwrite (&hdl->pconn->socket, buffer, &written);
switch (ret) {
case ret_ok:
break;
case ret_eagain:
if (written > 0) {
break;
}
TRACE (ENTRIES, "Post write: EAGAIN, wrote nothing of %d\n", buffer->len);
ret = cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl), conn,
hdl->pconn->socket.socket,
FDPOLL_MODE_WRITE, false);
if (ret != ret_ok) {
hdl->pconn->keepalive_in = false;
conn->error_code = http_bad_gateway;
return ret_error;
}
return ret_eagain;
default:
return ret_error;
}
conn->post.send.read += written;
cherokee_buffer_move_to_begin (buffer, written);
TRACE (ENTRIES, "sent=%d, remaining=%d\n", written, buffer->len);
if (! cherokee_buffer_is_empty (buffer)) {
return ret_eagain;
}
hdl->resending_post = false;
hdl->pconn->post.do_buf_sent = false;
TRACE (ENTRIES, "Promoted POST to non-buffered mode, after %s\n", "re-send");
}
else if ((hdl->pconn->post.do_buf_sent) &&
(hdl->pconn->post.sent < buffer->len))
{
TRACE (ENTRIES, "Sending post, type Buffered (has %d, to be sent %d)\n",
buffer->len, buffer->len - hdl->pconn->post.sent);
ret = cherokee_socket_write (&hdl->pconn->socket,
buffer->buf + hdl->pconn->post.sent,
buffer->len - hdl->pconn->post.sent,
&written);
switch (ret) {
case ret_ok:
break;
case ret_eagain:
if (written > 0) {
break;
}
TRACE (ENTRIES, "Post write: EAGAIN, wrote nothing of %d\n", buffer->len);
ret = cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl), conn,
hdl->pconn->socket.socket,
FDPOLL_MODE_WRITE, false);
if (ret != ret_ok) {
hdl->pconn->keepalive_in = false;
conn->error_code = http_bad_gateway;
return ret_error;
}
return ret_eagain;
default:
return ret_error;
}
hdl->pconn->post.sent += written;
TRACE (ENTRIES, "Wrote POST: %d bytes, total sent %d\n", written, hdl->pconn->post.sent);
/* fall down: read*/
}
/* Send: Straight
*/
else if ((! hdl->pconn->post.do_buf_sent) &&
(! cherokee_buffer_is_empty (buffer)))
{
TRACE (ENTRIES, "Sending post, type Straight. Buffer has %d\n", buffer->len);
ret = cherokee_socket_bufwrite (&hdl->pconn->socket, buffer, &written);
switch (ret) {
case ret_ok:
break;
case ret_eagain:
if (written > 0) {
break;
}
TRACE (ENTRIES, "Post write: EAGAIN, wrote nothing of %d\n", buffer->len);
ret = cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl), conn,
hdl->pconn->socket.socket,
FDPOLL_MODE_WRITE, false);
if (ret != ret_ok) {
hdl->pconn->keepalive_in = false;
conn->error_code = http_bad_gateway;
return ret_error;
}
return ret_eagain;
default:
return ret_error;
}
cherokee_buffer_move_to_begin (buffer, written);
TRACE (ENTRIES, "sent=%d, remaining=%d\n", written, buffer->len);
/* fall down: read*/
}
/* Has it finished?
*/
if (cherokee_post_read_finished (&conn->post)) {
TRACE(ENTRIES, "POST has been read completely: %s\n", "ok");
if (hdl->pconn->post.do_buf_sent) {
if (hdl->pconn->post.sent >= buffer->len) {
hdl->got_all = true;
return ret_ok;
}
} else {
if (cherokee_buffer_is_empty (buffer)) {
hdl->got_all = true;
return ret_ok;
}
}
return ret_eagain;
}
/* Still have data to be sent
*/
if (hdl->pconn->post.do_buf_sent) {
if (hdl->pconn->post.sent < buffer->len)
return ret_eagain;
} else {
if (! cherokee_buffer_is_empty (buffer))
return ret_eagain;
}
/* Read from the client
*/
ret = cherokee_post_read (&conn->post, &conn->socket, buffer);
switch (ret) {
case ret_ok:
TRACE (ENTRIES, "Post has %d bytes - after the addition\n", buffer->len);
cherokee_connection_update_timeout (conn);
break;
case ret_eagain:
TRACE (ENTRIES, "Post read: EAGAIN, buffer has %d bytes\n", buffer->len);
ret = cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl),
HANDLER_CONN(hdl),
conn->socket.socket,
FDPOLL_MODE_READ, false);
if (ret != ret_ok) {
hdl->pconn->keepalive_in = false;
conn->error_code = http_bad_gateway;
return ret_error;
}
return ret_eagain;
default:
return ret;
}
/* Buffered -> Non-buffered promotion
*/
if ((hdl->pconn->post.do_buf_sent) &&
(buffer->len >= DEFAULT_READ_SIZE * 3))
{
hdl->pconn->post.do_buf_sent = false;
cherokee_buffer_move_to_begin (buffer, hdl->pconn->post.sent);
TRACE (ENTRIES, "Promoted POST to non-buffered mode. Length afterwards: %d\n", buffer->len);
}
return ret_eagain;
}
ret_t
cherokee_handler_proxy_init (cherokee_handler_proxy_t *hdl)
{
ret_t ret;
cherokee_handler_proxy_poll_t *poll;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_handler_proxy_props_t *props = HDL_PROXY_PROPS(hdl);
switch (hdl->init_phase) {
case proxy_init_start:
/* Send the Post if needed
*/
if (conn->post.has_info) {
// TODO: Is it necessary?
}
hdl->init_phase = proxy_init_get_conn;
TRACE(ENTRIES, "Entering init '%s'\n", "get conn");
case proxy_init_get_conn:
/* Check with the load balancer
*/
if (hdl->src_ref == NULL) {
ret = cherokee_balancer_dispatch (props->balancer, conn, &hdl->src_ref);
if (ret != ret_ok) {
BIT_UNSET (HANDLER(hdl)->support, hsupport_error);
conn->error_code = http_service_unavailable;
return ret_error;
}
/* Sanity check */
if (unlikely (hdl->src_ref->port == -1)) {
hdl->src_ref->port = 80;
}
}
/* Get the connection poll
*/
ret = cherokee_handler_proxy_hosts_get (&props->hosts, hdl->src_ref,
&poll, props->reuse_max);
if (unlikely (ret != ret_ok)) {
conn->error_code = http_service_unavailable;
return ret_error;
}
/* Get a connection
*/
ret = cherokee_handler_proxy_poll_get (poll, &hdl->pconn, hdl->src_ref);
if (unlikely (ret != ret_ok)) {
conn->error_code = http_service_unavailable;
return ret_error;
}
reconnect:
hdl->init_phase = proxy_init_preconnect;
TRACE(ENTRIES, "Entering phase '%s'\n", "preconnect");
case proxy_init_preconnect:
/* Configure if respinned
*/
if (hdl->respinned) {
TRACE(ENTRIES, "Re-init %s connection\n", "respinned");
cherokee_socket_close (&hdl->pconn->socket);
cherokee_socket_clean (&hdl->pconn->socket);
if (conn->post.has_info) {
ret = send_post_reset (hdl);
if (ret != ret_ok) {
cherokee_balancer_report_fail (props->balancer, conn, hdl->src_ref);
hdl->pconn->keepalive_in = false;
conn->error_code = http_bad_gateway;
return ret_error;
}
if (! cherokee_buffer_is_empty (&hdl->pconn->post.buf_temp)) {
hdl->resending_post = true;
}
}
}
if (! cherokee_socket_configured (&hdl->pconn->socket))
{
ret = cherokee_proxy_util_init_socket (&hdl->pconn->socket, hdl->src_ref);
if (ret != ret_ok) {
hdl->pconn->keepalive_in = false;
conn->error_code = http_bad_gateway;
return ret_error;
}
}
hdl->init_phase = proxy_init_connect;
TRACE(ENTRIES, "Entering phase 'connect': pconn=%p\n", hdl->pconn);
case proxy_init_connect:
/* Connect to the target host
*/
if (! cherokee_socket_is_connected (&hdl->pconn->socket)) {
ret = do_connect (hdl);
switch (ret) {
case ret_ok:
break;
case ret_error:
conn->error_code = http_bad_gateway;
hdl->pconn->keepalive_in = false;
return ret_error;
case ret_eagain:
return ret_eagain;
case ret_deny:
if (hdl->respinned) {
cherokee_balancer_report_fail (props->balancer, conn, hdl->src_ref);
conn->error_code = http_bad_gateway;
hdl->pconn->keepalive_in = false;
return ret_error;
}
if (hdl->src_ref->type == source_interpreter) {
cherokee_logger_writer_t *error_writer = NULL;
/* Spawn a new process
*/
ret = cherokee_virtual_server_get_error_log (CONN_VSRV(conn),
&error_writer);
if (ret != ret_ok) {
return ret_error;
}
ret = cherokee_source_interpreter_spawn (SOURCE_INT(hdl->src_ref),
error_writer);
switch (ret) {
case ret_ok:
break;
case ret_eagain:
return ret_eagain;
default: