Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1038)

Unified Diff: runtime/bin/http_impl.dart

Issue 9965053: Add a new attachTo method to HttpServer, for using an existing ServerSocket. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« runtime/bin/http.dart ('K') | « runtime/bin/http.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/http_impl.dart
===================================================================
--- runtime/bin/http_impl.dart (revision 6032)
+++ runtime/bin/http_impl.dart (working copy)
@@ -354,16 +354,15 @@
bool _writeHeader() {
List<int> data;
- OutputStream stream = _httpConnection.outputStream;
// Write status line.
- stream.write(_Const.HTTP11);
+ _httpConnection.outputStream.write(_Const.HTTP11);
_writeSP();
data = _statusCode.toString().charCodes();
- stream.write(data);
+ _httpConnection.outputStream.write(data);
_writeSP();
data = reasonPhrase.charCodes();
- stream.write(data);
+ _httpConnection.outputStream.write(data);
_writeCRLF();
// Determine the value of the "Connection" header
@@ -468,7 +467,19 @@
_HttpRequestResponseBase _requestOrResponse;
}
Søren Gjesse 2012/04/02 07:32:41 Looks as if this change is mixed with the previous
Anders Johnsen 2012/04/02 07:36:52 Removed :)
+class _EmptyOutputStream implements OutputStream {
+ void close() {}
+ void destroy() {}
+ void set onVlosed(void callback()) {}
+ void set onError(void callback()) {}
+ void set onNoPendingWrites(void callback()) {}
+
+ bool write(List buffer, [bool copybuffer]) => true;
+ bool writeFrom(List buffer, [int offset, int len]) => true;
+ bool writeString(String string, [Encoding encoding]) => true;
+}
+
class _HttpConnectionBase implements Hashable {
static final int PHASE_IDLE = 0;
static final int PHASE_REQUEST = 1;
@@ -487,6 +498,7 @@
}
OutputStream get outputStream() {
+ if (_closing) return new _EmptyOutputStream();
return _socket.outputStream;
}
@@ -508,10 +520,10 @@
}
void _onClosed() {
+ _closing = true;
if (_phase != PHASE_IDLE) {
// Client closed socket for writing. Socket should still be open
// for writing the response.
- _closing = true;
} else {
// The connection is currently not used by any request just close it.
_socket.close();
@@ -520,6 +532,7 @@
}
void _onError(Exception e) {
+ _closing = true;
// If an error occurs, make sure to close the socket if one is associated.
if (_socket != null) {
_socket.close();
@@ -629,30 +642,39 @@
// HTTP server waiting for socket connections. The connections are
// managed by the server and as requests are received the request.
class _HttpServer implements HttpServer {
+ _HttpServer() :
+ _connections = new Set<_HttpConnection>();
+
void listen(String host, int port, [int backlog = 5]) {
+ _server = new ServerSocket(host, port, backlog);
+ attachTo(_server);
+ }
+ void attachTo(ServerSocket serverSocket) {
void onConnection(Socket socket) {
// Accept the client connection.
_HttpConnection connection = new _HttpConnection(this);
- connection._connectionEstablished(socket);
connection.requestReceived = _onRequest;
- _connections.add(connection);
connection.onDisconnect = () => _connections.remove(connection);
connection.onError = (e) {
if (_onError != null) _onError(e);
};
+ connection._connectionEstablished(socket);
+ _connections.add(connection);
}
- _connections = new Set<_HttpConnection>();
- _server = new ServerSocket(host, port, backlog);
- _server.onConnection = onConnection;
+ serverSocket.onConnection = onConnection;
}
void close() {
- _server.close();
+ if (_server !== null) {
+ _server.close();
+ _server = null;
+ }
for (_HttpConnection connection in _connections) {
connection._socket.close();
}
+ _connections.clear();
}
int get port() => _server.port;
@@ -665,7 +687,7 @@
_onRequest = callback;
}
- ServerSocket _server; // The server listen socket.
+ ServerSocket _server; // The server listen socket, if created internally.
Set<_HttpConnection> _connections; // Set of currently connected clients.
Function _onRequest;
Function _onError;
@@ -781,16 +803,15 @@
void _writeHeader() {
List<int> data;
- OutputStream stream = _httpConnection.outputStream;
// Write request line.
data = _method.toString().charCodes();
- stream.write(data);
+ _httpConnection.outputStream.write(data);
_writeSP();
data = _uri.toString().charCodes();
- stream.write(data);
+ _httpConnection.outputStream.write(data);
_writeSP();
- stream.write(_Const.HTTP11);
+ _httpConnection.outputStream.write(_Const.HTTP11);
_writeCRLF();
// Determine the value of the "Connection" header
« runtime/bin/http.dart ('K') | « runtime/bin/http.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698