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

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: 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
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((error) {
78 completer.completeError(
79 new ClientException("XMLHttpRequest error.", request.url),
Bob Nystrom 2014/05/01 18:30:12 Probably want to include the original error here.
nweiz 2014/05/01 19:34:54 There is no original error. The parameter name is
Bob Nystrom 2014/05/01 19:52:14 Oh, heh. In that case, leave a comment explaining
nweiz 2014/05/01 20:54:11 Done.
80 new Chain.current());
81 });
82
83 xhr.send(bytes);
84 return completer.future.whenComplete(() => _xhrs.remove(xhr));
85 });
86 }
87
88 /// Closes the client.
89 ///
90 /// This terminates all active requests.
91 void close() {
Bob Nystrom 2014/05/01 18:30:12 Stream.close() and other close() methods return Fu
nweiz 2014/05/01 19:34:54 The Client interface was created before that conve
Bob Nystrom 2014/05/01 19:52:14 SGTM.
92 for (var xhr in _xhrs) {
93 xhr.abort();
94 }
95 }
96 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698