From f617b2711056aad39a742fa28b2adbb22955eb0c Mon Sep 17 00:00:00 2001 From: l-n-s Date: Mon, 11 Feb 2019 17:18:01 -0500 Subject: [PATCH 1/2] Support websocket connections over HTTP proxy --- libi2pd_client/HTTPProxy.cpp | 4 +++- libi2pd_client/I2PTunnel.cpp | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/libi2pd_client/HTTPProxy.cpp b/libi2pd_client/HTTPProxy.cpp index e54ccda5..590b0719 100644 --- a/libi2pd_client/HTTPProxy.cpp +++ b/libi2pd_client/HTTPProxy.cpp @@ -219,7 +219,9 @@ namespace proxy { /* replace headers */ req.UpdateHeader("User-Agent", "MYOB/6.66 (AN/ON)"); /* add headers */ - req.UpdateHeader("Connection", "close"); /* keep-alive conns not supported yet */ + auto h = req.GetHeader ("Connection"); + if (h.find("upgrade") == std::string::npos && h.find("Upgrade") == std::string::npos) + req.UpdateHeader("Connection", "close"); /* close everything, except websocket */ } /** diff --git a/libi2pd_client/I2PTunnel.cpp b/libi2pd_client/I2PTunnel.cpp index 975cc4ce..1cb47cb3 100644 --- a/libi2pd_client/I2PTunnel.cpp +++ b/libi2pd_client/I2PTunnel.cpp @@ -256,7 +256,11 @@ namespace client { if (!m_ConnectionSent && !line.compare(0, 10, "Connection")) { - m_OutHeader << "Connection: close\r\n"; + if (line.find("upgrade") == std::string::npos && line.find("Upgrade") == std::string::npos) + m_OutHeader << "Connection: close\r\n"; /* close everything, except websocket */ + else + m_OutHeader << line << "\r\n"; + m_ConnectionSent = true; } else if (!m_ProxyConnectionSent && !line.compare(0, 16, "Proxy-Connection")) From 016ae3b9e92ccd171fad47090ab5796528ba5098 Mon Sep 17 00:00:00 2001 From: l-n-s Date: Tue, 12 Feb 2019 11:20:54 -0500 Subject: [PATCH 2/2] rewrite for efficiency --- libi2pd_client/HTTPProxy.cpp | 6 ++++-- libi2pd_client/I2PTunnel.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libi2pd_client/HTTPProxy.cpp b/libi2pd_client/HTTPProxy.cpp index 590b0719..2e50ebb0 100644 --- a/libi2pd_client/HTTPProxy.cpp +++ b/libi2pd_client/HTTPProxy.cpp @@ -219,9 +219,11 @@ namespace proxy { /* replace headers */ req.UpdateHeader("User-Agent", "MYOB/6.66 (AN/ON)"); /* add headers */ + /* close connection, if not Connection: (U|u)pgrade (for websocket) */ auto h = req.GetHeader ("Connection"); - if (h.find("upgrade") == std::string::npos && h.find("Upgrade") == std::string::npos) - req.UpdateHeader("Connection", "close"); /* close everything, except websocket */ + auto x = h.find("pgrade"); + if (!(x != std::string::npos && std::tolower(h[x - 1]) == 'u')) + req.UpdateHeader("Connection", "close"); } /** diff --git a/libi2pd_client/I2PTunnel.cpp b/libi2pd_client/I2PTunnel.cpp index 1cb47cb3..65600cf8 100644 --- a/libi2pd_client/I2PTunnel.cpp +++ b/libi2pd_client/I2PTunnel.cpp @@ -256,10 +256,12 @@ namespace client { if (!m_ConnectionSent && !line.compare(0, 10, "Connection")) { - if (line.find("upgrade") == std::string::npos && line.find("Upgrade") == std::string::npos) - m_OutHeader << "Connection: close\r\n"; /* close everything, except websocket */ - else + /* close connection, if not Connection: (U|u)pgrade (for websocket) */ + auto x = line.find("pgrade"); + if (x != std::string::npos && std::tolower(line[x - 1]) == 'u') m_OutHeader << line << "\r\n"; + else + m_OutHeader << "Connection: close\r\n"; m_ConnectionSent = true; }