ipfs-chromium
client.h
1 #ifndef IPFS_CONTEXT_API_H_
2 #define IPFS_CONTEXT_API_H_
3 
4 #include "crypto/hasher.h"
5 #include "crypto/signature_verifier.h"
6 #include "crypto/signing_key_type.h"
7 #include "ctx/cbor_parser.h"
8 #include "ctx/dns_txt_lookup.h"
9 #include "ctx/gateway_config.h"
10 #include "ctx/http_api.h"
11 #include "ctx/json_parser.h"
12 #include "dag_cbor_value.h"
13 #include "gw/requestor.h"
14 #include "http_request_description.h"
15 #include "ipns_cbor_entry.h"
16 #include "multi_hash.h"
17 
18 #include <vocab/byte_view.h>
19 
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 #include <optional>
24 #include <unordered_map>
25 
26 namespace boost::asio {
27 class io_context;
28 }
29 
30 namespace ipfs {
31 class Partition;
32 class IpfsRequest;
33 class DagJsonValue;
34 class DagCborValue;
35 
42 class Client : public std::enable_shared_from_this<Client> {
43  public:
44  using SigningKeyType = ::ipfs::crypto::SigningKeyType;
45  using MimeTypeDeduction = std::function<
46  std::string(std::string, std::string_view, std::string const&)>;
47  using UrlUnescaping = std::function<std::string(std::string_view)>;
48  using DnslinkFallbackSwitch = std::function<bool()>;
49 
50  Client();
51  virtual ~Client() noexcept {}
52 
53  std::shared_ptr<Partition> partition(std::string key);
54  ctx::HttpApi& http();
55  ctx::DnsTxtLookup& dns_txt();
56  ctx::GatewayConfig& gw_cfg();
57  ctx::JsonParser& json();
58  ctx::CborParser& cbor();
59  std::shared_ptr<gw::Requestor> requestor();
60 
61  Client& with(std::unique_ptr<ctx::HttpApi>);
62  Client& with(std::unique_ptr<ctx::DnsTxtLookup>);
63  Client& with(std::unique_ptr<ctx::GatewayConfig>);
64  Client& with(std::unique_ptr<ctx::JsonParser>);
65  Client& with(std::unique_ptr<ctx::CborParser>);
66  Client& with(std::shared_ptr<gw::Requestor>);
67  Client& with(SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>);
68  Client& with(MimeTypeDeduction);
69  Client& with(UrlUnescaping);
70  Client& with(DnslinkFallbackSwitch);
71 
78  std::string MimeType(std::string extension,
79  std::string_view content,
80  std::string const& url);
81 
88  std::string UnescapeUrlComponent(std::string_view url_comp);
89 
90  using ByteView = ::ipfs::ByteView;
91  bool VerifyKeySignature(SigningKeyType,
92  ByteView signature,
93  ByteView data,
94  ByteView key_bytes) const;
95 
96  std::optional<std::vector<Byte>> Hash(HashType, ByteView data);
97 
98  bool DnslinkFallback() const;
99 
100  protected:
101  std::unordered_map<HashType, std::unique_ptr<crypto::Hasher>> hashers_;
102  std::unordered_map<SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>>
103  verifiers_;
104  std::unique_ptr<ctx::HttpApi> http_api_;
105  std::unique_ptr<ctx::DnsTxtLookup> dns_txt_;
106  std::unique_ptr<ctx::GatewayConfig> gateway_config_;
107  std::unique_ptr<ctx::JsonParser> json_parser_;
108  std::unique_ptr<ctx::CborParser> cbor_parser_;
109  MimeTypeDeduction deduce_mime_type_;
110  UrlUnescaping unescape_;
111  std::shared_ptr<gw::Requestor> rtor_;
112  std::unordered_map<std::string, std::shared_ptr<Partition>> partitions_;
113  DnslinkFallbackSwitch dns_fb_;
114 };
115 
116 } // namespace ipfs
117 
118 #endif
Interface that provides functionality from whatever environment you're using this library in.
Definition: client.h:42
std::string MimeType(std::string extension, std::string_view content, std::string const &url)
Determine a mime type for a given file.
std::string UnescapeUrlComponent(std::string_view url_comp)
Remove URL escaping, e.g. %20.
Definition: cbor_parser.h:15
Definition: dns_txt_lookup.h:8
Definition: gateway_config.h:11
Definition: http_api.h:8
Definition: json_parser.h:12