From 65199b48c90cd1b6874225d793e82cf73fa2f4f5 Mon Sep 17 00:00:00 2001 From: Bryan Call Date: Sat, 12 Nov 2016 20:10:08 -0800 Subject: [PATCH] TS-1228: Support new Forwarded standard Code cleanup before implementation --- proxy/hdrs/MIME.h | 13 +++++++++++++ proxy/http/HttpTransact.cc | 35 ++++++++++++----------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/proxy/hdrs/MIME.h b/proxy/hdrs/MIME.h index 4c63f527eb3..1a5949809b2 100644 --- a/proxy/hdrs/MIME.h +++ b/proxy/hdrs/MIME.h @@ -979,6 +979,7 @@ class MIMEHdr : public HdrHeapSDKHandle // Other separators (e.g. ';' in Set-cookie/Cookie) are also possible void field_value_append(MIMEField *field, const char *value, int value_length, bool prepend_comma = false, const char separator = ','); + void value_append_or_set(const char *name, const int name_length, char *value, int value_length); void field_combine_dups(MIMEField *field, bool prepend_comma = false, const char separator = ','); time_t get_age(); int64_t get_content_length() const; @@ -1391,6 +1392,18 @@ MIMEHdr::field_combine_dups(MIMEField *field, bool prepend_comma, const char sep } } +inline void +MIMEHdr::value_append_or_set(const char *name, const int name_length, char *value, int value_length) +{ + MIMEField *field = nullptr; + + if ((field = field_find(name, name_length)) != nullptr) { + field_value_append(field, value, value_length, true); + } else { + value_set(name, name_length, value, value_length); + } +} + /*------------------------------------------------------------------------- -------------------------------------------------------------------------*/ diff --git a/proxy/http/HttpTransact.cc b/proxy/http/HttpTransact.cc index 25e40263061..c12a33473d3 100644 --- a/proxy/http/HttpTransact.cc +++ b/proxy/http/HttpTransact.cc @@ -5218,39 +5218,28 @@ HttpTransact::add_client_ip_to_outgoing_request(State *s, HTTPHdr *request) ip_string[0] = 0; } - //////////////////////////////////////////////////////////////// - // if we want client-ip headers, and there isn't one, add one // - //////////////////////////////////////////////////////////////// + // Check to see if the ip_string has been set + if (ip_string_size == 0) { + return; + } + + // if we want client-ip headers, and there isn't one, add one if ((s->txn_conf->anonymize_insert_client_ip) && (!s->txn_conf->anonymize_remove_client_ip)) { bool client_ip_set = request->presence(MIME_PRESENCE_CLIENT_IP); DebugTxn("http_trans", "client_ip_set = %d", client_ip_set); - if (!client_ip_set && ip_string_size > 0) { + if (client_ip_set == true) { request->value_set(MIME_FIELD_CLIENT_IP, MIME_LEN_CLIENT_IP, ip_string, ip_string_size); DebugTxn("http_trans", "inserted request header 'Client-ip: %s'", ip_string); } } + // Add or append to the X-Forwarded-For header if (s->txn_conf->insert_squid_x_forwarded_for) { - if (ip_string_size > 0) { - MIMEField *x_for; - - if ((x_for = request->field_find(MIME_FIELD_X_FORWARDED_FOR, MIME_LEN_X_FORWARDED_FOR)) != nullptr) { - // http://en.wikipedia.org/wiki/X-Forwarded-For - // The X-Forwarded-For (XFF) HTTP header field is a de facto standard - // for identifying the originating IP address of a client connecting - // to a web server through an HTTP proxy or load balancer. This is a - // non-RFC-standard request field which was introduced by the Squid - // caching proxy server's developers. - // X-Forwarded-For: client1, proxy1, proxy2 - request->field_value_append(x_for, ip_string, ip_string_size, true); // true => comma must be inserted - } else { - request->value_set(MIME_FIELD_X_FORWARDED_FOR, MIME_LEN_X_FORWARDED_FOR, ip_string, ip_string_size); - } - DebugTxn("http_trans", "[add_client_ip_to_outgoing_request] Appended connecting client's " - "(%s) to the X-Forwards header", - ip_string); - } + request->value_append_or_set(MIME_FIELD_X_FORWARDED_FOR, MIME_LEN_X_FORWARDED_FOR, ip_string, ip_string_size); + DebugTxn("http_trans", "[add_client_ip_to_outgoing_request] Appended connecting client's " + "(%s) to the X-Forwards header", + ip_string); } }