Browse Source

pass const strings for HTTP headers

pull/2101/head
orignal 3 months ago
parent
commit
0046a8b3ec
  1. 2
      libi2pd/HTTP.cpp
  2. 2
      libi2pd/HTTP.h
  3. 23
      libi2pd_client/HTTPProxy.cpp

2
libi2pd/HTTP.cpp

@ -269,7 +269,7 @@ namespace http
return host.rfind(".i2p") == ( host.size() - 4 ); return host.rfind(".i2p") == ( host.size() - 4 );
} }
void HTTPMsg::add_header(const char *name, std::string & value, bool replace) { void HTTPMsg::add_header(const char *name, const std::string & value, bool replace) {
add_header(name, value.c_str(), replace); add_header(name, value.c_str(), replace);
} }

2
libi2pd/HTTP.h

@ -70,7 +70,7 @@ namespace http
{ {
std::map<std::string, std::string> headers; std::map<std::string, std::string> headers;
void add_header(const char *name, std::string & value, bool replace = false); void add_header(const char *name, const std::string & value, bool replace = false);
void add_header(const char *name, const char *value, bool replace = false); void add_header(const char *name, const char *value, bool replace = false);
void del_header(const char *name); void del_header(const char *name);

23
libi2pd_client/HTTPProxy.cpp

@ -9,6 +9,7 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <string> #include <string>
#include <string_view>
#include <atomic> #include <atomic>
#include <memory> #include <memory>
#include <set> #include <set>
@ -59,7 +60,8 @@ namespace proxy {
"</head>\r\n" "</head>\r\n"
; ;
bool str_rmatch(std::string & str, const char *suffix) { static bool str_rmatch(std::string & str, const char *suffix)
{
auto pos = str.rfind (suffix); auto pos = str.rfind (suffix);
if (pos == std::string::npos) if (pos == std::string::npos)
return false; /* not found */ return false; /* not found */
@ -77,16 +79,16 @@ namespace proxy {
void Terminate(); void Terminate();
void AsyncSockRead(); void AsyncSockRead();
static bool ExtractAddressHelper(i2p::http::URL& url, std::string& jump, bool& confirm); static bool ExtractAddressHelper(i2p::http::URL& url, std::string& jump, bool& confirm);
static bool VerifyAddressHelper (const std::string& jump); static bool VerifyAddressHelper (std::string_view jump);
static void SanitizeHTTPRequest(i2p::http::HTTPReq& req); static void SanitizeHTTPRequest(i2p::http::HTTPReq& req);
void SentHTTPFailed(const boost::system::error_code & ecode); void SentHTTPFailed(const boost::system::error_code & ecode);
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream); void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
/* error helpers */ /* error helpers */
void GenericProxyError(const std::string& title, const std::string& description); void GenericProxyError(const std::string& title, const std::string& description);
void GenericProxyInfo(const std::string& title, const std::string& description); void GenericProxyInfo(const std::string& title, const std::string& description);
void HostNotFound(std::string& host); void HostNotFound(const std::string& host);
void SendProxyError(std::string& content); void SendProxyError(const std::string& content);
void SendRedirect(std::string& address); void SendRedirect(const std::string& address);
void ForwardToUpstreamProxy(); void ForwardToUpstreamProxy();
void HandleUpstreamHTTPProxyConnect(const boost::system::error_code & ec); void HandleUpstreamHTTPProxyConnect(const boost::system::error_code & ec);
@ -175,7 +177,8 @@ namespace proxy {
SendProxyError(content); SendProxyError(content);
} }
void HTTPReqHandler::HostNotFound(std::string& host) { void HTTPReqHandler::HostNotFound(const std::string& host)
{
std::stringstream ss; std::stringstream ss;
ss << "<h1>" << tr("Proxy error: Host not found") << "</h1>\r\n" ss << "<h1>" << tr("Proxy error: Host not found") << "</h1>\r\n"
<< "<p>" << tr("Remote host not found in router's addressbook") << "</p>\r\n" << "<p>" << tr("Remote host not found in router's addressbook") << "</p>\r\n"
@ -192,7 +195,7 @@ namespace proxy {
SendProxyError(content); SendProxyError(content);
} }
void HTTPReqHandler::SendProxyError(std::string& content) void HTTPReqHandler::SendProxyError(const std::string& content)
{ {
i2p::http::HTTPRes res; i2p::http::HTTPRes res;
res.code = 500; res.code = 500;
@ -208,7 +211,7 @@ namespace proxy {
std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1)); std::bind(&HTTPReqHandler::SentHTTPFailed, shared_from_this(), std::placeholders::_1));
} }
void HTTPReqHandler::SendRedirect(std::string& address) void HTTPReqHandler::SendRedirect(const std::string& address)
{ {
i2p::http::HTTPRes res; i2p::http::HTTPRes res;
res.code = 302; res.code = 302;
@ -272,7 +275,7 @@ namespace proxy {
return true; return true;
} }
bool HTTPReqHandler::VerifyAddressHelper (const std::string& jump) bool HTTPReqHandler::VerifyAddressHelper (std::string_view jump)
{ {
auto pos = jump.find(".b32.i2p"); auto pos = jump.find(".b32.i2p");
if (pos != std::string::npos) if (pos != std::string::npos)
@ -441,7 +444,7 @@ namespace proxy {
bool useConnect = false; bool useConnect = false;
if(m_ClientRequest.method == "CONNECT") if(m_ClientRequest.method == "CONNECT")
{ {
std::string uri(m_ClientRequest.uri); const std::string& uri = m_ClientRequest.uri;
auto pos = uri.find(":"); auto pos = uri.find(":");
if(pos == std::string::npos || pos == uri.size() - 1) if(pos == std::string::npos || pos == uri.size() - 1)
{ {

Loading…
Cancel
Save