Repository navigation
Expand file tree
/
Copy pathModuleTransport.h
More file actions
146 lines (124 loc) · 5.94 KB
/
Copy pathModuleTransport.h
File metadata and controls
146 lines (124 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef NATIVESCRIPT_COMMON_HTTP_MODULE_TRANSPORT_H_
#define NATIVESCRIPT_COMMON_HTTP_MODULE_TRANSPORT_H_
#include <chrono>
#include <cstddef>
#include <functional>
#include <optional>
#include <string>
#include <vector>
// The seam between the core's HTTP module loader and a platform's HTTP stack.
// The core owns everything about a module fetch except performing one attempt:
// the gate, the request headers, timeouts, retry, deadlines, classification,
// the cache switch and Clear-Site-Data. A transport performs single attempts
// and stores the cache.
namespace nativescript::common::http {
// Which caller an attempt serves. A transport keeps the synchronous fallback
// off the connections the graph walk holds, or a blocking fallback fetch can
// wait behind the very walk that is waiting for it.
enum class FetchPath { kGraphWalk, kSynchronousFallback };
struct RequestHeader {
std::string name;
std::string value;
};
struct AttemptRequest {
// Gated, in NormalizeHttpModuleUrl spelling, never canonicalized.
std::string url;
FetchPath path = FetchPath::kGraphWalk;
// Idle bound for the connect and for each read, always > 0.
std::chrono::milliseconds timeout{0};
// kSynchronousFallback only: how long the caller may be blocked in total. A
// transport that can cancel an attempt stops waiting after this and reports
// `waitExpired`; one that cannot may block longer.
std::chrono::milliseconds waitBound{0};
// Off: read nothing from and write nothing to the cache. `headers` then
// already carries the no-cache pair.
bool useCache = true;
// Sent as given, in order, on every attempt.
std::vector<RequestHeader> headers;
};
struct AttemptOutcome {
// A status line arrived and, for a 2xx, the whole body did too. Everything
// else is a transport error, which the core may retry.
bool arrived = false;
int status = 0;
std::string contentType; // as received
std::string body;
// After redirects; set whenever `arrived`. The core re-checks the gate
// against it and scopes Clear-Site-Data by it.
std::string finalUrl;
// Every Clear-Site-Data header value of the final response, unparsed.
std::vector<std::string> clearSiteData;
// The transport could not form a request for the URL at all. Not retried.
bool malformedUrl = false;
// The attempt was abandoned at `waitBound`. Not retried.
bool waitExpired = false;
// For logs only.
std::string errorDescription;
};
struct CacheOpResult {
bool ok = true;
std::string error; // reader-facing, set when !ok
};
class ModuleTransport {
public:
virtual ~ModuleTransport() = default;
// Short and stable, for diagnostics ("nsurlsession", "okhttp", ...).
virtual const char* Name() const = 0;
// One attempt, blocking the calling thread. Called for every
// kSynchronousFallback attempt, and for graph-walk attempts on the core's
// fetch threads when the transport does not schedule itself.
virtual AttemptOutcome FetchBlocking(const AttemptRequest& request) = 0;
// True when the transport queues and caps its own connections. The core then
// starts graph-walk attempts through FetchAsync; otherwise it runs them on
// its own fetch threads, capped per ModuleFetchHostClass.
virtual bool SchedulesItself() const { return false; }
// One attempt without blocking the caller. `done` runs exactly once, on any
// thread, possibly before FetchAsync returns. It must not throw. The default
// runs FetchBlocking inline.
virtual void FetchAsync(AttemptRequest request,
std::function<void(AttemptOutcome)> done);
// Runs `task` once, on any thread, no sooner than `delay` from now, without
// blocking the caller; false when it cannot, and `task` then never runs.
// Used for the retry delay of a transport that schedules itself. The task
// starts the next attempt and may deliver the fetch's result. The default
// uses one core thread, which sets up nothing for it (no autorelease pool).
virtual bool PostDelayed(std::chrono::milliseconds delay,
std::function<void()> task);
// Cache storage, from any thread. Durable before returning. Attempts already
// in flight may still complete from, or store into, what was cleared.
virtual CacheOpResult ClearAll() = 0;
// May clear more than `origin`.
virtual CacheOpResult ClearOrigin(const std::string& origin) = 0;
// Run on each core fetch thread, before its first attempt and after its
// last.
virtual void OnFetchThreadStart() {}
virtual void OnFetchThreadExit() {}
};
// What the platform knows about the app, read once per process, the first
// time the loader needs any of it.
struct HttpLoaderConfig {
// Debuggable app: the remote-module gate is open and attempts get the long
// timeout.
bool isDebug = false;
// security.allowRemoteModules. A platform that cannot read the whole
// allowlist reports false here rather than enable it with a partial list.
bool allowRemoteModules = false;
// security.remoteModuleAllowlist, raw: see RemoteModuleAllowlistFromConfig.
std::optional<std::vector<std::optional<std::string>>> remoteModuleAllowlist;
// The cache setting before anything changes it: the app config's
// `moduleHttpCache`, overridden by a no-cache launch option.
bool moduleHttpCacheAtStartup = true;
// Fetch thread caps per ModuleFetchHostClass ({loopback, remote}) when the
// transport does not schedule itself; 0 keeps the default.
size_t fetchConcurrency[2] = {0, 0};
};
} // namespace nativescript::common::http
// Implemented by each runtime, alongside ns/Platform.h's functions.
namespace nativescript::common::platform {
http::HttpLoaderConfig ReadHttpLoaderConfig();
// The transport for every attempt. Lives for the rest of the process; it may
// change what it does internally (fall back to another HTTP stack), but the
// same object is returned on every call.
http::ModuleTransport& HttpTransport();
} // namespace nativescript::common::platform
#endif // NATIVESCRIPT_COMMON_HTTP_MODULE_TRANSPORT_H_