ipfs-chromium
gateway_request.h
1 #ifndef IPFS_TRUSTLESS_REQUEST_H_
2 #define IPFS_TRUSTLESS_REQUEST_H_
3 
4 #include <ipfs_client/ipld/block_source.h>
5 
6 #include <ipfs_client/cid.h>
7 #include <ipfs_client/client.h>
8 
9 #include <vocab/flat_mapset.h>
10 #include <vocab/slash_delimited.h>
11 
12 #include <iosfwd>
13 #include <memory>
14 #include <optional>
15 #include <string>
16 #include "gateway_request_type.h"
17 
18 namespace ipfs {
19 class IpfsRequest;
20 class Partition;
21 namespace ipld {
22 class DagNode;
23 }
24 } // namespace ipfs
25 
26 namespace ipfs::gw {
27 class Requestor;
28 
29 std::string_view name(GatewayRequestType);
30 
31 constexpr std::size_t BLOCK_RESPONSE_BUFFER_SIZE = 2 * 1024 * 1024;
32 
33 // TODO this class has gotten large enough that it should probably be a base class
34  // with subclasses instead of switch statements on type
35 class GatewayRequest : public std::enable_shared_from_this<GatewayRequest> {
36  public:
37  using BytesReceivedHook =
38  std::function<void(std::string_view, ByteView, ipld::BlockSource const&)>;
39 
40  private:
41  std::shared_ptr<Partition> orchestrator_;
42  std::vector<BytesReceivedHook> bytes_received_hooks;
43 
44  void FleshOut(ipld::BlockSource&) const;
45  void AddDnsLink(std::string_view target, bool& success, ipld::BlockSource src);
46  void AddBlock(std::string_view bytes,
47  bool& success,
49  std::shared_ptr<Client> const& api,
50  bool* valid);
51 
52  public:
53  GatewayRequestType type = GatewayRequestType::Zombie;
54 
55  // TODO - encapsulate. It's not that these public data members are directly accessed everywhere
56  std::string main_param;
57  std::string path;
58  std::shared_ptr<IpfsRequest> dependent;
59  std::optional<Cid> cid;
60  short parallel = 0;
61  std::string affinity;
62  flat_set<std::string> failures;
63 
64  std::string url_suffix() const;
65  std::string_view accept() const;
66  std::string_view identity_data() const;
67  short timeout_seconds() const;
68  bool is_http() const;
69  std::optional<std::size_t> max_response_size() const;
70  std::optional<HttpRequestDescription> describe_http(std::string_view) const;
71  std::string debug_string() const;
72  void orchestrator(std::shared_ptr<Partition> const&);
73  bool cachable() const;
74 
75  bool RespondSuccessfully(std::string_view,
76  std::shared_ptr<Client> const& api,
78  std::string_view roots = "",
79  bool* valid = nullptr);
80  void Hook(BytesReceivedHook);
81  bool PartiallyRedundant() const;
82  std::string Key() const;
83  bool Finished() const;
84 
85  static std::shared_ptr<GatewayRequest> fromIpfsPath(SlashDelimited);
86 };
87 
88 } // namespace ipfs::gw
89 
90 inline std::ostream& operator<<(std::ostream& s,
91  ipfs::gw::GatewayRequestType t) {
92  return s << name(t);
93 }
94 
95 #endif // IPFS_TRUSTLESS_REQUEST_H_
Definition: gateway_request.h:35
std::string path
For CAR requests.
Definition: gateway_request.h:57
std::string main_param
CID, IPNS name, hostname.
Definition: gateway_request.h:56
Definition: slash_delimited.h:15
Definition: block_source.h:12