-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfetch_client.dart
223 lines (195 loc) · 7.17 KB
/
fetch_client.dart
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import 'package:fetch_api/compatibility_layer.dart' as fetch_compatibility_layer;
import 'package:fetch_api/fetch_api.dart';
import 'package:http/http.dart' show BaseClient, BaseRequest, ClientException;
import 'fetch_response.dart';
import 'on_done.dart';
import 'redirect_policy.dart';
/// HTTP client based on Fetch API.
/// It does support streaming and can handle non 200 responses.
///
/// This implementation has some restrictions:
/// * [BaseRequest.persistentConnection] is translated to
/// [FetchOptions.keepalive] (if [streamRequests] is disabled).
/// * [BaseRequest.contentLength] is ignored.
/// * When [BaseRequest.followRedirects] is `true` you can get redirect
/// information via [FetchResponse.redirected] and [FetchResponse.url]).
/// If [BaseRequest.followRedirects] is `false` [redirectPolicy] takes place
/// and dictates [FetchClient] actions.
/// * [BaseRequest.maxRedirects] is ignored.
class FetchClient extends BaseClient {
FetchClient({
this.mode = RequestMode.noCors,
this.credentials = RequestCredentials.sameOrigin,
this.cache = RequestCache.byDefault,
this.referrer = '',
this.referrerPolicy = RequestReferrerPolicy.strictOriginWhenCrossOrigin,
this.integrity = '',
this.redirectPolicy = RedirectPolicy.alwaysFollow,
this.streamRequests = false,
});
/// The mode you want to use for the request
final RequestMode mode;
/// Controls what browsers do with credentials (cookies, HTTP authentication
/// entries, and TLS client certificates).
final RequestCredentials credentials;
/// A string indicating how the request will interact with the browser's
/// HTTP cache.
final RequestCache cache;
/// A string specifying the referrer of the request.
/// This can be a same-origin URL, `about:client`, or an empty string.
final String referrer;
/// Specifies the referrer policy to use for the request.
final RequestReferrerPolicy referrerPolicy;
/// Contains the subresource integrity value of the request
/// (e.g.,`sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=`)
final String integrity;
/// Client redirect policy, defines how client should handle
/// [BaseRequest.followRedirects].
final RedirectPolicy redirectPolicy;
/// Whether to use [ReadableStream] as body for requests streaming.
///
/// **NOTICE**: This feature is supported only in __Chromium 105+__ based browsers and
/// requires server to be HTTP/2 or HTTP/3.
///
/// See [compatibility chart](https://developer.mozilla.org/en-US/docs/Web/API/Request#browser_compatibility)
/// and [Chrome Developers blog](https://developer.chrome.com/articles/fetch-streaming-requests/#doesnt-work-on-http1x)
/// for more info.
final bool streamRequests;
final _abortCallbacks = <void Function()>[];
var _closed = false;
@override
Future<FetchResponse> send(BaseRequest request) async {
if (_closed)
throw ClientException('Client is closed', request.url);
final byteStream = request.finalize();
final dynamic body;
if (['GET', 'HEAD'].contains(request.method.toUpperCase()))
body = null;
else if (streamRequests) {
body = fetch_compatibility_layer.createReadableStream(
fetch_compatibility_layer.createReadableStreamSourceFromStream(
byteStream,
),
);
} else {
final bytes = await byteStream.toBytes();
body = bytes.isEmpty ? null : bytes;
}
final abortController = AbortController();
final init = fetch_compatibility_layer.createFetchOptions(
body: body,
method: request.method,
redirect: (request.followRedirects || redirectPolicy == RedirectPolicy.alwaysFollow)
? RequestRedirect.follow
: RequestRedirect.manual,
headers: fetch_compatibility_layer.createHeadersFromMap(request.headers),
mode: mode,
credentials: credentials,
cache: cache,
referrer: referrer,
referrerPolicy: referrerPolicy,
keepalive: !streamRequests && request.persistentConnection,
signal: abortController.signal,
duplex: !streamRequests ? null : RequestDuplex.half,
);
final Response response;
try {
response = await fetch(request.url.toString(), init);
if (
response.type == 'opaqueredirect' &&
!request.followRedirects &&
redirectPolicy != RedirectPolicy.alwaysFollow
)
return _probeRedirect(
request: request,
initialResponse: response,
init: init,
abortController: abortController,
);
} catch (e) {
throw ClientException('Failed to execute fetch: $e', request.url);
}
if (response.status == 0)
throw ClientException(
'Fetch response status code 0',
request.url,
);
final reader = response.body!.getReader();
late final void Function() abort;
abort = () {
_abortCallbacks.remove(abort);
reader.cancel<dynamic>();
abortController.abort();
};
_abortCallbacks.add(abort);
final stream = onDone(reader.readAsStream(), abort);
final contentLength = response.headers.get('Content-Length');
return FetchResponse(
stream,
response.status,
cancel: abort,
url: response.url,
redirected: response.redirected,
request: request,
headers: {
for (final header in response.headers.entries())
header.first: header.last,
},
isRedirect: false,
persistentConnection: false,
reasonPhrase: response.statusText,
contentLength: contentLength == null ? null
: int.tryParse(contentLength),
);
}
/// Makes probe request and returns "redirect" response.
Future<FetchResponse> _probeRedirect({
required BaseRequest request,
required Response initialResponse,
required FetchOptions init,
required AbortController abortController,
}) async {
init.requestRedirect = RequestRedirect.follow;
if (redirectPolicy == RedirectPolicy.probeHead)
init.method = 'HEAD';
else
init.method = 'GET';
final Response response;
try {
response = await fetch(request.url.toString(), init);
// Cancel before even reading response
if (redirectPolicy == RedirectPolicy.probe)
abortController.abort();
} catch (e) {
throw ClientException('Failed to execute probe fetch: $e', request.url);
}
return FetchResponse(
const Stream.empty(),
302,
cancel: () {},
url: initialResponse.url,
redirected: false,
request: request,
headers: {
for (final header in response.headers.entries())
header.first: header.last,
'location': response.url,
},
isRedirect: true,
persistentConnection: false,
reasonPhrase: 'Found',
contentLength: null,
);
}
/// Closes the client.
///
/// This method also terminates all associated active requests.
@override
void close() {
if (!_closed) {
_closed = true;
for (final abort in _abortCallbacks.toList())
abort();
}
}
}