Skip to content

Commit

Permalink
Started moving toward http helper
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Jan 30, 2024
1 parent 91a4a3e commit 3669223
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 33 deletions.
9 changes: 3 additions & 6 deletions library/include/ipfs_client/context_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "crypto/hasher.h"
#include "crypto/signature_verifier.h"
#include "crypto/signing_key_type.h"
#include "ctx/http_api.h"
#include "dag_cbor_value.h"
#include "gateway_spec.h"
#include "http_request_description.h"
Expand Down Expand Up @@ -33,12 +34,7 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {
ContextApi();
virtual ~ContextApi() noexcept {}

using HttpRequestDescription = ::ipfs::HttpRequestDescription;
using HeaderAccess = std::function<std::string(std::string_view)>;
using HttpCompleteCallback =
std::function<void(std::int16_t, std::string_view, HeaderAccess)>;
virtual void SendHttpRequest(HttpRequestDescription,
HttpCompleteCallback cb) const = 0;
ctx::HttpApi& http();

using DnsTextResultsCallback =
std::function<void(std::vector<std::string> const&)>;
Expand Down Expand Up @@ -88,6 +84,7 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {
std::unordered_map<HashType, std::unique_ptr<crypto::Hasher>> hashers_;
std::unordered_map<SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>>
verifiers_;
std::unique_ptr<ctx::HttpApi> http_api_;
};

} // namespace ipfs
Expand Down
19 changes: 19 additions & 0 deletions library/include/ipfs_client/ctx/http_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef IPFS_CHROMIUM_HTTP_API_H
#define IPFS_CHROMIUM_HTTP_API_H

#include <ipfs_client/http_request_description.h>
#include <functional>

namespace ipfs::ctx {
class HttpApi {
public:
using HttpRequestDescription = ::ipfs::HttpRequestDescription;
using HeaderAccess = std::function<std::string(std::string_view)>;
using HttpCompleteCallback =
std::function<void(std::int16_t, std::string_view, HeaderAccess)>;
virtual void SendHttpRequest(HttpRequestDescription,
HttpCompleteCallback cb) const = 0;
};
} // namespace ipfs::ctx

#endif // IPFS_CHROMIUM_HTTP_API_H
2 changes: 0 additions & 2 deletions library/include/ipfs_client/test_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace ipfs {
// LCOV_EXCL_START

class TestContext final : public ContextApi {
void SendHttpRequest(HttpRequestDescription,
HttpCompleteCallback) const override;
struct DnsCbs {
DnsTextResultsCallback r;
DnsTextCompleteCallback c;
Expand Down
7 changes: 7 additions & 0 deletions library/src/ipfs_client/context_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "crypto/openssl_sha2_256.h"
#include "crypto/openssl_signature_verifier.h"
#include "ctx/null_http_provider.h"

#include "log_macros.h"

Expand Down Expand Up @@ -45,4 +46,10 @@ bool Self::VerifyKeySignature(SigningKeyType typ,
void Self::SetGatewayRate(std::string_view, unsigned int) {}
void Self::AddGateway(std::string_view gw) {
SetGatewayRate(gw, 60U);
}
auto Self::http() -> ctx::HttpApi& {
if (!http_api_) {
http_api_ = std::make_unique<ctx::NullHttpProvider>();
}
return *http_api_;
}
9 changes: 9 additions & 0 deletions library/src/ipfs_client/ctx/null_http_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "null_http_provider.h"

using Self = ipfs::ctx::NullHttpProvider;

void Self::SendHttpRequest(HttpRequestDescription,
HttpCompleteCallback cb) const {
auto hdrs = [](auto) { return std::string{}; };
cb(500, "No HTTP networking available.", hdrs);
}
12 changes: 12 additions & 0 deletions library/src/ipfs_client/ctx/null_http_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef IPFS_CHROMIUM_NULLHTTPPROVIDER_H
#define IPFS_CHROMIUM_NULLHTTPPROVIDER_H

#include <ipfs_client/ctx/http_api.h>

namespace ipfs::ctx {
class NullHttpProvider : public HttpApi {
void SendHttpRequest(HttpRequestDescription, HttpCompleteCallback cb) const;
};
} // namespace ipfs::ctx

#endif // IPFS_CHROMIUM_NULLHTTPPROVIDER_H
4 changes: 2 additions & 2 deletions library/src/ipfs_client/gw/multi_gateway_requestor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ void Self::DoSend(RequestPtr req, std::string const& gw, GatewayState& state) {
HandleResponse(*desc, req, gw, s, b, h, timed_out);
};
state.just_sent_one();
api_->SendHttpRequest(*desc, cb);
api_->http().SendHttpRequest(*desc, cb);
}
void Self::HandleResponse(HttpRequestDescription const& desc,
RequestPtr req,
std::string const& gw,
std::int16_t status,
std::string_view body,
ContextApi::HeaderAccess hdrs,
HeaderAccess hdrs,
bool timed_out) {
if (req->Finished() ||
(req->PartiallyRedundant() && req->type == Type::Block)) {
Expand Down
3 changes: 2 additions & 1 deletion library/src/ipfs_client/gw/multi_gateway_requestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class MultiGatewayRequestor : public Requestor {
std::deque<RequestPtr> q;
bool Process(RequestPtr const&);
void DoSend(RequestPtr, std::string const&, GatewayState&);
using HeaderAccess = ctx::HttpApi::HeaderAccess;
void HandleResponse(HttpRequestDescription const&,
RequestPtr,
std::string const&,
std::int16_t,
std::string_view,
ContextApi::HeaderAccess,
HeaderAccess,
bool);

public:
Expand Down
4 changes: 0 additions & 4 deletions library/src/ipfs_client/ipns_record_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ namespace i = ipfs;

namespace {
struct Api final : public i::ContextApi {
void SendHttpRequest(i::HttpRequestDescription,
HttpCompleteCallback cb) const {
throw 1;
}
void SendDnsTextRequest(std::string hostname,
DnsTextResultsCallback,
DnsTextCompleteCallback) {
Expand Down
2 changes: 0 additions & 2 deletions library/src/ipfs_client/orchestrator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ struct MockApi final : public ipfs::ContextApi {
DnsTextCompleteCallback ccb) {
EXPECT_GE(expected_dns.size(), 1U);
}
void SendHttpRequest(ipfs::HttpRequestDescription,
HttpCompleteCallback) const {}
std::vector<MockCbor> mutable cbors;
std::unique_ptr<ipfs::DagCborValue> ParseCbor(ByteView) const {
if (cbors.empty()) {
Expand Down
18 changes: 9 additions & 9 deletions library/src/ipfs_client/test_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class HttpSession : public std::enable_shared_from_this<HttpSession> {
boost::beast::ssl_stream<boost::beast::tcp_stream> stream_;
boost::beast::flat_buffer buffer_; // (Must persist between reads)
http::request<http::empty_body> req_;
ipfs::ContextApi::HttpCompleteCallback cb_;
using api = ipfs::ctx::HttpApi;
api::HttpCompleteCallback cb_;
int expiry_seconds_ = 91;
std::string host_, port_, target_;
ipfs::HttpRequestDescription desc_;
Expand Down Expand Up @@ -164,7 +165,7 @@ class HttpSession : public std::enable_shared_from_this<HttpSession> {
explicit HttpSession(boost::asio::io_context& ioc,
boost::asio::ssl::context& ssc,
ipfs::HttpRequestDescription& desc,
ipfs::ContextApi::HttpCompleteCallback cb)
api::HttpCompleteCallback cb)
: ioc_{ioc},
strand_{boost::asio::make_strand(ioc)},
resolver_(strand_),
Expand Down Expand Up @@ -294,8 +295,7 @@ class HttpSession : public std::enable_shared_from_this<HttpSession> {
if (ec)
return fail(ec, "read");
res_ = response_parser_.release();
ipfs::ContextApi::HeaderAccess get_hdr =
[this](std::string_view k) -> std::string {
api::HeaderAccess get_hdr = [this](std::string_view k) -> std::string {
std::string rv{(*res_)[k]};
return rv;
};
Expand Down Expand Up @@ -384,11 +384,11 @@ class HttpSession : public std::enable_shared_from_this<HttpSession> {
std::map<std::string, boost::asio::ip::tcp::resolver::results_type>
HttpSession::resolutions_;

void Self::SendHttpRequest(HttpRequestDescription desc,
HttpCompleteCallback cb) const {
auto sess = std::make_shared<HttpSession>(io_, ssl_ctx_, desc, cb);
sess->run();
}
// void Self::SendHttpRequest(HttpRequestDescription desc, HttpCompleteCallback
// cb) const {
// auto sess = std::make_shared<HttpSession>(io_, ssl_ctx_, desc, cb);
// sess->run();
// }
std::optional<ipfs::GatewaySpec> Self::GetGateway(std::size_t index) const {
if (index < gateways_.size()) {
return gateways_.at(index);
Expand Down
10 changes: 3 additions & 7 deletions test_data/include/mock_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ class GatewayRequest;

namespace i = ipfs;
namespace ig = i::gw;
namespace ic = i::ctx;

using namespace std::literals;

namespace {
struct MockApi final : public i::ContextApi {
std::vector<HttpRequestDescription> mutable http_requests_sent;
std::vector<HttpCompleteCallback> mutable cbs;
void SendHttpRequest(HttpRequestDescription d,
HttpCompleteCallback cb) const {
http_requests_sent.push_back(d);
cbs.push_back(cb);
}
std::vector<i::HttpRequestDescription> mutable http_requests_sent;
std::vector<ic::HttpApi::HttpCompleteCallback> mutable cbs;
struct DnsInvocation {
std::string host;
std::vector<std::string> txt_records;
Expand Down

0 comments on commit 3669223

Please sign in to comment.