| OLD | NEW |
| 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 // The close queue handles graceful closing of HTTP connections. When | 7 // The close queue handles graceful closing of HTTP connections. When |
| 8 // a connection is added to the queue it will enter a wait state | 8 // a connection is added to the queue it will enter a wait state |
| 9 // waiting for all data written and possibly socket shutdown from | 9 // waiting for all data written and possibly socket shutdown from |
| 10 // peer. | 10 // peer. |
| 11 class _CloseQueue { | 11 class _CloseQueue { |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 | 493 |
| 494 void _streamClose() { | 494 void _streamClose() { |
| 495 _ensureHeadersSent(); | 495 _ensureHeadersSent(); |
| 496 _state = _HttpRequestResponseBase.DONE; | 496 _state = _HttpRequestResponseBase.DONE; |
| 497 // Stop tracking no pending write events. | 497 // Stop tracking no pending write events. |
| 498 _httpConnection._onNoPendingWrites = null; | 498 _httpConnection._onNoPendingWrites = null; |
| 499 // Ensure that any trailing data is written. | 499 // Ensure that any trailing data is written. |
| 500 _writeDone(); | 500 _writeDone(); |
| 501 // Indicate to the connection that the response handling is done. | 501 // Indicate to the connection that the response handling is done. |
| 502 _httpConnection._responseClosed(); | 502 _httpConnection._responseClosed(); |
| 503 if (_streamClosedHandler != null) { | |
| 504 new Timer(0, (_) => _streamClosedHandler()); | |
| 505 } | |
| 506 } | 503 } |
| 507 | 504 |
| 508 void _streamSetNoPendingWriteHandler(callback()) { | 505 void _streamSetNoPendingWriteHandler(callback()) { |
| 509 if (_state != _HttpRequestResponseBase.DONE) { | 506 if (_state != _HttpRequestResponseBase.DONE) { |
| 510 _httpConnection._onNoPendingWrites = callback; | 507 _httpConnection._onNoPendingWrites = callback; |
| 511 } | 508 } |
| 512 } | 509 } |
| 513 | 510 |
| 514 void _streamSetClosedHandler(callback()) { | 511 void _streamSetCloseHandler(callback()) { |
| 515 _streamClosedHandler = callback; | 512 // TODO(sgjesse): Handle this. |
| 516 } | 513 } |
| 517 | 514 |
| 518 void _streamSetErrorHandler(callback(e)) { | 515 void _streamSetErrorHandler(callback(e)) { |
| 519 _streamErrorHandler = callback; | 516 _streamErrorHandler = callback; |
| 520 } | 517 } |
| 521 | 518 |
| 522 String _findReasonPhrase(int statusCode) { | 519 String _findReasonPhrase(int statusCode) { |
| 523 if (_reasonPhrase != null) { | 520 if (_reasonPhrase != null) { |
| 524 return _reasonPhrase; | 521 return _reasonPhrase; |
| 525 } | 522 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 // Write headers. | 614 // Write headers. |
| 618 _headers._finalize(_protocolVersion); | 615 _headers._finalize(_protocolVersion); |
| 619 bool allWritten = _writeHeaders(); | 616 bool allWritten = _writeHeaders(); |
| 620 _state = _HttpRequestResponseBase.HEADER_SENT; | 617 _state = _HttpRequestResponseBase.HEADER_SENT; |
| 621 return allWritten; | 618 return allWritten; |
| 622 } | 619 } |
| 623 | 620 |
| 624 int _statusCode; // Response status code. | 621 int _statusCode; // Response status code. |
| 625 String _reasonPhrase; // Response reason phrase. | 622 String _reasonPhrase; // Response reason phrase. |
| 626 _HttpOutputStream _outputStream; | 623 _HttpOutputStream _outputStream; |
| 627 Function _streamClosedHandler; | |
| 628 Function _streamErrorHandler; | 624 Function _streamErrorHandler; |
| 629 } | 625 } |
| 630 | 626 |
| 631 | 627 |
| 632 class _HttpInputStream extends _BaseDataInputStream implements InputStream { | 628 class _HttpInputStream extends _BaseDataInputStream implements InputStream { |
| 633 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { | 629 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { |
| 634 _checkScheduleCallbacks(); | 630 _checkScheduleCallbacks(); |
| 635 } | 631 } |
| 636 | 632 |
| 637 int available() { | 633 int available() { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 | 692 |
| 697 void destroy() { | 693 void destroy() { |
| 698 throw "Not implemented"; | 694 throw "Not implemented"; |
| 699 } | 695 } |
| 700 | 696 |
| 701 void set onNoPendingWrites(void callback()) { | 697 void set onNoPendingWrites(void callback()) { |
| 702 _requestOrResponse._streamSetNoPendingWriteHandler(callback); | 698 _requestOrResponse._streamSetNoPendingWriteHandler(callback); |
| 703 } | 699 } |
| 704 | 700 |
| 705 void set onClosed(void callback()) { | 701 void set onClosed(void callback()) { |
| 706 _requestOrResponse._streamSetClosedHandler(callback); | 702 _requestOrResponse._streamSetCloseHandler(callback); |
| 707 } | 703 } |
| 708 | 704 |
| 709 void set onError(void callback(e)) { | 705 void set onError(void callback(e)) { |
| 710 _requestOrResponse._streamSetErrorHandler(callback); | 706 _requestOrResponse._streamSetErrorHandler(callback); |
| 711 } | 707 } |
| 712 | 708 |
| 713 _HttpRequestResponseBase _requestOrResponse; | 709 _HttpRequestResponseBase _requestOrResponse; |
| 714 } | 710 } |
| 715 | 711 |
| 716 | 712 |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1196 } | 1192 } |
| 1197 | 1193 |
| 1198 void _streamClose() { | 1194 void _streamClose() { |
| 1199 _ensureHeadersSent(); | 1195 _ensureHeadersSent(); |
| 1200 _state = _HttpRequestResponseBase.DONE; | 1196 _state = _HttpRequestResponseBase.DONE; |
| 1201 // Stop tracking no pending write events. | 1197 // Stop tracking no pending write events. |
| 1202 _httpConnection._onNoPendingWrites = null; | 1198 _httpConnection._onNoPendingWrites = null; |
| 1203 // Ensure that any trailing data is written. | 1199 // Ensure that any trailing data is written. |
| 1204 _writeDone(); | 1200 _writeDone(); |
| 1205 _connection._requestClosed(); | 1201 _connection._requestClosed(); |
| 1206 if (_streamClosedHandler != null) { | |
| 1207 new Timer(0, (_) => _streamClosedHandler()); | |
| 1208 } | |
| 1209 } | 1202 } |
| 1210 | 1203 |
| 1211 void _streamSetNoPendingWriteHandler(callback()) { | 1204 void _streamSetNoPendingWriteHandler(callback()) { |
| 1212 if (_state != _HttpRequestResponseBase.DONE) { | 1205 if (_state != _HttpRequestResponseBase.DONE) { |
| 1213 _httpConnection._onNoPendingWrites = callback; | 1206 _httpConnection._onNoPendingWrites = callback; |
| 1214 } | 1207 } |
| 1215 } | 1208 } |
| 1216 | 1209 |
| 1217 void _streamSetClosedHandler(callback()) { | 1210 void _streamSetCloseHandler(callback()) { |
| 1218 _streamClosedHandler = callback; | 1211 // TODO(sgjesse): Handle this. |
| 1219 } | 1212 } |
| 1220 | 1213 |
| 1221 void _streamSetErrorHandler(callback(e)) { | 1214 void _streamSetErrorHandler(callback(e)) { |
| 1222 _streamErrorHandler = callback; | 1215 _streamErrorHandler = callback; |
| 1223 } | 1216 } |
| 1224 | 1217 |
| 1225 void _writeHeader() { | 1218 void _writeHeader() { |
| 1226 List<int> data; | 1219 List<int> data; |
| 1227 | 1220 |
| 1228 // Write request line. | 1221 // Write request line. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1265 // Write headers. | 1258 // Write headers. |
| 1266 _headers._finalize("1.1"); | 1259 _headers._finalize("1.1"); |
| 1267 _writeHeaders(); | 1260 _writeHeaders(); |
| 1268 _state = _HttpRequestResponseBase.HEADER_SENT; | 1261 _state = _HttpRequestResponseBase.HEADER_SENT; |
| 1269 } | 1262 } |
| 1270 | 1263 |
| 1271 String _method; | 1264 String _method; |
| 1272 Uri _uri; | 1265 Uri _uri; |
| 1273 _HttpClientConnection _connection; | 1266 _HttpClientConnection _connection; |
| 1274 _HttpOutputStream _outputStream; | 1267 _HttpOutputStream _outputStream; |
| 1275 Function _streamClosedHandler; | |
| 1276 Function _streamErrorHandler; | 1268 Function _streamErrorHandler; |
| 1277 bool _emptyBody = true; | 1269 bool _emptyBody = true; |
| 1278 } | 1270 } |
| 1279 | 1271 |
| 1280 class _HttpClientResponse | 1272 class _HttpClientResponse |
| 1281 extends _HttpRequestResponseBase | 1273 extends _HttpRequestResponseBase |
| 1282 implements HttpClientResponse { | 1274 implements HttpClientResponse { |
| 1283 _HttpClientResponse(_HttpClientConnection connection) | 1275 _HttpClientResponse(_HttpClientConnection connection) |
| 1284 : super(connection) { | 1276 : super(connection) { |
| 1285 _connection = connection; | 1277 _connection = connection; |
| (...skipping 1022 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2308 | 2300 |
| 2309 | 2301 |
| 2310 class _RedirectInfo implements RedirectInfo { | 2302 class _RedirectInfo implements RedirectInfo { |
| 2311 const _RedirectInfo(int this.statusCode, | 2303 const _RedirectInfo(int this.statusCode, |
| 2312 String this.method, | 2304 String this.method, |
| 2313 Uri this.location); | 2305 Uri this.location); |
| 2314 final int statusCode; | 2306 final int statusCode; |
| 2315 final String method; | 2307 final String method; |
| 2316 final Uri location; | 2308 final Uri location; |
| 2317 } | 2309 } |
| OLD | NEW |