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() : _connections = new Set<_HttpConnection>(); | |
| 633 | |
| 632 void listen(String host, int port, [int backlog = 5]) { | 634 void listen(String host, int port, [int backlog = 5]) { |
| 635 listenOn(new ServerSocket(host, port, backlog)); | |
| 636 _closeServer = true; | |
| 637 } | |
| 633 | 638 |
| 639 void listenOn(ServerSocket serverSocket) { | |
| 634 void onConnection(Socket socket) { | 640 void onConnection(Socket socket) { |
| 635 // Accept the client connection. | 641 // Accept the client connection. |
| 636 _HttpConnection connection = new _HttpConnection(this); | 642 _HttpConnection connection = new _HttpConnection(this); |
| 637 connection._connectionEstablished(socket); | |
| 638 connection.requestReceived = _onRequest; | 643 connection.requestReceived = _onRequest; |
| 639 _connections.add(connection); | |
| 640 connection.onDisconnect = () => _connections.remove(connection); | 644 connection.onDisconnect = () => _connections.remove(connection); |
| 641 connection.onError = (e) { | 645 connection.onError = (e) { |
| 642 if (_onError != null) _onError(e); | 646 if (_onError != null) _onError(e); |
| 643 }; | 647 }; |
| 648 connection._connectionEstablished(socket); | |
| 649 _connections.add(connection); | |
| 644 } | 650 } |
| 645 | 651 serverSocket.onConnection = onConnection; |
| 646 _connections = new Set<_HttpConnection>(); | 652 _server = serverSocket; |
| 647 _server = new ServerSocket(host, port, backlog); | 653 _closeServer = false; |
| 648 _server.onConnection = onConnection; | |
| 649 } | 654 } |
| 650 | 655 |
| 651 void close() { | 656 void close() { |
| 652 _server.close(); | 657 if (_server !== null && _closeServer) { |
| 658 _server.close(); | |
| 659 } | |
| 660 _server = null; | |
| 653 for (_HttpConnection connection in _connections) { | 661 for (_HttpConnection connection in _connections) { |
| 654 connection._socket.close(); | 662 connection._socket.close(); |
| 655 } | 663 } |
| 664 _connections.clear(); | |
| 656 } | 665 } |
| 657 | 666 |
| 658 int get port() => _server.port; | 667 int get port() => _server === null ? -1 : _server.port; |
|
Søren Gjesse
2012/04/02 08:16:45
How about throwing an exception if the server has
Anders Johnsen
2012/04/02 08:24:35
Even better! :)
| |
| 659 | 668 |
| 660 void set onError(void callback(Exception e)) { | 669 void set onError(void callback(Exception e)) { |
| 661 _onError = callback; | 670 _onError = callback; |
| 662 } | 671 } |
| 663 | 672 |
| 664 void set onRequest(void callback(HttpRequest, HttpResponse)) { | 673 void set onRequest(void callback(HttpRequest, HttpResponse)) { |
| 665 _onRequest = callback; | 674 _onRequest = callback; |
| 666 } | 675 } |
| 667 | 676 |
| 668 ServerSocket _server; // The server listen socket. | 677 ServerSocket _server; // The server listen socket. |
| 678 bool _closeServer = false; | |
| 669 Set<_HttpConnection> _connections; // Set of currently connected clients. | 679 Set<_HttpConnection> _connections; // Set of currently connected clients. |
| 670 Function _onRequest; | 680 Function _onRequest; |
| 671 Function _onError; | 681 Function _onError; |
| 672 } | 682 } |
| 673 | 683 |
| 674 | 684 |
| 675 class _HttpClientRequest | 685 class _HttpClientRequest |
| 676 extends _HttpRequestResponseBase implements HttpClientRequest { | 686 extends _HttpRequestResponseBase implements HttpClientRequest { |
| 677 static final int START = 0; | 687 static final int START = 0; |
| 678 static final int HEADERS_SENT = 1; | 688 static final int HEADERS_SENT = 1; |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1194 sockets.addFirst(socketConn); | 1204 sockets.addFirst(socketConn); |
| 1195 socketConn._markReturned(); | 1205 socketConn._markReturned(); |
| 1196 } | 1206 } |
| 1197 | 1207 |
| 1198 Function _onOpen; | 1208 Function _onOpen; |
| 1199 Map<String, Queue<_SocketConnection>> _openSockets; | 1209 Map<String, Queue<_SocketConnection>> _openSockets; |
| 1200 Set<_SocketConnection> _activeSockets; | 1210 Set<_SocketConnection> _activeSockets; |
| 1201 Timer _evictionTimer; | 1211 Timer _evictionTimer; |
| 1202 bool _shutdown; // Has this HTTP client been shutdown? | 1212 bool _shutdown; // Has this HTTP client been shutdown? |
| 1203 } | 1213 } |
| OLD | NEW |