| OLD | NEW |
| 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 1317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1328 } | 1328 } |
| 1329 | 1329 |
| 1330 void close() { | 1330 void close() { |
| 1331 closed = true; | 1331 closed = true; |
| 1332 _httpClient._connectionClosed(this); | 1332 _httpClient._connectionClosed(this); |
| 1333 _streamFuture | 1333 _streamFuture |
| 1334 // TODO(ajohnsen): Add timeout. | 1334 // TODO(ajohnsen): Add timeout. |
| 1335 .then((_) => _socket.destroy()); | 1335 .then((_) => _socket.destroy()); |
| 1336 } | 1336 } |
| 1337 | 1337 |
| 1338 Future<_HttpClientConnection> createProxyTunnel(host, port, proxy) { | 1338 Future<_HttpClientConnection> createProxyTunnel(host, port, proxy, callback) { |
| 1339 _HttpClientRequest request = | 1339 _HttpClientRequest request = |
| 1340 send(new Uri(host: host, port: port), | 1340 send(new Uri(host: host, port: port), |
| 1341 port, | 1341 port, |
| 1342 "CONNECT", | 1342 "CONNECT", |
| 1343 proxy); | 1343 proxy); |
| 1344 if (proxy.isAuthenticated) { | 1344 if (proxy.isAuthenticated) { |
| 1345 // If the proxy configuration contains user information use that | 1345 // If the proxy configuration contains user information use that |
| 1346 // for proxy basic authorization. | 1346 // for proxy basic authorization. |
| 1347 String auth = _CryptoUtils.bytesToBase64( | 1347 String auth = _CryptoUtils.bytesToBase64( |
| 1348 _encodeString("${proxy.username}:${proxy.password}")); | 1348 _encodeString("${proxy.username}:${proxy.password}")); |
| 1349 request.headers.set(HttpHeaders.PROXY_AUTHORIZATION, "Basic $auth"); | 1349 request.headers.set(HttpHeaders.PROXY_AUTHORIZATION, "Basic $auth"); |
| 1350 } | 1350 } |
| 1351 return request.close() | 1351 return request.close() |
| 1352 .then((response) { | 1352 .then((response) { |
| 1353 if (response.statusCode != HttpStatus.OK) { | 1353 if (response.statusCode != HttpStatus.OK) { |
| 1354 throw "Proxy failed to establish tunnel " | 1354 throw "Proxy failed to establish tunnel " |
| 1355 "(${response.statusCode} ${response.reasonPhrase})"; | 1355 "(${response.statusCode} ${response.reasonPhrase})"; |
| 1356 } | 1356 } |
| 1357 var socket = response._httpRequest._httpClientConnection._socket; | 1357 var socket = response._httpRequest._httpClientConnection._socket; |
| 1358 return SecureSocket.secure(socket, host: host); | 1358 return SecureSocket.secure( |
| 1359 socket, host: host, onBadCertificate: callback); |
| 1359 }) | 1360 }) |
| 1360 .then((secureSocket) { | 1361 .then((secureSocket) { |
| 1361 String key = _HttpClientConnection.makeKey(true, host, port); | 1362 String key = _HttpClientConnection.makeKey(true, host, port); |
| 1362 return new _HttpClientConnection( | 1363 return new _HttpClientConnection( |
| 1363 key, secureSocket, request._httpClient, true); | 1364 key, secureSocket, request._httpClient, true); |
| 1364 }); | 1365 }); |
| 1365 } | 1366 } |
| 1366 | 1367 |
| 1367 HttpConnectionInfo get connectionInfo => _HttpConnectionInfo.create(_socket); | 1368 HttpConnectionInfo get connectionInfo => _HttpConnectionInfo.create(_socket); |
| 1368 | 1369 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1402 final Map<String, Set<_HttpClientConnection>> _idleConnections | 1403 final Map<String, Set<_HttpClientConnection>> _idleConnections |
| 1403 = new Map<String, Set<_HttpClientConnection>>(); | 1404 = new Map<String, Set<_HttpClientConnection>>(); |
| 1404 final Set<_HttpClientConnection> _activeConnections | 1405 final Set<_HttpClientConnection> _activeConnections |
| 1405 = new Set<_HttpClientConnection>(); | 1406 = new Set<_HttpClientConnection>(); |
| 1406 final List<_Credentials> _credentials = []; | 1407 final List<_Credentials> _credentials = []; |
| 1407 final List<_ProxyCredentials> _proxyCredentials = []; | 1408 final List<_ProxyCredentials> _proxyCredentials = []; |
| 1408 Function _authenticate; | 1409 Function _authenticate; |
| 1409 Function _authenticateProxy; | 1410 Function _authenticateProxy; |
| 1410 Function _findProxy = HttpClient.findProxyFromEnvironment; | 1411 Function _findProxy = HttpClient.findProxyFromEnvironment; |
| 1411 Duration _idleTimeout = const Duration(seconds: 15); | 1412 Duration _idleTimeout = const Duration(seconds: 15); |
| 1413 Function _badCertificateCallback; |
| 1412 | 1414 |
| 1413 Timer _noActiveTimer; | 1415 Timer _noActiveTimer; |
| 1414 | 1416 |
| 1415 Duration get idleTimeout => _idleTimeout; | 1417 Duration get idleTimeout => _idleTimeout; |
| 1416 | 1418 |
| 1417 String userAgent = _getHttpVersion(); | 1419 String userAgent = _getHttpVersion(); |
| 1418 | 1420 |
| 1419 void set idleTimeout(Duration timeout) { | 1421 void set idleTimeout(Duration timeout) { |
| 1420 _idleTimeout = timeout; | 1422 _idleTimeout = timeout; |
| 1421 _idleConnections.values.forEach( | 1423 _idleConnections.values.forEach( |
| 1422 (l) => l.forEach((c) { | 1424 (l) => l.forEach((c) { |
| 1423 // Reset timer. This is fine, as it's not happening often. | 1425 // Reset timer. This is fine, as it's not happening often. |
| 1424 c.stopTimer(); | 1426 c.stopTimer(); |
| 1425 c.startTimer(); | 1427 c.startTimer(); |
| 1426 })); | 1428 })); |
| 1427 } | 1429 } |
| 1428 | 1430 |
| 1431 set badCertificateCallback(bool callback(X509Certificate cert, |
| 1432 String host, |
| 1433 int port)) { |
| 1434 _badCertificateCallback = callback; |
| 1435 } |
| 1436 |
| 1429 | 1437 |
| 1430 Future<HttpClientRequest> open(String method, | 1438 Future<HttpClientRequest> open(String method, |
| 1431 String host, | 1439 String host, |
| 1432 int port, | 1440 int port, |
| 1433 String path) { | 1441 String path) { |
| 1434 // TODO(sgjesse): The path set here can contain both query and | 1442 // TODO(sgjesse): The path set here can contain both query and |
| 1435 // fragment. They should be cracked and set correctly. | 1443 // fragment. They should be cracked and set correctly. |
| 1436 return _openUrl(method, new Uri( | 1444 return _openUrl(method, new Uri( |
| 1437 scheme: "http", host: host, port: port, path: path)); | 1445 scheme: "http", host: host, port: port, path: path)); |
| 1438 } | 1446 } |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1667 var connection = _idleConnections[key].first; | 1675 var connection = _idleConnections[key].first; |
| 1668 _idleConnections[key].remove(connection); | 1676 _idleConnections[key].remove(connection); |
| 1669 if (_idleConnections[key].isEmpty) { | 1677 if (_idleConnections[key].isEmpty) { |
| 1670 _idleConnections.remove(key); | 1678 _idleConnections.remove(key); |
| 1671 } | 1679 } |
| 1672 connection.stopTimer(); | 1680 connection.stopTimer(); |
| 1673 _activeConnections.add(connection); | 1681 _activeConnections.add(connection); |
| 1674 _updateTimers(); | 1682 _updateTimers(); |
| 1675 return new Future.value(new _ConnnectionInfo(connection, proxy)); | 1683 return new Future.value(new _ConnnectionInfo(connection, proxy)); |
| 1676 } | 1684 } |
| 1685 var currentBadCertificateCallback = _badCertificateCallback; |
| 1686 bool callback(X509Certificate certificate) => |
| 1687 currentBadCertificateCallback == null ? false : |
| 1688 currentBadCertificateCallback(certificate, uriHost, uriPort); |
| 1677 return (isSecure && proxy.isDirect | 1689 return (isSecure && proxy.isDirect |
| 1678 ? SecureSocket.connect(host, | 1690 ? SecureSocket.connect(host, |
| 1679 port, | 1691 port, |
| 1680 sendClientCertificate: true) | 1692 sendClientCertificate: true, |
| 1693 onBadCertificate: callback) |
| 1681 : Socket.connect(host, port)) | 1694 : Socket.connect(host, port)) |
| 1682 .then((socket) { | 1695 .then((socket) { |
| 1683 socket.setOption(SocketOption.TCP_NODELAY, true); | 1696 socket.setOption(SocketOption.TCP_NODELAY, true); |
| 1684 var connection = new _HttpClientConnection(key, socket, this); | 1697 var connection = new _HttpClientConnection(key, socket, this); |
| 1685 if (isSecure && !proxy.isDirect) { | 1698 if (isSecure && !proxy.isDirect) { |
| 1686 connection._dispose = true; | 1699 connection._dispose = true; |
| 1687 return connection.createProxyTunnel(uriHost, uriPort, proxy) | 1700 return connection.createProxyTunnel( |
| 1701 uriHost, uriPort, proxy, callback) |
| 1688 .then((tunnel) { | 1702 .then((tunnel) { |
| 1689 _activeConnections.add(tunnel); | 1703 _activeConnections.add(tunnel); |
| 1690 return new _ConnnectionInfo(tunnel, proxy); | 1704 return new _ConnnectionInfo(tunnel, proxy); |
| 1691 }); | 1705 }); |
| 1692 } else { | 1706 } else { |
| 1693 _activeConnections.add(connection); | 1707 _activeConnections.add(connection); |
| 1694 return new _ConnnectionInfo(connection, proxy); | 1708 return new _ConnnectionInfo(connection, proxy); |
| 1695 } | 1709 } |
| 1696 }, onError: (error) { | 1710 }, onError: (error) { |
| 1697 // Continue with next proxy. | 1711 // Continue with next proxy. |
| (...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2431 final Uri location; | 2445 final Uri location; |
| 2432 } | 2446 } |
| 2433 | 2447 |
| 2434 String _getHttpVersion() { | 2448 String _getHttpVersion() { |
| 2435 var version = Platform.version; | 2449 var version = Platform.version; |
| 2436 // Only include major and minor version numbers. | 2450 // Only include major and minor version numbers. |
| 2437 int index = version.indexOf('.', version.indexOf('.') + 1); | 2451 int index = version.indexOf('.', version.indexOf('.') + 1); |
| 2438 version = version.substring(0, index); | 2452 version = version.substring(0, index); |
| 2439 return 'Dart/$version (dart:io)'; | 2453 return 'Dart/$version (dart:io)'; |
| 2440 } | 2454 } |
| OLD | NEW |