OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _HttpIncoming | 7 class _HttpIncoming |
8 extends Stream<List<int>> implements StreamSink<List<int>> { | 8 extends Stream<List<int>> implements StreamSink<List<int>> { |
9 final Function _pause; | 9 final Function _pause; |
10 final Function _resume; | 10 final Function _resume; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 String get method => _incoming.method; | 154 String get method => _incoming.method; |
155 | 155 |
156 HttpSession get session { | 156 HttpSession get session { |
157 if (_session != null) { | 157 if (_session != null) { |
158 // It's already mapped, use it. | 158 // It's already mapped, use it. |
159 return _session; | 159 return _session; |
160 } | 160 } |
161 // Create session, store it in connection, and return. | 161 // Create session, store it in connection, and return. |
162 return _session = _httpServer._sessionManager.createSession(); | 162 return _session = _httpServer._sessionManager.createSession(); |
163 } | 163 } |
| 164 |
| 165 HttpConnectionInfo get connectionInfo => _httpConnection.connectionInfo; |
164 } | 166 } |
165 | 167 |
166 | 168 |
167 class _HttpClientResponse | 169 class _HttpClientResponse |
168 extends _HttpInboundMessage implements HttpClientResponse { | 170 extends _HttpInboundMessage implements HttpClientResponse { |
169 List<RedirectInfo> get redirects => _httpRequest._responseRedirects; | 171 List<RedirectInfo> get redirects => _httpRequest._responseRedirects; |
170 | 172 |
171 // The HttpClient this response belongs to. | 173 // The HttpClient this response belongs to. |
172 final _HttpClient _httpClient; | 174 final _HttpClient _httpClient; |
173 | 175 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 onError: onError, | 255 onError: onError, |
254 onDone: onDone, | 256 onDone: onDone, |
255 unsubscribeOnError: unsubscribeOnError); | 257 unsubscribeOnError: unsubscribeOnError); |
256 } | 258 } |
257 | 259 |
258 Future<Socket> detachSocket() { | 260 Future<Socket> detachSocket() { |
259 _httpClient._connectionClosed(_httpRequest._httpClientConnection); | 261 _httpClient._connectionClosed(_httpRequest._httpClientConnection); |
260 return _httpRequest._httpClientConnection.detachSocket(); | 262 return _httpRequest._httpClientConnection.detachSocket(); |
261 } | 263 } |
262 | 264 |
| 265 HttpConnectionInfo get connectionInfo => _httpRequest.connectionInfo; |
| 266 |
263 bool get _shouldAuthenticate { | 267 bool get _shouldAuthenticate { |
264 // Only try to authenticate if there is a challenge in the response. | 268 // Only try to authenticate if there is a challenge in the response. |
265 List<String> challenge = headers[HttpHeaders.WWW_AUTHENTICATE]; | 269 List<String> challenge = headers[HttpHeaders.WWW_AUTHENTICATE]; |
266 return statusCode == HttpStatus.UNAUTHORIZED && | 270 return statusCode == HttpStatus.UNAUTHORIZED && |
267 challenge != null && challenge.length == 1; | 271 challenge != null && challenge.length == 1; |
268 } | 272 } |
269 | 273 |
270 Future<HttpClientResponse> _authenticate() { | 274 Future<HttpClientResponse> _authenticate() { |
271 Future<HttpClientResponse> retryWithCredentials(_Credentials cr) { | 275 Future<HttpClientResponse> retryWithCredentials(_Credentials cr) { |
272 if (cr != null) { | 276 if (cr != null) { |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 | 439 |
436 Future<Socket> detachSocket() { | 440 Future<Socket> detachSocket() { |
437 if (_headersWritten) throw new StateError("Headers already sent"); | 441 if (_headersWritten) throw new StateError("Headers already sent"); |
438 _writeHeaders(); | 442 _writeHeaders(); |
439 var future = _httpRequest._httpConnection.detachSocket(); | 443 var future = _httpRequest._httpConnection.detachSocket(); |
440 // Close connection so the socket is 'free'. | 444 // Close connection so the socket is 'free'. |
441 close(); | 445 close(); |
442 return future; | 446 return future; |
443 } | 447 } |
444 | 448 |
| 449 HttpConnectionInfo get connectionInfo => _httpRequest.connectionInfo; |
| 450 |
445 void _fullBodyWritten() { | 451 void _fullBodyWritten() { |
446 if (!_httpRequest._incoming.fullBodyRead) { | 452 if (!_httpRequest._incoming.fullBodyRead) { |
447 _httpRequest._httpConnection._socket.destroy(); | 453 _httpRequest._httpConnection._socket.destroy(); |
448 } | 454 } |
449 } | 455 } |
450 | 456 |
451 void _writeHeader() { | 457 void _writeHeader() { |
452 writeSP() => add([_CharCode.SP]); | 458 writeSP() => add([_CharCode.SP]); |
453 writeCRLF() => add([_CharCode.CR, _CharCode.LF]); | 459 writeCRLF() => add([_CharCode.CR, _CharCode.LF]); |
454 | 460 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 if (_headersWritten) throw new StateError("Request already sent"); | 606 if (_headersWritten) throw new StateError("Request already sent"); |
601 _maxRedirects = maxRedirects; | 607 _maxRedirects = maxRedirects; |
602 } | 608 } |
603 | 609 |
604 bool get followRedirects => _followRedirects; | 610 bool get followRedirects => _followRedirects; |
605 void set followRedirects(bool followRedirects) { | 611 void set followRedirects(bool followRedirects) { |
606 if (_headersWritten) throw new StateError("Request already sent"); | 612 if (_headersWritten) throw new StateError("Request already sent"); |
607 _followRedirects = followRedirects; | 613 _followRedirects = followRedirects; |
608 } | 614 } |
609 | 615 |
| 616 HttpConnectionInfo get connectionInfo => _httpClientConnection.connectionInfo; |
| 617 |
610 void _onIncoming(_HttpIncoming incoming) { | 618 void _onIncoming(_HttpIncoming incoming) { |
611 var response = new _HttpClientResponse(incoming, | 619 var response = new _HttpClientResponse(incoming, |
612 this, | 620 this, |
613 _httpClient); | 621 _httpClient); |
614 Future<HttpClientResponse> future; | 622 Future<HttpClientResponse> future; |
615 if (followRedirects && response.isRedirect) { | 623 if (followRedirects && response.isRedirect) { |
616 if (response.redirects.length < maxRedirects) { | 624 if (response.redirects.length < maxRedirects) { |
617 // Redirect and drain response. | 625 // Redirect and drain response. |
618 future = response.reduce(null, (x, y) {}) | 626 future = response.reduce(null, (x, y) {}) |
619 .then((_) => response.redirect()); | 627 .then((_) => response.redirect()); |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 }); | 920 }); |
913 } | 921 } |
914 | 922 |
915 Future<Socket> detachSocket() { | 923 Future<Socket> detachSocket() { |
916 return new Future.immediate( | 924 return new Future.immediate( |
917 new _DetachedSocket(_socket, _parser.detachIncoming())); | 925 new _DetachedSocket(_socket, _parser.detachIncoming())); |
918 } | 926 } |
919 | 927 |
920 // TODO(ajohnsen): Remove socket from httpclient. | 928 // TODO(ajohnsen): Remove socket from httpclient. |
921 void destroy() => _socket.destroy(); | 929 void destroy() => _socket.destroy(); |
| 930 |
| 931 HttpConnectionInfo get connectionInfo => _HttpConnectionInfo.create(_socket); |
922 } | 932 } |
923 | 933 |
924 class _ConnnectionInfo { | 934 class _ConnnectionInfo { |
925 _ConnnectionInfo(_HttpClientConnection this.connection, _Proxy this.proxy); | 935 _ConnnectionInfo(_HttpClientConnection this.connection, _Proxy this.proxy); |
926 final _HttpClientConnection connection; | 936 final _HttpClientConnection connection; |
927 final _Proxy proxy; | 937 final _Proxy proxy; |
928 } | 938 } |
929 | 939 |
930 | 940 |
931 class _HttpClient implements HttpClient { | 941 class _HttpClient implements HttpClient { |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1229 // Remove connection from server. | 1239 // Remove connection from server. |
1230 _httpServer._connectionClosed(this); | 1240 _httpServer._connectionClosed(this); |
1231 | 1241 |
1232 _HttpDetachedIncoming detachedIncoming = _httpParser.detachIncoming(); | 1242 _HttpDetachedIncoming detachedIncoming = _httpParser.detachIncoming(); |
1233 | 1243 |
1234 return _writeDoneFuture.then((_) { | 1244 return _writeDoneFuture.then((_) { |
1235 return new _DetachedSocket(_socket, detachedIncoming); | 1245 return new _DetachedSocket(_socket, detachedIncoming); |
1236 }); | 1246 }); |
1237 } | 1247 } |
1238 | 1248 |
| 1249 HttpConnectionInfo get connectionInfo => _HttpConnectionInfo.create(_socket); |
| 1250 |
1239 bool get _isActive => _state == _ACTIVE; | 1251 bool get _isActive => _state == _ACTIVE; |
1240 bool get _isIdle => _state == _IDLE; | 1252 bool get _isIdle => _state == _IDLE; |
1241 bool get _isClosing => _state == _CLOSING; | 1253 bool get _isClosing => _state == _CLOSING; |
1242 } | 1254 } |
1243 | 1255 |
1244 | 1256 |
1245 // HTTP server waiting for socket connections. | 1257 // HTTP server waiting for socket connections. |
1246 class _HttpServer extends Stream<HttpRequest> implements HttpServer { | 1258 class _HttpServer extends Stream<HttpRequest> implements HttpServer { |
1247 | 1259 |
1248 static Future<HttpServer> bind(String host, int port, int backlog) { | 1260 static Future<HttpServer> bind(String host, int port, int backlog) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1404 const _Proxy(this.host, this.port) : isDirect = false; | 1416 const _Proxy(this.host, this.port) : isDirect = false; |
1405 const _Proxy.direct() : host = null, port = null, isDirect = true; | 1417 const _Proxy.direct() : host = null, port = null, isDirect = true; |
1406 | 1418 |
1407 final String host; | 1419 final String host; |
1408 final int port; | 1420 final int port; |
1409 final bool isDirect; | 1421 final bool isDirect; |
1410 } | 1422 } |
1411 | 1423 |
1412 | 1424 |
1413 class _HttpConnectionInfo implements HttpConnectionInfo { | 1425 class _HttpConnectionInfo implements HttpConnectionInfo { |
| 1426 static _HttpConnectionInfo create(Socket socket) { |
| 1427 if (socket == null) return null; |
| 1428 try { |
| 1429 _HttpConnectionInfo info = new _HttpConnectionInfo._(); |
| 1430 info.remoteHost = socket.remoteHost; |
| 1431 info.remotePort = socket.remotePort; |
| 1432 info.localPort = socket.port; |
| 1433 return info; |
| 1434 } catch (e) { } |
| 1435 return null; |
| 1436 } |
| 1437 |
| 1438 _HttpConnectionInfo._(); |
| 1439 |
1414 String remoteHost; | 1440 String remoteHost; |
1415 int remotePort; | 1441 int remotePort; |
1416 int localPort; | 1442 int localPort; |
1417 } | 1443 } |
1418 | 1444 |
| 1445 |
1419 class _DetachedSocket implements Socket { | 1446 class _DetachedSocket implements Socket { |
1420 final Stream<List<int>> _incoming; | 1447 final Stream<List<int>> _incoming; |
1421 final Socket _socket; | 1448 final Socket _socket; |
1422 | 1449 |
1423 _DetachedSocket(this._socket, this._incoming); | 1450 _DetachedSocket(this._socket, this._incoming); |
1424 | 1451 |
1425 StreamSubscription<List<int>> listen(void onData(List<int> event), | 1452 StreamSubscription<List<int>> listen(void onData(List<int> event), |
1426 {void onError(AsyncError error), | 1453 {void onError(AsyncError error), |
1427 void onDone(), | 1454 void onDone(), |
1428 bool unsubscribeOnError}) { | 1455 bool unsubscribeOnError}) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1551 | 1578 |
1552 | 1579 |
1553 class _RedirectInfo implements RedirectInfo { | 1580 class _RedirectInfo implements RedirectInfo { |
1554 const _RedirectInfo(int this.statusCode, | 1581 const _RedirectInfo(int this.statusCode, |
1555 String this.method, | 1582 String this.method, |
1556 Uri this.location); | 1583 Uri this.location); |
1557 final int statusCode; | 1584 final int statusCode; |
1558 final String method; | 1585 final String method; |
1559 final Uri location; | 1586 final Uri location; |
1560 } | 1587 } |
OLD | NEW |