Skip to content

Commit

Permalink
someut
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Nov 16, 2023
1 parent b40c99a commit 5d63748
Show file tree
Hide file tree
Showing 22 changed files with 280 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/library.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
conan profile path default
grep -n . `conan profile path default`
echo "compiler=apple-clang" >> `conan profile path default`
echo "compiler.cppstd=17" >> `conan profile path default`
echo "compiler.cppstd=20" >> `conan profile path default`
echo "compiler.libcxx=libc++" >> `conan profile path default`
echo "compiler.version=14.0" >> `conan profile path default`
conan profile show
Expand Down
4 changes: 2 additions & 2 deletions component/block_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void Self::OnResponse(std::shared_ptr<Self>,
ContextApi::HeaderAccess header_accessor = [](auto) { return std::string{}; };
auto sz = body ? body->size() : 0UL;
if (loader_->NetError()) {
LOG(WARNING) << "NetErr " << loader_->NetError() << " for " << inf_.url;
LOG(INFO) << "NetErr " << loader_->NetError() << " for " << inf_.url;
}
VLOG(1) << "Handling HTTP response body of size " << sz << " with NetErr "
<< loader_->NetError() << " for " << inf_.url;
Expand All @@ -86,7 +86,7 @@ void Self::OnResponse(std::shared_ptr<Self>,
return val;
};
} else {
LOG(WARNING) << "No response header info?";
LOG(INFO) << "No response header info?";
}
if (body) {
callback_(status, *body, header_accessor);
Expand Down
2 changes: 1 addition & 1 deletion component/ipns_cbor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void Assign(cbor::Value::MapValue const& m, char const* n, V& out) {
return;
}
if (Assign(i->second, out)) {
LOG(INFO) << "Assigned successfully '" << n << "'='" << out << "'.";
VLOG(1) << "Assigned successfully '" << n << "'='" << out << "'.";
} else {
LOG(ERROR) << "Type mismatch on " << n
<< " type received: " << static_cast<int>(i->second.type());
Expand Down
1 change: 1 addition & 0 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ if(GTest_FOUND)
PRIVATE
include/
src/
../test_data/include
)
target_link_libraries(unit_test_runner
PUBLIC
Expand Down
8 changes: 6 additions & 2 deletions library/include/ipfs_client/ipld/dag_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ struct MoreDataNeeded {
std::vector<std::string> ipfs_abs_paths_;
};
enum class ProvenAbsent {};
using NewPath = std::string;
struct PathChange {
std::string new_path;
};

using ResolveResult =
std::variant<MoreDataNeeded, Response, ProvenAbsent, NewPath>;
std::variant<MoreDataNeeded, Response, ProvenAbsent, PathChange>;
/**
* @brief A block, an IPNS record, etc.
*/
Expand Down Expand Up @@ -64,4 +66,6 @@ class DagNode : public std::enable_shared_from_this<DagNode> {
};
} // namespace ipfs::ipld

std::ostream& operator<<(std::ostream&, ipfs::ipld::PathChange const&);

#endif // IPFS_DAG_NODE_H_
2 changes: 1 addition & 1 deletion library/include/libp2p/multi/content_identifier_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ContentIdentifierCodec {
* @return CID
*/
static ipfs::expected<ContentIdentifier, DecodeError> fromString(
const std::string& str);
std::string_view str);
};

std::string_view Stringify(libp2p::multi::ContentIdentifierCodec::DecodeError);
Expand Down
12 changes: 6 additions & 6 deletions library/include/vocab/span.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#ifndef IPFS_SPAN_H_
#define IPFS_SPAN_H_

#if __has_include("base/containers/span.h")
#if __cpp_lib_span
#include <span>

#include "base/containers/span.h"
namespace ipfs {
template <class Value>
using span = base::span<Value>;
using span = std::span<Value>;
} // namespace ipfs

#elif __has_cpp_attribute(__cpp_lib_span)
#elif __has_include("base/containers/span.h")

#include <span>
#include "base/containers/span.h"
namespace ipfs {
template <class Value>
using span = std::span<Value>;
using span = base::span<Value>;
} // namespace ipfs

#elif __has_include(<absl/types/span.h>)
Expand Down
2 changes: 1 addition & 1 deletion library/src/ipfs_client/dag_block_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TEST(BlockTest, AdHoc) {
std::for_each(directory_iterator("."), directory_iterator(), [](auto e) {
if (is_regular_file(e) && e.path().extension() == ".block") {
LOG(INFO) << e.path();
auto cid = Codec ::fromString(e.path().stem()).value();
auto cid = Codec::fromString(e.path().stem().string()).value();
std::ifstream f{e.path()};
std::stringstream buffer;
buffer << f.rdbuf();
Expand Down
9 changes: 4 additions & 5 deletions library/src/ipfs_client/generated_directory_listing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ ipfs::GeneratedDirectoryListing::GeneratedDirectoryListing(
}

void ipfs::GeneratedDirectoryListing::AddEntry(std::string_view name) {
// auto path = base_path_;
// path.append(name);
// AddLink(name, path);
AddLink(name, name);
auto path = base_path_;
path.append(name);
AddLink(name, path);
}
void ipfs::GeneratedDirectoryListing::AddLink(std::string_view name,
std::string_view path) {
LOG(INFO) << "Adding link to generated index.html " << name << '=' << path;
VLOG(1) << "Adding link to generated index.html " << name << '=' << path;
html_.append(" <li>\n")
.append(" <a href='")
.append(path)
Expand Down
9 changes: 4 additions & 5 deletions library/src/ipfs_client/gw/gateway_http_requestor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ auto Self::handle(ipfs::gw::RequestPtr r) -> HandleOutcome {
<< " forwarding request " << r->debug_string()
<< " to next requestor.";
}
}
} else {
LOG(INFO) << r->debug_string() << " got a failure of status " << status
<< " from " << prefix_;
} else {
VLOG(1) << r->debug_string() << " got a failure of status " << status
<< " from " << prefix_;
}
LOG(INFO) << "Demote(" << prefix_ << ')';
aff_bad_.insert(r->affinity);
Expand All @@ -100,7 +99,7 @@ auto Self::handle(ipfs::gw::RequestPtr r) -> HandleOutcome {
DCHECK(api_);
api_->SendHttpRequest(desc.value(), cb);
seen_.insert(req_key);
pending++;
pending_++;
return HandleOutcome::PENDING;
}

Expand Down
37 changes: 2 additions & 35 deletions library/src/ipfs_client/gw/gateway_http_requestor_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <ipfs_client/gw/gateway_http_requestor.h>

#include <gtest/gtest.h>
#include <mock_api.h>

#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/orchestrator.h>
Expand All @@ -12,40 +12,7 @@ using P = ipfs::gw::RequestPtr;
using namespace std::literals;

namespace {
struct FakeApi final : public ipfs::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);
}

void SendDnsTextRequest(std::string hostname,
DnsTextResultsCallback,
DnsTextCompleteCallback) {}
std::string MimeType(std::string extension,
std::string_view content,
std::string const& url) const {
return "";
}
std::string UnescapeUrlComponent(std::string_view url_comp) const {
return "";
}
IpnsCborEntry deserialize_cbor(ByteView) const { return {}; }
bool verify_key_signature(SigningKeyType,
ByteView signature,
ByteView data,
ByteView key_bytes) const {
return true;
}

void Discover(std::function<void(std::vector<std::string>)> cb) {}
std::shared_ptr<ipfs::GatewayRequest> InitiateGatewayRequest(
ipfs::BusyGateway) {
return {};
}
};
struct FakeRtor : public ipfs::gw::Requestor {
std::string_view name() const { return "fake requestor"; }
std::vector<P> requests_forwarded;
Expand All @@ -55,7 +22,7 @@ struct FakeRtor : public ipfs::gw::Requestor {
}
};
struct GatewayHttpRequestorTest : public ::testing::Test {
std::shared_ptr<FakeApi> api = std::make_shared<FakeApi>();
std::shared_ptr<MockApi> api = std::make_shared<MockApi>();
std::shared_ptr<T> t = std::make_shared<T>("scheme://host", api);
std::shared_ptr<FakeRtor> chain = std::make_shared<FakeRtor>();
std::shared_ptr<ipfs::Orchestrator> orc;
Expand Down
46 changes: 46 additions & 0 deletions library/src/ipfs_client/gw/requestor_pool_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <ipfs_client/gw/requestor_pool.h>

#include <ipfs_client/gw/gateway_request.h>

#include <mock_api.h>
#include <mock_requestor.h>

namespace {
struct RequestorPoolTest : public ::testing::Test {
std::vector<std::shared_ptr<MockRequestor>> members;
std::shared_ptr<MockApi> api = std::make_shared<MockApi>();
std::shared_ptr<ig::RequestorPool> tested =
std::make_shared<ig::RequestorPool>();
std::shared_ptr<ig::GatewayRequest> req =
ig::GatewayRequest::fromIpfsPath("/ipns/ipfs.io"sv);
void add() {
auto p = std::make_shared<MockRequestor>();
members.push_back(p);
tested->add(p);
}
void set_api() {
auto p = std::make_shared<MockRequestor>();
p->api(api);
p->or_else(tested);
}
};
} // namespace

TEST_F(RequestorPoolTest, add_with_api_sets_member_api) {
add();
EXPECT_EQ(members.size(), 1U);
set_api();
add();
EXPECT_EQ(members.size(), 2U);
EXPECT_TRUE(members.at(0));
EXPECT_TRUE(members.at(1));
EXPECT_FALSE(members.at(0)->api());
EXPECT_TRUE(members.at(1)->api());
}
TEST_F(RequestorPoolTest, pending_doesnt_stop_parallel_requests) {
add();
add();
members.at(0)->outcomes.push_back(MockRequestor::O::PENDING);
members.at(1)->outcomes.push_back(MockRequestor::O::PENDING);
tested->request(req);
}
6 changes: 5 additions & 1 deletion library/src/ipfs_client/ipld/dag_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ auto Node::as_hamt() -> DirShard* {
}
void Node::set_api(std::shared_ptr<ContextApi> api) {
api_ = api;
}
}

std::ostream& operator<<(std::ostream& s, ipfs::ipld::PathChange const& c) {
return s << "PathChange{" << c.new_path << '}';

Check warning on line 72 in library/src/ipfs_client/ipld/dag_node.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/ipld/dag_node.cc#L71-L72

Added lines #L71 - L72 were not covered by tests
}
10 changes: 6 additions & 4 deletions library/src/ipfs_client/ipld/small_directory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ auto Self::resolve(SlashDelimited path, BlockLookup blu, std::string& to_here)
-> ResolveResult {
if (!path) {
LOG(INFO) << "Directory listing requested.";
GeneratedDirectoryListing index_html{to_here};
SlashDelimited dir_path{to_here};
dir_path.pop_n(2);
GeneratedDirectoryListing index_html{dir_path.to_string()};
for (auto& [name, link] : links_) {
LOG(INFO) << "Listing " << to_here << " encountered " << name << '='
<< link.cid;
VLOG(1) << "Listing " << dir_path.to_string() << " encountered " << name
<< '=' << link.cid;
if (name == "index.html") {
auto& n = node(link, blu);
if (n) {
Expand All @@ -52,7 +54,7 @@ auto Self::resolve(SlashDelimited path, BlockLookup blu, std::string& to_here)
auto match = [&name](auto& l) { return l.first == name; };
auto it = std::find_if(links_.begin(), links_.end(), match);
if (links_.end() == it) {
LOG(INFO) << name << " does not exist in directory.";
LOG(INFO) << name << " does not exist in directory " << to_here;
return ProvenAbsent{};
}
auto& link = it->second;
Expand Down
74 changes: 56 additions & 18 deletions library/src/ipfs_client/ipld/symlink.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
#include "symlink.h"

#include <libp2p/multi/content_identifier_codec.hpp>

#include "log_macros.h"

using Self = ipfs::ipld::Symlink;

Self::Symlink(std::string target) : target_{target} {}
Self::Symlink(std::string target)
: style_{from_target(target)}, target_{target} {}

Self::~Symlink() {}

auto Self::resolve(SlashDelimited path, BlockLookup, std::string& buf)
auto Self::resolve(SlashDelimited path, BlockLookup, std::string& to_here)
-> ResolveResult {
if (target_.at(0) == '/') {
buf.assign(target_);
} else {
if (std::count(buf.begin(), buf.end(), '/') > 1 && buf.back() == '/') {
buf.resize(buf.size() - 1);
}
auto popped = buf.find_last_of('/');
if (popped < buf.size()) {
buf.resize(popped + 1); // include the slash at the end
}
buf.append(target_);
std::string result;
switch (style_) {
case Style::Absolute:
result.assign(target_);
break;
case Style::Relative: {
auto c = to_here.find_last_not_of('/');
c = to_here.find_last_of('/', c);
DCHECK(c != to_here.size()) << to_here;
result.assign(to_here, 0, c + 1).append(target_);
} break;
case Style::FromRoot:
result.assign(SlashDelimited{to_here}.pop_n(2))
.append("/")
.append(target_);
}
if (path) {
if (buf.back() != '/') {
buf.push_back('/');
}
buf.append(path.pop_all());
result.append("/").append(path.pop_all());
}
std::size_t i;
while ((i = result.find("//")) != std::string::npos) {
result.erase(i, 1);
}
DCHECK_GT(result.size(), 0U);
if (result.back() == '/') {
result.resize(result.size() - 1);

Check warning on line 41 in library/src/ipfs_client/ipld/symlink.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/ipld/symlink.cc#L41

Added line #L41 was not covered by tests
}
return PathChange{result};
}

auto Self::from_target(std::string const& target) -> Style {
DCHECK_GT(target.size(), 0U);
if (target.at(0) != '/') {
return Style::Relative;
}
SlashDelimited t{target};
auto ns = t.pop();
if (ns != "ipfs" && ns != "ipns") {
return Style::FromRoot;
}
auto root = t.pop();
using namespace libp2p::multi;
auto cid = ContentIdentifierCodec::fromString(root);
if (!cid.has_value()) {
return Style::FromRoot;

Check warning on line 60 in library/src/ipfs_client/ipld/symlink.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/ipld/symlink.cc#L60

Added line #L60 was not covered by tests
}
if (cid.value().content_type == MulticodecType::Code::LIBP2P_KEY) {
return ns == "ipns" ? Style::Absolute : Style::FromRoot;

Check warning on line 63 in library/src/ipfs_client/ipld/symlink.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/ipld/symlink.cc#L63

Added line #L63 was not covered by tests
} else {
return ns == "ipfs" ? Style::Absolute : Style::FromRoot;
}
return buf;
}
7 changes: 7 additions & 0 deletions library/src/ipfs_client/ipld/symlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@

namespace ipfs::ipld {
class Symlink : public DagNode {
enum class Style {
Relative,
Absolute,
FromRoot,
};
Style const style_;
std::string const target_;

Style from_target(std::string const&);
ResolveResult resolve(SlashDelimited path,
BlockLookup,
std::string& up_to_here) override;
Expand Down
Loading

0 comments on commit 5d63748

Please sign in to comment.