Skip to content

Commit

Permalink
Factoring out DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Feb 2, 2024
1 parent 39b1db5 commit b3a2cc7
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 1,049 deletions.
23 changes: 23 additions & 0 deletions component/chromium_dns_txt_lookup.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "chromium_dns_txt_lookup.h"

#include "inter_request_state.h"

using Self = ipfs::ChromiumDnsTxtLookup;

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.";
}
auto don_wrap = [don, this, host]() {
don();
VLOG(2) << "Finished resolving " << host << " via DNSLink";
dns_reqs_.erase(host);
};
auto* nc = state_->network_context();
dns_reqs_[host] = std::make_unique<DnsTxtRequest>(host, res, don_wrap, nc);
}
Self::ChromiumDnsTxtLookup(InterRequestState& st) : state_{&st} {}
Self::~ChromiumDnsTxtLookup() {}
24 changes: 24 additions & 0 deletions component/chromium_dns_txt_lookup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef IPFS_CHROMIUM_CHROMIUM_DNS_TXT_LOOKUP_H
#define IPFS_CHROMIUM_CHROMIUM_DNS_TXT_LOOKUP_H

#include "dns_txt_request.h"

#include <ipfs_client/ctx/dns_txt_lookup.h>

namespace ipfs {
class InterRequestState;
class ChromiumDnsTxtLookup : public ctx::DnsTxtLookup {
raw_ptr<InterRequestState> state_;
std::map<std::string, std::unique_ptr<DnsTxtRequest>> dns_reqs_;

void SendDnsTextRequest(std::string,
DnsTextResultsCallback,
DnsTextCompleteCallback) override;

public:
ChromiumDnsTxtLookup(InterRequestState&);
~ChromiumDnsTxtLookup() noexcept override;
};
} // namespace ipfs

#endif // IPFS_CHROMIUM_CHROMIUM_DNS_TXT_LOOKUP_H
21 changes: 4 additions & 17 deletions component/chromium_ipfs_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include "block_http_request.h"
#include "chromium_cbor_adapter.h"
#include "chromium_dns_txt_lookup.h"
#include "chromium_http.h"
#include "chromium_json_adapter.h"
#include "inter_request_state.h"
#include "preferences.h"

#include <components/cbor/reader.h>
#include <services/network/public/cpp/simple_url_loader.h>
Expand Down Expand Up @@ -57,21 +57,6 @@ std::string Self::UnescapeUrlComponent(std::string_view comp) const {
auto result = base::UnescapeURLComponent({comp.data(), comp.size()}, rules);
return result;
}
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.";
}
auto don_wrap = [don, this, host]() {
don();
VLOG(2) << "Finished resolving " << host << " via DNSLink";
dns_reqs_.erase(host);
};
auto* nc = state_->network_context();
dns_reqs_[host] = std::make_unique<DnsTxtRequest>(host, res, don_wrap, nc);
}
auto Self::ParseCbor(ipfs::ContextApi::ByteView bytes) const
-> std::unique_ptr<DagCborValue> {
cbor::Reader::Config cfg;
Expand Down Expand Up @@ -109,7 +94,9 @@ void Self ::AddGateway(std::string_view prefix) {
}

Self::ChromiumIpfsContext(InterRequestState& state, PrefService* prefs)
: state_{state}, rates_{prefs} {}
: rates_{prefs} {
with(std::make_unique<ChromiumDnsTxtLookup>(state));
}
Self::~ChromiumIpfsContext() noexcept {
LOG(WARNING) << "API dtor - are all URIs loaded?";
}
Expand Down
5 changes: 0 additions & 5 deletions component/chromium_ipfs_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ class IpfsRequest;
class NetworkRequestor;

class ChromiumIpfsContext final : public ContextApi {
raw_ref<InterRequestState> state_;
std::map<std::string, std::unique_ptr<DnsTxtRequest>> dns_reqs_;
GatewayRates rates_;

std::string MimeType(std::string extension,
std::string_view content,
std::string const& url) const override;
std::string UnescapeUrlComponent(std::string_view) const override;
void SendDnsTextRequest(std::string,
DnsTextResultsCallback,
DnsTextCompleteCallback) override;

std::unique_ptr<DagCborValue> ParseCbor(ByteView) const override;
std::unique_ptr<DagJsonValue> ParseJson(std::string_view) const override;
Expand Down
4 changes: 2 additions & 2 deletions component/dns_txt_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace moj = network::mojom;
using Self = ipfs::DnsTxtRequest;

Self::DnsTxtRequest(std::string host,
ipfs::ContextApi::DnsTextResultsCallback res,
ipfs::ContextApi::DnsTextCompleteCallback don,
DnsTextResultsCallback res,
DnsTextCompleteCallback don,
moj::NetworkContext* network_context)
: results_callback_{res}, completion_callback_{don} {
auto params = moj::ResolveHostParameters::New();
Expand Down
13 changes: 9 additions & 4 deletions component/dns_txt_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class NetworkContext;

namespace ipfs {
class DnsTxtRequest final : public network::ResolveHostClientBase {
ipfs::ContextApi::DnsTextResultsCallback results_callback_;
ipfs::ContextApi::DnsTextCompleteCallback completion_callback_;
public:
using DnsTextResultsCallback = ctx::DnsTxtLookup::DnsTextResultsCallback;
using DnsTextCompleteCallback = ctx::DnsTxtLookup::DnsTextCompleteCallback;

private:
DnsTextResultsCallback results_callback_;
DnsTextCompleteCallback completion_callback_;
mojo::Receiver<network::mojom::ResolveHostClient> recv_{this};

using Endpoints = std::vector<::net::HostResolverEndpointResult>;
Expand All @@ -25,8 +30,8 @@ class DnsTxtRequest final : public network::ResolveHostClientBase {

public:
DnsTxtRequest(std::string,
ipfs::ContextApi::DnsTextResultsCallback,
ipfs::ContextApi::DnsTextCompleteCallback,
DnsTextResultsCallback,
DnsTextCompleteCallback,
network::mojom::NetworkContext*);
DnsTxtRequest(DnsTxtRequest&&) = delete;
~DnsTxtRequest() noexcept override;
Expand Down
Loading

0 comments on commit b3a2cc7

Please sign in to comment.