Codechange: replace instance of char * with std::string

This commit is contained in:
Patric Stout 2023-02-15 20:10:30 +01:00 committed by Patric Stout
parent 0722bb3bf4
commit 1c17556f96
5 changed files with 23 additions and 44 deletions

View File

@ -42,9 +42,9 @@ public:
* *
* @param uri the URI to connect to (https://.../..). * @param uri the URI to connect to (https://.../..).
* @param callback the callback to send data back on. * @param callback the callback to send data back on.
* @param data optionally, the data we want to send. When set, 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 char *data = nullptr); static void Connect(const std::string &uri, HTTPCallback *callback, const std::string data = "");
/** /**
* Do the receiving for all HTTP connections. * Do the receiving for all HTTP connections.

View File

@ -53,26 +53,18 @@ public:
* *
* @param uri the URI to connect to (https://.../..). * @param uri the URI to connect to (https://.../..).
* @param callback the callback to send data back on. * @param callback the callback to send data back on.
* @param data optionally, the data we want to send. When set, 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 char *data = nullptr) : NetworkHTTPRequest(const std::string &uri, HTTPCallback *callback, const std::string &data) :
uri(uri), uri(uri),
callback(callback), callback(callback),
data(data) data(data)
{ {
} }
/** const std::string uri; ///< URI to connect to.
* Destructor of the HTTP request. HTTPCallback *callback; ///< Callback to send data back on.
*/ const std::string data; ///< Data to send, if any.
~NetworkHTTPRequest()
{
free(this->data);
}
std::string uri; ///< URI to connect to.
HTTPCallback *callback; ///< Callback to send data back on.
const char *data; ///< Data to send, if any.
}; };
static std::thread _http_thread; static std::thread _http_thread;
@ -85,7 +77,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 char *data) /* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const 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()) {
@ -154,9 +146,9 @@ void HttpThread()
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
/* Prepare POST body and URI. */ /* Prepare POST body and URI. */
if (request->data != nullptr) { if (!request->data.empty()) {
curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request->data); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request->data.c_str());
} }
curl_easy_setopt(curl, CURLOPT_URL, request->uri.c_str()); curl_easy_setopt(curl, CURLOPT_URL, request->uri.c_str());

View File

@ -18,7 +18,7 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const char *data) /* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const std::string data)
{ {
/* 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

@ -25,9 +25,9 @@ static HINTERNET _winhttp_session = nullptr;
/** Single HTTP request. */ /** Single HTTP request. */
class NetworkHTTPRequest { class NetworkHTTPRequest {
private: private:
std::wstring uri; ///< URI to connect to. const std::wstring uri; ///< URI to connect to.
HTTPCallback *callback; ///< Callback to send data back on. HTTPCallback *callback; ///< Callback to send data back on.
const char *data; ///< Data to send, if any. const std::string data; ///< Data to send, if any.
HINTERNET connection = nullptr; ///< Current connection object. HINTERNET connection = nullptr; ///< Current connection object.
HINTERNET request = nullptr; ///< Current request object. HINTERNET request = nullptr; ///< Current request object.
@ -35,7 +35,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 char *data = nullptr); NetworkHTTPRequest(const std::wstring &uri, HTTPCallback *callback, const std::string &data);
~NetworkHTTPRequest(); ~NetworkHTTPRequest();
@ -52,9 +52,9 @@ static std::vector<NetworkHTTPRequest *> _new_http_requests;
* *
* @param uri the URI to connect to (https://.../..). * @param uri the URI to connect to (https://.../..).
* @param callback the callback to send data back on. * @param callback the callback to send data back on.
* @param data optionally, the data we want to send. When set, 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 char *data) : NetworkHTTPRequest::NetworkHTTPRequest(const std::wstring &uri, HTTPCallback *callback, const std::string &data) :
uri(uri), uri(uri),
callback(callback), callback(callback),
data(data) data(data)
@ -224,7 +224,7 @@ void NetworkHTTPRequest::Connect()
return; return;
} }
this->request = WinHttpOpenRequest(connection, data == nullptr ? L"GET" : L"POST", url_components.lpszUrlPath, nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, url_components.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0); this->request = WinHttpOpenRequest(connection, data.empty() ? L"GET" : L"POST", url_components.lpszUrlPath, nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, url_components.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
if (this->request == nullptr) { if (this->request == nullptr) {
WinHttpCloseHandle(this->connection); WinHttpCloseHandle(this->connection);
@ -235,10 +235,10 @@ void NetworkHTTPRequest::Connect()
} }
/* Send the request (possibly with a payload). */ /* Send the request (possibly with a payload). */
if (data == nullptr) { if (data.empty()) {
WinHttpSendRequest(this->request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, reinterpret_cast<DWORD_PTR>(this)); WinHttpSendRequest(this->request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, reinterpret_cast<DWORD_PTR>(this));
} else { } else {
WinHttpSendRequest(this->request, L"Content-Type: application/x-www-form-urlencoded\r\n", -1, const_cast<char *>(data), static_cast<DWORD>(strlen(data)), static_cast<DWORD>(strlen(data)), reinterpret_cast<DWORD_PTR>(this)); WinHttpSendRequest(this->request, L"Content-Type: application/x-www-form-urlencoded\r\n", -1, const_cast<char *>(data.c_str()), static_cast<DWORD>(data.size()), static_cast<DWORD>(data.size()), reinterpret_cast<DWORD_PTR>(this));
} }
} }
@ -263,11 +263,9 @@ NetworkHTTPRequest::~NetworkHTTPRequest()
WinHttpCloseHandle(this->request); WinHttpCloseHandle(this->request);
WinHttpCloseHandle(this->connection); WinHttpCloseHandle(this->connection);
} }
free(this->data);
} }
/* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const char *data) /* static */ void NetworkHTTPSocketHandler::Connect(const std::string &uri, HTTPCallback *callback, const 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, data);
request->Connect(); request->Connect();

View File

@ -337,25 +337,14 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uin
*/ */
void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content) void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
{ {
uint count = (uint)content.size(); std::string content_request;
/* Allocate memory for the whole request.
* Requests are "id\nid\n..." (as strings), so assume the maximum ID,
* which is uint32 so 10 characters long. Then the newlines and
* multiply that all with the count and then add the '\0'. */
uint bytes = (10 + 1) * count + 1;
char *content_request = MallocT<char>(bytes);
const char *lastof = content_request + bytes - 1;
char *p = content_request;
for (const ContentID &id : content) { for (const ContentID &id : content) {
p += seprintf(p, lastof, "%d\n", id); content_request += std::to_string(id) + "\n";
} }
this->http_response_index = -1; this->http_response_index = -1;
NetworkHTTPSocketHandler::Connect(NetworkContentMirrorUriString(), this, content_request); NetworkHTTPSocketHandler::Connect(NetworkContentMirrorUriString(), this, content_request);
/* NetworkHTTPContentConnecter takes over freeing of content_request! */
} }
/** /**