From 66c301c03109088dc7b44f5708fa50b58440cdb6 Mon Sep 17 00:00:00 2001 From: hagen Date: Sat, 4 Jun 2016 00:00:00 +0000 Subject: [PATCH] * HTTPProxy.cpp : allow "tranparent" proxy (#508) --- HTTPProxy.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/HTTPProxy.cpp b/HTTPProxy.cpp index 0d305a47..e8926dc6 100644 --- a/HTTPProxy.cpp +++ b/HTTPProxy.cpp @@ -192,9 +192,28 @@ namespace proxy { std::string dest_host = url.host; uint16_t dest_port = url.port; - /* convert proxy-style http req to ordinary one: */ - /* 1) replace Host header, 2) make relative url */ - req.add_header("Host", url.host, true); + /* set proper 'Host' header in upstream request */ + auto h = req.headers.find("Host"); + if (dest_host != "") { + /* absolute url, replace 'Host' header */ + std::string h = dest_host; + if (dest_port != 0 && dest_port != 80) + h += ":" + std::to_string(dest_port); + req.add_header("Host", h, true); + } else if (h != req.headers.end()) { + /* relative url and 'Host' header provided. transparent proxy mode? */ + i2p::http::URL u; + std::string t = "http://" + h->second; + u.parse(t); + dest_host = u.host; + dest_port = u.port; + } else { + /* relative url and missing 'Host' header */ + std::string message = "Can't detect destination host from request"; + HTTPRequestFailed(message.c_str()); + return true; + } + /* make relative url */ url.schema = ""; url.host = ""; req.uri = url.to_string();