Chromium Code Reviews| 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 _HttpHeaders implements HttpHeaders { | |
| 6 _HttpHeaders() : _headers = new Map<String, List<String>>(); | |
| 7 | |
| 8 List<String> operator[](String name) { | |
| 9 name = name.toLowerCase(); | |
| 10 return _headers[name]; | |
| 11 } | |
| 12 | |
| 13 void add(String name, Object value) { | |
| 14 if (value is List) { | |
| 15 for (int i = 0; i < value.length; i++) { | |
| 16 _add(name, vlaie[i]); | |
| 17 } | |
| 18 } else { | |
| 19 _add(name, value); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 void set(String name, Object value) { | |
| 24 removeAll(name); | |
| 25 add(name, value); | |
| 26 } | |
| 27 | |
| 28 void remove(String name, Object value) { | |
| 29 name = name.toLowerCase(); | |
| 30 List<String> values = _headers[name]; | |
| 31 if (values != null) { | |
| 32 int index = values.indexOf(value); | |
| 33 if (index != -1) { | |
| 34 values.removeRange(index, 1); | |
| 35 } | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 void removeAll(String name) { | |
| 40 name = name.toLowerCase(); | |
| 41 _headers.remove(name); | |
| 42 } | |
| 43 | |
| 44 String get host() => _host; | |
| 45 void set host(String host) { | |
| 46 _host = host; | |
| 47 _updateHostHeader(); | |
| 48 } | |
| 49 | |
| 50 int get port() => _port; | |
| 51 void set port(int port) { | |
| 52 _port = port; | |
| 53 _updateHostHeader(); | |
| 54 } | |
| 55 | |
| 56 Date get expires() { | |
| 57 if (_expires == null) { | |
| 58 List<String> values = _headers["expires"]; | |
| 59 if (values != null) { | |
| 60 _expires = _HttpUtils.parseDate(values[0]); | |
| 61 } | |
| 62 } | |
| 63 return _expires; | |
| 64 } | |
| 65 | |
| 66 void set expires(Date expires) { | |
| 67 _expires = expires; | |
| 68 // Format "Expires" header with date in Greenwich Mean Time (GMT). | |
| 69 String formatted = | |
| 70 _HttpUtils.formatDate(_expires.changeTimeZone(new TimeZone.utc())); | |
| 71 _set("expires", formatted); | |
| 72 } | |
| 73 | |
| 74 void _add(String name, Object value) { | |
| 75 // TODO(sgjesse): Add immutable state throw HttpException is immutable. | |
| 76 if (name.toLowerCase() == "expires") { | |
| 77 if (value is Date) { | |
| 78 expires = value; | |
| 79 } else if (value is String) { | |
| 80 expires = _HttpUtils.parseDate(value); | |
| 81 } else { | |
| 82 throw new HttpException("Unexpected type for header named $name"); | |
| 83 } | |
| 84 } else if (name.toLowerCase() == "host") { | |
| 85 int pos = value.indexOf(":"); | |
| 86 if (pos == -1) { | |
| 87 _host = value; | |
| 88 _port = HttpClient.DEFAULT_HTTP_PORT; | |
| 89 } else { | |
| 90 _host = value.substring(0, pos); | |
| 91 if (pos + 1 == value.length) { | |
| 92 _port = HttpClient.DEFAULT_HTTP_PORT; | |
| 93 } else { | |
| 94 _port = Math.parseInt(value.substring(pos + 1)); | |
| 95 } | |
| 96 } | |
| 97 _updateHostHeader(); | |
| 98 } else { | |
| 99 name = name.toLowerCase(); | |
| 100 List<String> values = _headers[name]; | |
| 101 if (values == null) { | |
| 102 values = new List<String>(); | |
| 103 _headers[name] = values; | |
| 104 } | |
| 105 values.add(value.toString()); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void _set(String name, String value) { | |
| 110 name = name.toLowerCase(); | |
| 111 List<String> values = new List<String>(); | |
| 112 _headers[name] = values; | |
| 113 values.add(value); | |
| 114 } | |
| 115 | |
| 116 _updateHostHeader() { | |
| 117 String portPart = _port == HttpClient.DEFAULT_HTTP_PORT ? "" : ":$_port"; | |
| 118 _set("host", "$host$portPart"); | |
| 119 } | |
| 120 | |
| 121 _write(_HttpConnectionBase connection) { | |
| 122 final COLONSP = const [_CharCode.COLON, _CharCode.SP]; | |
| 123 final COMMASP = const [_CharCode.COMMA, _CharCode.SP]; | |
| 124 final CRLF = const [_CharCode.CR, _CharCode.LF]; | |
| 125 | |
| 126 // Format headers. | |
| 127 _headers.forEach((String name, List<String> values) { | |
| 128 List<int> data; | |
| 129 data = name.charCodes(); | |
| 130 connection._write(data); | |
| 131 connection._write(COLONSP); | |
| 132 for (int i = 0; i < values.length; i++) { | |
| 133 if (i > 0) { | |
| 134 connection._write(COMMASP); | |
| 135 } | |
| 136 data = values[i].charCodes(); | |
| 137 connection._write(data); | |
| 138 } | |
| 139 connection._write(CRLF); | |
| 140 }); | |
| 141 } | |
| 142 | |
| 143 String toString() { | |
|
Anders Johnsen
2012/04/16 11:50:46
Looks good, and yes, let's use \n for line feed he
Søren Gjesse
2012/04/16 12:17:31
Good catch. Added return of sb.toString().
| |
| 144 StringBuffer sb = new StringBuffer(); | |
| 145 _headers.forEach((String name, List<String> values) { | |
| 146 sb.add(name); | |
| 147 sb.add(": "); | |
| 148 for (int i = 0; i < values.length; i++) { | |
| 149 if (i > 0) { | |
| 150 sb.add(": "); | |
| 151 } | |
| 152 sb.add(values[i]); | |
| 153 } | |
| 154 sb.add("\n"); | |
| 155 }); | |
| 156 } | |
| 157 | |
| 158 Map<String, List<String>> _headers; | |
| 159 | |
| 160 String _host; | |
| 161 int _port; | |
| 162 Date _expires; | |
| 163 } | |
| 164 | |
| 165 | |
| 5 class _HttpRequestResponseBase { | 166 class _HttpRequestResponseBase { |
| 6 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) | 167 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) |
| 7 : _contentLength = -1, | 168 : _contentLength = -1, |
| 8 _headers = new Map(); | 169 _headers = new _HttpHeaders(); |
| 9 | 170 |
| 10 int get contentLength() => _contentLength; | 171 int get contentLength() => _contentLength; |
| 11 Map get headers() => _headers; | 172 HttpHeaders get headers() => _headers; |
| 12 | |
| 13 void _setHeader(String name, String value) { | |
| 14 _headers[name.toLowerCase()] = value; | |
| 15 } | |
| 16 | 173 |
| 17 bool _write(List<int> data, bool copyBuffer) { | 174 bool _write(List<int> data, bool copyBuffer) { |
| 18 bool allWritten = true; | 175 bool allWritten = true; |
| 19 if (data.length > 0) { | 176 if (data.length > 0) { |
| 20 if (_contentLength < 0) { | 177 if (_contentLength < 0) { |
| 21 // Write chunk size if transfer encoding is chunked. | 178 // Write chunk size if transfer encoding is chunked. |
| 22 _writeHexString(data.length); | 179 _writeHexString(data.length); |
| 23 _writeCRLF(); | 180 _writeCRLF(); |
| 24 _httpConnection._write(data, copyBuffer); | 181 _httpConnection._write(data, copyBuffer); |
| 25 allWritten = _writeCRLF(); | 182 allWritten = _writeCRLF(); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 49 bool _writeDone() { | 206 bool _writeDone() { |
| 50 bool allWritten = true; | 207 bool allWritten = true; |
| 51 if (_contentLength < 0) { | 208 if (_contentLength < 0) { |
| 52 // Terminate the content if transfer encoding is chunked. | 209 // Terminate the content if transfer encoding is chunked. |
| 53 allWritten = _httpConnection._write(_Const.END_CHUNKED); | 210 allWritten = _httpConnection._write(_Const.END_CHUNKED); |
| 54 } | 211 } |
| 55 return allWritten; | 212 return allWritten; |
| 56 } | 213 } |
| 57 | 214 |
| 58 bool _writeHeaders() { | 215 bool _writeHeaders() { |
| 59 List<int> data; | 216 _headers._write(_httpConnection); |
| 60 | |
| 61 // Format headers. | |
| 62 _headers.forEach((String name, String value) { | |
| 63 data = name.charCodes(); | |
| 64 _httpConnection._write(data); | |
| 65 data = ": ".charCodes(); | |
| 66 _httpConnection._write(data); | |
| 67 data = value.charCodes(); | |
| 68 _httpConnection._write(data); | |
| 69 _writeCRLF(); | |
| 70 }); | |
| 71 // Terminate header. | 217 // Terminate header. |
| 72 return _writeCRLF(); | 218 return _writeCRLF(); |
| 73 } | 219 } |
| 74 | 220 |
| 75 bool _writeHexString(int x) { | 221 bool _writeHexString(int x) { |
| 76 final List<int> hexDigits = [0x30, 0x31, 0x32, 0x33, 0x34, | 222 final List<int> hexDigits = [0x30, 0x31, 0x32, 0x33, 0x34, |
| 77 0x35, 0x36, 0x37, 0x38, 0x39, | 223 0x35, 0x36, 0x37, 0x38, 0x39, |
| 78 0x41, 0x42, 0x43, 0x44, 0x45, 0x46]; | 224 0x41, 0x42, 0x43, 0x44, 0x45, 0x46]; |
| 79 ByteArray hex = new ByteArray(10); | 225 ByteArray hex = new ByteArray(10); |
| 80 int index = hex.length; | 226 int index = hex.length; |
| 81 while (x > 0) { | 227 while (x > 0) { |
| 82 index--; | 228 index--; |
| 83 hex[index] = hexDigits[x % 16]; | 229 hex[index] = hexDigits[x % 16]; |
| 84 x = x >> 4; | 230 x = x >> 4; |
| 85 } | 231 } |
| 86 return _httpConnection._writeFrom(hex, index, hex.length - index); | 232 return _httpConnection._writeFrom(hex, index, hex.length - index); |
| 87 } | 233 } |
| 88 | 234 |
| 89 bool _writeCRLF() { | 235 bool _writeCRLF() { |
| 90 final CRLF = const [_CharCode.CR, _CharCode.LF]; | 236 final CRLF = const [_CharCode.CR, _CharCode.LF]; |
| 91 return _httpConnection._write(CRLF); | 237 return _httpConnection._write(CRLF); |
| 92 } | 238 } |
| 93 | 239 |
| 94 bool _writeSP() { | 240 bool _writeSP() { |
| 95 final SP = const [_CharCode.SP]; | 241 final SP = const [_CharCode.SP]; |
| 96 return _httpConnection._write(SP); | 242 return _httpConnection._write(SP); |
| 97 } | 243 } |
| 98 | 244 |
| 99 _HttpConnectionBase _httpConnection; | 245 _HttpConnectionBase _httpConnection; |
| 100 Map<String, String> _headers; | 246 _HttpHeaders _headers; |
| 101 | 247 |
| 102 // Length of the content body. If this is set to -1 (default value) | 248 // Length of the content body. If this is set to -1 (default value) |
| 103 // when starting to send data chunked transfer encoding will be | 249 // when starting to send data chunked transfer encoding will be |
| 104 // used. | 250 // used. |
| 105 int _contentLength; | 251 int _contentLength; |
| 106 } | 252 } |
| 107 | 253 |
| 108 | 254 |
| 109 // Parsed HTTP request providing information on the HTTP headers. | 255 // Parsed HTTP request providing information on the HTTP headers. |
| 110 class _HttpRequest extends _HttpRequestResponseBase implements HttpRequest { | 256 class _HttpRequest extends _HttpRequestResponseBase implements HttpRequest { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 123 return _inputStream; | 269 return _inputStream; |
| 124 } | 270 } |
| 125 | 271 |
| 126 void _onRequestStart(String method, String uri, String version) { | 272 void _onRequestStart(String method, String uri, String version) { |
| 127 _method = method; | 273 _method = method; |
| 128 _uri = uri; | 274 _uri = uri; |
| 129 _parseRequestUri(uri); | 275 _parseRequestUri(uri); |
| 130 } | 276 } |
| 131 | 277 |
| 132 void _onHeaderReceived(String name, String value) { | 278 void _onHeaderReceived(String name, String value) { |
| 133 _setHeader(name, value); | 279 _headers.add(name, value); |
| 134 } | 280 } |
| 135 | 281 |
| 136 void _onHeadersComplete() { | 282 void _onHeadersComplete() { |
| 137 // Prepare for receiving data. | 283 // Prepare for receiving data. |
| 138 _buffer = new _BufferList(); | 284 _buffer = new _BufferList(); |
| 139 } | 285 } |
| 140 | 286 |
| 141 void _onDataReceived(List<int> data) { | 287 void _onDataReceived(List<int> data) { |
| 142 _buffer.add(data); | 288 _buffer.add(data); |
| 143 if (_inputStream != null) _inputStream._dataReceived(); | 289 if (_inputStream != null) _inputStream._dataReceived(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 if (_outputStream != null) throw new HttpException("Header already sent"); | 358 if (_outputStream != null) throw new HttpException("Header already sent"); |
| 213 _statusCode = statusCode; | 359 _statusCode = statusCode; |
| 214 } | 360 } |
| 215 | 361 |
| 216 String get reasonPhrase() => _findReasonPhrase(_statusCode); | 362 String get reasonPhrase() => _findReasonPhrase(_statusCode); |
| 217 void set reasonPhrase(String reasonPhrase) { | 363 void set reasonPhrase(String reasonPhrase) { |
| 218 if (_outputStream != null) throw new HttpException("Header already sent"); | 364 if (_outputStream != null) throw new HttpException("Header already sent"); |
| 219 _reasonPhrase = reasonPhrase; | 365 _reasonPhrase = reasonPhrase; |
| 220 } | 366 } |
| 221 | 367 |
| 222 Date get expires() => _expires; | |
| 223 void set expires(Date expires) { | |
| 224 if (_outputStream != null) throw new HttpException("Header already sent"); | |
| 225 _expires = expires; | |
| 226 // Format "Expires" header with date in Greenwich Mean Time (GMT). | |
| 227 String formatted = | |
| 228 _HttpUtils.formatDate(_expires.changeTimeZone(new TimeZone.utc())); | |
| 229 _setHeader("Expires", formatted); | |
| 230 } | |
| 231 | |
| 232 // Set a header on the response. NOTE: If the same header is set | |
| 233 // more than once only the last one will be part of the response. | |
| 234 void setHeader(String name, String value) { | |
| 235 if (_outputStream != null) return new HttpException("Header already sent"); | |
| 236 if (name.toLowerCase() == "expires") { | |
| 237 expires = _HttpUtils.parseDate(value); | |
| 238 } else { | |
| 239 _setHeader(name, value); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 OutputStream get outputStream() { | 368 OutputStream get outputStream() { |
| 244 if (_state == DONE) throw new HttpException("Response closed"); | 369 if (_state == DONE) throw new HttpException("Response closed"); |
| 245 if (_outputStream == null) { | 370 if (_outputStream == null) { |
| 246 // Ensure that headers are written. | 371 // Ensure that headers are written. |
| 247 if (_state == START) { | 372 if (_state == START) { |
| 248 _writeHeader(); | 373 _writeHeader(); |
| 249 } | 374 } |
| 250 _outputStream = new _HttpOutputStream(this); | 375 _outputStream = new _HttpOutputStream(this); |
| 251 } | 376 } |
| 252 return _outputStream; | 377 return _outputStream; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 _writeSP(); | 481 _writeSP(); |
| 357 data = _statusCode.toString().charCodes(); | 482 data = _statusCode.toString().charCodes(); |
| 358 _httpConnection._write(data); | 483 _httpConnection._write(data); |
| 359 _writeSP(); | 484 _writeSP(); |
| 360 data = reasonPhrase.charCodes(); | 485 data = reasonPhrase.charCodes(); |
| 361 _httpConnection._write(data); | 486 _httpConnection._write(data); |
| 362 _writeCRLF(); | 487 _writeCRLF(); |
| 363 | 488 |
| 364 // Determine the value of the "Connection" header. | 489 // Determine the value of the "Connection" header. |
| 365 if (_protocolVersion == "1.1" && !_persistentConnection) { | 490 if (_protocolVersion == "1.1" && !_persistentConnection) { |
| 366 setHeader("Connection", "close"); | 491 _headers.set("Connection", "close"); |
| 367 } else if (_protocolVersion == "1.0" && _persistentConnection) { | 492 } else if (_protocolVersion == "1.0" && _persistentConnection) { |
| 368 setHeader("Connection", "keep-alive"); | 493 _headers.set("Connection", "keep-alive"); |
| 369 } | 494 } |
| 370 // Determine the value of the "Transfer-Encoding" header based on | 495 // Determine the value of the "Transfer-Encoding" header based on |
| 371 // whether the content length is known. | 496 // whether the content length is known. |
| 372 if (_contentLength >= 0) { | 497 if (_contentLength >= 0) { |
| 373 setHeader("Content-Length", _contentLength.toString()); | 498 _headers.set("Content-Length", _contentLength.toString()); |
| 374 } else { | 499 } else { |
| 375 setHeader("Transfer-Encoding", "chunked"); | 500 _headers.set("Transfer-Encoding", "chunked"); |
| 376 } | 501 } |
| 377 | 502 |
| 378 // Write headers. | 503 // Write headers. |
| 379 bool allWritten = _writeHeaders(); | 504 bool allWritten = _writeHeaders(); |
| 380 _state = HEADERS_SENT; | 505 _state = HEADERS_SENT; |
| 381 return allWritten; | 506 return allWritten; |
| 382 } | 507 } |
| 383 | 508 |
| 384 // Response status code. | 509 // Response status code. |
| 385 int _statusCode; | 510 int _statusCode; |
| 386 String _reasonPhrase; | 511 String _reasonPhrase; |
| 387 String _protocolVersion; | 512 String _protocolVersion; |
| 388 Date _expires; | |
| 389 bool _persistentConnection; | 513 bool _persistentConnection; |
| 390 _HttpOutputStream _outputStream; | 514 _HttpOutputStream _outputStream; |
| 391 int _state; | 515 int _state; |
| 392 Function _streamErrorHandler; | 516 Function _streamErrorHandler; |
| 393 } | 517 } |
| 394 | 518 |
| 395 | 519 |
| 396 class _HttpInputStream extends _BaseDataInputStream implements InputStream { | 520 class _HttpInputStream extends _BaseDataInputStream implements InputStream { |
| 397 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { | 521 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { |
| 398 _checkScheduleCallbacks(); | 522 _checkScheduleCallbacks(); |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 _state = START { | 850 _state = START { |
| 727 _connection = connection; | 851 _connection = connection; |
| 728 // Default GET requests to have no content. | 852 // Default GET requests to have no content. |
| 729 if (_method == "GET") { | 853 if (_method == "GET") { |
| 730 _contentLength = 0; | 854 _contentLength = 0; |
| 731 } | 855 } |
| 732 } | 856 } |
| 733 | 857 |
| 734 void set contentLength(int contentLength) => _contentLength = contentLength; | 858 void set contentLength(int contentLength) => _contentLength = contentLength; |
| 735 | 859 |
| 736 String get host() => _host; | |
| 737 void set host(String host) { | |
| 738 _host = host; | |
| 739 _updateHostHeader(); | |
| 740 } | |
| 741 | |
| 742 int get port() => _port; | |
| 743 void set port(int port) { | |
| 744 _port = port; | |
| 745 _updateHostHeader(); | |
| 746 } | |
| 747 | |
| 748 void setHeader(String name, String value) { | |
| 749 if (_state != START) throw new HttpException("Header already sent"); | |
| 750 if (name.toLowerCase() == "host") { | |
| 751 int pos = value.indexOf(":"); | |
| 752 if (pos == -1) { | |
| 753 _host = value; | |
| 754 _port = HttpClient.DEFAULT_HTTP_PORT; | |
| 755 } else { | |
| 756 _host = value.substring(0, pos); | |
| 757 if (pos + 1 == value.length) { | |
| 758 _port = HttpClient.DEFAULT_HTTP_PORT; | |
| 759 } else { | |
| 760 _port = Math.parseInt(value.substring(pos + 1)); | |
| 761 } | |
| 762 } | |
| 763 _updateHostHeader(); | |
| 764 return; | |
| 765 } | |
| 766 _setHeader(name, value); | |
| 767 } | |
| 768 | |
| 769 OutputStream get outputStream() { | 860 OutputStream get outputStream() { |
| 770 if (_state == DONE) throw new HttpException("Request closed"); | 861 if (_state == DONE) throw new HttpException("Request closed"); |
| 771 if (_outputStream == null) { | 862 if (_outputStream == null) { |
| 772 // Ensure that headers are written. | 863 // Ensure that headers are written. |
| 773 if (_state == START) { | 864 if (_state == START) { |
| 774 _writeHeader(); | 865 _writeHeader(); |
| 775 } | 866 } |
| 776 _outputStream = new _HttpOutputStream(this); | 867 _outputStream = new _HttpOutputStream(this); |
| 777 } | 868 } |
| 778 return _outputStream; | 869 return _outputStream; |
| 779 } | 870 } |
| 780 | 871 |
| 781 _updateHostHeader() { | |
| 782 String portPart = _port == HttpClient.DEFAULT_HTTP_PORT ? "" : ":$_port"; | |
| 783 _setHeader("Host", "$host$portPart"); | |
| 784 } | |
| 785 | |
| 786 // Delegate functions for the HttpOutputStream implementation. | 872 // Delegate functions for the HttpOutputStream implementation. |
| 787 bool _streamWrite(List<int> buffer, bool copyBuffer) { | 873 bool _streamWrite(List<int> buffer, bool copyBuffer) { |
| 788 return _write(buffer, copyBuffer); | 874 return _write(buffer, copyBuffer); |
| 789 } | 875 } |
| 790 | 876 |
| 791 bool _streamWriteFrom(List<int> buffer, int offset, int len) { | 877 bool _streamWriteFrom(List<int> buffer, int offset, int len) { |
| 792 return _writeList(buffer, offset, len); | 878 return _writeList(buffer, offset, len); |
| 793 } | 879 } |
| 794 | 880 |
| 795 void _streamClose() { | 881 void _streamClose() { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 823 _writeSP(); | 909 _writeSP(); |
| 824 data = _uri.toString().charCodes(); | 910 data = _uri.toString().charCodes(); |
| 825 _httpConnection._write(data); | 911 _httpConnection._write(data); |
| 826 _writeSP(); | 912 _writeSP(); |
| 827 _httpConnection._write(_Const.HTTP11); | 913 _httpConnection._write(_Const.HTTP11); |
| 828 _writeCRLF(); | 914 _writeCRLF(); |
| 829 | 915 |
| 830 // Determine the value of the "Transfer-Encoding" header based on | 916 // Determine the value of the "Transfer-Encoding" header based on |
| 831 // whether the content length is known. | 917 // whether the content length is known. |
| 832 if (_contentLength >= 0) { | 918 if (_contentLength >= 0) { |
| 833 setHeader("Content-Length", _contentLength.toString()); | 919 _headers.set("Content-Length", _contentLength.toString()); |
| 834 } else { | 920 } else { |
| 835 setHeader("Transfer-Encoding", "chunked"); | 921 _headers.set("Transfer-Encoding", "chunked"); |
| 836 } | 922 } |
| 837 | 923 |
| 838 // Write headers. | 924 // Write headers. |
| 839 _writeHeaders(); | 925 _writeHeaders(); |
| 840 _state = HEADERS_SENT; | 926 _state = HEADERS_SENT; |
| 841 } | 927 } |
| 842 | 928 |
| 843 String _method; | 929 String _method; |
| 844 String _uri; | 930 String _uri; |
| 845 String _host; | |
| 846 int _port; | |
| 847 _HttpClientConnection _connection; | 931 _HttpClientConnection _connection; |
| 848 _HttpOutputStream _outputStream; | 932 _HttpOutputStream _outputStream; |
| 849 int _state; | 933 int _state; |
| 850 Function _streamErrorHandler; | 934 Function _streamErrorHandler; |
| 851 } | 935 } |
| 852 | 936 |
| 853 | 937 |
| 854 class _HttpClientResponse | 938 class _HttpClientResponse |
| 855 extends _HttpRequestResponseBase implements HttpClientResponse { | 939 extends _HttpRequestResponseBase implements HttpClientResponse { |
| 856 _HttpClientResponse(_HttpClientConnection connection) | 940 _HttpClientResponse(_HttpClientConnection connection) |
| 857 : super(connection) { | 941 : super(connection) { |
| 858 _connection = connection; | 942 _connection = connection; |
| 859 } | 943 } |
| 860 | 944 |
| 861 int get statusCode() => _statusCode; | 945 int get statusCode() => _statusCode; |
| 862 String get reasonPhrase() => _reasonPhrase; | 946 String get reasonPhrase() => _reasonPhrase; |
| 863 | 947 |
| 864 Date get expires() { | |
| 865 String str = _headers["expires"]; | |
| 866 if (str == null) return null; | |
| 867 return _HttpUtils.parseDate(str); | |
| 868 } | |
| 869 | |
| 870 Map get headers() => _headers; | |
| 871 | |
| 872 InputStream get inputStream() { | 948 InputStream get inputStream() { |
| 873 if (_inputStream == null) { | 949 if (_inputStream == null) { |
| 874 _inputStream = new _HttpInputStream(this); | 950 _inputStream = new _HttpInputStream(this); |
| 875 } | 951 } |
| 876 return _inputStream; | 952 return _inputStream; |
| 877 } | 953 } |
| 878 | 954 |
| 879 void _onRequestStart(String method, String uri, String version) { | 955 void _onRequestStart(String method, String uri, String version) { |
| 880 // TODO(sgjesse): Error handling | 956 // TODO(sgjesse): Error handling |
| 881 } | 957 } |
| 882 | 958 |
| 883 void _onResponseStart(int statusCode, String reasonPhrase, String version) { | 959 void _onResponseStart(int statusCode, String reasonPhrase, String version) { |
| 884 _statusCode = statusCode; | 960 _statusCode = statusCode; |
| 885 _reasonPhrase = reasonPhrase; | 961 _reasonPhrase = reasonPhrase; |
| 886 } | 962 } |
| 887 | 963 |
| 888 void _onHeaderReceived(String name, String value) { | 964 void _onHeaderReceived(String name, String value) { |
| 889 _setHeader(name, value); | 965 _headers.add(name, value); |
| 890 } | 966 } |
| 891 | 967 |
| 892 void _onHeadersComplete() { | 968 void _onHeadersComplete() { |
| 893 _buffer = new _BufferList(); | 969 _buffer = new _BufferList(); |
| 894 if (_connection._onResponse != null) { | 970 if (_connection._onResponse != null) { |
| 895 _connection._onResponse(this); | 971 _connection._onResponse(this); |
| 896 } | 972 } |
| 897 } | 973 } |
| 898 | 974 |
| 899 void _onDataReceived(List<int> data) { | 975 void _onDataReceived(List<int> data) { |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1142 return "$host:$port"; | 1218 return "$host:$port"; |
| 1143 } | 1219 } |
| 1144 | 1220 |
| 1145 HttpClientConnection _prepareHttpClientConnection( | 1221 HttpClientConnection _prepareHttpClientConnection( |
| 1146 String host, int port, String method, String path) { | 1222 String host, int port, String method, String path) { |
| 1147 | 1223 |
| 1148 void _connectionOpened(_SocketConnection socketConn, | 1224 void _connectionOpened(_SocketConnection socketConn, |
| 1149 _HttpClientConnection connection) { | 1225 _HttpClientConnection connection) { |
| 1150 connection._connectionEstablished(socketConn); | 1226 connection._connectionEstablished(socketConn); |
| 1151 HttpClientRequest request = connection.open(method, path); | 1227 HttpClientRequest request = connection.open(method, path); |
| 1152 request.host = host; | 1228 request.headers.host = host; |
| 1153 request.port = port; | 1229 request.headers.port = port; |
| 1154 if (connection._onRequest != null) { | 1230 if (connection._onRequest != null) { |
| 1155 connection._onRequest(request); | 1231 connection._onRequest(request); |
| 1156 } else { | 1232 } else { |
| 1157 request.outputStream.close(); | 1233 request.outputStream.close(); |
| 1158 } | 1234 } |
| 1159 } | 1235 } |
| 1160 | 1236 |
| 1161 _HttpClientConnection connection = new _HttpClientConnection(this); | 1237 _HttpClientConnection connection = new _HttpClientConnection(this); |
| 1162 | 1238 |
| 1163 // If there are active connections for this key get the first one | 1239 // If there are active connections for this key get the first one |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1243 sockets.addFirst(socketConn); | 1319 sockets.addFirst(socketConn); |
| 1244 socketConn._markReturned(); | 1320 socketConn._markReturned(); |
| 1245 } | 1321 } |
| 1246 | 1322 |
| 1247 Function _onOpen; | 1323 Function _onOpen; |
| 1248 Map<String, Queue<_SocketConnection>> _openSockets; | 1324 Map<String, Queue<_SocketConnection>> _openSockets; |
| 1249 Set<_SocketConnection> _activeSockets; | 1325 Set<_SocketConnection> _activeSockets; |
| 1250 Timer _evictionTimer; | 1326 Timer _evictionTimer; |
| 1251 bool _shutdown; // Has this HTTP client been shutdown? | 1327 bool _shutdown; // Has this HTTP client been shutdown? |
| 1252 } | 1328 } |
| OLD | NEW |