From c4aa272dbbe8b2140c7816ab09be661c81c9a481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Thu, 5 Dec 2019 13:17:49 -0500 Subject: [PATCH] http/request: add constructor --- include/opendht/http.h | 2 ++ src/http.cpp | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/opendht/http.h b/include/opendht/http.h index d43e78cb..38080226 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -212,6 +212,7 @@ public: using OnDataCb = std::function<void(const char* at, size_t length)>; using OnStateChangeCb = std::function<void(State state, const Response& response)>; using OnJsonCb = std::function<void(Json::Value value, unsigned int status_code)>; + using OnDoneCb = std::function<void(const Response& response)>; // resolves implicitly Request(asio::io_context& ctx, const std::string& url, const Json::Value& json, OnJsonCb jsoncb, @@ -219,6 +220,7 @@ public: Request(asio::io_context& ctx, const std::string& url, std::shared_ptr<dht::Logger> logger = {}); Request(asio::io_context& ctx, const std::string& host, const std::string& service, const bool ssl = false, std::shared_ptr<dht::Logger> logger = {}); + Request(asio::io_context& ctx, const std::string& url, OnDoneCb onDone, std::shared_ptr<dht::Logger> logger = {}); // user defined resolver Request(asio::io_context& ctx, std::shared_ptr<Resolver> resolver, sa_family_t family = AF_UNSPEC); diff --git a/src/http.cpp b/src/http.cpp index abb9611b..d34da523 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -443,11 +443,13 @@ Request::Request(asio::io_context& ctx, const std::string& url, const Json::Valu if (state != Request::State::DONE) return; Json::Value json; - std::string err; - Json::CharReaderBuilder rbuilder; - auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); - if (!reader->parse(response.body.data(), response.body.data() + response.body.size(), &json, &err) and logger_) - logger_->e("[http:request:%i] can't parse response to json", id_, err.c_str()); + if (response.status_code != 0) { + std::string err; + Json::CharReaderBuilder rbuilder; + auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + if (!reader->parse(response.body.data(), response.body.data() + response.body.size(), &json, &err) and logger_) + logger_->e("[http:request:%i] can't parse response to json", id_, err.c_str()); + } if (jsoncb) jsoncb(json, response.status_code); }); @@ -460,6 +462,15 @@ Request::Request(asio::io_context& ctx, const std::string& url, std::shared_ptr< init_default_headers(); } +Request::Request(asio::io_context& ctx, const std::string& url, OnDoneCb onDone, std::shared_ptr<dht::Logger> logger) + : logger_(logger), id_(Request::ids_++), ctx_(ctx), resolver_(std::make_shared<Resolver>(ctx, url, logger)) +{ + add_on_state_change_callback([this, onDone](State state, const Response& response){ + if (state == Request::State::DONE) + onDone(response); + }); +} + Request::Request(asio::io_context& ctx, const std::string& host, const std::string& service, const bool ssl, std::shared_ptr<dht::Logger> logger) : logger_(logger), id_(Request::ids_++), ctx_(ctx), -- GitLab