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:
44  std::function<void(std::string_view, ByteView, ipld::BlockSource const&)>;
45 
46  private:
47  std::optional<Cid> cid_;
48  std::shared_ptr<Partition> orchestrator_;
49  std::vector<BytesReceivedHook> bytes_received_hooks;
50  std::string main_param;
51 
52  void FleshOut(ipld::BlockSource&) const;
53  void AddDnsLink(std::string_view target, bool& success, ipld::BlockSource src);
54  void AddBlock(std::string_view bytes,
55  bool& success,
57  std::shared_ptr<Client> const& api,
58  bool* valid);
59  void AddBlocks(Car& car,
60  std::shared_ptr<Client> const& api,
61  bool& success, bool* valid, ipld::BlockSource src);
62  bool IpnsResponse(ByteView bytes, std::shared_ptr<Client> const& api, bool& success, bool* valid, ipld::BlockSource src);
63 
64  public:
65  GatewayRequestType type = GatewayRequestType::Zombie;
66 
67  // TODO - encapsulate. Hopefully these public data members aren't directly accessed everywhere
68  std::string path;
69  std::shared_ptr<IpfsRequest> dependent;
70  short parallel = 0;
71  std::string affinity;
72  std::unordered_set<std::string> failures;
73 
74  std::optional<Cid> const& cid() const;
75  void cid(Cid c);
76  std::string url_suffix() const;
77  std::string_view accept() const;
78  std::string_view identity_data() const;
79  short timeout_seconds() const;
80  bool is_http() const;
81  std::optional<std::size_t> max_response_size() const;
82  std::optional<HttpRequestDescription> describe_http(std::string_view) const;
83  std::string debug_string() const;
84  void orchestrator(std::shared_ptr<Partition> const&);
85  bool cachable() const;
86 
89  std::string_view root_component() const;
90  void root_component(std::string_view);
91 
92  bool RespondSuccessfully(std::string_view,
93  std::shared_ptr<Client> const& api,
95  std::string_view roots = "",
96  bool* valid = nullptr);
97  void Hook(BytesReceivedHook);
98  bool PartiallyRedundant() const;
99  std::string Key() const;
100  bool Finished() const;
101 
102  static std::shared_ptr<GatewayRequest> fromIpfsPath(SlashDelimited ipfs_path);
103 };
104 
105 } // namespace ipfs::gw
106 
107 inline std::ostream& operator<<(std::ostream& s,
108  ipfs::gw::GatewayRequestType t) {
109  return s << name(t);
110 }
111 
112 #endif // IPFS_TRUSTLESS_REQUEST_H_
Definition: car.h:17
Definition: cid.h:16
Definition: gateway_request.h:39
std::string_view root_component() const
std::function< void(std::string_view, ByteView, ipld::BlockSource const &)> BytesReceivedHook
Definition: gateway_request.h:44
std::string path
For CAR requests.
Definition: gateway_request.h:68
Definition: slash_delimited.h:15
Definition: block_source.h:12