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

Side by Side Diff: sdk/lib/io/http_impl.dart

Issue 13548002: Add Iterable.fold (and Stream.fold) which replace `reduce`. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 8 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
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of dart.io; 5 part of dart.io;
6 6
7 class _HttpIncoming extends Stream<List<int>> { 7 class _HttpIncoming extends Stream<List<int>> {
8 final int _transferLength; 8 final int _transferLength;
9 final Completer _dataCompleter = new Completer(); 9 final Completer _dataCompleter = new Completer();
10 Stream<List<int>> _stream; 10 Stream<List<int>> _stream;
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 return statusCode == HttpStatus.UNAUTHORIZED && 253 return statusCode == HttpStatus.UNAUTHORIZED &&
254 challenge != null && challenge.length == 1; 254 challenge != null && challenge.length == 1;
255 } 255 }
256 256
257 Future<HttpClientResponse> _authenticate() { 257 Future<HttpClientResponse> _authenticate() {
258 Future<HttpClientResponse> retryWithCredentials(_Credentials cr) { 258 Future<HttpClientResponse> retryWithCredentials(_Credentials cr) {
259 if (cr != null) { 259 if (cr != null) {
260 // TODO(sgjesse): Support digest. 260 // TODO(sgjesse): Support digest.
261 if (cr.scheme == _AuthenticationScheme.BASIC) { 261 if (cr.scheme == _AuthenticationScheme.BASIC) {
262 // Drain body and retry. 262 // Drain body and retry.
263 return reduce(null, (x, y) {}).then((_) { 263 return fold(null, (x, y) {}).then((_) {
264 return _httpClient._openUrlFromRequest(_httpRequest.method, 264 return _httpClient._openUrlFromRequest(_httpRequest.method,
265 _httpRequest.uri, 265 _httpRequest.uri,
266 _httpRequest) 266 _httpRequest)
267 .then((request) => request.close()); 267 .then((request) => request.close());
268 }); 268 });
269 } 269 }
270 } 270 }
271 271
272 // Fall through to here to perform normal response handling if 272 // Fall through to here to perform normal response handling if
273 // there is no sensible authorization handling. 273 // there is no sensible authorization handling.
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 } 453 }
454 _writeHeader(); 454 _writeHeader();
455 _ioSink = new IOSink(new _HttpOutboundConsumer(_ioSink, _consume, asGZip)); 455 _ioSink = new IOSink(new _HttpOutboundConsumer(_ioSink, _consume, asGZip));
456 _ioSink.encoding = encoding; 456 _ioSink.encoding = encoding;
457 } 457 }
458 458
459 Future _consume(IOSink ioSink, Stream<List<int>> stream, bool asGZip) { 459 Future _consume(IOSink ioSink, Stream<List<int>> stream, bool asGZip) {
460 int contentLength = headers.contentLength; 460 int contentLength = headers.contentLength;
461 if (_ignoreBody) { 461 if (_ignoreBody) {
462 ioSink.close(); 462 ioSink.close();
463 return stream.reduce(null, (x, y) {}).then((_) => this); 463 return stream.fold(null, (x, y) {}).then((_) => this);
464 } 464 }
465 stream = stream.transform(new _BufferTransformer()); 465 stream = stream.transform(new _BufferTransformer());
466 if (headers.chunkedTransferEncoding) { 466 if (headers.chunkedTransferEncoding) {
467 if (asGZip) { 467 if (asGZip) {
468 stream = stream.transform(new ZLibDeflater(gzip: true, level: 6)); 468 stream = stream.transform(new ZLibDeflater(gzip: true, level: 6));
469 } 469 }
470 stream = stream.transform(new _ChunkedTransformer()); 470 stream = stream.transform(new _ChunkedTransformer());
471 } else if (contentLength >= 0) { 471 } else if (contentLength >= 0) {
472 stream = stream.transform(new _ContentLengthValidator(contentLength)); 472 stream = stream.transform(new _ContentLengthValidator(contentLength));
473 } 473 }
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 HttpConnectionInfo get connectionInfo => _httpClientConnection.connectionInfo; 728 HttpConnectionInfo get connectionInfo => _httpClientConnection.connectionInfo;
729 729
730 void _onIncoming(_HttpIncoming incoming) { 730 void _onIncoming(_HttpIncoming incoming) {
731 var response = new _HttpClientResponse(incoming, 731 var response = new _HttpClientResponse(incoming,
732 this, 732 this,
733 _httpClient); 733 _httpClient);
734 Future<HttpClientResponse> future; 734 Future<HttpClientResponse> future;
735 if (followRedirects && response.isRedirect) { 735 if (followRedirects && response.isRedirect) {
736 if (response.redirects.length < maxRedirects) { 736 if (response.redirects.length < maxRedirects) {
737 // Redirect and drain response. 737 // Redirect and drain response.
738 future = response.reduce(null, (x, y) {}) 738 future = response.fold(null, (x, y) {})
739 .then((_) => response.redirect()); 739 .then((_) => response.redirect());
740 } else { 740 } else {
741 // End with exception, too many redirects. 741 // End with exception, too many redirects.
742 future = response.reduce(null, (x, y) {}) 742 future = response.fold(null, (x, y) {})
743 .then((_) => new Future.immediateError( 743 .then((_) => new Future.immediateError(
744 new RedirectLimitExceededException(response.redirects))); 744 new RedirectLimitExceededException(response.redirects)));
745 } 745 }
746 } else if (response._shouldAuthenticate) { 746 } else if (response._shouldAuthenticate) {
747 future = response._authenticate(); 747 future = response._authenticate();
748 } else { 748 } else {
749 future = new Future<HttpClientResponse>.immediate(response); 749 future = new Future<HttpClientResponse>.immediate(response);
750 } 750 }
751 future.then( 751 future.then(
752 (v) => _responseCompleter.complete(v), 752 (v) => _responseCompleter.complete(v),
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 } 1090 }
1091 1091
1092 Future<HttpClientRequest> postUrl(Uri url) { 1092 Future<HttpClientRequest> postUrl(Uri url) {
1093 return _openUrl("post", url); 1093 return _openUrl("post", url);
1094 } 1094 }
1095 1095
1096 void close({bool force: false}) { 1096 void close({bool force: false}) {
1097 _closing = true; 1097 _closing = true;
1098 // Create flattened copy of _idleConnections, as 'destory' will manipulate 1098 // Create flattened copy of _idleConnections, as 'destory' will manipulate
1099 // it. 1099 // it.
1100 var idle = _idleConnections.values.reduce( 1100 var idle = _idleConnections.values.fold(
1101 [], 1101 [],
1102 (l, e) { 1102 (l, e) {
1103 l.addAll(e); 1103 l.addAll(e);
1104 return l; 1104 return l;
1105 }); 1105 });
1106 idle.forEach((e) { 1106 idle.forEach((e) {
1107 e.close(); 1107 e.close();
1108 }); 1108 });
1109 assert(_idleConnections.isEmpty); 1109 assert(_idleConnections.isEmpty);
1110 if (force) { 1110 if (force) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 // Continue with next proxy. 1242 // Continue with next proxy.
1243 return connect(error.error); 1243 return connect(error.error);
1244 }); 1244 });
1245 } 1245 }
1246 return connect(new HttpException("No proxies given")); 1246 return connect(new HttpException("No proxies given"));
1247 } 1247 }
1248 1248
1249 _Credentials _findCredentials(Uri url, [_AuthenticationScheme scheme]) { 1249 _Credentials _findCredentials(Uri url, [_AuthenticationScheme scheme]) {
1250 // Look for credentials. 1250 // Look for credentials.
1251 _Credentials cr = 1251 _Credentials cr =
1252 _credentials.reduce(null, (_Credentials prev, _Credentials value) { 1252 _credentials.fold(null, (_Credentials prev, _Credentials value) {
1253 if (value.applies(url, scheme)) { 1253 if (value.applies(url, scheme)) {
1254 if (prev == null) return value; 1254 if (prev == null) return value;
1255 return value.uri.path.length > prev.uri.path.length ? value : prev; 1255 return value.uri.path.length > prev.uri.path.length ? value : prev;
1256 } else { 1256 } else {
1257 return prev; 1257 return prev;
1258 } 1258 }
1259 }); 1259 });
1260 return cr; 1260 return cr;
1261 } 1261 }
1262 1262
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
1780 1780
1781 1781
1782 class _RedirectInfo implements RedirectInfo { 1782 class _RedirectInfo implements RedirectInfo {
1783 const _RedirectInfo(int this.statusCode, 1783 const _RedirectInfo(int this.statusCode,
1784 String this.method, 1784 String this.method,
1785 Uri this.location); 1785 Uri this.location);
1786 final int statusCode; 1786 final int statusCode;
1787 final String method; 1787 final String method;
1788 final Uri location; 1788 final Uri location;
1789 } 1789 }
OLDNEW
« no previous file with comments | « sdk/lib/html/html_common/filtered_element_list.dart ('k') | sdk/lib/svg/dart2js/svg_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698