|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -import 'dart:async'; |
6 |
| -import 'dart:html'; |
7 |
| -import 'dart:typed_data'; |
8 |
| - |
9 |
| -import 'src/base_client.dart'; |
10 |
| -import 'src/base_request.dart'; |
11 |
| -import 'src/byte_stream.dart'; |
12 |
| -import 'src/exception.dart'; |
13 |
| -import 'src/streamed_response.dart'; |
14 |
| - |
15 |
| -// TODO(nweiz): Move this under src/, re-export from lib/http.dart, and use this |
16 |
| -// automatically from [new Client] once sdk#24581 is fixed. |
17 |
| - |
18 |
| -/// A `dart:html`-based HTTP client that runs in the browser and is backed by |
19 |
| -/// XMLHttpRequests. |
20 |
| -/// |
21 |
| -/// This client inherits some of the limitations of XMLHttpRequest. It ignores |
22 |
| -/// the [BaseRequest.contentLength], [BaseRequest.persistentConnection], |
23 |
| -/// [BaseRequest.followRedirects], and [BaseRequest.maxRedirects] fields. It is |
24 |
| -/// also unable to stream requests or responses; a request will only be sent and |
25 |
| -/// a response will only be returned once all the data is available. |
26 |
| -class BrowserClient extends BaseClient { |
27 |
| - /// The currently active XHRs. |
28 |
| - /// |
29 |
| - /// These are aborted if the client is closed. |
30 |
| - final _xhrs = new Set<HttpRequest>(); |
31 |
| - |
32 |
| - /// Creates a new HTTP client. |
33 |
| - BrowserClient(); |
34 |
| - |
35 |
| - /// Whether to send credentials such as cookies or authorization headers for |
36 |
| - /// cross-site requests. |
37 |
| - /// |
38 |
| - /// Defaults to `false`. |
39 |
| - bool withCredentials = false; |
40 |
| - |
41 |
| - /// Sends an HTTP request and asynchronously returns the response. |
42 |
| - Future<StreamedResponse> send(BaseRequest request) async { |
43 |
| - var bytes = await request.finalize().toBytes(); |
44 |
| - var xhr = new HttpRequest(); |
45 |
| - _xhrs.add(xhr); |
46 |
| - _openHttpRequest(xhr, request.method, request.url.toString(), asynch: true); |
47 |
| - xhr.responseType = 'blob'; |
48 |
| - xhr.withCredentials = withCredentials; |
49 |
| - request.headers.forEach(xhr.setRequestHeader); |
50 |
| - |
51 |
| - var completer = new Completer<StreamedResponse>(); |
52 |
| - xhr.onLoad.first.then((_) { |
53 |
| - // TODO(nweiz): Set the response type to "arraybuffer" when issue 18542 |
54 |
| - // is fixed. |
55 |
| - var blob = xhr.response == null ? new Blob([]) : xhr.response; |
56 |
| - var reader = new FileReader(); |
57 |
| - |
58 |
| - reader.onLoad.first.then((_) { |
59 |
| - var body = reader.result as Uint8List; |
60 |
| - completer.complete(new StreamedResponse( |
61 |
| - new ByteStream.fromBytes(body), xhr.status, |
62 |
| - contentLength: body.length, |
63 |
| - request: request, |
64 |
| - headers: xhr.responseHeaders, |
65 |
| - reasonPhrase: xhr.statusText)); |
66 |
| - }); |
67 |
| - |
68 |
| - reader.onError.first.then((error) { |
69 |
| - completer.completeError( |
70 |
| - new ClientException(error.toString(), request.url), |
71 |
| - StackTrace.current); |
72 |
| - }); |
73 |
| - |
74 |
| - reader.readAsArrayBuffer(blob); |
75 |
| - }); |
76 |
| - |
77 |
| - xhr.onError.first.then((_) { |
78 |
| - // Unfortunately, the underlying XMLHttpRequest API doesn't expose any |
79 |
| - // specific information about the error itself. |
80 |
| - completer.completeError( |
81 |
| - new ClientException("XMLHttpRequest error.", request.url), |
82 |
| - StackTrace.current); |
83 |
| - }); |
84 |
| - |
85 |
| - xhr.send(bytes); |
86 |
| - |
87 |
| - try { |
88 |
| - return await completer.future; |
89 |
| - } finally { |
90 |
| - _xhrs.remove(xhr); |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - // TODO(nweiz): Remove this when sdk#24637 is fixed. |
95 |
| - void _openHttpRequest(HttpRequest request, String method, String url, |
96 |
| - {bool asynch, String user, String password}) { |
97 |
| - request.open(method, url, async: asynch, user: user, password: password); |
98 |
| - } |
99 |
| - |
100 |
| - /// Closes the client. |
101 |
| - /// |
102 |
| - /// This terminates all active requests. |
103 |
| - void close() { |
104 |
| - for (var xhr in _xhrs) { |
105 |
| - xhr.abort(); |
106 |
| - } |
107 |
| - } |
108 |
| -} |
| 5 | +export 'src/browser_client.dart' show BrowserClient; |
0 commit comments