From 7c0b0a4e3e206b84bb4583c71a77a34e4b8dc74e Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 10 Mar 2021 22:47:31 -0500 Subject: [PATCH] common HTTP basic authorization string --- libi2pd/HTTP.cpp | 33 ++++++++++++++++++++++++--------- libi2pd/HTTP.h | 5 ++++- libi2pd/NTCP2.cpp | 4 ++-- libi2pd_client/HTTPProxy.cpp | 8 ++++---- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/libi2pd/HTTP.cpp b/libi2pd/HTTP.cpp index b967cbb5..da4299e9 100644 --- a/libi2pd/HTTP.cpp +++ b/libi2pd/HTTP.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2020, The PurpleI2P Project +* Copyright (c) 2013-2021, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -9,12 +9,15 @@ #include #include #include +#include #include "util.h" +#include "Base.h" #include "HTTP.h" -#include -namespace i2p { -namespace http { +namespace i2p +{ +namespace http +{ const std::vector HTTP_METHODS = { "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT", // HTTP basic methods "COPY", "LOCK", "MKCOL", "MOVE", "PROPFIND", "PROPPATCH", "UNLOCK", "SEARCH" // WebDAV methods, for SEARCH see rfc5323 @@ -471,12 +474,15 @@ namespace http { return ptr; } - std::string UrlDecode(const std::string& data, bool allow_null) { + std::string UrlDecode(const std::string& data, bool allow_null) + { std::string decoded(data); size_t pos = 0; - while ((pos = decoded.find('%', pos)) != std::string::npos) { + while ((pos = decoded.find('%', pos)) != std::string::npos) + { char c = strtol(decoded.substr(pos + 1, 2).c_str(), NULL, 16); - if (c == '\0' && !allow_null) { + if (c == '\0' && !allow_null) + { pos += 3; continue; } @@ -486,9 +492,11 @@ namespace http { return decoded; } - bool MergeChunkedResponse (std::istream& in, std::ostream& out) { + bool MergeChunkedResponse (std::istream& in, std::ostream& out) + { std::string hexLen; - while (!in.eof ()) { + while (!in.eof ()) + { std::getline (in, hexLen); errno = 0; long int len = strtoul(hexLen.c_str(), (char **) NULL, 16); @@ -506,5 +514,12 @@ namespace http { } return true; } + + std::string CreateBasicAuthorizationString (const std::string& user, const std::string& pass) + { + if (user.empty () && pass.empty ()) return ""; + return "Basic " + i2p::data::ToBase64Standard (user + ":" + pass); + } + } // http } // i2p diff --git a/libi2pd/HTTP.h b/libi2pd/HTTP.h index c0cf1285..1d73461e 100644 --- a/libi2pd/HTTP.h +++ b/libi2pd/HTTP.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2020, The PurpleI2P Project +* Copyright (c) 2013-2021, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -166,6 +166,9 @@ namespace http * @return true on success, false otherwise */ bool MergeChunkedResponse (std::istream& in, std::ostream& out); + + std::string CreateBasicAuthorizationString (const std::string& user, const std::string& pass); + } // http } // i2p diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index 5d7b0790..ffd9bb7d 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -1471,8 +1471,8 @@ namespace transport m_ProxyType = proxytype; m_ProxyAddress = addr; m_ProxyPort = port; - if ((!user.empty () || !pass.empty ()) && m_ProxyType == eHTTPProxy ) - m_ProxyAuthorization = "Basic " + i2p::data::ToBase64Standard (user + ":" + pass); + if (m_ProxyType == eHTTPProxy ) + m_ProxyAuthorization = i2p::http::CreateBasicAuthorizationString (user, pass); } void NTCP2Server::HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer) diff --git a/libi2pd_client/HTTPProxy.cpp b/libi2pd_client/HTTPProxy.cpp index 5deaa7b5..6b2b8df7 100644 --- a/libi2pd_client/HTTPProxy.cpp +++ b/libi2pd_client/HTTPProxy.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2020, The PurpleI2P Project +* Copyright (c) 2013-2021, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -433,13 +433,13 @@ namespace proxy { if (m_ProxyURL.is_i2p()) { m_ClientRequest.uri = origURI; - if (!m_ProxyURL.user.empty () || !m_ProxyURL.pass.empty ()) + auto auth = i2p::http::CreateBasicAuthorizationString (m_ProxyURL.user, m_ProxyURL.pass); + if (!auth.empty ()) { // remove existing authorization if any m_ClientRequest.RemoveHeader("Proxy-"); // add own http proxy authorization - std::string s = "Basic " + i2p::data::ToBase64Standard (m_ProxyURL.user + ":" + m_ProxyURL.pass); - m_ClientRequest.AddHeader("Proxy-Authorization", s); + m_ClientRequest.AddHeader("Proxy-Authorization", auth); } m_send_buf = m_ClientRequest.to_string(); m_recv_buf.erase(0, m_req_len);