| 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 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 | 474 |
| 475 void _connectionEstablished(Socket socket) { | 475 void _connectionEstablished(Socket socket) { |
| 476 _socket = socket; | 476 _socket = socket; |
| 477 // Register handler for socket events. | 477 // Register handler for socket events. |
| 478 _socket.onData = _onData; | 478 _socket.onData = _onData; |
| 479 _socket.onClosed = _onClosed; | 479 _socket.onClosed = _onClosed; |
| 480 _socket.onError = _onError; | 480 _socket.onError = _onError; |
| 481 } | 481 } |
| 482 | 482 |
| 483 bool _write(List<int> data, [bool copyBuffer = false]) { | 483 bool _write(List<int> data, [bool copyBuffer = false]) { |
| 484 if (!_error) { | 484 if (!_error && !_closing) { |
| 485 return _socket.outputStream.write(data, copyBuffer); | 485 return _socket.outputStream.write(data, copyBuffer); |
| 486 } | 486 } |
| 487 } | 487 } |
| 488 | 488 |
| 489 bool _writeFrom(List<int> buffer, [int offset, int len]) { | 489 bool _writeFrom(List<int> buffer, [int offset, int len]) { |
| 490 if (!_error) { | 490 if (!_error && !_closing) { |
| 491 return _socket.outputStream.writeFrom(buffer, offset, len); | 491 return _socket.outputStream.writeFrom(buffer, offset, len); |
| 492 } | 492 } |
| 493 } | 493 } |
| 494 | 494 |
| 495 bool _close() { | 495 bool _close() { |
| 496 _socket.close(); | 496 _socket.close(); |
| 497 } | 497 } |
| 498 | 498 |
| 499 void _onData() { | 499 void _onData() { |
| 500 int available = _socket.available(); | 500 int available = _socket.available(); |
| 501 if (available == 0) { | 501 if (available == 0) { |
| 502 return; | 502 return; |
| 503 } | 503 } |
| 504 | 504 |
| 505 ByteArray buffer = new ByteArray(available); | 505 ByteArray buffer = new ByteArray(available); |
| 506 int bytesRead = _socket.readList(buffer, 0, available); | 506 int bytesRead = _socket.readList(buffer, 0, available); |
| 507 if (bytesRead > 0) { | 507 if (bytesRead > 0) { |
| 508 int parsed = _httpParser.writeList(buffer, 0, bytesRead); | 508 int parsed = _httpParser.writeList(buffer, 0, bytesRead); |
| 509 if (parsed != bytesRead) { | 509 if (parsed != bytesRead) { |
| 510 // TODO(sgjesse): Error handling. | 510 // TODO(sgjesse): Error handling. |
| 511 _socket.close(); | 511 _socket.close(); |
| 512 } | 512 } |
| 513 } | 513 } |
| 514 } | 514 } |
| 515 | 515 |
| 516 void _onClosed() { | 516 void _onClosed() { |
| 517 _closing = true; |
| 517 _onConnectionClosed(null); | 518 _onConnectionClosed(null); |
| 518 } | 519 } |
| 519 | 520 |
| 520 void _onError(Exception e) { | 521 void _onError(Exception e) { |
| 521 // If an error occurs, make sure to close the socket if one is associated. | 522 // If an error occurs, make sure to close the socket if one is associated. |
| 522 _error = true; | 523 _error = true; |
| 523 if (_socket != null) { | 524 if (_socket != null) { |
| 524 _socket.close(); | 525 _socket.close(); |
| 525 } | 526 } |
| 526 _onConnectionClosed(e); | 527 _onConnectionClosed(e); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 // Don't call onClosed if onError has been called. | 583 // Don't call onClosed if onError has been called. |
| 583 onClosed(); | 584 onClosed(); |
| 584 } | 585 } |
| 585 return; | 586 return; |
| 586 } | 587 } |
| 587 | 588 |
| 588 // Processing a request. | 589 // Processing a request. |
| 589 if (e == null) { | 590 if (e == null) { |
| 590 // Indicate connection close to the HTTP parser. | 591 // Indicate connection close to the HTTP parser. |
| 591 _httpParser.connectionClosed(); | 592 _httpParser.connectionClosed(); |
| 592 _closing = true; | |
| 593 } | 593 } |
| 594 } | 594 } |
| 595 | 595 |
| 596 void _onRequestStart(String method, String uri) { | 596 void _onRequestStart(String method, String uri) { |
| 597 // Create new request and response objects for this request. | 597 // Create new request and response objects for this request. |
| 598 _request = new _HttpRequest(this); | 598 _request = new _HttpRequest(this); |
| 599 _response = new _HttpResponse(this); | 599 _response = new _HttpResponse(this); |
| 600 _request._onRequestStart(method, uri); | 600 _request._onRequestStart(method, uri); |
| 601 } | 601 } |
| 602 | 602 |
| (...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1245 sockets.addFirst(socketConn); | 1245 sockets.addFirst(socketConn); |
| 1246 socketConn._markReturned(); | 1246 socketConn._markReturned(); |
| 1247 } | 1247 } |
| 1248 | 1248 |
| 1249 Function _onOpen; | 1249 Function _onOpen; |
| 1250 Map<String, Queue<_SocketConnection>> _openSockets; | 1250 Map<String, Queue<_SocketConnection>> _openSockets; |
| 1251 Set<_SocketConnection> _activeSockets; | 1251 Set<_SocketConnection> _activeSockets; |
| 1252 Timer _evictionTimer; | 1252 Timer _evictionTimer; |
| 1253 bool _shutdown; // Has this HTTP client been shutdown? | 1253 bool _shutdown; // Has this HTTP client been shutdown? |
| 1254 } | 1254 } |
| OLD | NEW |