Skip to content

Commit

Permalink
CborParser
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Feb 7, 2024
1 parent 67cc51f commit 6163de3
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 35 deletions.
5 changes: 3 additions & 2 deletions component/chromium_dns_txt_lookup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ void Self::SendDnsTextRequest(std::string host,
DnsTextResultsCallback res,
DnsTextCompleteCallback don) {
if (dns_reqs_.find(host) != dns_reqs_.end()) {
LOG(INFO) << "Requested resolution of DNSLink host " << host
<< " multiple times.";
// TODO: do combine two or delay this one. Don't drop the first one!!
VLOG(1) << "Requested resolution of DNSLink host " << host
<< " multiple times.";
}
auto don_wrap = [don, this, host]() {
don();
Expand Down
15 changes: 15 additions & 0 deletions component/json_parser_adapter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "json_parser_adapter.h"

#include "chromium_json_adapter.h"

#include <base/json/json_reader.h>

using Self = ipfs::JsonParserAdapter;

auto Self::Parse(std::string_view j_str) -> std::unique_ptr<ipfs::DagJsonValue> {
auto d = base::JSONReader::Read(j_str, base::JSON_ALLOW_TRAILING_COMMAS);
if (d) {
return std::make_unique<ChromiumJsonAdapter>(std::move(d.value()));
}
return {};
}
2 changes: 1 addition & 1 deletion component/json_parser_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ipfs {
class JsonParserAdapter final : public ctx::JsonParser {
public:
~JsonParserAdapter() noexcept override {}
std::unique_ptr<DagJsonValue> Parse(std::string_view) const override;
std::unique_ptr<DagJsonValue> Parse(std::string_view) override;
};
}

Expand Down
5 changes: 5 additions & 0 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/cbor_parser.h"
#include "ctx/dns_txt_lookup.h"
#include "ctx/gateway_config.h"
#include "ctx/http_api.h"
Expand All @@ -28,6 +29,7 @@ class io_context;
namespace ipfs {
class IpfsRequest;
class DagJsonValue;
class DagCborValue;

/**
* \brief Interface that provides functionality from whatever
Expand All @@ -46,10 +48,12 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {
ctx::DnsTxtLookup& dns_txt();
ctx::GatewayConfig& gw_cfg();
ctx::JsonParser& json();
ctx::CborParser& cbor();
ContextApi& with(std::unique_ptr<ctx::HttpApi>);
ContextApi& with(std::unique_ptr<ctx::DnsTxtLookup>);
ContextApi& with(std::unique_ptr<ctx::GatewayConfig>);
ContextApi& with(std::unique_ptr<ctx::JsonParser>);
ContextApi& with(std::unique_ptr<ctx::CborParser>);
ContextApi& with(SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>);

/*!
Expand Down Expand Up @@ -88,6 +92,7 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {
std::unique_ptr<ctx::DnsTxtLookup> dns_txt_;
std::unique_ptr<ctx::GatewayConfig> gateway_config_;
std::unique_ptr<ctx::JsonParser> json_parser_;
std::unique_ptr<ctx::CborParser> cbor_parser_;
};

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

#include <vocab/byte_view.h>

#include <memory>

namespace ipfs {
class DagCborValue;
}

namespace ipfs::ctx {
class CborParser {
public:
virtual std::unique_ptr<DagCborValue> Parse(ByteView) = 0;
virtual ~CborParser() noexcept {}
};
} // namespace ipfs::ctx

#endif // IPFS_CHROMIUM_CBOR_PARSER_H
2 changes: 1 addition & 1 deletion library/include/ipfs_client/ctx/json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ipfs::ctx {
class JsonParser {
public:
virtual ~JsonParser() noexcept {}
virtual std::unique_ptr<DagJsonValue> Parse(std::string_view) const = 0;
virtual std::unique_ptr<DagJsonValue> Parse(std::string_view) = 0;
};
} // namespace ipfs::ctx

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

#include "cbor_parser.h"

#include <ipfs_client/json_cbor_adapter.h>

#if HAS_JSON_CBOR_ADAPTER

namespace ipfs::ctx {
class NlohmannCborParser final : public CborParser {
public:
std::unique_ptr<DagCborValue> Parse(ByteView) override;
~NlohmannCborParser() noexcept override {}
};
} // namespace ipfs::ctx

#endif // HAS_CBOR_CBOR_ADAPTER
#endif // IPFS_CHROMIUM_NLOHMANN_CBOR_PARSER_H
2 changes: 1 addition & 1 deletion library/include/ipfs_client/ctx/nlohmann_json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace ipfs::ctx {
class NlohmannJsonParser : public JsonParser {
public:
std::unique_ptr<DagJsonValue> Parse(std::string_view) const override;
std::unique_ptr<DagJsonValue> Parse(std::string_view) override;
~NlohmannJsonParser() noexcept override {}
};
} // namespace ipfs::ctx
Expand Down
16 changes: 16 additions & 0 deletions library/src/ipfs_client/context_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "crypto/openssl_sha2_256.h"
#include "ipfs_client/crypto/openssl_signature_verifier.h"
#include "ipfs_client/ctx/boost_beast_http.h"
#include "ipfs_client/ctx/nlohmann_cbor_parser.h"
#include "ipfs_client/ctx/nlohmann_json_parser.h"
#include "ipfs_client/ctx/null_dns_txt_lookup.h"
#include "ipfs_client/ctx/null_http_provider.h"
Expand Down Expand Up @@ -77,10 +78,25 @@ auto Self::json() -> ctx::JsonParser& {
DCHECK(json_parser_);
return *json_parser_;
}
auto Self::cbor() -> ctx::CborParser& {
if (!cbor_parser_) {
#if HAS_JSON_CBOR_ADAPTER
cbor_parser_ = std::make_unique<ctx::NlohmannCborParser>();
#else
LOG(FATAL) << "A CBOR parser must be provided.";
#endif
}
DCHECK(cbor_parser_);
return *cbor_parser_;
}
Self& Self::with(std::unique_ptr<ctx::JsonParser> p) {
json_parser_ = std::move(p);
return *this;
}
Self& Self::with(std::unique_ptr<ctx::CborParser> p) {
cbor_parser_ = std::move(p);
return *this;
}
Self& Self::with(std::unique_ptr<ctx::HttpApi> p) {
http_api_ = std::move(p);
return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ bool ctx::LoadGatewaysFromEnvironmentVariable(ipfs::ctx::GatewayConfig& cfg) {

void ctx::LoadStaticGatewayList(ipfs::ctx::GatewayConfig& cfg) {
auto static_list = {
std::pair<std::string_view, int>{"http://localhost:8080/", 7000},
{"https://jcsl.hopto.org/", 744},
{"https://ipfs.io/", 739},
std::pair<std::string_view, int>{"http://localhost:8080/", 8000},
{"https://jcsl.hopto.org/", 745},
{"https://ipfs.io/", 741},
{"https://gateway.ipfs.io/", 626},
{"https://human.mypinata.cloud/", 342},
{"https://dag.w3s.link/", 216},
{"https://ipfs.runfission.com/", 113},
{"https://ipfs.runfission.com/", 114},
{"https://delegated-ipfs.dev/", 75},
{"https://permaweb.eu.org/", 65},
{"https://permaweb.eu.org/", 66},
{"https://cesginc.com/", 56},
{"https://dweb.link/", 55},
{"https://http.f02620.devtty.eu/", 45},
{"https://f010479.twinquasar.io/", 32},
{"https://ipfs.omnicloudstorage.com:9443/", 25},
{"https://http.f02620.devtty.eu/", 46},
{"https://f010479.twinquasar.io/", 33},
{"https://ipfs.omnicloudstorage.com:9443/", 26},
{"http://f02095132.datasetcreators.com/", 23},
{"https://gateway.pinata.cloud/", 16},
{"https://ipfs.joaoleitao.org/", 15},
{"https://ipfs.joaoleitao.org/", 16},
{"https://data.filstorage.io/", 8},
{"https://nftstorage.link/", 6},
{"https://ipfs.fleek.co/", 5},
{"https://nftstorage.link/", 7},
{"https://ipfs.fleek.co/", 6},
{"https://w3s.link/", 3},
{"https://hardbin.com/", 2},
{"https://jorropo.net/", 1},
Expand Down
13 changes: 13 additions & 0 deletions library/src/ipfs_client/ctx/nlohmann_cbor_parser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <ipfs_client/ctx/nlohmann_cbor_parser.h>

#if HAS_JSON_CBOR_ADAPTER

using Self = ipfs::ctx::NlohmannCborParser;

auto Self::Parse(ipfs::ByteView bytes) -> std::unique_ptr<DagCborValue> {
auto data = nlohmann::json::from_cbor(
bytes, false, true, nlohmann::detail::cbor_tag_handler_t::store);
return std::make_unique<ipfs::JsonCborAdapter>(data);
}

#endif
2 changes: 1 addition & 1 deletion library/src/ipfs_client/ctx/nlohmann_json_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using Self = ipfs::ctx::NlohmannJsonParser;

auto Self::Parse(std::string_view j_str) const
auto Self::Parse(std::string_view j_str)
-> std::unique_ptr<DagJsonValue> {
auto data = nlohmann::json::parse(j_str);
std::ostringstream oss;
Expand Down
17 changes: 4 additions & 13 deletions library/src/ipfs_client/orchestrator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,6 @@ struct MockApi final : public ipfs::ContextApi {
return std::nullopt;
}
void Discover(std::function<void(std::vector<std::string>)> cb) {}
/*
struct DnsInvocation {
std::string host;
DnsTextResultsCallback rcb;
DnsTextCompleteCallback ccb;
};
std::vector<DnsInvocation> expected_dns;
void SendDnsTextRequest(std::string host,
DnsTextResultsCallback rcb,
DnsTextCompleteCallback ccb) {
EXPECT_GE(expected_dns.size(), 1U);
}
*/
std::vector<MockCbor> mutable cbors;
std::unique_ptr<ipfs::DagCborValue> ParseCbor(ByteView) const {
if (cbors.empty()) {
Expand Down Expand Up @@ -126,6 +113,10 @@ struct TestRequestor final : public ig::Requestor {
auto blocs_dir = base_dir / "test_data" / "blocks";
EXPECT_TRUE(is_directory(blocs_dir));
auto f = blocs_dir / cid;
auto canon = i::Cid{cid}.to_string();
if (!is_regular_file(f) && cid != canon) {
f = blocs_dir / canon;
}
EXPECT_TRUE(is_regular_file(f)) << cid << " missing";
if (is_regular_file(f)) {
std::ifstream fs{f};
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 6163de3

Please sign in to comment.