add network.post
This commit is contained in:
parent
5b6ad81eba
commit
96b904e61f
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "network/Network.hpp"
|
#include "network/Network.hpp"
|
||||||
|
#include "coders/json.hpp"
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
@ -36,6 +37,25 @@ static int l_get_binary(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_post(lua::State* L) {
|
||||||
|
std::string url(lua::require_lstring(L, 1));
|
||||||
|
auto data = lua::tovalue(L, 2);
|
||||||
|
|
||||||
|
lua::pushvalue(L, 3);
|
||||||
|
auto onResponse = lua::create_lambda_nothrow(L);
|
||||||
|
|
||||||
|
auto string = json::stringify(data, false);
|
||||||
|
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
|
||||||
|
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
||||||
|
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
||||||
|
);
|
||||||
|
engine->postRunnable([=]() {
|
||||||
|
onResponse({std::string(bytes.data(), bytes.size())});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int l_connect(lua::State* L) {
|
static int l_connect(lua::State* L) {
|
||||||
std::string address = lua::require_string(L, 1);
|
std::string address = lua::require_string(L, 1);
|
||||||
int port = lua::tointeger(L, 2);
|
int port = lua::tointeger(L, 2);
|
||||||
@ -200,6 +220,7 @@ static int l_get_total_download(lua::State* L) {
|
|||||||
const luaL_Reg networklib[] = {
|
const luaL_Reg networklib[] = {
|
||||||
{"get", lua::wrap<l_get>},
|
{"get", lua::wrap<l_get>},
|
||||||
{"get_binary", lua::wrap<l_get_binary>},
|
{"get_binary", lua::wrap<l_get_binary>},
|
||||||
|
{"post", lua::wrap<l_post>},
|
||||||
{"get_total_upload", lua::wrap<l_get_total_upload>},
|
{"get_total_upload", lua::wrap<l_get_total_upload>},
|
||||||
{"get_total_download", lua::wrap<l_get_total_download>},
|
{"get_total_download", lua::wrap<l_get_total_download>},
|
||||||
{"__open", lua::wrap<l_open>},
|
{"__open", lua::wrap<l_open>},
|
||||||
|
|||||||
@ -42,12 +42,18 @@ static size_t write_callback(
|
|||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class RequestType {
|
||||||
|
GET, POST
|
||||||
|
};
|
||||||
|
|
||||||
struct Request {
|
struct Request {
|
||||||
|
RequestType type;
|
||||||
std::string url;
|
std::string url;
|
||||||
OnResponse onResponse;
|
OnResponse onResponse;
|
||||||
OnReject onReject;
|
OnReject onReject;
|
||||||
long maxSize;
|
long maxSize;
|
||||||
bool followLocation = false;
|
bool followLocation = false;
|
||||||
|
std::string data;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CurlRequests : public Requests {
|
class CurlRequests : public Requests {
|
||||||
@ -73,22 +79,33 @@ public:
|
|||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
curl_multi_cleanup(multiHandle);
|
curl_multi_cleanup(multiHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
void get(
|
void get(
|
||||||
const std::string& url,
|
const std::string& url,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject,
|
OnReject onReject,
|
||||||
long maxSize
|
long maxSize
|
||||||
) override {
|
) override {
|
||||||
Request request {url, onResponse, onReject, maxSize};
|
Request request {RequestType::GET, url, onResponse, onReject, maxSize};
|
||||||
if (url.empty()) {
|
processRequest(std::move(request));
|
||||||
processRequest(request);
|
|
||||||
} else {
|
|
||||||
requests.push(request);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void processRequest(const Request& request) {
|
void post(
|
||||||
|
const std::string& url,
|
||||||
|
const std::string& data,
|
||||||
|
OnResponse onResponse,
|
||||||
|
OnReject onReject=nullptr,
|
||||||
|
long maxSize=0
|
||||||
|
) override {
|
||||||
|
Request request {RequestType::POST, url, onResponse, onReject, maxSize};
|
||||||
|
request.data = data;
|
||||||
|
processRequest(std::move(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
void processRequest(Request request) {
|
||||||
|
if (!url.empty()) {
|
||||||
|
requests.push(request);
|
||||||
|
return;
|
||||||
|
}
|
||||||
onResponse = request.onResponse;
|
onResponse = request.onResponse;
|
||||||
onReject = request.onReject;
|
onReject = request.onReject;
|
||||||
url = request.url;
|
url = request.url;
|
||||||
@ -96,9 +113,25 @@ public:
|
|||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_POST, request.type == RequestType::POST);
|
||||||
|
|
||||||
|
curl_slist* hs = NULL;
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, request.followLocation);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, request.followLocation);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.81.0");
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.81.0");
|
||||||
if (request.maxSize == 0) {
|
if (request.maxSize == 0) {
|
||||||
curl_easy_setopt(
|
curl_easy_setopt(
|
||||||
@ -164,7 +197,7 @@ public:
|
|||||||
if (url.empty() && !requests.empty()) {
|
if (url.empty() && !requests.empty()) {
|
||||||
auto request = std::move(requests.front());
|
auto request = std::move(requests.front());
|
||||||
requests.pop();
|
requests.pop();
|
||||||
processRequest(request);
|
processRequest(std::move(request));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -562,6 +595,16 @@ void Network::get(
|
|||||||
requests->get(url, onResponse, onReject, maxSize);
|
requests->get(url, onResponse, onReject, maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::post(
|
||||||
|
const std::string& url,
|
||||||
|
const std::string& fieldsData,
|
||||||
|
OnResponse onResponse,
|
||||||
|
OnReject onReject,
|
||||||
|
long maxSize
|
||||||
|
) {
|
||||||
|
requests->post(url, fieldsData, onResponse, onReject, maxSize);
|
||||||
|
}
|
||||||
|
|
||||||
Connection* Network::getConnection(u64id_t id) {
|
Connection* Network::getConnection(u64id_t id) {
|
||||||
std::lock_guard lock(connectionsMutex);
|
std::lock_guard lock(connectionsMutex);
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,15 @@ namespace network {
|
|||||||
OnReject onReject=nullptr,
|
OnReject onReject=nullptr,
|
||||||
long maxSize=0
|
long maxSize=0
|
||||||
) = 0;
|
) = 0;
|
||||||
|
|
||||||
|
virtual void post(
|
||||||
|
const std::string& url,
|
||||||
|
const std::string& data,
|
||||||
|
OnResponse onResponse,
|
||||||
|
OnReject onReject=nullptr,
|
||||||
|
long maxSize=0
|
||||||
|
) = 0;
|
||||||
|
|
||||||
virtual size_t getTotalUpload() const = 0;
|
virtual size_t getTotalUpload() const = 0;
|
||||||
virtual size_t getTotalDownload() const = 0;
|
virtual size_t getTotalDownload() const = 0;
|
||||||
|
|
||||||
@ -84,6 +93,14 @@ namespace network {
|
|||||||
long maxSize=0
|
long maxSize=0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void post(
|
||||||
|
const std::string& url,
|
||||||
|
const std::string& fieldsData,
|
||||||
|
OnResponse onResponse,
|
||||||
|
OnReject onReject = nullptr,
|
||||||
|
long maxSize=0
|
||||||
|
);
|
||||||
|
|
||||||
[[nodiscard]] Connection* getConnection(u64id_t id);
|
[[nodiscard]] Connection* getConnection(u64id_t id);
|
||||||
[[nodiscard]] TcpServer* getServer(u64id_t id) const;
|
[[nodiscard]] TcpServer* getServer(u64id_t id) const;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user