Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(808)

Side by Side Diff: pkg/http/lib/browser_client.dart

Issue 261763002: Rip out the last dart:io dependency from pkg/http. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http/README.md ('k') | pkg/http/lib/src/byte_stream.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library http.browser_client;
6
7 import 'dart:async';
8 import 'dart:html';
9 import 'dart:typed_data';
10
11 import 'package:stack_trace/stack_trace.dart';
12
13 import 'src/base_client.dart';
14 import 'src/base_request.dart';
15 import 'src/byte_stream.dart';
16 import 'src/exception.dart';
17 import 'src/streamed_response.dart';
18
19 // TODO(nweiz): Move this under src/, re-export from lib/http.dart, and use this
20 // automatically from [new Client] once we can create an HttpRequest using
21 // mirrors on dart2js (issue 18541) and dart2js doesn't crash on pkg/collection
22 // (issue 18535).
23
24 /// A `dart:html`-based HTTP client that runs in the browser and is backed by
25 /// XMLHttpRequests.
26 ///
27 /// This client inherits some of the limitations of XMLHttpRequest. It ignores
28 /// the [BaseRequest.contentLength], [BaseRequest.persistentConnection],
29 /// [BaseRequest.followRedirects], and [BaseRequest.maxRedirects] fields. It is
30 /// also unable to stream requests or responses; a request will only be sent and
31 /// a response will only be returned once all the data is available.
32 class BrowserClient extends BaseClient {
33 /// The currently active XHRs.
34 ///
35 /// These are aborted if the client is closed.
36 final _xhrs = new Set<HttpRequest>();
37
38 /// Creates a new HTTP client.
39 BrowserClient();
40
41 /// Sends an HTTP request and asynchronously returns the response.
42 Future<StreamedResponse> send(BaseRequest request) {
43 return request.finalize().toBytes().then((bytes) {
44 var xhr = new HttpRequest();
45 _xhrs.add(xhr);
46 xhr.open(request.method, request.url.toString(), async: true);
47 xhr.responseType = 'blob';
48 request.headers.forEach(xhr.setRequestHeader);
49
50 var completer = new Completer();
51 xhr.onLoad.first.then((_) {
52 // TODO(nweiz): Set the response type to "arraybuffer" when issue 18542
53 // is fixed.
54 var blob = xhr.response == null ? new Blob([]) : xhr.response;
55 var reader = new FileReader();
56
57 reader.onLoad.first.then((_) {
58 var body = reader.result;
59 completer.complete(new StreamedResponse(
60 new ByteStream.fromBytes(body),
61 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.complete(
70 new ClientException(error.toString(), request.url),
71 new Chain.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 new Chain.current());
83 });
84
85 xhr.send(bytes);
86 return completer.future.whenComplete(() => _xhrs.remove(xhr));
87 });
88 }
89
90 /// Closes the client.
91 ///
92 /// This terminates all active requests.
93 void close() {
94 for (var xhr in _xhrs) {
95 xhr.abort();
96 }
97 }
98 }
OLDNEW
« no previous file with comments | « pkg/http/README.md ('k') | pkg/http/lib/src/byte_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698