1
0
Fork 0

Codechange: use std::string view for URI and std::move for data

pull/14115/head
Rubidium 2025-04-26 11:38:25 +02:00 committed by rubidium42
parent d1a7d30572
commit a5812a45f8
4 changed files with 12 additions and 12 deletions

View File

@ -55,7 +55,7 @@ public:
* @param callback the callback to send data back on. * @param callback the callback to send data back on.
* @param data the data we want to send. When non-empty, this will be a POST request, otherwise a GET request. * @param data the data we want to send. When non-empty, this will be a POST request, otherwise a GET request.
*/ */
static void Connect(const std::string &uri, HTTPCallback *callback, const std::string data = ""); static void Connect(std::string_view uri, HTTPCallback *callback, std::string &&data = "");
/** /**
* Do the receiving for all HTTP connections. * Do the receiving for all HTTP connections.

View File

@ -60,10 +60,10 @@ public:
* @param callback the callback to send data back on. * @param callback the callback to send data back on.
* @param data the data we want to send. When non-empty, this will be a POST request, otherwise a GET request. * @param data the data we want to send. When non-empty, this will be a POST request, otherwise a GET request.
*/ */
NetworkHTTPRequest(const std::string &uri, HTTPCallback *callback, const std::string &data) : NetworkHTTPRequest(std::string_view uri, HTTPCallback *callback, std::string &&data) :
uri(uri), uri(uri),
callback(callback), callback(callback),
data(data) data(std::move(data))
{ {
std::lock_guard<std::mutex> lock(_new_http_callback_mutex); std::lock_guard<std::mutex> lock(_new_http_callback_mutex);
_new_http_callbacks.push_back(&this->callback); _new_http_callbacks.push_back(&this->callback);
@ -90,7 +90,7 @@ static std::string _http_ca_file = "";
static std::string _http_ca_path = ""; static std::string _http_ca_path = "";
#endif /* UNIX */ #endif /* UNIX */
/* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const std::string data) /* static */ void NetworkHTTPSocketHandler::Connect(std::string_view uri, HTTPCallback *callback, std::string &&data)
{ {
#if defined(UNIX) #if defined(UNIX)
if (_http_ca_file.empty() && _http_ca_path.empty()) { if (_http_ca_file.empty() && _http_ca_path.empty()) {
@ -100,7 +100,7 @@ static std::string _http_ca_path = "";
#endif /* UNIX */ #endif /* UNIX */
std::lock_guard<std::mutex> lock(_http_mutex); std::lock_guard<std::mutex> lock(_http_mutex);
_http_requests.push(std::make_unique<NetworkHTTPRequest>(uri, callback, data)); _http_requests.push(std::make_unique<NetworkHTTPRequest>(uri, callback, std::move(data)));
_http_cv.notify_one(); _http_cv.notify_one();
} }

View File

@ -18,7 +18,7 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/* static */ void NetworkHTTPSocketHandler::Connect(const std::string &, HTTPCallback *callback, const std::string) /* static */ void NetworkHTTPSocketHandler::Connect(std::string_view, HTTPCallback *callback, std::string&&)
{ {
/* No valid HTTP backend was compiled in, so we fail all HTTP requests. */ /* No valid HTTP backend was compiled in, so we fail all HTTP requests. */
callback->OnFailure(); callback->OnFailure();

View File

@ -37,7 +37,7 @@ private:
int depth = 0; ///< Current redirect depth we are in. int depth = 0; ///< Current redirect depth we are in.
public: public:
NetworkHTTPRequest(const std::wstring &uri, HTTPCallback *callback, const std::string &data); NetworkHTTPRequest(std::wstring &&uri, HTTPCallback *callback, std::string &&data);
~NetworkHTTPRequest(); ~NetworkHTTPRequest();
@ -62,10 +62,10 @@ static std::mutex _new_http_callback_mutex;
* @param callback the callback to send data back on. * @param callback the callback to send data back on.
* @param data the data we want to send. When non-empty, this will be a POST request, otherwise a GET request. * @param data the data we want to send. When non-empty, this will be a POST request, otherwise a GET request.
*/ */
NetworkHTTPRequest::NetworkHTTPRequest(const std::wstring &uri, HTTPCallback *callback, const std::string &data) : NetworkHTTPRequest::NetworkHTTPRequest(std::wstring &&uri, HTTPCallback *callback, std::string &&data) :
uri(uri), uri(std::move(uri)),
callback(callback), callback(callback),
data(data) data(std::move(data))
{ {
std::lock_guard<std::mutex> lock(_new_http_callback_mutex); std::lock_guard<std::mutex> lock(_new_http_callback_mutex);
_new_http_callbacks.push_back(&this->callback); _new_http_callbacks.push_back(&this->callback);
@ -291,9 +291,9 @@ NetworkHTTPRequest::~NetworkHTTPRequest()
_http_callbacks.erase(std::remove(_http_callbacks.begin(), _http_callbacks.end(), &this->callback), _http_callbacks.end()); _http_callbacks.erase(std::remove(_http_callbacks.begin(), _http_callbacks.end(), &this->callback), _http_callbacks.end());
} }
/* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const std::string data) /* static */ void NetworkHTTPSocketHandler::Connect(std::string_view uri, HTTPCallback *callback, std::string &&data)
{ {
auto request = new NetworkHTTPRequest(std::wstring(uri.begin(), uri.end()), callback, data); auto request = new NetworkHTTPRequest(std::wstring(uri.begin(), uri.end()), callback, std::move(data));
request->Connect(); request->Connect();
std::lock_guard<std::mutex> lock(_new_http_requests_mutex); std::lock_guard<std::mutex> lock(_new_http_requests_mutex);