-
Notifications
You must be signed in to change notification settings - Fork 10
/
http_client_request.dart
390 lines (337 loc) · 10.3 KB
/
http_client_request.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2020 terrier989@gmail.com.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:typed_data/typed_buffers.dart';
import '../http/http.dart' show HttpHeadersImpl;
import '../io_impl_js.dart';
import 'http_client.dart';
import 'http_client_exception.dart';
import 'http_client_response.dart';
import 'io_sink_base.dart';
class BrowserHttpClientRequest extends HttpClientRequest with IOSinkBase {
final BrowserHttpClient client;
String? _browserResponseType;
/// Enables [CORS "credentials mode"](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials)
/// in _XHR_ request. Disabled by default.
///
/// # Example
/// ```
/// Future<void> main() async {
/// final client = HttpClient();
/// final request = client.getUrl(Url.parse('http://host/path'));
/// if (request is BrowserHttpClientRequest) {
/// request.browserCredentialsMode = true;
/// }
/// final response = await request.close();
/// // ...
/// }
/// ```
bool browserCredentialsMode = false;
@override
final String method;
@override
final Uri uri;
@override
final HttpHeaders headers = HttpHeadersImpl('1.1');
final Completer<HttpClientResponse> _completer =
Completer<HttpClientResponse>();
Future? _addStreamFuture;
@override
final List<Cookie> cookies = <Cookie>[];
final bool _supportsBody;
@override
Encoding encoding = utf8;
Future<HttpClientResponse>? _result;
final _buffer = Uint8Buffer();
@internal
BrowserHttpClientRequest(this.client, this.method, this.uri)
: _supportsBody = _httpMethodSupportsBody(method) {
// Add "User-Agent" header
final userAgent = client.userAgent;
if (userAgent != null) {
headers.set(HttpHeaders.userAgentHeader, userAgent);
}
// Set default values
browserCredentialsMode = client.browserCredentialsMode;
followRedirects = true;
maxRedirects = 5;
bufferOutput = true;
}
/// Sets _responseType_ in _XHR_ request.
///
/// By default null, which means that the [HttpClientResponse] should contain
/// bytes.
String? get browserResponseType => _browserResponseType;
set browserResponseType(String? value) {
if (value != null) {
const validValues = <String>{
'arraybuffer',
'blob',
'document',
'json',
'text'
};
if (!validValues.contains(value)) {
throw ArgumentError.value(value);
}
}
_browserResponseType = value;
}
@override
HttpConnectionInfo? get connectionInfo => null;
@override
Future<HttpClientResponse> get done {
return _completer.future;
}
@override
void abort([Object? exception, StackTrace? stackTrace]) {}
@override
void add(List<int> event) {
if (!_supportsBody) {
throw StateError('HTTP method $method does not support body');
}
if (_completer.isCompleted) {
throw StateError('StreamSink is closed');
}
if (_addStreamFuture != null) {
throw StateError('StreamSink is bound to a stream');
}
_buffer.addAll(event);
}
@override
void addError(Object error, [StackTrace? stackTrace]) {
if (_completer.isCompleted) {
throw StateError('HTTP request is closed already');
}
_completer.completeError(error, stackTrace);
}
@override
Future<void> addStream(Stream<List<int>> stream) async {
if (_completer.isCompleted) {
throw StateError('StreamSink is closed');
}
if (_addStreamFuture != null) {
throw StateError('StreamSink is bound to a stream');
}
final future = stream.listen((item) {
add(item);
}, onError: (error) {
addError(error);
}, cancelOnError: true).asFuture(null);
_addStreamFuture = future;
await future;
_addStreamFuture = null;
return null;
}
@override
Future<HttpClientResponse> close() async {
return _result ??= _close();
}
@override
Future flush() async {
// Wait for added stream
if (_addStreamFuture != null) {
await _addStreamFuture;
_addStreamFuture = null;
}
}
Future<HttpClientResponse> _close() async {
await flush();
if (cookies.isNotEmpty) {
_completer.completeError(StateError(
'Attempted to send cookies, but XMLHttpRequest does not support them.',
));
return _completer.future;
}
// Callback
if (browserResponseType == null) {
browserResponseType = 'arraybuffer';
final accept = headers.value('Accept');
if (accept != null) {
final isText = isTextContentType(accept);
if (isText) {
browserResponseType = 'text';
}
}
}
final callback = client.onBrowserHttpClientRequestClose;
if (callback != null) {
await Future(() => callback(this));
}
try {
final xhr = html.HttpRequest();
// Set method and URI
final method = this.method;
final uriString = uri.toString();
xhr.open(method, uriString);
// Set response body type
final browserResponseType = this.browserResponseType ?? 'arraybuffer';
xhr.responseType = browserResponseType;
// Timeout
final timeout = client.connectionTimeout;
if (timeout != null) {
xhr.timeout = timeout.inMilliseconds;
}
// Credentials mode?
final browserCredentialsMode = this.browserCredentialsMode;
xhr.withCredentials = browserCredentialsMode;
// Copy headers to html.HttpRequest
final headers = this.headers;
headers.forEach((name, values) {
for (var value in values) {
xhr.setRequestHeader(name, value);
}
});
final headersCompleter = _completer;
final controller = StreamController<Uint8List>();
var bodySeenLength = 0;
BrowserHttpClientResponse? currentHttpClientResponse;
void completeHeaders() {
if (headersCompleter.isCompleted) {
return;
}
// Create HttpClientResponse
final httpClientResponse = BrowserHttpClientResponse(
this,
xhr.status ?? 200,
xhr.statusText ?? 'OK',
controller.stream,
);
currentHttpClientResponse = httpClientResponse;
final headers = httpClientResponse.headers;
xhr.responseHeaders.forEach((name, value) {
headers.add(name, value);
});
// Complete the future
headersCompleter.complete(httpClientResponse);
}
void addChunk() {
currentHttpClientResponse?.browserResponse = xhr.response;
// Close stream
if (!headersCompleter.isCompleted || controller.isClosed) {
return;
}
final body = xhr.response;
if (body == null) {
return;
} else if (body is String) {
final chunk = body.substring(bodySeenLength);
bodySeenLength = body.length;
controller.add(Utf8Encoder().convert(chunk));
} else if (body is ByteBuffer) {
final chunk = Uint8List.view(body, bodySeenLength);
bodySeenLength = body.lengthInBytes;
controller.add(chunk);
} else {
return;
}
}
xhr.onReadyStateChange.listen((event) {
switch (xhr.readyState) {
case html.HttpRequest.HEADERS_RECEIVED:
// Complete future
completeHeaders();
break;
}
});
xhr.onProgress.listen((html.ProgressEvent event) {
addChunk();
});
// ignore: unawaited_futures
xhr.onLoad.first.then((event) {
addChunk();
controller.close();
});
// ignore: unawaited_futures
xhr.onTimeout.first.then((event) {
if (!headersCompleter.isCompleted) {
headersCompleter.completeError(TimeoutException('Timeout'));
} else {
controller.addError(TimeoutException('Timeout'));
controller.close();
}
});
final origin = html.window.origin;
// ignore: unawaited_futures
xhr.onError.first.then((html.ProgressEvent event) {
// The underlying XMLHttpRequest API doesn't expose any specific
// information about the error itself.
//
// We gather the information that we have and try to produce a
// descriptive exception.
final error = BrowserHttpClientException(
method: method,
url: uriString,
origin: origin,
headers: headers,
browserResponseType: browserResponseType,
browserCredentialsMode: browserCredentialsMode,
);
if (!headersCompleter.isCompleted) {
// Complete future
headersCompleter.completeError(error, StackTrace.current);
} else if (!controller.isClosed) {
// Close stream
controller.addError(error);
controller.close();
}
});
final buffer = _buffer;
if (buffer.isNotEmpty) {
// Send with body
xhr.send(Uint8List.fromList(buffer));
} else {
// Send without body
xhr.send();
}
} catch (e) {
// Something went wrong
_completer.completeError(e);
}
return _completer.future;
}
/// Determines whether the content type is text.
static bool isTextContentType(String value) {
final contentType = ContentType.parse(value);
switch (contentType.primaryType) {
case 'application':
switch (contentType.subType) {
case 'grpc-web':
return true;
default:
return false;
}
case 'text':
return true;
default:
return false;
}
}
static bool _httpMethodSupportsBody(String method) {
switch (method) {
case 'GET':
return false;
case 'HEAD':
return false;
case 'OPTIONS':
return false;
default:
return true;
}
}
}