This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
rproxy.c
1644 lines (1307 loc) · 50.8 KB
/
rproxy.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 [2012] [Mandiant, inc]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef USE_MALLOPT
#include <malloc.h>
#endif
#ifndef NO_RLIMIT
#include <sys/resource.h>
#endif
#include "rproxy.h"
static void rproxy_process_pending(int, short, void *);
int
append_ssl_x_headers(headers_cfg_t * headers_cfg, evhtp_request_t * upstream_req) {
evhtp_headers_t * headers;
x509_ext_cfg_t * x509_ext_cfg;
evhtp_ssl_t * ssl;
if (!headers_cfg || !upstream_req) {
return -1;
}
if (!(ssl = upstream_req->conn->ssl)) {
return 0;
}
if (!(headers = upstream_req->headers_in)) {
return -1;
}
/* remove all headers the client might have sent, if any of these
* configurations are set to false, this allows the client to send
* their own versions. This is potentially dangerous, so we remove them
*/
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Subject"));
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Issuer"));
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Notbefore"));
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Notafter"));
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Serial"));
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Cipher"));
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-SSL-Certificate"));
if (headers_cfg->x_ssl_subject == true) {
unsigned char * subj_str;
if ((subj_str = ssl_subject_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Subject", subj_str, 0, 1));
free(subj_str);
}
}
if (headers_cfg->x_ssl_issuer == true) {
unsigned char * issr_str;
if ((issr_str = ssl_issuer_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Issuer", issr_str, 0, 1));
free(issr_str);
}
}
if (headers_cfg->x_ssl_notbefore == true) {
unsigned char * nbf_str;
if ((nbf_str = ssl_notbefore_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Notbefore", nbf_str, 0, 1));
free(nbf_str);
}
}
if (headers_cfg->x_ssl_notafter == true) {
unsigned char * naf_str;
if ((naf_str = ssl_notafter_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Notafter", naf_str, 0, 1));
free(naf_str);
}
}
if (headers_cfg->x_ssl_serial == true) {
unsigned char * ser_str;
if ((ser_str = ssl_serial_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Serial", ser_str, 0, 1));
free(ser_str);
}
}
if (headers_cfg->x_ssl_cipher == true) {
unsigned char * cip_str;
if ((cip_str = ssl_cipher_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Cipher", cip_str, 0, 1));
free(cip_str);
}
}
if (headers_cfg->x_ssl_sha1 == true) {
unsigned char * sha1_str;
if ((sha1_str = ssl_sha1_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Sha1", sha1_str, 0, 1));
free(sha1_str);
}
}
if (headers_cfg->x_ssl_certificate == true) {
unsigned char * cert_str;
if ((cert_str = ssl_cert_tostr(ssl))) {
evhtp_headers_add_header(headers,
evhtp_header_new("X-SSL-Certificate", cert_str, 0, 1));
free(cert_str);
}
}
{
lztq_elem * x509_elem;
lztq_elem * x509_save;
for (x509_elem = lztq_first(headers_cfg->x509_exts); x509_elem; x509_elem = x509_save) {
unsigned char * ext_str;
x509_ext_cfg = lztq_elem_data(x509_elem);
assert(x509_ext_cfg != NULL);
if ((ext_str = ssl_x509_ext_tostr(ssl, x509_ext_cfg->oid))) {
evhtp_headers_add_header(headers,
evhtp_header_new(x509_ext_cfg->name, ext_str, 0, 1));
free(ext_str);
}
x509_save = lztq_next(x509_elem);
}
}
return 0;
} /* append_ssl_x_headers */
int
append_x_headers(headers_cfg_t * headers_cfg, evhtp_request_t * upstream_req) {
evhtp_headers_t * headers;
char tmp1[1024];
char tmp2[1024];
if (!headers_cfg || !upstream_req) {
return -1;
}
if (!(headers = upstream_req->headers_in)) {
return -1;
}
if (headers_cfg->x_forwarded_for == true) {
struct sockaddr * sa;
void * src;
char * fmt;
unsigned short port;
int sres;
src = NULL;
sa = upstream_req->conn->saddr;
if (sa->sa_family == AF_INET) {
src = &(((struct sockaddr_in *)sa)->sin_addr);
port = ntohs(((struct sockaddr_in *)sa)->sin_port);
fmt = "%s:%hu";
} else if (sa->sa_family == AF_INET6) {
src = &(((struct sockaddr_in6 *)sa)->sin6_addr);
port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
fmt = "[%s]:%hu";
}
if (!src || !evutil_inet_ntop(sa->sa_family, src, tmp1, sizeof(tmp1))) {
return -1;
}
sres = snprintf(tmp2, sizeof(tmp2), fmt, tmp1, port);
if (sres < 0 || sres >= sizeof(tmp2)) {
return -1;
}
evhtp_kv_rm_and_free(headers, evhtp_kvs_find_kv(headers, "X-Forwarded-For"));
evhtp_headers_add_header(headers,
evhtp_header_new("X-Forwarded-For", tmp2, 0, 1));
}
if (upstream_req->conn->ssl) {
if (append_ssl_x_headers(headers_cfg, upstream_req) < 0) {
return -1;
}
}
return 0;
} /* append_x_headers */
evhtp_res
upstream_on_write(evhtp_connection_t * conn, void * args) {
request_t * ds_req;
ds_req = args;
assert(ds_req != NULL);
if (ds_req->hit_upstream_highwm) {
/* upstream hit a high watermark for its write buffer, but now the
* buffer has been fully flushed, so we can enable the read side of the
* downstream again.
*/
bufferevent_enable(ds_req->downstream_conn->connection, EV_READ);
ds_req->hit_upstream_highwm = 0;
}
return EVHTP_RES_OK;
}
/**
* @brief data was read from an upstream connection, pass it down to the
* downstream.
*
* @param bev
* @param arg
*/
void
passthrough_readcb(evbev_t * bev, void * arg) {
request_t * request;
request = arg;
assert(request != NULL);
/* write the data read from the upstream bufferevent to the downstream
* bufferevent.
*/
bufferevent_write_buffer(request->downstream_bev, bufferevent_get_input(bev));
}
void
passthrough_writecb(evbev_t * bev, void * arg) {
return;
}
void
passthrough_eventcb(evbev_t * bev, short events, void * arg) {
int res;
request_t * request;
downstream_c_t * ds_conn;
request = arg;
assert(request != NULL);
ds_conn = request->downstream_conn;
bufferevent_free(request->upstream_bev);
downstream_connection_set_down(ds_conn);
request_free(request);
ds_conn->request = NULL;
}
evhtp_res
send_upstream_headers(evhtp_request_t * upstream_req, evhtp_headers_t * hdrs, void * arg) {
/* evhtp has parsed the inital request (request + headers). From this
* request generate a proper request which can be pipelined to the
* downstream connection
*/
request_t * request;
rproxy_t * rproxy;
evhtp_header_t * connection_hdr;
evbuf_t * buf;
rule_cfg_t * rule_cfg;
rule_t * rule;
headers_cfg_t * headers;
char * query_args;
evhtp_res res = EVHTP_RES_OK;
request = arg;
assert(request != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
rule = request->rule;
assert(rule != NULL);
rule_cfg = rule->config;
assert(rule_cfg != NULL);
if (request->pending == 1) {
abort();
}
if (!request->downstream_conn->connection) {
logger_log_request_error(rule->err_log, request, "%s(): conn == NULL", __FUNCTION__);
return EVHTP_RES_ERROR;
}
/* default the x-header configuration to the rule, but if not found,
* fallback to the vhost parent's config.
*/
headers = rule->config->headers;
if (!headers) {
headers = rule->parent_vhost->config->headers;
}
/* Add X-Headers to the request if applicable */
if (headers) {
if (append_x_headers(headers, upstream_req) < 0) {
logger_log_request_error(rule->err_log, request,
"%s(): append_x_headers < 0", __FUNCTION__);
return EVHTP_RES_ERROR;
}
}
/* checks to determine of the upstream request is set to a
* non keep-alive state, and if it is it magically converts
* it to a keep-alive request to send to the downstream so
* the connection remains open
*/
switch (upstream_req->proto) {
case EVHTP_PROTO_10:
if (upstream_req->keepalive > 0) {
break;
}
/* upstream request is HTTP/1.0 with no keep-alive header,
* to keep our connection alive to the downstream we insert
* a Connection: Keep-Alive header
*/
if ((connection_hdr = evhtp_headers_find_header(hdrs, "Connection"))) {
evhtp_header_rm_and_free(hdrs, connection_hdr);
}
connection_hdr = evhtp_header_new("Connection", "Keep-Alive", 0, 0);
if (connection_hdr == NULL) {
logger_log_request_error(rule->err_log, request,
"%s(): couldn't create new header %s",
__FUNCTION__, strerror(errno));
exit(EXIT_FAILURE);
}
evhtp_headers_add_header(hdrs, connection_hdr);
break;
case EVHTP_PROTO_11:
if (upstream_req->keepalive > 0) {
break;
}
/* upstream request is HTTP/1.1 but the Connection: close header was
* present, so just remove the header to keep downstream connection
* alive.
*/
const char * v = evhtp_kv_find(hdrs, "Connection");
connection_hdr = evhtp_headers_find_header(hdrs, "Connection");
if (!strcasecmp(v, "close")) {
upstream_req->keepalive = 0;
}
evhtp_header_rm_and_free(hdrs, connection_hdr);
break;
default:
logger_log_request_error(rule->err_log, request,
"%s(): unknown proto %d",
__FUNCTION__, upstream_req->proto);
return EVHTP_RES_ERROR;
} /* switch */
buf = util_request_to_evbuffer(upstream_req);
assert(buf != NULL);
bufferevent_write_buffer(request->downstream_bev, buf);
evbuffer_free(buf);
/* TODO: if the upstream's input bev still has data, write it here... */
if (rule_cfg->passthrough == true) {
/* rule configured to be passthrough, so we take ownership of the
* bufferevent and ignore any state based processing of the request.
* This allows for non-http specific protocols to flow such as
* websocket.
*/
evbev_t * bev;
bev = evhtp_connection_take_ownership(evhtp_request_get_connection(upstream_req));
assert(bev != NULL);
/* our on_headers_complete hook will call this function before the last
* \n in the \r\n\r\n part, so we have to remove this.
*/
evbuffer_drain(bufferevent_get_input(bev), 1);
bufferevent_enable(bev, EV_READ | EV_WRITE);
bufferevent_setcb(bev,
passthrough_readcb,
passthrough_writecb,
passthrough_eventcb, request);
request->upstream_request = NULL;
return EVHTP_RES_USER;
}
return res;
} /* send_upstream_headers */
evhtp_res
send_upstream_body(evhtp_request_t * upstream_req, evbuf_t * buf, void * arg) {
/* stream upstream request body to the downstream server */
request_t * request;
rule_t * rule;
rproxy_t * rproxy;
downstream_c_t * ds_conn;
request = arg;
assert(request != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
rule = request->rule;
assert(rule != NULL);
if (!upstream_req || !buf) {
logger_log_request_error(rule->err_log, request,
"%s(): up_req = %p, buf = %p", __FUNCTION__, upstream_req, buf);
return EVHTP_RES_FATAL;
}
if (!(ds_conn = request->downstream_conn)) {
logger_log_request_error(rule->err_log, request,
"%s(): downstream_conn == NULL", __FUNCTION__);
return EVHTP_RES_FATAL;
}
if (!ds_conn->connection || request->error > 0) {
logger_log_request_error(rule->err_log, request,
"%s(): conn = %p, req error = %d",
__FUNCTION__, ds_conn->connection, request->error);
evbuffer_drain(buf, -1);
return EVHTP_RES_ERROR;
}
bufferevent_write_buffer(ds_conn->connection, buf);
if (ds_conn->parent->config->high_watermark > 0) {
if (evbuffer_get_length(bufferevent_get_output(ds_conn->connection)) >= ds_conn->parent->config->high_watermark) {
request->hit_highwm = 1;
#ifdef RPROXY_DEBUG
printf("Hit high-watermark %zu: %zu in output\n",
ds_conn->parent->config->high_watermark,
evbuffer_get_length(bufferevent_get_output(ds_conn->connection)));
#endif
evhtp_request_pause(upstream_req);
return EVHTP_RES_PAUSE;
}
}
return EVHTP_RES_OK;
} /* send_upstream_body */
evhtp_res
send_upstream_new_chunk(evhtp_request_t * upstream_req, uint64_t len, void * arg) {
request_t * request;
rproxy_t * rproxy;
downstream_c_t * ds_conn;
rule_t * rule;
request = arg;
assert(request != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
rule = request->rule;
assert(rproxy != NULL);
if (!upstream_req) {
logger_log(rproxy->err_log, lzlog_err, "%s(): !upstream_req", __FUNCTION__);
return EVHTP_RES_FATAL;
}
if (!(ds_conn = request->downstream_conn)) {
logger_log_request_error(rule->err_log, request,
"%s(): downstream_conn == NULL", __FUNCTION__);
return EVHTP_RES_FATAL;
}
if (!ds_conn->connection || request->error > 0) {
logger_log_request_error(rule->err_log, request,
"%s(): conn = %p, err = %d",
__FUNCTION__, ds_conn->connection, request->error);
return EVHTP_RES_ERROR;
}
evbuffer_add_printf(bufferevent_get_output(ds_conn->connection),
"%x\r\n", (unsigned int)len);
return EVHTP_RES_OK;
} /* send_upstream_new_chunk */
evhtp_res
send_upstream_chunk_done(evhtp_request_t * upstream_req, void * arg) {
request_t * request;
rproxy_t * rproxy;
downstream_c_t * ds_conn;
rule_t * rule;
request = arg;
assert(request != NULL);
rule = request->rule;
assert(rule != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
ds_conn = request->downstream_conn;
assert(ds_conn != NULL);
if (!ds_conn->connection || request->error > 0) {
logger_log_request_error(rule->err_log, request,
"%s(): conn = %p, err = %d",
__FUNCTION__, ds_conn->connection, request->error);
return EVHTP_RES_ERROR;
}
bufferevent_write(ds_conn->connection, "\r\n", 2);
return EVHTP_RES_OK;
}
evhtp_res
send_upstream_chunks_done(evhtp_request_t * upstream_req, void * arg) {
request_t * request;
rproxy_t * rproxy;
rule_t * rule;
downstream_c_t * ds_conn;
request = arg;
assert(request != NULL);
rule = request->rule;
assert(rule != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
ds_conn = request->downstream_conn;
assert(ds_conn != NULL);
if (!ds_conn->connection || request->error > 0) {
logger_log_request_error(rule->err_log, request,
"%s(): dsconn = %p, err = %d", __FUNCTION__,
ds_conn->connection, request->error);
return EVHTP_RES_ERROR;
}
bufferevent_write(ds_conn->connection, "0\r\n\r\n", 5);
return EVHTP_RES_OK;
}
evhtp_res
upstream_fini(evhtp_request_t * upstream_req, void * arg) {
request_t * request;
rproxy_t * rproxy;
downstream_c_t * ds_conn;
downstream_t * downstream;
rule_t * rule;
int res;
request = arg;
assert(request != NULL);
rule = request->rule;
assert(rule != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
/* if this downstream request is still pending, remove it from the queue */
if (request->pending) {
TAILQ_REMOVE(&rproxy->pending, request, next);
rproxy->n_pending -= 1;
request_free(request);
return EVHTP_RES_OK;
}
ds_conn = request->downstream_conn;
assert(ds_conn != NULL);
if (REQUEST_HAS_ERROR(request)) {
logger_log_request_error(rule->err_log, request,
"%s(): we should never get here!", __FUNCTION__);
downstream_connection_set_down(ds_conn);
} else {
downstream_connection_set_idle(ds_conn);
}
request_free(request);
return EVHTP_RES_OK;
} /* upstream_fini */
/**
* @brief called when an upstream socket encounters an error.
*
* @param upstream_req
* @param arg
*/
static void
upstream_error(evhtp_request_t * upstream_req, short events, void * arg) {
request_t * request;
rproxy_t * rproxy;
downstream_c_t * ds_conn;
rule_t * rule;
request = arg;
assert(request != NULL);
rule = request->rule;
assert(rule != NULL);
rproxy = request->rproxy;
assert(rproxy != NULL);
evhtp_unset_all_hooks(&upstream_req->hooks);
logger_log(rule->err_log, lzlog_warn, "%s(): client aborted, err = %x",
__FUNCTION__, events);
if (request->pending) {
/* upstream encountered socket error while still in a pending state */
assert(request->downstream_conn == NULL);
TAILQ_REMOVE(&rproxy->pending, request, next);
rproxy->n_pending -= 1;
request_free(request);
return;
}
request->upstream_err = 1;
ds_conn = request->downstream_conn;
assert(ds_conn != NULL);
if (!request->reading) {
/* since we are not currently dealing with data being parsed by
* downstream_connection_readcb, we must do all the resource cleanup
* here.
*/
if (request->done) {
/* the request was completely finished, so we can safely set the
* downstream as idle.
*/
logger_log_request_error(rule->err_log, request,
"%s(): req completed, client aborted", __FUNCTION__);
downstream_connection_set_idle(ds_conn);
} else {
/* request never completed, set the connection to down */
logger_log_request_error(rule->err_log, request,
"req incomplete, client aborted", __FUNCTION__);
downstream_connection_set_down(ds_conn);
}
if (ds_conn->request == request) {
ds_conn->request = NULL;
}
request_free(request);
}
} /* upstream_error */
/**
* @brief allocates a new downstream_t, and appends it to the
* rproxy->downstreams list. This is callback for the
* lztq_for_each function from rproxy_thread_init().
*
* @param elem
* @param arg
*
* @return
*/
static int
add_downstream(lztq_elem * elem, void * arg) {
rproxy_t * rproxy = arg;
downstream_cfg_t * ds_cfg = lztq_elem_data(elem);
downstream_t * downstream;
lztq_elem * nelem;
assert(rproxy != NULL);
assert(ds_cfg != NULL);
downstream = downstream_new(rproxy, ds_cfg);
assert(downstream != NULL);
nelem = lztq_append(rproxy->downstreams, downstream,
sizeof(downstream), downstream_free);
assert(nelem != NULL);
return 0;
}
/**
* @brief creates n connections to the server information contained in a
* downstream_t instance. This is the callback for the lztq_for_each
* function from rproxy_thread_init() (after the downstream list has been
* created.
*
* @param elem
* @param arg
*
* @return
*/
static int
start_downstream(lztq_elem * elem, void * arg) {
evbase_t * evbase = arg;
downstream_t * downstream = lztq_elem_data(elem);
assert(evbase != NULL);
assert(downstream != NULL);
return downstream_connection_init(evbase, downstream);
}
/**
* @brief match up names in the list of downstream_cfg_t's in rule_cfg->downstreams
* to the downstream_t's in the rproxy->downstreams list. If found,
* create a rule_t and appends it to the rproxy->rules list.
*
* @param elem a lztq elem with the type of vhost_cfg_t *
* @param arg the vhost_t *
*
* @return
*/
static int
map_vhost_rules_to_downstreams(lztq_elem * elem, void * arg) {
vhost_t * vhost = arg;
rproxy_t * rproxy;
rule_cfg_t * rule_cfg;
lztq_elem * name_elem;
lztq_elem * name_elem_temp;
rule_t * rule;
vhost = arg;
assert(arg != NULL);
rproxy = vhost->rproxy;
assert(rproxy != NULL);
rule_cfg = lztq_elem_data(elem);
assert(rule_cfg != NULL);
rule = calloc(sizeof(rule_t), 1);
assert(rule != NULL);
rule->rproxy = rproxy;
rule->config = rule_cfg;
rule->parent_vhost = vhost;
/*
* if a rule specific logging is found then all is good to go. otherwise
* if a vhost specific logging is found then set it to rule. otherwise
* if a server specific logging is found, set both vhost and rule to this.
*/
if (rule_cfg->req_log) {
rule->req_log = logger_init(rule_cfg->req_log, 0);
} else if (vhost->req_log) {
rule->req_log = vhost->req_log;
} else {
rule->req_log = rproxy->req_log;
vhost->req_log = rproxy->req_log;
}
/*
* the same logic applies as above for error logging.
*/
if (rule_cfg->err_log) {
rule->err_log = logger_init(rule_cfg->err_log, 0);
} else if (vhost->err_log) {
rule->err_log = vhost->err_log;
} else {
rule->err_log = rproxy->err_log;
vhost->err_log = rproxy->err_log;
}
rule->downstreams = lztq_new();
assert(rule->downstreams != NULL);
/* for each string in the rule_cfg's downstreams section, find the matching
* downstream_t and append it.
*/
for (name_elem = lztq_first(rule_cfg->downstreams); name_elem != NULL; name_elem = name_elem_temp) {
const char * ds_name;
downstream_t * ds;
lztq_elem * nelem;
ds_name = lztq_elem_data(name_elem);
assert(ds_name != NULL);
if (!(ds = downstream_find_by_name(rproxy->downstreams, ds_name))) {
/* could not find a downstream_t which has this name! */
fprintf(stderr, "Could not find downstream named '%s!\n", ds_name);
exit(EXIT_FAILURE);
}
nelem = lztq_append(rule->downstreams, ds, sizeof(ds), NULL);
assert(nelem != NULL);
name_elem_temp = lztq_next(name_elem);
}
/* upstream_request_start is passed only the rule_cfg as an argument. this
* function will call file_rule_from_cfg to get the actual rule from the
* global rproxy->rules list. Since we compare pointers, it is safe to keep
* this as one single list.
*/
lztq_append(rproxy->rules, rule, sizeof(rule), NULL);
return 0;
} /* map_vhost_rules_to_downstreams */
static rule_t *
find_rule_from_cfg(rule_cfg_t * rule_cfg, lztq * rules) {
lztq_elem * rule_elem;
lztq_elem * rule_elem_temp;
for (rule_elem = lztq_first(rules); rule_elem != NULL; rule_elem = rule_elem_temp) {
rule_t * rule = lztq_elem_data(rule_elem);
if (rule->config == rule_cfg) {
return rule;
}
rule_elem_temp = lztq_next(rule_elem);
}
return NULL;
}
/**
* @brief Called when an upstream request is in the pending queue and the
* configured timeout has been reached.
*
* @param fd
* @param what
* @param arg
*/
static void
downstream_pending_timeout(evutil_socket_t fd, short what, void * arg) {
request_t * ds_req;
rproxy_t * rproxy;
evhtp_request_t * up_req;
rule_t * rule;
rule_cfg_t * rule_cfg;
ds_req = arg;
assert(ds_req != NULL);
rule = ds_req->rule;
assert(rule != NULL);
rule_cfg = rule->config;
assert(rule_cfg != NULL);
rproxy = ds_req->rproxy;
assert(rproxy != NULL);
if (rule_cfg->passthrough == true) {
/*
* since we're in passthrough mode, we don't need to free any evhtp
* resources; so we just close the upstream socket and free the request
*/
if (ds_req->pending) {
TAILQ_REMOVE(&rproxy->pending, ds_req, next);
rproxy->n_pending -= 1;
}
bufferevent_free(ds_req->upstream_bev);
logger_log(rule->err_log, lzlog_notice,
"%s(): pending timeout hit for upstream (passthrough)", __FUNCTION__);
return request_free(ds_req);
}
up_req = ds_req->upstream_request;
assert(up_req != NULL);
/* unset all hooks except for the fini, evhtp_send_reply() will call the
* fini function after the 503 message has been delivered */
evhtp_unset_hook(&up_req->hooks, evhtp_hook_on_headers);
evhtp_unset_hook(&up_req->hooks, evhtp_hook_on_new_chunk);
evhtp_unset_hook(&up_req->hooks, evhtp_hook_on_chunk_complete);
evhtp_unset_hook(&up_req->hooks, evhtp_hook_on_chunks_complete);
evhtp_unset_hook(&up_req->hooks, evhtp_hook_on_read);
evhtp_unset_hook(&up_req->hooks, evhtp_hook_on_error);
up_req->keepalive = 0;
evhtp_headers_add_header(up_req->headers_out, evhtp_header_new("Connection", "close", 0, 0));
evhtp_request_resume(up_req);
evhtp_send_reply(up_req, 503);
if (ds_req->rule) {
logger_log(rule->err_log, lzlog_notice,
"%s(): pending timeout hit for upstream client", __FUNCTION__);
}
} /* downstream_pending_timeout */
/**
* @brief Before accepting an upstream connection, evhtp will call this function
* which will check whether we have hit our max-pending limits, and if so,
* inform evhtp to not accept().
*
* @param up_conn
* @param arg
*
* @return
*/
evhtp_res
upstream_pre_accept(evhtp_connection_t * up_conn, void * arg) {
rproxy_t * rproxy;
if (!(rproxy = evthr_get_aux(up_conn->thread))) {
return EVHTP_RES_FATAL;
}
if (rproxy->server_cfg->max_pending <= 0) {
/* configured with unlimited pending */
return EVHTP_RES_OK;
}
/* check to see if we have too many pending requests, and if so, drop this
* connection.
*/
if ((rproxy->n_pending + 1) > rproxy->server_cfg->max_pending) {
#ifdef RPROXY_DEBUG
printf("Dropped connection, too many pending requests\n");
#endif
return EVHTP_RES_ERROR;
}
if (rproxy->server_cfg->disable_client_nagle == 1) {
/* config has requested that client sockets have the nagle algorithm