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 // 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 | 51 |
| 52 | 52 |
| 53 class _HttpRequestResponseBase { | 53 class _HttpRequestResponseBase { |
| 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 | 62 |
| 62 void _setHeader(String name, String value) { | 63 void _setHeader(String name, String value) { |
| 63 _headers[name] = value; | 64 _headers[name] = value; |
| 64 } | 65 } |
| 65 | 66 |
| 66 bool _write(List<int> data, bool copyBuffer) { | 67 bool _write(List<int> data, bool copyBuffer) { |
| 67 bool allWritten = true; | 68 bool allWritten = true; |
| 68 if (data.length > 0) { | 69 if (data.length > 0) { |
| 69 if (_contentLength < 0) { | 70 if (_contentLength < 0) { |
| 70 // Write chunk size if transfer encoding is chunked. | 71 // Write chunk size if transfer encoding is chunked. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 } | 167 } |
| 167 | 168 |
| 168 | 169 |
| 169 // Parsed HTTP request providing information on the HTTP headers. | 170 // Parsed HTTP request providing information on the HTTP headers. |
| 170 class _HttpRequest extends _HttpRequestResponseBase implements HttpRequest { | 171 class _HttpRequest extends _HttpRequestResponseBase implements HttpRequest { |
| 171 _HttpRequest(_HttpConnection connection) : super(connection); | 172 _HttpRequest(_HttpConnection connection) : super(connection); |
| 172 | 173 |
| 173 String get method() => _method; | 174 String get method() => _method; |
| 174 String get uri() => _uri; | 175 String get uri() => _uri; |
| 175 String get path() => _path; | 176 String get path() => _path; |
| 176 Map get headers() => _headers; | |
| 177 String get queryString() => _queryString; | 177 String get queryString() => _queryString; |
| 178 Map get queryParameters() => _queryParameters; | 178 Map get queryParameters() => _queryParameters; |
| 179 | 179 |
| 180 InputStream get inputStream() { | 180 InputStream get inputStream() { |
| 181 if (_inputStream == null) { | 181 if (_inputStream == null) { |
| 182 _inputStream = new _HttpInputStream(this); | 182 _inputStream = new _HttpInputStream(this); |
| 183 } | 183 } |
| 184 return _inputStream; | 184 return _inputStream; |
| 185 } | 185 } |
| 186 | 186 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 static final int START = 0; | 252 static final int START = 0; |
| 253 static final int HEADERS_SENT = 1; | 253 static final int HEADERS_SENT = 1; |
| 254 static final int DONE = 2; | 254 static final int DONE = 2; |
| 255 | 255 |
| 256 _HttpResponse(_HttpConnection httpConnection) | 256 _HttpResponse(_HttpConnection httpConnection) |
| 257 : super(httpConnection), | 257 : super(httpConnection), |
| 258 _statusCode = HttpStatus.OK, | 258 _statusCode = HttpStatus.OK, |
| 259 _state = START; | 259 _state = START; |
| 260 | 260 |
| 261 void set contentLength(int contentLength) { | 261 void set contentLength(int contentLength) { |
| 262 if (_outputStream != null) return new HttpException("Header already sent"); | 262 if (_outputStream != null) throw new HttpException("Header already sent"); |
| 263 _contentLength = contentLength; | 263 _contentLength = contentLength; |
| 264 } | 264 } |
| 265 | 265 |
| 266 void set keepAlive(bool keepAlive) { | 266 void set keepAlive(bool keepAlive) { |
| 267 if (_outputStream != null) return new HttpException("Header already sent"); | 267 if (_outputStream != null) throw new HttpException("Header already sent"); |
| 268 _keepAlive = keepAlive; | 268 _keepAlive = keepAlive; |
| 269 } | 269 } |
| 270 | 270 |
| 271 int get statusCode() => _statusCode; | 271 int get statusCode() => _statusCode; |
| 272 void set statusCode(int statusCode) { | 272 void set statusCode(int statusCode) { |
| 273 if (_outputStream != null) return 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) return 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 // Set a header on the response. NOTE: If the same header is set | 283 // 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. | 284 // more than once only the last one will be part of the response. |
| 285 void setHeader(String name, String value) { | 285 void setHeader(String name, String value) { |
| 286 if (_outputStream != null) return new HttpException("Header already sent"); | 286 if (_outputStream != null) return new HttpException("Header already sent"); |
| 287 _setHeader(name, value); | 287 _setHeader(name, value); |
| 288 } | 288 } |
| 289 | 289 |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 707 _connection = connection; | 707 _connection = connection; |
| 708 // Default GET requests to have no content. | 708 // Default GET requests to have no content. |
| 709 if (_method == "GET") { | 709 if (_method == "GET") { |
| 710 _contentLength = 0; | 710 _contentLength = 0; |
| 711 } | 711 } |
| 712 } | 712 } |
| 713 | 713 |
| 714 void set contentLength(int contentLength) => _contentLength = contentLength; | 714 void set contentLength(int contentLength) => _contentLength = contentLength; |
| 715 void set keepAlive(bool keepAlive) => _keepAlive = keepAlive; | 715 void set keepAlive(bool keepAlive) => _keepAlive = keepAlive; |
| 716 | 716 |
| 717 Date get host() => _host; | |
|
Anders Johnsen
2012/03/06 10:29:57
String
Søren Gjesse
2012/03/06 10:59:05
Oops, forgot to run tests with type check turned o
| |
| 718 void set host(String host) { | |
| 719 _host = host; | |
| 720 _updateHostHeader(); | |
| 721 } | |
| 722 | |
| 723 int get port() => _port; | |
| 724 void set port(int port) { | |
| 725 _port = port; | |
| 726 _updateHostHeader(); | |
| 727 } | |
| 728 | |
| 717 void setHeader(String name, String value) { | 729 void setHeader(String name, String value) { |
| 730 if (_state != START) throw new HttpException("Header already sent"); | |
| 731 if (name.toLowerCase() == "host") { | |
| 732 int pos = value.indexOf(":"); | |
| 733 if (pos == -1) { | |
| 734 _host = value; | |
| 735 _port = HttpClient.DEFAULT_HTTP_PORT; | |
| 736 } else { | |
| 737 _host = value.substring(0, pos); | |
| 738 if (pos + 1 == value.length) { | |
| 739 _port = HttpClient.DEFAULT_HTTP_PORT; | |
| 740 } else { | |
| 741 _port = Math.parseInt(value.substring(pos + 1)); | |
| 742 } | |
| 743 } | |
| 744 _updateHostHeader(); | |
| 745 return; | |
| 746 } | |
| 718 _setHeader(name, value); | 747 _setHeader(name, value); |
| 719 } | 748 } |
| 720 | 749 |
| 721 bool writeString(String string) { | 750 bool writeString(String string) { |
| 722 outputStream; | 751 outputStream; |
| 723 return _writeString(string); | 752 return _writeString(string); |
| 724 } | 753 } |
| 725 | 754 |
| 726 OutputStream get outputStream() { | 755 OutputStream get outputStream() { |
| 727 if (_state == DONE) throw new HttpException("Request closed"); | 756 if (_state == DONE) throw new HttpException("Request closed"); |
| 728 if (_outputStream == null) { | 757 if (_outputStream == null) { |
| 729 // Ensure that headers are written. | 758 // Ensure that headers are written. |
| 730 if (_state == START) { | 759 if (_state == START) { |
| 731 _writeHeader(); | 760 _writeHeader(); |
| 732 } | 761 } |
| 733 _outputStream = new _HttpOutputStream(this); | 762 _outputStream = new _HttpOutputStream(this); |
| 734 } | 763 } |
| 735 return _outputStream; | 764 return _outputStream; |
| 736 } | 765 } |
| 737 | 766 |
| 767 _updateHostHeader() { | |
| 768 String portPart = _port == HttpClient.DEFAULT_HTTP_PORT ? "" : ":$_port"; | |
| 769 _setHeader("host", "$host$portPart"); | |
| 770 } | |
| 771 | |
| 738 // Delegate functions for the HttpOutputStream implementation. | 772 // Delegate functions for the HttpOutputStream implementation. |
| 739 bool _streamWrite(List<int> buffer, bool copyBuffer) { | 773 bool _streamWrite(List<int> buffer, bool copyBuffer) { |
| 740 return _write(buffer, copyBuffer); | 774 return _write(buffer, copyBuffer); |
| 741 } | 775 } |
| 742 | 776 |
| 743 bool _streamWriteFrom(List<int> buffer, int offset, int len) { | 777 bool _streamWriteFrom(List<int> buffer, int offset, int len) { |
| 744 return _writeList(buffer, offset, len); | 778 return _writeList(buffer, offset, len); |
| 745 } | 779 } |
| 746 | 780 |
| 747 void _streamClose() { | 781 void _streamClose() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 796 setHeader("Transfer-Encoding", "chunked"); | 830 setHeader("Transfer-Encoding", "chunked"); |
| 797 } | 831 } |
| 798 | 832 |
| 799 // Write headers. | 833 // Write headers. |
| 800 _writeHeaders(); | 834 _writeHeaders(); |
| 801 _state = HEADERS_SENT; | 835 _state = HEADERS_SENT; |
| 802 } | 836 } |
| 803 | 837 |
| 804 String _method; | 838 String _method; |
| 805 String _uri; | 839 String _uri; |
| 840 String _host; | |
| 841 int _port; | |
| 806 _HttpClientConnection _connection; | 842 _HttpClientConnection _connection; |
| 807 _HttpOutputStream _outputStream; | 843 _HttpOutputStream _outputStream; |
| 808 int _state; | 844 int _state; |
| 809 } | 845 } |
| 810 | 846 |
| 811 | 847 |
| 812 class _HttpClientResponse | 848 class _HttpClientResponse |
| 813 extends _HttpRequestResponseBase implements HttpClientResponse { | 849 extends _HttpRequestResponseBase implements HttpClientResponse { |
| 814 _HttpClientResponse(_HttpClientConnection connection) | 850 _HttpClientResponse(_HttpClientConnection connection) |
| 815 : super(connection) { | 851 : super(connection) { |
| 816 _connection = connection; | 852 _connection = connection; |
| 817 } | 853 } |
| 818 | 854 |
| 819 int get statusCode() => _statusCode; | 855 int get statusCode() => _statusCode; |
| 820 String get reasonPhrase() => _reasonPhrase; | 856 String get reasonPhrase() => _reasonPhrase; |
| 821 Map get headers() => _headers; | |
| 822 | 857 |
| 823 InputStream get inputStream() { | 858 InputStream get inputStream() { |
| 824 if (_inputStream == null) { | 859 if (_inputStream == null) { |
| 825 _inputStream = new _HttpInputStream(this); | 860 _inputStream = new _HttpInputStream(this); |
| 826 } | 861 } |
| 827 return _inputStream; | 862 return _inputStream; |
| 828 } | 863 } |
| 829 | 864 |
| 830 void _onRequestStart(String method, String uri) { | 865 void _onRequestStart(String method, String uri) { |
| 831 // TODO(sgjesse): Error handling | 866 // TODO(sgjesse): Error handling |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1026 return "$host:$port"; | 1061 return "$host:$port"; |
| 1027 } | 1062 } |
| 1028 | 1063 |
| 1029 HttpClientConnection _prepareHttpClientConnection( | 1064 HttpClientConnection _prepareHttpClientConnection( |
| 1030 String host, int port, String method, String path) { | 1065 String host, int port, String method, String path) { |
| 1031 | 1066 |
| 1032 void _connectionOpened(_SocketConnection socketConn, | 1067 void _connectionOpened(_SocketConnection socketConn, |
| 1033 _HttpClientConnection connection) { | 1068 _HttpClientConnection connection) { |
| 1034 connection._connectionEstablished(socketConn); | 1069 connection._connectionEstablished(socketConn); |
| 1035 HttpClientRequest request = connection.open(method, path); | 1070 HttpClientRequest request = connection.open(method, path); |
| 1071 request.host = host; | |
| 1072 request.port = port; | |
| 1036 if (connection._onRequest != null) { | 1073 if (connection._onRequest != null) { |
| 1037 connection._onRequest(request); | 1074 connection._onRequest(request); |
| 1038 } else { | 1075 } else { |
| 1039 request.outputStream.close(); | 1076 request.outputStream.close(); |
| 1040 } | 1077 } |
| 1041 } | 1078 } |
| 1042 | 1079 |
| 1043 _HttpClientConnection connection = new _HttpClientConnection(this); | 1080 _HttpClientConnection connection = new _HttpClientConnection(this); |
| 1044 | 1081 |
| 1045 // If there are active connections for this key get the first one | 1082 // If there are active connections for this key get the first one |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1122 _onError = callback; | 1159 _onError = callback; |
| 1123 } | 1160 } |
| 1124 | 1161 |
| 1125 Function _onOpen; | 1162 Function _onOpen; |
| 1126 Function _onError; | 1163 Function _onError; |
| 1127 Map<String, Queue<_SocketConnection>> _openSockets; | 1164 Map<String, Queue<_SocketConnection>> _openSockets; |
| 1128 Set<_SocketConnection> _activeSockets; | 1165 Set<_SocketConnection> _activeSockets; |
| 1129 Timer _evictionTimer; | 1166 Timer _evictionTimer; |
| 1130 bool _shutdown; // Has this HTTP client been shutdown? | 1167 bool _shutdown; // Has this HTTP client been shutdown? |
| 1131 } | 1168 } |
| OLD | NEW |