Chromium Code Reviews| 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 class _HttpRequestResponseBase { | 5 class _HttpRequestResponseBase { |
| 6 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) | 6 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) |
| 7 : _contentLength = -1, | 7 : _contentLength = -1, |
| 8 _keepAlive = false, | 8 _keepAlive = false, |
| 9 _headers = new Map(); | 9 _headers = new Map(); |
| 10 | 10 |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 622 HttpResponse _response; | 622 HttpResponse _response; |
| 623 | 623 |
| 624 // Callbacks. | 624 // Callbacks. |
| 625 var requestReceived; | 625 var requestReceived; |
| 626 } | 626 } |
| 627 | 627 |
| 628 | 628 |
| 629 // HTTP server waiting for socket connections. The connections are | 629 // HTTP server waiting for socket connections. The connections are |
| 630 // managed by the server and as requests are received the request. | 630 // managed by the server and as requests are received the request. |
| 631 class _HttpServer implements HttpServer { | 631 class _HttpServer implements HttpServer { |
| 632 _HttpServer() : | |
|
Søren Gjesse
2012/04/02 07:42:15
Fits on one line?
Anders Johnsen
2012/04/02 08:12:34
Done.
| |
| 633 _connections = new Set<_HttpConnection>(); | |
| 634 | |
| 632 void listen(String host, int port, [int backlog = 5]) { | 635 void listen(String host, int port, [int backlog = 5]) { |
| 636 _server = new ServerSocket(host, port, backlog); | |
| 637 attachTo(_server); | |
| 638 } | |
| 633 | 639 |
| 640 void attachTo(ServerSocket serverSocket) { | |
| 634 void onConnection(Socket socket) { | 641 void onConnection(Socket socket) { |
| 635 // Accept the client connection. | 642 // Accept the client connection. |
| 636 _HttpConnection connection = new _HttpConnection(this); | 643 _HttpConnection connection = new _HttpConnection(this); |
| 637 connection._connectionEstablished(socket); | |
| 638 connection.requestReceived = _onRequest; | 644 connection.requestReceived = _onRequest; |
| 639 _connections.add(connection); | |
| 640 connection.onDisconnect = () => _connections.remove(connection); | 645 connection.onDisconnect = () => _connections.remove(connection); |
| 641 connection.onError = (e) { | 646 connection.onError = (e) { |
| 642 if (_onError != null) _onError(e); | 647 if (_onError != null) _onError(e); |
| 643 }; | 648 }; |
| 649 connection._connectionEstablished(socket); | |
| 650 _connections.add(connection); | |
| 644 } | 651 } |
| 645 | 652 serverSocket.onConnection = onConnection; |
| 646 _connections = new Set<_HttpConnection>(); | |
| 647 _server = new ServerSocket(host, port, backlog); | |
| 648 _server.onConnection = onConnection; | |
| 649 } | 653 } |
| 650 | 654 |
| 651 void close() { | 655 void close() { |
| 652 _server.close(); | 656 if (_server !== null) { |
| 657 _server.close(); | |
| 658 _server = null; | |
| 659 } | |
| 653 for (_HttpConnection connection in _connections) { | 660 for (_HttpConnection connection in _connections) { |
| 654 connection._socket.close(); | 661 connection._socket.close(); |
| 655 } | 662 } |
| 663 _connections.clear(); | |
| 656 } | 664 } |
| 657 | 665 |
| 658 int get port() => _server.port; | 666 int get port() => _server.port; |
|
Søren Gjesse
2012/04/02 07:42:15
This will give an exception if _server is null. Yo
Anders Johnsen
2012/04/02 08:12:34
Agreed, I've changed the logic a bit.
| |
| 659 | 667 |
| 660 void set onError(void callback(Exception e)) { | 668 void set onError(void callback(Exception e)) { |
| 661 _onError = callback; | 669 _onError = callback; |
| 662 } | 670 } |
| 663 | 671 |
| 664 void set onRequest(void callback(HttpRequest, HttpResponse)) { | 672 void set onRequest(void callback(HttpRequest, HttpResponse)) { |
| 665 _onRequest = callback; | 673 _onRequest = callback; |
| 666 } | 674 } |
| 667 | 675 |
| 668 ServerSocket _server; // The server listen socket. | 676 ServerSocket _server; // The server listen socket, if created internally. |
| 669 Set<_HttpConnection> _connections; // Set of currently connected clients. | 677 Set<_HttpConnection> _connections; // Set of currently connected clients. |
| 670 Function _onRequest; | 678 Function _onRequest; |
| 671 Function _onError; | 679 Function _onError; |
| 672 } | 680 } |
| 673 | 681 |
| 674 | 682 |
| 675 class _HttpClientRequest | 683 class _HttpClientRequest |
| 676 extends _HttpRequestResponseBase implements HttpClientRequest { | 684 extends _HttpRequestResponseBase implements HttpClientRequest { |
| 677 static final int START = 0; | 685 static final int START = 0; |
| 678 static final int HEADERS_SENT = 1; | 686 static final int HEADERS_SENT = 1; |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1194 sockets.addFirst(socketConn); | 1202 sockets.addFirst(socketConn); |
| 1195 socketConn._markReturned(); | 1203 socketConn._markReturned(); |
| 1196 } | 1204 } |
| 1197 | 1205 |
| 1198 Function _onOpen; | 1206 Function _onOpen; |
| 1199 Map<String, Queue<_SocketConnection>> _openSockets; | 1207 Map<String, Queue<_SocketConnection>> _openSockets; |
| 1200 Set<_SocketConnection> _activeSockets; | 1208 Set<_SocketConnection> _activeSockets; |
| 1201 Timer _evictionTimer; | 1209 Timer _evictionTimer; |
| 1202 bool _shutdown; // Has this HTTP client been shutdown? | 1210 bool _shutdown; // Has this HTTP client been shutdown? |
| 1203 } | 1211 } |
| OLD | NEW |