| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Global constants. | 5 // Global constants. |
| 6 class Const { | 6 class Const { |
| 7 // Bytes for "HTTP/1.0". | 7 // Bytes for "HTTP/1.0". |
| 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; | 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; |
| 9 // Bytes for "HTTP/1.1". | 9 // Bytes for "HTTP/1.1". |
| 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; | 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; |
| (...skipping 1368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1379 Socket _socket; | 1379 Socket _socket; |
| 1380 Date _returnTime; | 1380 Date _returnTime; |
| 1381 } | 1381 } |
| 1382 | 1382 |
| 1383 | 1383 |
| 1384 class HTTPClientImplementation implements HTTPClient{ | 1384 class HTTPClientImplementation implements HTTPClient{ |
| 1385 static final int DEFAULT_EVICTION_TIMEOUT = 60000; | 1385 static final int DEFAULT_EVICTION_TIMEOUT = 60000; |
| 1386 | 1386 |
| 1387 HTTPClientImplementation() : _openSockets = new Map(), _shutdown = false; | 1387 HTTPClientImplementation() : _openSockets = new Map(), _shutdown = false; |
| 1388 | 1388 |
| 1389 HTTPClientRequest open(String method, | 1389 void open(String method, String host, int port, String path) { |
| 1390 String host, | 1390 if (_shutdown && _openHandler) { |
| 1391 int port, | 1391 _openHandler(null); |
| 1392 String path) { | 1392 return; |
| 1393 // TODO(sgjesse): Throw exception. | 1393 } |
| 1394 if (_shutdown) return null; | 1394 _getSocketConnection(host, port, method, path); |
| 1395 SocketConnection socketConn = _getSocketConnection(host, port); | |
| 1396 HTTPClientConnection connection = | |
| 1397 new HTTPClientConnection(this, socketConn); | |
| 1398 HTTPClientRequest request = connection.open(method, path); | |
| 1399 return request; | |
| 1400 } | 1395 } |
| 1401 | 1396 |
| 1402 void shutdown() { | 1397 void shutdown() { |
| 1403 _openSockets.forEach( | 1398 _openSockets.forEach( |
| 1404 void _(String key, Queue<SocketConnection> connections) { | 1399 void _(String key, Queue<SocketConnection> connections) { |
| 1405 while (!connections.isEmpty()) { | 1400 while (!connections.isEmpty()) { |
| 1406 var socketConn = connections.removeFirst(); | 1401 var socketConn = connections.removeFirst(); |
| 1407 socketConn._socket.close(); | 1402 socketConn._socket.close(); |
| 1408 } | 1403 } |
| 1409 }); | 1404 }); |
| 1410 if (_evictionTimer != null) { | 1405 if (_evictionTimer != null) { |
| 1411 _evictionTimer.cancel(); | 1406 _evictionTimer.cancel(); |
| 1412 } | 1407 } |
| 1413 _shutdown = true; | 1408 _shutdown = true; |
| 1414 } | 1409 } |
| 1415 | 1410 |
| 1416 String _connectionKey(String host, int port) { | 1411 String _connectionKey(String host, int port) { |
| 1417 return "$host:$port"; | 1412 return "$host:$port"; |
| 1418 } | 1413 } |
| 1419 | 1414 |
| 1420 SocketConnection _getSocketConnection(String host, int port) { | 1415 void _getSocketConnection(String host, int port, String method, String path) { |
| 1421 SocketConnection entry; | 1416 |
| 1417 void _connectionOpened(SocketConnection socketConn) { |
| 1418 HTTPClientConnection connection = |
| 1419 new HTTPClientConnection(this, socketConn); |
| 1420 HTTPClientRequest request = connection.open(method, path); |
| 1421 if (_openHandler != null) { |
| 1422 _openHandler(request); |
| 1423 } |
| 1424 } |
| 1422 | 1425 |
| 1423 // If there are active connections for this key get the first one | 1426 // If there are active connections for this key get the first one |
| 1424 // otherwise create a new one. | 1427 // otherwise create a new one. |
| 1425 Queue socketConnections = _openSockets[_connectionKey(host, port)]; | 1428 Queue socketConnections = _openSockets[_connectionKey(host, port)]; |
| 1426 if (socketConnections == null || socketConnections.isEmpty()) { | 1429 if (socketConnections == null || socketConnections.isEmpty()) { |
| 1427 Socket socket = new Socket(host, port); | 1430 Socket socket = new Socket(host, port); |
| 1428 entry = new SocketConnection(host, port, socket); | 1431 socket.connectHandler = () { |
| 1432 SocketConnection socketConn = new SocketConnection(host, port, socket); |
| 1433 _connectionOpened(socketConn); |
| 1434 }; |
| 1429 } else { | 1435 } else { |
| 1430 entry = socketConnections.removeFirst(); | 1436 SocketConnection socketConn = socketConnections.removeFirst(); |
| 1437 _connectionOpened(socketConn); |
| 1431 | 1438 |
| 1432 // Get rid of eviction timer if there are no more active connections. | 1439 // Get rid of eviction timer if there are no more active connections. |
| 1433 if (socketConnections.isEmpty()) { | 1440 if (socketConnections.isEmpty()) { |
| 1434 _evictionTimer.cancel(); | 1441 _evictionTimer.cancel(); |
| 1435 _evictionTimer = null; | 1442 _evictionTimer = null; |
| 1436 } | 1443 } |
| 1437 } | 1444 } |
| 1438 | |
| 1439 return entry; | |
| 1440 } | 1445 } |
| 1441 | 1446 |
| 1442 void _returnSocketConnection(SocketConnection socketConn) { | 1447 void _returnSocketConnection(SocketConnection socketConn) { |
| 1443 // If the HTTP client is beeing shutdown don't return the connection. | 1448 // If the HTTP client is beeing shutdown don't return the connection. |
| 1444 if (_shutdown) { | 1449 if (_shutdown) { |
| 1445 socketConn._socket.close(); | 1450 socketConn._socket.close(); |
| 1446 return; | 1451 return; |
| 1447 }; | 1452 }; |
| 1448 | 1453 |
| 1449 String key = _connectionKey(socketConn._host, socketConn._port); | 1454 String key = _connectionKey(socketConn._host, socketConn._port); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1475 }); | 1480 }); |
| 1476 } | 1481 } |
| 1477 _evictionTimer = new Timer(_handleEviction, 10000, true); | 1482 _evictionTimer = new Timer(_handleEviction, 10000, true); |
| 1478 } | 1483 } |
| 1479 | 1484 |
| 1480 // Return connection. | 1485 // Return connection. |
| 1481 sockets.addFirst(socketConn); | 1486 sockets.addFirst(socketConn); |
| 1482 socketConn._markReturned(); | 1487 socketConn._markReturned(); |
| 1483 } | 1488 } |
| 1484 | 1489 |
| 1490 void set openHandler(void callback(HTTPClientRequest request)) { |
| 1491 _openHandler = callback; |
| 1492 } |
| 1493 |
| 1494 var _openHandler; |
| 1485 Map<String, Queue<SocketConnection>> _openSockets; | 1495 Map<String, Queue<SocketConnection>> _openSockets; |
| 1486 Timer _evictionTimer; | 1496 Timer _evictionTimer; |
| 1487 bool _shutdown; // Has this HTTP client been shutdown? | 1497 bool _shutdown; // Has this HTTP client been shutdown? |
| 1488 } | 1498 } |
| OLD | NEW |