| 316 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) { |
316 int http_reverse_proxy_service(pblock *param, Session *sn, Request *rq) { |
| 317 EventHandler *ev = sn->ev; |
317 EventHandler *ev = sn->ev; |
| 318 const char *method = pblock_findkeyval(pb_key_method, rq->reqpb); |
318 const char *method = pblock_findkeyval(pb_key_method, rq->reqpb); |
| 319 const char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); |
319 const char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); |
| 320 const char *query = pblock_findkeyval(pb_key_query, rq->reqpb); |
320 const char *query = pblock_findkeyval(pb_key_query, rq->reqpb); |
| |
321 const char *forward = pblock_findval("forwarded", rq->reqpb); |
| |
322 |
| |
323 char *host = pblock_findval("host", rq->headers); |
| |
324 |
| |
325 int forwarded_headers = 0; |
| |
326 if(forward) { |
| |
327 if(!strcmp(forward, "true")) { |
| |
328 forwarded_headers = 1; |
| |
329 } else if(!strcmp(forward, "forwarded")) { |
| |
330 forwarded_headers = 1; |
| |
331 } else if(!strcmp(forward, "rfc7239")) { |
| |
332 forwarded_headers = 1; |
| |
333 } else if(!strcmp(forward, "xforwarded")) { |
| |
334 forwarded_headers = 2; |
| |
335 } |
| |
336 } |
| 321 |
337 |
| 322 //log_ereport(LOG_INFORM, "reverse-proxy: %s %s", method, uri); |
338 //log_ereport(LOG_INFORM, "reverse-proxy: %s %s", method, uri); |
| 323 |
339 |
| 324 char *host_header = pblock_findval("host-header", param); |
340 char *host_header = pblock_findval("host-header", param); |
| 325 |
341 |
| 487 |
503 |
| 488 // add host header |
504 // add host header |
| 489 if(http_client_add_request_header(client, cx_mutstr("host"), cx_mutstr(host_header))) { |
505 if(http_client_add_request_header(client, cx_mutstr("host"), cx_mutstr(host_header))) { |
| 490 http_client_free(client); |
506 http_client_free(client); |
| 491 return REQ_ABORTED; |
507 return REQ_ABORTED; |
| |
508 } |
| |
509 |
| |
510 char *ip = pblock_findkeyval(pb_key_ip, sn->client); |
| |
511 if(forwarded_headers == 1) { |
| |
512 char buf[512]; |
| |
513 int n = snprintf(buf, 512, "host=\"%s\";for=\"%s\";proto=%s", host, ip, "https"); // TODO: proto |
| |
514 if(n > 511 || http_client_add_request_header_copy(client, cx_str("forwarded"), cx_str(buf))) { |
| |
515 http_client_free(client); |
| |
516 return REQ_ABORTED; |
| |
517 } |
| |
518 } else if(forwarded_headers == 2) { |
| |
519 if(http_client_add_request_header(client, cx_mutstr("x-forwarded-for"), cx_mutstr(ip))) { |
| |
520 http_client_free(client); |
| |
521 return REQ_ABORTED; |
| |
522 } |
| |
523 if(http_client_add_request_header(client, cx_mutstr("x-forwarded-host"), cx_mutstr(host))) { |
| |
524 http_client_free(client); |
| |
525 return REQ_ABORTED; |
| |
526 } |
| 492 } |
527 } |
| 493 |
528 |
| 494 // add request headers to the client |
529 // add request headers to the client |
| 495 CxIterator i = pblock_iterator(rq->headers); |
530 CxIterator i = pblock_iterator(rq->headers); |
| 496 cx_foreach(pb_entry*, entry, i) { |
531 cx_foreach(pb_entry*, entry, i) { |