Chromium Code Reviews| 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 1367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1378 int _port; | 1378 int _port; |
| 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 |
|
Søren Gjesse
2011/12/01 11:15:43
You should also update http.dart where this interf
Mads Ager (google)
2011/12/01 11:43:59
Done.
| |
| 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 print('connect handler set'); |
|
Søren Gjesse
2011/12/01 11:15:43
Remove print.
Mads Ager (google)
2011/12/01 11:43:59
Done.
| |
| 1432 socket.connectHandler = () { | |
| 1433 print('connect handler'); | |
|
Søren Gjesse
2011/12/01 11:15:43
Ditto.
Mads Ager (google)
2011/12/01 11:43:59
Done.
| |
| 1434 SocketConnection socketConn = new SocketConnection(host, port, socket); | |
| 1435 _connectionOpened(socketConn); | |
| 1436 }; | |
| 1429 } else { | 1437 } else { |
| 1430 entry = socketConnections.removeFirst(); | 1438 SocketConnection socketConn = socketConnections.removeFirst(); |
| 1439 _connectionOpened(socketConn); | |
| 1431 | 1440 |
| 1432 // Get rid of eviction timer if there are no more active connections. | 1441 // Get rid of eviction timer if there are no more active connections. |
| 1433 if (socketConnections.isEmpty()) { | 1442 if (socketConnections.isEmpty()) { |
| 1434 _evictionTimer.cancel(); | 1443 _evictionTimer.cancel(); |
| 1435 _evictionTimer = null; | 1444 _evictionTimer = null; |
| 1436 } | 1445 } |
| 1437 } | 1446 } |
| 1438 | |
| 1439 return entry; | |
| 1440 } | 1447 } |
| 1441 | 1448 |
| 1442 void _returnSocketConnection(SocketConnection socketConn) { | 1449 void _returnSocketConnection(SocketConnection socketConn) { |
| 1443 // If the HTTP client is beeing shutdown don't return the connection. | 1450 // If the HTTP client is beeing shutdown don't return the connection. |
| 1444 if (_shutdown) { | 1451 if (_shutdown) { |
| 1445 socketConn._socket.close(); | 1452 socketConn._socket.close(); |
| 1446 return; | 1453 return; |
| 1447 }; | 1454 }; |
| 1448 | 1455 |
| 1449 String key = _connectionKey(socketConn._host, socketConn._port); | 1456 String key = _connectionKey(socketConn._host, socketConn._port); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1475 }); | 1482 }); |
| 1476 } | 1483 } |
| 1477 _evictionTimer = new Timer(_handleEviction, 10000, true); | 1484 _evictionTimer = new Timer(_handleEviction, 10000, true); |
| 1478 } | 1485 } |
| 1479 | 1486 |
| 1480 // Return connection. | 1487 // Return connection. |
| 1481 sockets.addFirst(socketConn); | 1488 sockets.addFirst(socketConn); |
| 1482 socketConn._markReturned(); | 1489 socketConn._markReturned(); |
| 1483 } | 1490 } |
| 1484 | 1491 |
| 1492 void set openHandler(void callback(HTTPClientRequest request)) { | |
| 1493 _openHandler = callback; | |
| 1494 } | |
| 1495 | |
| 1496 var _openHandler; | |
|
Søren Gjesse
2011/12/01 11:15:43
Remove empty line.
Mads Ager (google)
2011/12/01 11:43:59
Done.
| |
| 1497 | |
| 1485 Map<String, Queue<SocketConnection>> _openSockets; | 1498 Map<String, Queue<SocketConnection>> _openSockets; |
| 1486 Timer _evictionTimer; | 1499 Timer _evictionTimer; |
| 1487 bool _shutdown; // Has this HTTP client been shutdown? | 1500 bool _shutdown; // Has this HTTP client been shutdown? |
| 1488 } | 1501 } |
| OLD | NEW |