cleanup http requests (part 1)

This commit is contained in:
MihailRis 2026-09-09 23:43:44 +03:00
parent 9a87c20823
commit 74bb695807
6 changed files with 73 additions and 143 deletions

View file

@ -3,6 +3,7 @@
#include "engine/Engine.hpp"
#include "network/Network.hpp"
#include "devtools/Project.hpp"
#include "util/stringutil.hpp"
#include <variant>
#include <utility>
@ -91,24 +92,26 @@ static int perform_get(lua::State* L, network::Network& network, bool binary) {
int currentRequestId = request_id++;
network.get(
url,
network::HttpRequest request {};
request.url = std::move(url);
request.onResponse =
[currentRequestId, binary](std::vector<char> bytes) {
push_event(NetworkEvent(
RESPONSE,
ResponseEventDto {
200, binary, currentRequestId, std::move(bytes)}
));
},
};
request.onReject =
[currentRequestId, binary](int code, std::vector<char> bytes) {
push_event(NetworkEvent(
RESPONSE,
ResponseEventDto {
code, binary, currentRequestId, std::move(bytes)}
));
},
std::move(headers)
);
};
request.headers = std::move(headers);
network.request(std::move(request));
return lua::pushinteger(L, currentRequestId);
}
@ -134,25 +137,28 @@ static int l_post(lua::State* L, network::Network& network) {
auto headers = read_headers(L, 3);
int currentRequestId = request_id++;
network.post(
url,
string,
network::HttpRequest request {};
request.url = std::move(url);
request.body = string;
request.headers = std::move(headers);
request.headers.emplace_back("Content-Type: application/json");
request.onResponse =
[currentRequestId](std::vector<char> bytes) {
push_event(NetworkEvent(
RESPONSE,
ResponseEventDto {
200, false, currentRequestId, std::move(bytes)}
));
},
};
request.onReject =
[currentRequestId](int code, std::vector<char> bytes) {
push_event(NetworkEvent(
RESPONSE,
ResponseEventDto {
code, false, currentRequestId, std::move(bytes)}
));
},
std::move(headers)
);
};
network.request(std::move(request));
return lua::pushinteger(L, currentRequestId);
}

View file

@ -23,21 +23,6 @@ static size_t write_callback(
return size * nmemb;
}
enum class RequestType {
GET, POST
};
struct Request {
RequestType type;
std::string url;
OnResponse onResponse;
OnReject onReject;
long maxSize;
bool followLocation = false;
std::string data;
std::vector<std::string> headers;
};
class CurlRequests : public Requests {
CURLM* multiHandle;
CURL* curl;
@ -50,7 +35,7 @@ class CurlRequests : public Requests {
std::vector<char> buffer;
std::string url;
std::queue<Request> requests;
std::queue<HttpRequest> requests;
public:
CurlRequests(CURLM* multiHandle, CURL* curl)
: multiHandle(multiHandle), curl(curl) {
@ -61,47 +46,12 @@ public:
curl_easy_cleanup(curl);
curl_multi_cleanup(multiHandle);
}
void get(
const std::string& url,
OnResponse onResponse,
OnReject onReject,
std::vector<std::string> headers,
long maxSize
) override {
Request request {
RequestType::GET,
url,
onResponse,
onReject,
maxSize,
true,
"",
std::move(headers)};
void request(HttpRequest request) override {
processRequest(std::move(request));
}
void post(
const std::string& url,
const std::string& data,
OnResponse onResponse,
OnReject onReject=nullptr,
std::vector<std::string> headers = {},
long maxSize=0
) override {
Request request {
RequestType::POST,
url,
onResponse,
onReject,
maxSize,
false,
"",
std::move(headers)};
request.data = data;
processRequest(std::move(request));
}
void processRequest(Request request) {
void processRequest(HttpRequest request) {
if (!url.empty()) {
requests.push(request);
return;
@ -113,7 +63,19 @@ public:
buffer.clear();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POST, request.type == RequestType::POST);
switch (request.method) {
case HttpMethod::GET:
break;
case HttpMethod::POST:
curl_easy_setopt(curl, CURLOPT_POST, true);
break;
case HttpMethod::PUT:
curl_easy_setopt(curl, CURLOPT_UPLOAD, true);
break;
case HttpMethod::DELETE:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
}
curl_slist* hs = nullptr;
@ -121,16 +83,9 @@ public:
hs = curl_slist_append(hs, header.c_str());
}
switch (request.type) {
case RequestType::GET:
break;
case RequestType::POST:
hs = curl_slist_append(hs, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request.data.length());
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, request.data.c_str());
break;
default:
throw std::runtime_error("not implemented");
if (!request.body.empty()) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request.body.length());
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, request.body.data());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, request.followLocation);

View file

@ -51,27 +51,8 @@ Network::Network(std::unique_ptr<Requests> requests)
Network::~Network() = default;
void Network::get(
const std::string& url,
OnResponse onResponse,
OnReject onReject,
std::vector<std::string> headers,
long maxSize
) {
requests->get(url, onResponse, onReject, std::move(headers), maxSize);
}
void Network::post(
const std::string& url,
const std::string& fieldsData,
OnResponse onResponse,
OnReject onReject,
std::vector<std::string> headers,
long maxSize
) {
requests->post(
url, fieldsData, onResponse, onReject, std::move(headers), maxSize
);
void Network::request(HttpRequest request) {
requests->request(std::move(request));
}
Connection* Network::getConnection(u64id_t id, bool includePrivate) {

View file

@ -68,22 +68,7 @@ namespace network {
Network(std::unique_ptr<Requests> requests);
~Network();
void get(
const std::string& url,
OnResponse onResponse,
OnReject onReject = nullptr,
std::vector<std::string> headers = {},
long maxSize=0
);
void post(
const std::string& url,
const std::string& fieldsData,
OnResponse onResponse,
OnReject onReject = nullptr,
std::vector<std::string> headers = {},
long maxSize=0
);
void request(HttpRequest request);
[[nodiscard]] Connection* getConnection(u64id_t id, bool includePrivate);
[[nodiscard]] Server* getServer(u64id_t id, bool includePrivate) const;

View file

@ -10,6 +10,10 @@
#include <mutex>
namespace network {
enum class HttpMethod {
GET, POST, PUT, DELETE,
};
using OnResponse = std::function<void(std::vector<char>)>;
using OnReject = std::function<void(int, std::vector<char>)>;
using ConnectCallback = std::function<void(u64id_t, u64id_t)>;
@ -17,26 +21,23 @@ namespace network {
using ServerDatagramCallback = std::function<void(u64id_t sid, const std::string& addr, int port, const char* buffer, size_t length)>;
using ClientDatagramCallback = std::function<void(u64id_t cid, const char* buffer, size_t length)>;
struct HttpRequest {
HttpMethod method;
std::string url;
std::string body;
std::vector<std::string> headers;
OnResponse onResponse;
OnReject onReject;
bool followLocation = false;
long maxSize = -1;
};
class Requests {
public:
virtual ~Requests() {}
virtual void get(
const std::string& url,
OnResponse onResponse,
OnReject onReject=nullptr,
std::vector<std::string> headers = {},
long maxSize=0
) = 0;
virtual void post(
const std::string& url,
const std::string& data,
OnResponse onResponse,
OnReject onReject=nullptr,
std::vector<std::string> headers = {},
long maxSize=0
) = 0;
virtual void request(HttpRequest request) = 0;
[[nodiscard]] virtual size_t getTotalUpload() const = 0;
[[nodiscard]] virtual size_t getTotalDownload() const = 0;