Skip to content

Commit

Permalink
add httpGet error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 11, 2024
1 parent d7389c2 commit 4328c83
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
23 changes: 18 additions & 5 deletions src/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ using namespace network;

static debug::Logger logger("network");

size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
static size_t write_callback(
char* ptr, size_t size, size_t nmemb, void* userdata
) {
auto& buffer = *reinterpret_cast<std::vector<char>*>(userdata);
size_t psize = buffer.size();
buffer.resize(psize + size * nmemb);
Expand All @@ -30,7 +32,8 @@ class CurlHttp : public Http {
curl_easy_cleanup(curl);
}

void get(const std::string& url, const OnResponse& callback) override {
void get(const std::string& url, OnResponse onResponse, OnReject onReject)
override {
std::vector<char> buffer;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
Expand All @@ -45,8 +48,16 @@ class CurlHttp : public Http {
totalDownload += size;
}
totalDownload += buffer.size();
if (onResponse) {
onResponse(std::move(buffer));
}
} else {
auto message = curl_easy_strerror(res);
logger.error() << message << " (" << url << ")";
if (onReject) {
onReject(message);
}
}
callback(res, std::move(buffer));
}

size_t getTotalUpload() const override {
Expand All @@ -71,8 +82,10 @@ Network::Network(std::unique_ptr<Http> http) : http(std::move(http)) {

Network::~Network() = default;

void Network::httpGet(const std::string& url, const OnResponse& callback) {
http->get(url, callback);
void Network::httpGet(
const std::string& url, OnResponse onResponse, OnReject onReject
) {
http->get(url, onResponse, onReject);
}

size_t Network::getTotalUpload() const {
Expand Down
15 changes: 12 additions & 3 deletions src/network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
#include "util/Buffer.hpp"

namespace network {
using OnResponse = std::function<void(int, std::vector<char>)>;
using OnResponse = std::function<void(std::vector<char>)>;
using OnReject = std::function<void(const char*)>;

class Http {
public:
virtual ~Http() {}

virtual void get(const std::string& url, const OnResponse& callback) = 0;
virtual void get(
const std::string& url,
OnResponse onResponse,
OnReject onReject=nullptr
) = 0;
virtual size_t getTotalUpload() const = 0;
virtual size_t getTotalDownload() const = 0;
};
Expand All @@ -26,7 +31,11 @@ namespace network {
Network(std::unique_ptr<Http> http);
~Network();

void httpGet(const std::string& url, const OnResponse& callback);
void httpGet(
const std::string& url,
OnResponse onResponse,
OnReject onReject = nullptr
);

size_t getTotalUpload() const;
size_t getTotalDownload() const;
Expand Down
9 changes: 6 additions & 3 deletions test/network/curltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ TEST(curltest, curltest) {
network->httpGet(
"https://raw.githubusercontent.com/MihailRis/VoxelEngine-Cpp/refs/"
"heads/curl/res/content/base/blocks/lamp.json",
[=](int code, std::vector<char> data) {
auto v = std::string_view(data.data(), data.size());
auto value = json::parse(v);
[](std::vector<char> data) {
if (data.empty()) {
return;
}
auto view = std::string_view(data.data(), data.size());
auto value = json::parse(view);
std::cout << value << std::endl;
}
);
Expand Down

0 comments on commit 4328c83

Please sign in to comment.