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

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

Issue 810223002: Remove the http package from the repo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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
10 import 'package:stack_trace/stack_trace.dart';
11
12 import 'src/base_client.dart';
13 import 'src/base_request.dart';
14 import 'src/byte_stream.dart';
15 import 'src/exception.dart';
16 import 'src/streamed_response.dart';
17
18 // TODO(nweiz): Move this under src/, re-export from lib/http.dart, and use this
19 // automatically from [new Client] once we can create an HttpRequest using
20 // mirrors on dart2js (issue 18541) and dart2js doesn't crash on pkg/collection
21 // (issue 18535).
22
23 /// A `dart:html`-based HTTP client that runs in the browser and is backed by
24 /// XMLHttpRequests.
25 ///
26 /// This client inherits some of the limitations of XMLHttpRequest. It ignores
27 /// the [BaseRequest.contentLength], [BaseRequest.persistentConnection],
28 /// [BaseRequest.followRedirects], and [BaseRequest.maxRedirects] fields. It is
29 /// also unable to stream requests or responses; a request will only be sent and
30 /// a response will only be returned once all the data is available.
31 class BrowserClient extends BaseClient {
32 /// The currently active XHRs.
33 ///
34 /// These are aborted if the client is closed.
35 final _xhrs = new Set<HttpRequest>();
36
37 /// Creates a new HTTP client.
38 BrowserClient();
39
40 /// Sends an HTTP request and asynchronously returns the response.
41 Future<StreamedResponse> send(BaseRequest request) {
42 return request.finalize().toBytes().then((bytes) {
43 var xhr = new HttpRequest();
44 _xhrs.add(xhr);
45 xhr.open(request.method, request.url.toString(), async: true);
46 xhr.responseType = 'blob';
47 request.headers.forEach(xhr.setRequestHeader);
48
49 var completer = new Completer();
50 xhr.onLoad.first.then((_) {
51 // TODO(nweiz): Set the response type to "arraybuffer" when issue 18542
52 // is fixed.
53 var blob = xhr.response == null ? new Blob([]) : xhr.response;
54 var reader = new FileReader();
55
56 reader.onLoad.first.then((_) {
57 var body = reader.result;
58 completer.complete(new StreamedResponse(
59 new ByteStream.fromBytes(body),
60 xhr.status,
61 contentLength: body.length,
62 request: request,
63 headers: xhr.responseHeaders,
64 reasonPhrase: xhr.statusText));
65 });
66
67 reader.onError.first.then((error) {
68 completer.completeError(
69 new ClientException(error.toString(), request.url),
70 new Chain.current());
71 });
72
73 reader.readAsArrayBuffer(blob);
74 });
75
76 xhr.onError.first.then((_) {
77 // Unfortunately, the underlying XMLHttpRequest API doesn't expose any
78 // specific information about the error itself.
79 completer.completeError(
80 new ClientException("XMLHttpRequest error.", request.url),
81 new Chain.current());
82 });
83
84 xhr.send(bytes);
85 return completer.future.whenComplete(() => _xhrs.remove(xhr));
86 });
87 }
88
89 /// Closes the client.
90 ///
91 /// This terminates all active requests.
92 void close() {
93 for (var xhr in _xhrs) {
94 xhr.abort();
95 }
96 }
97 }
OLDNEW
« no previous file with comments | « pkg/http/README.md ('k') | pkg/http/lib/http.dart » ('j') | pkg/pkg.gyp » ('J')

Powered by Google App Engine
This is Rietveld 408576698