Skip to content

Commit

Permalink
Begin moving http to a helper class.
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Jan 31, 2024
1 parent bd479a7 commit 33700c6
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 1,194 deletions.
1 change: 1 addition & 0 deletions cmake/setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function(with_vocab target)
target_link_libraries(${target}
PUBLIC
OpenSSL::Crypto
OpenSSL::SSL
)
endif()
find_package(nlohmann_json)
Expand Down
6 changes: 3 additions & 3 deletions component/block_http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <ipfs_client/gw/gateway_request.h>

#include <ipfs_client/context_api.h>
#include <ipfs_client/ctx/http_api.h>
#include <vocab/raw_ptr.h>

namespace network {
Expand All @@ -23,7 +23,7 @@ class BlockHttpRequest : public std::enable_shared_from_this<BlockHttpRequest> {
std::unique_ptr<network::SimpleURLLoader> loader_;

public:
using HttpCompleteCallback = ipfs::ContextApi::HttpCompleteCallback;
using HttpCompleteCallback = ctx::HttpApi::HttpCompleteCallback;
BlockHttpRequest(ipfs::HttpRequestDescription, HttpCompleteCallback);
~BlockHttpRequest() noexcept;

Expand All @@ -33,7 +33,7 @@ class BlockHttpRequest : public std::enable_shared_from_this<BlockHttpRequest> {
ipfs::HttpRequestDescription const inf_;
HttpCompleteCallback callback_;
std::string status_line_;
ContextApi::HeaderAccess header_accessor_ = [](auto) {
ctx::HttpApi::HeaderAccess header_accessor_ = [](auto) {
return std::string{};
};

Expand Down
14 changes: 14 additions & 0 deletions component/chromium_http.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "chromium_http.h"

#include "block_http_request.h"

using Self = ipfs::ChromiumHttp;

Self::ChromiumHttp(network::mojom::URLLoaderFactory& lf)
: loader_factory_{&lf} {}

void Self::SendHttpRequest(ipfs::ctx::HttpApi::HttpRequestDescription desc,
ipfs::ctx::HttpApi::HttpCompleteCallback cb) const {
auto ptr = std::make_shared<BlockHttpRequest>(desc, cb);
ptr->send(loader_factory_);
}
23 changes: 23 additions & 0 deletions component/chromium_http.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef IPFS_CHROMIUM_CHROMIUM_HTTP_H
#define IPFS_CHROMIUM_CHROMIUM_HTTP_H

#include <ipfs_client/ctx/http_api.h>

#include <vocab/raw_ptr.h>

namespace network::mojom {
class URLLoaderFactory;
} // namespace network::mojom

namespace ipfs {
class ChromiumHttp : public ctx::HttpApi {
raw_ptr<network::mojom::URLLoaderFactory> loader_factory_ = nullptr;

public:
void SendHttpRequest(HttpRequestDescription,
HttpCompleteCallback cb) const override;
ChromiumHttp(network::mojom::URLLoaderFactory&);
};
} // namespace ipfs

#endif // IPFS_CHROMIUM_CHROMIUM_HTTP_H
12 changes: 3 additions & 9 deletions component/chromium_ipfs_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include "block_http_request.h"
#include "chromium_cbor_adapter.h"
#include "chromium_http.h"
#include "chromium_json_adapter.h"
#include "inter_request_state.h"
#include "ipfs_client/crypto_api.h"
#include "preferences.h"

#include <components/cbor/reader.h>
Expand All @@ -27,8 +27,8 @@

using Self = ipfs::ChromiumIpfsContext;

void Self::SetLoaderFactory(network::mojom::URLLoaderFactory& lf) {
loader_factory_ = &lf;
void Self::SetupHttp(network::mojom::URLLoaderFactory& lf) {
http_api_ = std::make_unique<ChromiumHttp>(lf);
}

std::string Self::MimeType(std::string extension,
Expand Down Expand Up @@ -72,12 +72,6 @@ void Self::SendDnsTextRequest(std::string host,
auto* nc = state_->network_context();
dns_reqs_[host] = std::make_unique<DnsTxtRequest>(host, res, don_wrap, nc);
}
void Self::SendHttpRequest(HttpRequestDescription req_inf,
HttpCompleteCallback cb) const {
DCHECK(loader_factory_);
auto ptr = std::make_shared<BlockHttpRequest>(req_inf, cb);
ptr->send(loader_factory_);
}
auto Self::ParseCbor(ipfs::ContextApi::ByteView bytes) const
-> std::unique_ptr<DagCborValue> {
cbor::Reader::Config cfg;
Expand Down
5 changes: 1 addition & 4 deletions component/chromium_ipfs_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class IpfsRequest;
class NetworkRequestor;

class ChromiumIpfsContext final : public ContextApi {
raw_ptr<network::mojom::URLLoaderFactory> loader_factory_ = nullptr;
raw_ref<InterRequestState> state_;
std::map<std::string, std::unique_ptr<DnsTxtRequest>> dns_reqs_;
GatewayRates rates_;
Expand All @@ -41,8 +40,6 @@ class ChromiumIpfsContext final : public ContextApi {
void SendDnsTextRequest(std::string,
DnsTextResultsCallback,
DnsTextCompleteCallback) override;
void SendHttpRequest(HttpRequestDescription req_inf,
HttpCompleteCallback cb) const override;

std::unique_ptr<DagCborValue> ParseCbor(ByteView) const override;
std::unique_ptr<DagJsonValue> ParseJson(std::string_view) const override;
Expand All @@ -55,7 +52,7 @@ class ChromiumIpfsContext final : public ContextApi {
public:
ChromiumIpfsContext(InterRequestState&, PrefService* prefs);
~ChromiumIpfsContext() noexcept override;
void SetLoaderFactory(network::mojom::URLLoaderFactory&);
void SetupHttp(network::mojom::URLLoaderFactory&);
};

} // namespace ipfs
Expand Down
2 changes: 1 addition & 1 deletion component/ipfs_url_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void ipfs::IpfsUrlLoader::StartRequest(
auto path = resource_request.url.path();
auto abs_path = "/" + ns + "/" + cid_str + path;
me->root_ = cid_str;
me->api_->SetLoaderFactory(*(me->lower_loader_factory_));
me->api_->SetupHttp(*(me->lower_loader_factory_));
auto whendone = [me](IpfsRequest const& req, ipfs::Response const& res) {
VLOG(2) << "whendone(" << req.path().to_string() << ',' << res.status_
<< ',' << res.body_.size() << "B mime=" << res.mime_ << ')';
Expand Down
Loading

0 comments on commit 33700c6

Please sign in to comment.