mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
http: using string_view to avoid unnecessary copy in Headers
This commit is contained in:
parent
7856f20fec
commit
54a36093f7
3 changed files with 9 additions and 9 deletions
|
@ -130,7 +130,7 @@ static bool multiUserAuthorized(std::string strUserPass)
|
|||
return false;
|
||||
}
|
||||
|
||||
static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
|
||||
static bool RPCAuthorized(const std::string_view& strAuth, std::string& strAuthUsernameOut)
|
||||
{
|
||||
if (strAuth.substr(0, 6) != "Basic ")
|
||||
return false;
|
||||
|
@ -159,7 +159,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
|
|||
return false;
|
||||
}
|
||||
// Check authorization
|
||||
std::pair<bool, std::string> authHeader = req->GetHeader("authorization");
|
||||
std::pair<bool, std::string_view> authHeader = req->GetHeader("authorization");
|
||||
if (!authHeader.first) {
|
||||
req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
|
||||
req->WriteReply(HTTP_UNAUTHORIZED);
|
||||
|
|
|
@ -805,11 +805,11 @@ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
|
|||
namespace http_bitcoin {
|
||||
using util::Split;
|
||||
|
||||
std::optional<std::string> HTTPHeaders::Find(const std::string key) const
|
||||
std::optional<std::string_view> HTTPHeaders::Find(const std::string key) const
|
||||
{
|
||||
const auto it = m_map.find(key);
|
||||
if (it == m_map.end()) return std::nullopt;
|
||||
return it->second;
|
||||
return std::string_view(it->second);
|
||||
}
|
||||
|
||||
void HTTPHeaders::Write(const std::string key, const std::string value)
|
||||
|
@ -817,7 +817,7 @@ void HTTPHeaders::Write(const std::string key, const std::string value)
|
|||
// If present, append value to list
|
||||
const auto existing_value = Find(key);
|
||||
if (existing_value) {
|
||||
m_map[key] = existing_value.value() + ", " + value;
|
||||
m_map[key] = std::string(existing_value.value()) + ", " + value;
|
||||
} else {
|
||||
m_map[key] = value;
|
||||
}
|
||||
|
@ -1008,9 +1008,9 @@ std::optional<std::string> GetQueryParameterFromUri(const std::string& uri, cons
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string& hdr) const
|
||||
std::pair<bool, std::string_view> HTTPRequest::GetHeader(const std::string& hdr) const
|
||||
{
|
||||
std::optional<std::string> found{m_headers.Find(hdr)};
|
||||
std::optional<std::string_view> found{m_headers.Find(hdr)};
|
||||
if (found.has_value()) {
|
||||
return std::make_pair(true, found.value());
|
||||
} else
|
||||
|
|
|
@ -209,7 +209,7 @@ static const size_t MAX_HEADERS_SIZE{8192};
|
|||
class HTTPHeaders
|
||||
{
|
||||
public:
|
||||
std::optional<std::string> Find(const std::string key) const;
|
||||
std::optional<std::string_view> Find(const std::string key) const;
|
||||
void Write(const std::string key, const std::string value);
|
||||
void Remove(const std::string key);
|
||||
bool Read(util::LineReader& reader);
|
||||
|
@ -266,7 +266,7 @@ public:
|
|||
CService GetPeer() const;
|
||||
HTTPRequestMethod GetRequestMethod() const;
|
||||
std::optional<std::string> GetQueryParameter(const std::string& key) const;
|
||||
std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
|
||||
std::pair<bool, std::string_view> GetHeader(const std::string& hdr) const;
|
||||
std::string ReadBody() const {return m_body;};
|
||||
void WriteHeader(const std::string& hdr, const std::string& value);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue