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 // Utility class for encoding a string into UTF-8 byte stream. | 5 // Utility class for encoding a string into UTF-8 byte stream. |
6 class _UTF8Encoder { | 6 class _UTF8Encoder { |
7 static List<int> encodeString(String string) { | 7 static List<int> encodeString(String string) { |
8 int size = _encodingSize(string); | 8 int size = _encodingSize(string); |
9 ByteArray result = new ByteArray(size); | 9 ByteArray result = new ByteArray(size); |
10 _encodeString(string, result); | 10 _encodeString(string, result); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) | 54 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) |
55 : _contentLength = -1, | 55 : _contentLength = -1, |
56 _keepAlive = false, | 56 _keepAlive = false, |
57 _headers = new Map(); | 57 _headers = new Map(); |
58 | 58 |
59 int get contentLength() => _contentLength; | 59 int get contentLength() => _contentLength; |
60 bool get keepAlive() => _keepAlive; | 60 bool get keepAlive() => _keepAlive; |
61 Map get headers() => _headers; | 61 Map get headers() => _headers; |
62 | 62 |
63 void _setHeader(String name, String value) { | 63 void _setHeader(String name, String value) { |
64 _headers[name] = value; | 64 _headers[name.toLowerCase()] = value; |
65 } | 65 } |
66 | 66 |
67 bool _write(List<int> data, bool copyBuffer) { | 67 bool _write(List<int> data, bool copyBuffer) { |
68 bool allWritten = true; | 68 bool allWritten = true; |
69 if (data.length > 0) { | 69 if (data.length > 0) { |
70 if (_contentLength < 0) { | 70 if (_contentLength < 0) { |
71 // Write chunk size if transfer encoding is chunked. | 71 // Write chunk size if transfer encoding is chunked. |
72 _writeHexString(data.length); | 72 _writeHexString(data.length); |
73 _writeCRLF(); | 73 _writeCRLF(); |
74 _httpConnection.outputStream.write(data, copyBuffer); | 74 _httpConnection.outputStream.write(data, copyBuffer); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 if (_outputStream != null) throw new HttpException("Header already sent"); | 273 if (_outputStream != null) throw new HttpException("Header already sent"); |
274 _statusCode = statusCode; | 274 _statusCode = statusCode; |
275 } | 275 } |
276 | 276 |
277 String get reasonPhrase() => _findReasonPhrase(_statusCode); | 277 String get reasonPhrase() => _findReasonPhrase(_statusCode); |
278 void set reasonPhrase(String reasonPhrase) { | 278 void set reasonPhrase(String reasonPhrase) { |
279 if (_outputStream != null) throw new HttpException("Header already sent"); | 279 if (_outputStream != null) throw new HttpException("Header already sent"); |
280 _reasonPhrase = reasonPhrase; | 280 _reasonPhrase = reasonPhrase; |
281 } | 281 } |
282 | 282 |
| 283 Date get expires() => _expires; |
| 284 void set expires(Date expires) { |
| 285 if (_outputStream != null) throw new HttpException("Header already sent"); |
| 286 _expires = expires; |
| 287 // Format "Expires" header with date in Greenwich Mean Time (GMT). |
| 288 String formatted = |
| 289 _HttpUtils.formatDate(_expires.changeTimeZone(new TimeZone.utc())); |
| 290 _setHeader("Expires", formatted); |
| 291 } |
| 292 |
283 // Set a header on the response. NOTE: If the same header is set | 293 // Set a header on the response. NOTE: If the same header is set |
284 // more than once only the last one will be part of the response. | 294 // more than once only the last one will be part of the response. |
285 void setHeader(String name, String value) { | 295 void setHeader(String name, String value) { |
286 if (_outputStream != null) return new HttpException("Header already sent"); | 296 if (_outputStream != null) return new HttpException("Header already sent"); |
287 _setHeader(name, value); | 297 if (name.toLowerCase() == "expires") { |
| 298 expires = _HttpUtils.parseDate(value); |
| 299 } else { |
| 300 _setHeader(name, value); |
| 301 } |
288 } | 302 } |
289 | 303 |
290 OutputStream get outputStream() { | 304 OutputStream get outputStream() { |
291 if (_state == DONE) throw new HttpException("Response closed"); | 305 if (_state == DONE) throw new HttpException("Response closed"); |
292 if (_outputStream == null) { | 306 if (_outputStream == null) { |
293 // Ensure that headers are written. | 307 // Ensure that headers are written. |
294 if (_state == START) { | 308 if (_state == START) { |
295 _writeHeader(); | 309 _writeHeader(); |
296 } | 310 } |
297 _outputStream = new _HttpOutputStream(this); | 311 _outputStream = new _HttpOutputStream(this); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 438 |
425 // Write headers. | 439 // Write headers. |
426 bool allWritten = _writeHeaders(); | 440 bool allWritten = _writeHeaders(); |
427 _state = HEADERS_SENT; | 441 _state = HEADERS_SENT; |
428 return allWritten; | 442 return allWritten; |
429 } | 443 } |
430 | 444 |
431 // Response status code. | 445 // Response status code. |
432 int _statusCode; | 446 int _statusCode; |
433 String _reasonPhrase; | 447 String _reasonPhrase; |
| 448 Date _expires; |
434 _HttpOutputStream _outputStream; | 449 _HttpOutputStream _outputStream; |
435 int _state; | 450 int _state; |
436 } | 451 } |
437 | 452 |
438 | 453 |
439 class _HttpInputStream extends _BaseDataInputStream implements InputStream { | 454 class _HttpInputStream extends _BaseDataInputStream implements InputStream { |
440 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { | 455 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { |
441 _checkScheduleCallbacks(); | 456 _checkScheduleCallbacks(); |
442 } | 457 } |
443 | 458 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 if (_state == START) { | 774 if (_state == START) { |
760 _writeHeader(); | 775 _writeHeader(); |
761 } | 776 } |
762 _outputStream = new _HttpOutputStream(this); | 777 _outputStream = new _HttpOutputStream(this); |
763 } | 778 } |
764 return _outputStream; | 779 return _outputStream; |
765 } | 780 } |
766 | 781 |
767 _updateHostHeader() { | 782 _updateHostHeader() { |
768 String portPart = _port == HttpClient.DEFAULT_HTTP_PORT ? "" : ":$_port"; | 783 String portPart = _port == HttpClient.DEFAULT_HTTP_PORT ? "" : ":$_port"; |
769 _setHeader("host", "$host$portPart"); | 784 _setHeader("Host", "$host$portPart"); |
770 } | 785 } |
771 | 786 |
772 // Delegate functions for the HttpOutputStream implementation. | 787 // Delegate functions for the HttpOutputStream implementation. |
773 bool _streamWrite(List<int> buffer, bool copyBuffer) { | 788 bool _streamWrite(List<int> buffer, bool copyBuffer) { |
774 return _write(buffer, copyBuffer); | 789 return _write(buffer, copyBuffer); |
775 } | 790 } |
776 | 791 |
777 bool _streamWriteFrom(List<int> buffer, int offset, int len) { | 792 bool _streamWriteFrom(List<int> buffer, int offset, int len) { |
778 return _writeList(buffer, offset, len); | 793 return _writeList(buffer, offset, len); |
779 } | 794 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 class _HttpClientResponse | 863 class _HttpClientResponse |
849 extends _HttpRequestResponseBase implements HttpClientResponse { | 864 extends _HttpRequestResponseBase implements HttpClientResponse { |
850 _HttpClientResponse(_HttpClientConnection connection) | 865 _HttpClientResponse(_HttpClientConnection connection) |
851 : super(connection) { | 866 : super(connection) { |
852 _connection = connection; | 867 _connection = connection; |
853 } | 868 } |
854 | 869 |
855 int get statusCode() => _statusCode; | 870 int get statusCode() => _statusCode; |
856 String get reasonPhrase() => _reasonPhrase; | 871 String get reasonPhrase() => _reasonPhrase; |
857 | 872 |
| 873 Date get expires() { |
| 874 String str = _headers["expires"]; |
| 875 if (str == null) return null; |
| 876 return _HttpUtils.parseDate(str); |
| 877 } |
| 878 |
| 879 Map get headers() => _headers; |
| 880 |
858 InputStream get inputStream() { | 881 InputStream get inputStream() { |
859 if (_inputStream == null) { | 882 if (_inputStream == null) { |
860 _inputStream = new _HttpInputStream(this); | 883 _inputStream = new _HttpInputStream(this); |
861 } | 884 } |
862 return _inputStream; | 885 return _inputStream; |
863 } | 886 } |
864 | 887 |
865 void _onRequestStart(String method, String uri) { | 888 void _onRequestStart(String method, String uri) { |
866 // TODO(sgjesse): Error handling | 889 // TODO(sgjesse): Error handling |
867 } | 890 } |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1159 _onError = callback; | 1182 _onError = callback; |
1160 } | 1183 } |
1161 | 1184 |
1162 Function _onOpen; | 1185 Function _onOpen; |
1163 Function _onError; | 1186 Function _onError; |
1164 Map<String, Queue<_SocketConnection>> _openSockets; | 1187 Map<String, Queue<_SocketConnection>> _openSockets; |
1165 Set<_SocketConnection> _activeSockets; | 1188 Set<_SocketConnection> _activeSockets; |
1166 Timer _evictionTimer; | 1189 Timer _evictionTimer; |
1167 bool _shutdown; // Has this HTTP client been shutdown? | 1190 bool _shutdown; // Has this HTTP client been shutdown? |
1168 } | 1191 } |
OLD | NEW |