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

Side by Side Diff: pkg/http/lib/src/io_client.dart

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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 | « no previous file | pkg/http/lib/src/multipart_file.dart » ('j') | runtime/tests/vm/vm.status » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library io_client; 5 library io_client;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 import 'base_client.dart'; 10 import 'base_client.dart';
11 import 'base_request.dart'; 11 import 'base_request.dart';
12 import 'streamed_response.dart'; 12 import 'streamed_response.dart';
13 import 'utils.dart'; 13 import 'utils.dart';
14 14
15 /// A `dart:io`-based HTTP client. This is the default client. 15 /// A `dart:io`-based HTTP client. This is the default client.
16 class IOClient extends BaseClient { 16 class IOClient extends BaseClient {
17 /// The underlying `dart:io` HTTP client. 17 /// The underlying `dart:io` HTTP client.
18 HttpClient _inner; 18 HttpClient _inner;
19 19
20 /// Creates a new HTTP client. 20 /// Creates a new HTTP client.
21 IOClient() : _inner = new HttpClient(); 21 IOClient() : _inner = new HttpClient();
22 22
23 /// Sends an HTTP request and asynchronously returns the response. 23 /// Sends an HTTP request and asynchronously returns the response.
24 Future<StreamedResponse> send(BaseRequest request) { 24 Future<StreamedResponse> send(BaseRequest request) {
25 var stream = request.finalize(); 25 var stream = request.finalize();
26 26
27 var completer = new Completer<StreamedResponse>(); 27 return _inner.openUrl(request.method, request.url).then((ioRequest) {
28 var connection = _inner.openUrl(request.method, request.url); 28 ioRequest.followRedirects = request.followRedirects;
29 bool completed = false; 29 ioRequest.maxRedirects = request.maxRedirects;
30 connection.followRedirects = request.followRedirects; 30 ioRequest.contentLength = request.contentLength;
31 connection.maxRedirects = request.maxRedirects; 31 ioRequest.persistentConnection = request.persistentConnection;
32 connection.onError = (e) { 32 request.headers.forEach((name, value) {
33 async.then((_) { 33 ioRequest.headers.set(name, value);
34 // TODO(nweiz): issue 4974 means that any errors that appear in the
35 // onRequest or onResponse callbacks get passed to onError. If the
36 // completer has already fired, we want to re-throw those exceptions
37 // to the top level so that they aren't silently ignored.
38 if (completed) throw e;
39
40 completed = true;
41 completer.completeError(e);
42 }); 34 });
43 }; 35 return Future.wait([stream.pipe(ioRequest), ioRequest.response])
44 36 .then((list) => list[1]);
45 var pipeCompleter = new Completer(); 37 }).then((response) {
46 connection.onRequest = (underlyingRequest) {
47 underlyingRequest.contentLength = request.contentLength;
48 underlyingRequest.persistentConnection = request.persistentConnection;
49 request.headers.forEach((name, value) {
50 underlyingRequest.headers.set(name, value);
51 });
52
53 chainToCompleter(
54 stream.pipe(wrapOutputStream(underlyingRequest.outputStream)),
55 pipeCompleter);
56 };
57
58 connection.onResponse = (response) {
59 var headers = {}; 38 var headers = {};
60 response.headers.forEach((key, values) { 39 response.headers.forEach((key, values) {
61 headers[key] = values.join(','); 40 headers[key] = values.join(',');
62 }); 41 });
63 42
64 if (completed) return; 43 return new StreamedResponse(
65 44 response,
66 completed = true;
67 completer.complete(new StreamedResponse(
68 wrapInputStream(response.inputStream),
69 response.statusCode, 45 response.statusCode,
70 response.contentLength, 46 response.contentLength,
71 request: request, 47 request: request,
72 headers: headers, 48 headers: headers,
73 isRedirect: response.isRedirect, 49 isRedirect: response.isRedirect,
74 persistentConnection: response.persistentConnection, 50 persistentConnection: response.persistentConnection,
75 reasonPhrase: response.reasonPhrase)); 51 reasonPhrase: response.reasonPhrase);
76 }; 52 });
77
78 return Future.wait([
79 completer.future,
80 pipeCompleter.future
81 ]).then((values) => values.first);
82 } 53 }
83 54
84 /// Closes the client. This terminates all active connections. If a client 55 /// Closes the client. This terminates all active connections. If a client
85 /// remains unclosed, the Dart process may not terminate. 56 /// remains unclosed, the Dart process may not terminate.
86 void close() { 57 void close() {
87 if (_inner != null) _inner.shutdown(force: true); 58 if (_inner != null) _inner.close(force: true);
88 _inner = null; 59 _inner = null;
89 } 60 }
90 } 61 }
OLDNEW
« no previous file with comments | « no previous file | pkg/http/lib/src/multipart_file.dart » ('j') | runtime/tests/vm/vm.status » ('J')

Powered by Google App Engine
This is Rietveld 408576698