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

Unified Diff: sdk/lib/io/http_impl.dart

Issue 11364134: Merge libv1. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error Created 8 years, 1 month 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
« no previous file with comments | « sdk/lib/io/directory_impl.dart ('k') | sdk/lib/io/http_parser.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/http_impl.dart
diff --git a/sdk/lib/io/http_impl.dart b/sdk/lib/io/http_impl.dart
index cb2c2e0a8c714948cc91a268ae6bbfca75fbeaf4..440b635feaa658de76a6114336d35e374c35bfdb 100644
--- a/sdk/lib/io/http_impl.dart
+++ b/sdk/lib/io/http_impl.dart
@@ -1307,15 +1307,7 @@ abstract class _HttpConnectionBase {
List<int> buffer = new Uint8List(available);
int bytesRead = _socket.readList(buffer, 0, available);
if (bytesRead > 0) {
- int parsed = _httpParser.writeList(buffer, 0, bytesRead);
- if (!_httpParser.upgrade) {
- if (parsed != bytesRead) {
- if (_socket != null) {
- // TODO(sgjesse): Error handling.
- _destroy();
- }
- }
- }
+ _httpParser.writeList(buffer, 0, bytesRead);
}
}
@@ -1341,7 +1333,7 @@ abstract class _HttpConnectionBase {
Socket socket = _socket;
_socket = null;
if (onDetach != null) onDetach();
- return new _DetachedSocket(socket, _httpParser.unparsedData);
+ return new _DetachedSocket(socket, _httpParser.readUnparsedData());
}
HttpConnectionInfo get connectionInfo {
@@ -1399,7 +1391,7 @@ class _HttpConnection extends _HttpConnectionBase {
// Don't report errors when HTTP parser is in idle state. Clients
// can close the connection and cause a connection reset by peer
// error which is OK.
- if (e != null && onError != null && !_httpParser.isIdle) {
+ if (e != null && !_httpParser.isIdle) {
onError(e);
// Propagate the error to the streams.
if (_request != null && _request._streamErrorHandler != null) {
@@ -1413,23 +1405,27 @@ class _HttpConnection extends _HttpConnectionBase {
// If currently not processing any request close the socket when
// we are done writing the response.
if (_httpParser.isIdle) {
- _socket.outputStream.onClosed = () {
- _destroy();
- if (onClosed != null && e == null) {
- // Don't call onClosed if onError has been called.
+ // If the httpParser is idle and we get an error from the
+ // connection we deal with that as a closed connection and not
+ // as an error. When the client disappears we get a connection
+ // reset by peer and that is OK.
+ if (e != null) {
+ onClosed();
+ } else {
+ _socket.outputStream.onClosed = () {
+ _destroy();
onClosed();
- }
- };
- // If the client closes and we are done writing the response
- // the connection should be closed.
- if (_response == null) _close();
- return;
- }
-
- // Processing a request.
- if (e == null) {
- // Indicate connection close to the HTTP parser.
- _httpParser.connectionClosed();
+ };
+ // If the client closes and we are done writing the response
+ // the connection should be closed.
+ if (_response == null) _close();
+ }
+ } else {
+ // Processing a request.
+ if (e == null) {
+ // Indicate connection close to the HTTP parser.
+ _httpParser.connectionClosed();
+ }
}
}
@@ -1473,6 +1469,7 @@ class _HttpConnection extends _HttpConnectionBase {
if (_closing) {
_socket.outputStream.onClosed = () {
_socket.close();
+ onClosed();
};
}
_response = null;
@@ -1667,6 +1664,7 @@ class _HttpClientRequest
_httpConnection._onNoPendingWrites = null;
// Ensure that any trailing data is written.
_writeDone();
+ _connection._requestDone();
}
void _streamSetNoPendingWriteHandler(callback()) {
@@ -1803,7 +1801,7 @@ class _HttpClientResponse
// TODO(sgjesse): Support digest.
if (cr.scheme == _AuthenticationScheme.BASIC) {
inputStream.onData = inputStream.read;
- inputStream.onClosed = _connection.retry;
+ _connection._retry();
return;
}
}
@@ -1897,7 +1895,7 @@ class _HttpClientResponse
}
// Drain body and redirect.
inputStream.onData = inputStream.read;
- inputStream.onClosed = _connection.redirect;
+ _connection.redirect();
} else {
throw new RedirectLimitExceededException(_connection._redirects);
}
@@ -1952,6 +1950,11 @@ class _HttpClientResponse
class _HttpClientConnection
extends _HttpConnectionBase implements HttpClientConnection {
+ static const int NONE = 0;
+ static const int REQUEST_DONE = 1;
+ static const int RESPONSE_DONE = 2;
+ static const int ALL_DONE = REQUEST_DONE | RESPONSE_DONE;
+
_HttpClientConnection(_HttpClient this._client);
void _connectionEstablished(_SocketConnection socketConn) {
@@ -1971,16 +1974,41 @@ class _HttpClientConnection
_httpParser.error = (e) => _onError(e);
}
+ bool get _isRequestDone => (_state & REQUEST_DONE) == REQUEST_DONE;
+ bool get _isResponseDone => (_state & RESPONSE_DONE) == RESPONSE_DONE;
+ bool get _isAllDone => (_state & ALL_DONE) == ALL_DONE;
+
+ void _checkSocketDone() {
+ if (_isAllDone) {
+ if (!_closing) {
+ _client._returnSocketConnection(_socketConn);
+ }
+ _socket = null;
+ _socketConn = null;
+ assert(_pendingRedirect == null || _pendingRetry == null);
+ if (_pendingRedirect != null) {
+ _doRedirect(_pendingRedirect);
+ _pendingRedirect = null;
+ } else if (_pendingRetry != null) {
+ _doRetry(_pendingRetry);
+ _pendingRetry = null;
+ }
+ }
+ }
+
+ void _requestDone() {
+ _state |= REQUEST_DONE;
+ _checkSocketDone();
+ }
+
void _responseDone() {
if (_closing) {
if (_socket != null) {
_socket.close();
}
- } else {
- _client._returnSocketConnection(_socketConn);
}
- _socket = null;
- _socketConn = null;
+ _state |= RESPONSE_DONE;
+ _checkSocketDone();
}
HttpClientRequest open(String method, Uri uri) {
@@ -2057,32 +2085,58 @@ class _HttpClientConnection
_onErrorCallback = callback;
}
- void retry() {
- if (_socketConn != null) {
- throw new HttpException("Cannot retry with body data pending");
- }
+ void _doRetry(_RedirectInfo retry) {
+ assert(_socketConn == null);
+ _request = null;
+ _response = null;
+
// Retry the URL using the same connection instance.
- _client._openUrl(_method, _request._uri, this);
+ _state = NONE;
+ _client._openUrl(retry.method, retry.location, this);
}
- void redirect([String method, Uri url]) {
- if (_socketConn != null) {
- throw new HttpException("Cannot redirect with body data pending");
+ void _retry() {
+ var retry = new _RedirectInfo(_response.statusCode, _method, _request._uri);
+ // The actual retry is postponed until both response and request
+ // are done.
+ if (_isAllDone) {
+ _doRetry(retry);
+ } else {
+ // Prepare for retry.
+ assert(_pendingRedirect == null);
+ _pendingRetry = retry;
}
+ }
+
+ void _doRedirect(_RedirectInfo redirect) {
+ assert(_socketConn == null);
+
+ if (_redirects == null) {
+ _redirects = new List<_RedirectInfo>();
+ }
+ _redirects.add(redirect);
+ _doRetry(redirect);
+ }
+
+ void redirect([String method, Uri url]) {
if (method == null) method = _method;
if (url == null) {
url = new Uri.fromString(_response.headers.value(HttpHeaders.LOCATION));
}
- if (_redirects == null) {
- _redirects = new List<_RedirectInfo>();
+ var redirect = new _RedirectInfo(_response.statusCode, method, url);
+ // The actual redirect is postponed until both response and
+ // request are done.
+ if (_isAllDone) {
+ _doRedirect(redirect);
+ } else {
+ // Prepare for redirect.
+ assert(_pendingRetry == null);
+ _pendingRedirect = redirect;
}
- _redirects.add(new _RedirectInfo(_response.statusCode, method, url));
- _request = null;
- _response = null;
- // Open redirect URL using the same connection instance.
- _client._openUrl(method, url, this);
}
+ int _state = NONE;
+
List<RedirectInfo> get redirects => _redirects;
Function _onRequest;
@@ -2100,6 +2154,8 @@ class _HttpClientConnection
bool followRedirects = true;
int maxRedirects = 5;
List<_RedirectInfo> _redirects;
+ _RedirectInfo _pendingRedirect;
+ _RedirectInfo _pendingRetry;
// Callbacks.
var requestReceived;
« no previous file with comments | « sdk/lib/io/directory_impl.dart ('k') | sdk/lib/io/http_parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698