OLD | NEW |
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'; |
(...skipping 24 matching lines...) Expand all Loading... |
35 // onRequest or onResponse callbacks get passed to onError. If the | 35 // onRequest or onResponse callbacks get passed to onError. If the |
36 // completer has already fired, we want to re-throw those exceptions | 36 // completer has already fired, we want to re-throw those exceptions |
37 // to the top level so that they aren't silently ignored. | 37 // to the top level so that they aren't silently ignored. |
38 if (completed) throw e; | 38 if (completed) throw e; |
39 | 39 |
40 completed = true; | 40 completed = true; |
41 completer.completeError(e); | 41 completer.completeError(e); |
42 }); | 42 }); |
43 }; | 43 }; |
44 | 44 |
| 45 var pipeCompleter = new Completer(); |
45 connection.onRequest = (underlyingRequest) { | 46 connection.onRequest = (underlyingRequest) { |
46 underlyingRequest.contentLength = request.contentLength; | 47 underlyingRequest.contentLength = request.contentLength; |
47 underlyingRequest.persistentConnection = request.persistentConnection; | 48 underlyingRequest.persistentConnection = request.persistentConnection; |
48 request.headers.forEach((name, value) { | 49 request.headers.forEach((name, value) { |
49 underlyingRequest.headers.set(name, value); | 50 underlyingRequest.headers.set(name, value); |
50 }); | 51 }); |
51 | 52 |
52 if (stream.closed) { | 53 chainToCompleter( |
53 underlyingRequest.outputStream.close(); | 54 stream.pipe(wrapOutputStream(underlyingRequest.outputStream)), |
54 } else { | 55 pipeCompleter); |
55 stream.pipe(underlyingRequest.outputStream); | |
56 } | |
57 }; | 56 }; |
58 | 57 |
59 connection.onResponse = (response) { | 58 connection.onResponse = (response) { |
60 var headers = <String>{}; | 59 var headers = <String>{}; |
61 response.headers.forEach((key, value) => headers[key] = value); | 60 response.headers.forEach((key, value) => headers[key] = value); |
62 | 61 |
63 if (completed) return; | 62 if (completed) return; |
64 | 63 |
65 completed = true; | 64 completed = true; |
66 completer.complete(new StreamedResponse( | 65 completer.complete(new StreamedResponse( |
67 response.inputStream, | 66 wrapInputStream(response.inputStream), |
68 response.statusCode, | 67 response.statusCode, |
69 response.contentLength, | 68 response.contentLength, |
70 request: request, | 69 request: request, |
71 headers: headers, | 70 headers: headers, |
72 isRedirect: response.isRedirect, | 71 isRedirect: response.isRedirect, |
73 persistentConnection: response.persistentConnection, | 72 persistentConnection: response.persistentConnection, |
74 reasonPhrase: response.reasonPhrase)); | 73 reasonPhrase: response.reasonPhrase)); |
75 }; | 74 }; |
76 | 75 |
77 return completer.future; | 76 return pipeCompleter.future.then((_) => completer.future); |
78 } | 77 } |
79 | 78 |
80 /// Closes the client. This terminates all active connections. If a client | 79 /// Closes the client. This terminates all active connections. If a client |
81 /// remains unclosed, the Dart process may not terminate. | 80 /// remains unclosed, the Dart process may not terminate. |
82 void close() { | 81 void close() { |
83 if (_inner != null) _inner.shutdown(force: true); | 82 if (_inner != null) _inner.shutdown(force: true); |
84 _inner = null; | 83 _inner = null; |
85 } | 84 } |
86 } | 85 } |
OLD | NEW |