-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
64 changed files
with
2,244 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "block_http_request.h" | ||
|
||
#include <services/network/public/cpp/resource_request.h> | ||
#include <services/network/public/cpp/simple_url_loader.h> | ||
#include <services/network/public/mojom/url_response_head.mojom.h> | ||
|
||
using Self = ipfs::BlockHttpRequest; | ||
|
||
namespace { | ||
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation = | ||
net::DefineNetworkTrafficAnnotation("ipfs_gateway_request", R"( | ||
semantics { | ||
sender: "IPFS component" | ||
description: | ||
"Sends a request to an IPFS gateway." | ||
trigger: | ||
"Processing of an ipfs:// or ipns:// URL." | ||
data: "None" | ||
destination: WEBSITE | ||
} | ||
policy { | ||
cookies_allowed: NO | ||
setting: "EnableIpfs" | ||
} | ||
)"); | ||
} | ||
|
||
Self::BlockHttpRequest(ipfs::HttpRequestDescription req_inf, | ||
HttpCompleteCallback cb, | ||
raw_ptr<network::mojom::URLLoaderFactory> loader_factory) | ||
: callback_{cb} { | ||
auto req = std::make_unique<network::ResourceRequest>(); | ||
req->url = GURL{req_inf.url}; | ||
req->priority = net::HIGHEST; // TODO | ||
if (!req_inf.accept.empty()) { | ||
req->headers.SetHeader("Accept", req_inf.accept); | ||
} | ||
using L = network::SimpleURLLoader; | ||
loader_ = L::Create(std::move(req), kTrafficAnnotation, FROM_HERE); | ||
loader_->SetTimeoutDuration(base::Seconds(req_inf.timeout_seconds)); | ||
loader_->SetAllowHttpErrorResults(true); | ||
auto bound = base::BindOnce(&Self::OnResponse, base::Unretained(this)); | ||
DCHECK(loader_factory); | ||
loader_->DownloadToString(loader_factory, std::move(bound), | ||
gw::BLOCK_RESPONSE_BUFFER_SIZE); | ||
} | ||
Self::~BlockHttpRequest() noexcept {} | ||
void Self::OnResponse(std::unique_ptr<std::string> body) { | ||
auto const* head = loader_->ResponseInfo(); | ||
auto status_text = head->headers->GetStatusText(); | ||
LOG(INFO) << "Handling body of size " << body->size() << " and status of " | ||
<< status_text; | ||
int status = std::atoi(status_text.c_str()); | ||
auto hdrs = [head](std::string_view k) { | ||
std::string val; | ||
head->headers->EnumerateHeader(nullptr, k, &val); | ||
return val; | ||
}; | ||
callback_(status, *body, hdrs); | ||
} | ||
void Self::self_ownership(std::unique_ptr<BlockHttpRequest>& p) { | ||
self_.swap(p); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef IPFS_BLOCK_HTTP_REQUEST_H_ | ||
#define IPFS_BLOCK_HTTP_REQUEST_H_ | ||
|
||
#include <ipfs_client/gw/gateway_request.h> | ||
|
||
#include <ipfs_client/context_api.h> | ||
|
||
namespace network { | ||
struct ResourceRequest; | ||
class SimpleURLLoader; | ||
} // namespace network | ||
namespace network::mojom { | ||
class URLLoaderFactory; | ||
} | ||
|
||
namespace ipfs { | ||
class BlockHttpRequest { | ||
// TODO ween oneself off of SimpleURLLoader | ||
// std::array<char, gw::BLOCK_RESPONSE_BUFFER_SIZE> buffer_; | ||
std::unique_ptr<BlockHttpRequest> self_; | ||
std::unique_ptr<network::SimpleURLLoader> loader_; | ||
|
||
public: | ||
using HttpCompleteCallback = ipfs::ContextApi::HttpCompleteCallback; | ||
BlockHttpRequest(ipfs::HttpRequestDescription, | ||
HttpCompleteCallback, | ||
raw_ptr<network::mojom::URLLoaderFactory>); | ||
~BlockHttpRequest() noexcept; | ||
void self_ownership(std::unique_ptr<BlockHttpRequest>&); | ||
|
||
private: | ||
HttpCompleteCallback callback_; | ||
|
||
void OnResponse(std::unique_ptr<std::string> body); | ||
}; | ||
} // namespace ipfs | ||
|
||
#endif // IPFS_BLOCK_HTTP_REQUEST_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "dns_txt_request.h" | ||
|
||
#include <services/network/network_context.h> | ||
#include <services/network/public/mojom/host_resolver.mojom.h> | ||
|
||
namespace moj = network::mojom; | ||
using Self = ipfs::DnsTxtRequest; | ||
|
||
Self::DnsTxtRequest(std::string host, | ||
ipfs::ContextApi::DnsTextResultsCallback res, | ||
ipfs::ContextApi::DnsTextCompleteCallback don, | ||
moj::NetworkContext* network_context) | ||
: results_callback_{res}, completion_callback_{don} { | ||
auto params = moj::ResolveHostParameters::New(); | ||
params->dns_query_type = net::DnsQueryType::TXT; | ||
params->initial_priority = net::RequestPriority::HIGHEST; | ||
params->source = net::HostResolverSource::ANY; | ||
params->cache_usage = moj::ResolveHostParameters_CacheUsage::STALE_ALLOWED; | ||
params->secure_dns_policy = moj::SecureDnsPolicy::ALLOW; | ||
params->purpose = moj::ResolveHostParameters::Purpose::kUnspecified; | ||
LOG(INFO) << "Querying DNS for TXT records on " << host; | ||
auto hrh = moj::HostResolverHost::NewHostPortPair({host, 0}); | ||
auto nak = net::NetworkAnonymizationKey::CreateTransient(); | ||
network_context->ResolveHost(std::move(hrh), nak, std::move(params), | ||
recv_.BindNewPipeAndPassRemote()); | ||
} | ||
Self::~DnsTxtRequest() {} | ||
|
||
void Self::OnTextResults(std::vector<std::string> const& results) { | ||
results_callback_(results); | ||
} | ||
void Self::OnComplete(int32_t, | ||
const ::net::ResolveErrorInfo&, | ||
const absl::optional<::net::AddressList>&, | ||
const absl::optional<Endpoints>&) { | ||
completion_callback_(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef IPFS_DNS_TXT_REQUEST_H_ | ||
#define IPFS_DNS_TXT_REQUEST_H_ | ||
|
||
#include <ipfs_client/context_api.h> | ||
|
||
#include <mojo/public/cpp/bindings/receiver.h> | ||
#include <services/network/public/cpp/resolve_host_client_base.h> | ||
|
||
namespace network::mojom { | ||
class NetworkContext; | ||
} | ||
|
||
namespace ipfs { | ||
class DnsTxtRequest final : public network::ResolveHostClientBase { | ||
ipfs::ContextApi::DnsTextResultsCallback results_callback_; | ||
ipfs::ContextApi::DnsTextCompleteCallback completion_callback_; | ||
mojo::Receiver<network::mojom::ResolveHostClient> recv_{this}; | ||
|
||
using Endpoints = std::vector<::net::HostResolverEndpointResult>; | ||
void OnTextResults(std::vector<std::string> const&) override; | ||
void OnComplete(int32_t result, | ||
::net::ResolveErrorInfo const&, | ||
absl::optional<::net::AddressList> const&, | ||
absl::optional<Endpoints> const&) override; | ||
|
||
public: | ||
DnsTxtRequest(std::string, | ||
ipfs::ContextApi::DnsTextResultsCallback, | ||
ipfs::ContextApi::DnsTextCompleteCallback, | ||
network::mojom::NetworkContext*); | ||
DnsTxtRequest(DnsTxtRequest&&) = delete; | ||
~DnsTxtRequest() noexcept override; | ||
}; | ||
} // namespace ipfs | ||
|
||
#endif // IPFS_DNS_TXT_REQUEST_H_ |
Oops, something went wrong.