Skip to content

Commit

Permalink
use std::string_view instead const std::string&
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 12, 2025
1 parent 634cece commit 08a680b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
42 changes: 20 additions & 22 deletions libi2pd_client/HTTPProxy.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -60,7 +60,7 @@ namespace proxy {
"</head>\r\n"
;

static bool str_rmatch(std::string & str, const char *suffix)
static bool str_rmatch(std::string_view str, const char *suffix)
{
auto pos = str.rfind (suffix);
if (pos == std::string::npos)
Expand All @@ -84,16 +84,16 @@ namespace proxy {
void SentHTTPFailed(const boost::system::error_code & ecode);
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
/* error helpers */
void GenericProxyError(const std::string& title, const std::string& description);
void GenericProxyInfo(const std::string& title, const std::string& description);
void HostNotFound(const std::string& host);
void SendProxyError(const std::string& content);
void GenericProxyError(std::string_view title, std::string_view description);
void GenericProxyInfo(std::string_view title, std::string_view description);
void HostNotFound(std::string_view host);
void SendProxyError(std::string_view content);
void SendRedirect(const std::string& address);

void ForwardToUpstreamProxy();
void HandleUpstreamHTTPProxyConnect(const boost::system::error_code & ec);
void HandleUpstreamSocksProxyConnect(const boost::system::error_code & ec);
void HTTPConnect(const std::string & host, uint16_t port);
void HTTPConnect(std::string_view host, uint16_t port);
void HandleHTTPConnectStreamRequestComplete(std::shared_ptr<i2p::stream::Stream> stream);

typedef std::function<void(boost::asio::ip::tcp::endpoint)> ProxyResolvedHandler;
Expand Down Expand Up @@ -162,23 +162,23 @@ namespace proxy {
Done(shared_from_this());
}

void HTTPReqHandler::GenericProxyError(const std::string& title, const std::string& description) {
void HTTPReqHandler::GenericProxyError(std::string_view title, std::string_view description)
{
std::stringstream ss;
ss << "<h1>" << tr("Proxy error") << ": " << title << "</h1>\r\n";
ss << "<p>" << description << "</p>\r\n";
std::string content = ss.str();
SendProxyError(content);
SendProxyError(ss.str ());
}

void HTTPReqHandler::GenericProxyInfo(const std::string& title, const std::string& description) {
void HTTPReqHandler::GenericProxyInfo(std::string_view title, std::string_view description)
{
std::stringstream ss;
ss << "<h1>" << tr("Proxy info") << ": " << title << "</h1>\r\n";
ss << "<p>" << description << "</p>\r\n";
std::string content = ss.str();
SendProxyError(content);
SendProxyError(ss.str ());
}

void HTTPReqHandler::HostNotFound(const std::string& host)
void HTTPReqHandler::HostNotFound(std::string_view host)
{
std::stringstream ss;
ss << "<h1>" << tr("Proxy error: Host not found") << "</h1>\r\n"
Expand All @@ -192,11 +192,10 @@ namespace proxy {
ss << " <li><a href=\"" << js->second << host << "\">" << js->first << "</a></li>\r\n";
}
ss << "</ul>\r\n";
std::string content = ss.str();
SendProxyError(content);
SendProxyError(ss.str ());
}

void HTTPReqHandler::SendProxyError(const std::string& content)
void HTTPReqHandler::SendProxyError(std::string_view content)
{
i2p::http::HTTPRes res;
res.code = 500;
Expand Down Expand Up @@ -473,7 +472,7 @@ namespace proxy {
if (dest_host != "")
{
/* absolute url, replace 'Host' header */
std::string h = dest_host;
std::string h (dest_host);
if (dest_port != 0 && dest_port != 80)
h += ":" + std::to_string(dest_port);
m_ClientRequest.UpdateHeader("Host", h);
Expand Down Expand Up @@ -513,7 +512,7 @@ namespace proxy {
GenericProxyError(tr("Outproxy failure"), tr("Bad outproxy settings"));
} else {
LogPrint (eLogWarning, "HTTPProxy: Outproxy failure for ", dest_host, ": no outproxy enabled");
std::stringstream ss; ss << tr("Host %s is not inside I2P network, but outproxy is not enabled", dest_host.c_str());
std::stringstream ss; ss << tr("Host %s is not inside I2P network, but outproxy is not enabled", dest_host.c_str ());
GenericProxyError(tr("Outproxy failure"), ss.str());
}
return true;
Expand Down Expand Up @@ -653,11 +652,10 @@ namespace proxy {
Terminate();
}

void HTTPReqHandler::HTTPConnect(const std::string & host, uint16_t port)
void HTTPReqHandler::HTTPConnect(std::string_view host, uint16_t port)
{
LogPrint(eLogDebug, "HTTPProxy: CONNECT ",host, ":", port);
std::string hostname(host);
if(str_rmatch(hostname, ".i2p"))
if(str_rmatch(host, ".i2p"))
GetOwner()->CreateStream (std::bind (&HTTPReqHandler::HandleHTTPConnectStreamRequestComplete,
shared_from_this(), std::placeholders::_1), host, port);
else
Expand Down
4 changes: 2 additions & 2 deletions libi2pd_client/I2PService.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace client
m_ReadyTimerTriggered = false;
}

void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, uint16_t port) {
void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, std::string_view dest, uint16_t port) {
assert(streamRequestComplete);
auto address = i2p::client::context.GetAddressBook ().GetAddress (dest);
if (address)
Expand Down
4 changes: 2 additions & 2 deletions libi2pd_client/I2PService.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -59,7 +59,7 @@ namespace client
if (dest) dest->Acquire ();
m_LocalDestination = dest;
}
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, uint16_t port = 0);
void CreateStream (StreamRequestComplete streamRequestComplete, std::string_view dest, uint16_t port = 0);
void CreateStream(StreamRequestComplete complete, std::shared_ptr<const Address> address, uint16_t port);
auto& GetService () { return m_LocalDestination->GetService (); }

Expand Down

0 comments on commit 08a680b

Please sign in to comment.