ipfs-chromium
gateway_request.h
1 #ifndef IPFS_TRUSTLESS_REQUEST_H_
2 #define IPFS_TRUSTLESS_REQUEST_H_
3 
4 #include "gateway_request_type.h"
5 
6 #include <ipfs_client/ipld/block_source.h>
7 #include <ipfs_client/cid.h>
8 #include <ipfs_client/client.h>
9 
10 #include <vocab/slash_delimited.h>
11 
12 #include <iosfwd>
13 #include <memory>
14 #include <optional>
15 #include <string>
16 #include <unordered_set>
17 
18 namespace ipfs {
19 class Car;
20 class IpfsRequest;
21 class Partition;
22 namespace ipld {
23 class DagNode;
24 }
25 } // namespace ipfs
26 
27 namespace ipfs::gw {
28 class Requestor;
29 
30 std::string_view name(GatewayRequestType);
31 
32 constexpr std::size_t BLOCK_RESPONSE_BUFFER_SIZE = 2UL * 1024UL * 1024UL;
33 constexpr std::size_t CAR_RESPONSE_BUFFER_SIZE = 5UL * 1024UL * 1024UL;
34 
39 class GatewayRequest : public std::enable_shared_from_this<GatewayRequest> {
40  public:
41  using BytesReceivedHook =
42  std::function<void(std::string_view, ByteView, ipld::BlockSource const&)>;
43 
44  private:
45  std::shared_ptr<Partition> orchestrator_;
46  std::vector<BytesReceivedHook> bytes_received_hooks;
47  std::string main_param;
48 
49  void FleshOut(ipld::BlockSource&) const;
50  void AddDnsLink(std::string_view target, bool& success, ipld::BlockSource src);
51  void AddBlock(std::string_view bytes,
52  bool& success,
54  std::shared_ptr<Client> const& api,
55  bool* valid);
56  void AddBlocks(Car& car,
57  std::shared_ptr<Client> const& api,
58  bool& success, bool* valid, ipld::BlockSource src);
59  bool IpnsResponse(ByteView bytes, std::shared_ptr<Client> const& api, bool& success, bool* valid, ipld::BlockSource src);
60 
61  public:
62  GatewayRequestType type = GatewayRequestType::Zombie;
63 
64  // TODO - encapsulate. Hopefully these public data members aren't directly accessed everywhere
65  std::string path;
66  std::shared_ptr<IpfsRequest> dependent;
67  std::optional<Cid> cid;
68  short parallel = 0;
69  std::string affinity;
70  std::unordered_set<std::string> failures;
71 
72  std::string url_suffix() const;
73  std::string_view accept() const;
74  std::string_view identity_data() const;
75  short timeout_seconds() const;
76  bool is_http() const;
77  std::optional<std::size_t> max_response_size() const;
78  std::optional<HttpRequestDescription> describe_http(std::string_view) const;
79  std::string debug_string() const;
80  void orchestrator(std::shared_ptr<Partition> const&);
81  bool cachable() const;
82  std::string_view root_component() const;
83  void root_component(std::string_view);
84 
85  bool RespondSuccessfully(std::string_view,
86  std::shared_ptr<Client> const& api,
88  std::string_view roots = "",
89  bool* valid = nullptr);
90  void Hook(BytesReceivedHook);
91  bool PartiallyRedundant() const;
92  std::string Key() const;
93  bool Finished() const;
94 
95  static std::shared_ptr<GatewayRequest> fromIpfsPath(SlashDelimited ipfs_path);
96 };
97 
98 } // namespace ipfs::gw
99 
100 inline std::ostream& operator<<(std::ostream& s,
101  ipfs::gw::GatewayRequestType t) {
102  return s << name(t);
103 }
104 
105 #endif // IPFS_TRUSTLESS_REQUEST_H_
Definition: car.h:17
Definition: gateway_request.h:39
std::string path
For CAR requests.
Definition: gateway_request.h:65
Definition: slash_delimited.h:15
Definition: block_source.h:12