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 { | 5 class _HttpHeaders implements HttpHeaders { |
6 _HttpHeaders() : _headers = new Map<String, List<String>>(); | 6 _HttpHeaders() : _headers = new Map<String, List<String>>(); |
7 | 7 |
8 List<String> operator[](String name) { | 8 List<String> operator[](String name) { |
9 name = name.toLowerCase(); | 9 name = name.toLowerCase(); |
10 return _headers[name]; | 10 return _headers[name]; |
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 headers.remove(HttpHeaders.CONNECTION, "keep-alive"); | 641 headers.remove(HttpHeaders.CONNECTION, "keep-alive"); |
642 if (_protocolVersion == "1.1" && !persistentConnection) { | 642 if (_protocolVersion == "1.1" && !persistentConnection) { |
643 headers.add(HttpHeaders.CONNECTION, "close"); | 643 headers.add(HttpHeaders.CONNECTION, "close"); |
644 } else if (_protocolVersion == "1.0" && persistentConnection) { | 644 } else if (_protocolVersion == "1.0" && persistentConnection) { |
645 headers.add(HttpHeaders.CONNECTION, "keep-alive"); | 645 headers.add(HttpHeaders.CONNECTION, "keep-alive"); |
646 } | 646 } |
647 } | 647 } |
648 | 648 |
649 | 649 |
650 bool _write(List<int> data, bool copyBuffer) { | 650 bool _write(List<int> data, bool copyBuffer) { |
651 if (_headResponse) return; | 651 if (_headResponse) return true; |
652 _ensureHeadersSent(); | 652 _ensureHeadersSent(); |
653 bool allWritten = true; | 653 bool allWritten = true; |
654 if (data.length > 0) { | 654 if (data.length > 0) { |
655 if (_contentLength < 0) { | 655 if (_contentLength < 0) { |
656 // Write chunk size if transfer encoding is chunked. | 656 // Write chunk size if transfer encoding is chunked. |
657 _writeHexString(data.length); | 657 _writeHexString(data.length); |
658 _writeCRLF(); | 658 _writeCRLF(); |
659 _httpConnection._write(data, copyBuffer); | 659 _httpConnection._write(data, copyBuffer); |
660 allWritten = _writeCRLF(); | 660 allWritten = _writeCRLF(); |
661 } else { | 661 } else { |
662 _updateContentLength(data.length); | 662 _updateContentLength(data.length); |
663 allWritten = _httpConnection._write(data, copyBuffer); | 663 allWritten = _httpConnection._write(data, copyBuffer); |
664 } | 664 } |
665 } | 665 } |
666 return allWritten; | 666 return allWritten; |
667 } | 667 } |
668 | 668 |
669 bool _writeList(List<int> data, int offset, int count) { | 669 bool _writeList(List<int> data, int offset, int count) { |
670 if (_headResponse) return; | 670 if (_headResponse) return true; |
671 _ensureHeadersSent(); | 671 _ensureHeadersSent(); |
672 bool allWritten = true; | 672 bool allWritten = true; |
673 if (count > 0) { | 673 if (count > 0) { |
674 if (_contentLength < 0) { | 674 if (_contentLength < 0) { |
675 // Write chunk size if transfer encoding is chunked. | 675 // Write chunk size if transfer encoding is chunked. |
676 _writeHexString(count); | 676 _writeHexString(count); |
677 _writeCRLF(); | 677 _writeCRLF(); |
678 _httpConnection._writeFrom(data, offset, count); | 678 _httpConnection._writeFrom(data, offset, count); |
679 allWritten = _writeCRLF(); | 679 allWritten = _writeCRLF(); |
680 } else { | 680 } else { |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 } else if (_contentLength < 0 && _protocolVersion == "1.1") { | 1136 } else if (_contentLength < 0 && _protocolVersion == "1.1") { |
1137 _headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); | 1137 _headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
1138 } | 1138 } |
1139 | 1139 |
1140 var session = _httpConnection._request._session; | 1140 var session = _httpConnection._request._session; |
1141 if (session != null && !session._destroyed) { | 1141 if (session != null && !session._destroyed) { |
1142 // Make sure we only send the current session id. | 1142 // Make sure we only send the current session id. |
1143 bool found = false; | 1143 bool found = false; |
1144 for (int i = 0; i < cookies.length; i++) { | 1144 for (int i = 0; i < cookies.length; i++) { |
1145 if (cookies[i].name.toUpperCase() == _DART_SESSION_ID) { | 1145 if (cookies[i].name.toUpperCase() == _DART_SESSION_ID) { |
1146 cookie.value = session.id; | 1146 cookies[i].value = session.id; |
1147 found = true; | 1147 found = true; |
1148 break; | 1148 break; |
1149 } | 1149 } |
1150 } | 1150 } |
1151 if (!found) { | 1151 if (!found) { |
1152 cookies.add(new Cookie(_DART_SESSION_ID, session.id)); | 1152 cookies.add(new Cookie(_DART_SESSION_ID, session.id)); |
1153 } | 1153 } |
1154 } | 1154 } |
1155 // Add all the cookies set to the headers. | 1155 // Add all the cookies set to the headers. |
1156 if (_cookies != null) { | 1156 if (_cookies != null) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1247 } | 1247 } |
1248 | 1248 |
1249 void set onError(void callback(e)) { | 1249 void set onError(void callback(e)) { |
1250 _requestOrResponse._streamSetErrorHandler(callback); | 1250 _requestOrResponse._streamSetErrorHandler(callback); |
1251 } | 1251 } |
1252 | 1252 |
1253 _HttpRequestResponseBase _requestOrResponse; | 1253 _HttpRequestResponseBase _requestOrResponse; |
1254 } | 1254 } |
1255 | 1255 |
1256 | 1256 |
1257 class _HttpConnectionBase { | 1257 abstract class _HttpConnectionBase { |
1258 _HttpConnectionBase() : _httpParser = new _HttpParser(), | 1258 _HttpConnectionBase() : _httpParser = new _HttpParser(), |
1259 hashCode = _nextHashCode { | 1259 hashCode = _nextHashCode { |
1260 _nextHashCode = (_nextHashCode + 1) & 0xFFFFFFF; | 1260 _nextHashCode = (_nextHashCode + 1) & 0xFFFFFFF; |
1261 } | 1261 } |
1262 | 1262 |
1263 void _connectionEstablished(Socket socket) { | 1263 void _connectionEstablished(Socket socket) { |
1264 _socket = socket; | 1264 _socket = socket; |
1265 // Register handler for socket events. | 1265 // Register handler for socket events. |
1266 _socket.onData = _onData; | 1266 _socket.onData = _onData; |
1267 _socket.onClosed = _onClosed; | 1267 _socket.onClosed = _onClosed; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1348 try { | 1348 try { |
1349 _HttpConnectionInfo info = new _HttpConnectionInfo(); | 1349 _HttpConnectionInfo info = new _HttpConnectionInfo(); |
1350 info.remoteHost = _socket.remoteHost; | 1350 info.remoteHost = _socket.remoteHost; |
1351 info.remotePort = _socket.remotePort; | 1351 info.remotePort = _socket.remotePort; |
1352 info.localPort = _socket.port; | 1352 info.localPort = _socket.port; |
1353 return info; | 1353 return info; |
1354 } catch (e) { } | 1354 } catch (e) { } |
1355 return null; | 1355 return null; |
1356 } | 1356 } |
1357 | 1357 |
1358 abstract void _onConnectionClosed(e); | 1358 void _onConnectionClosed(e); |
1359 abstract void _responseDone(); | 1359 void _responseDone(); |
1360 | 1360 |
1361 void set _onNoPendingWrites(void callback()) { | 1361 void set _onNoPendingWrites(void callback()) { |
1362 if (!_error) { | 1362 if (!_error) { |
1363 _socket.outputStream.onNoPendingWrites = callback; | 1363 _socket.outputStream.onNoPendingWrites = callback; |
1364 } | 1364 } |
1365 } | 1365 } |
1366 | 1366 |
1367 Socket _socket; | 1367 Socket _socket; |
1368 bool _closing = false; // Is the socket closed by the client? | 1368 bool _closing = false; // Is the socket closed by the client? |
1369 bool _error = false; // Is the socket closed due to an error? | 1369 bool _error = false; // Is the socket closed due to an error? |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1557 if (_server === null) { | 1557 if (_server === null) { |
1558 throw new HttpException("The HttpServer is not listening on a port."); | 1558 throw new HttpException("The HttpServer is not listening on a port."); |
1559 } | 1559 } |
1560 return _server.port; | 1560 return _server.port; |
1561 } | 1561 } |
1562 | 1562 |
1563 void set onError(void callback(e)) { | 1563 void set onError(void callback(e)) { |
1564 _onError = callback; | 1564 _onError = callback; |
1565 } | 1565 } |
1566 | 1566 |
1567 int set sessionTimeout(int timeout) { | 1567 set sessionTimeout(int timeout) { |
1568 _sessionManager.sessionTimeout = timeout; | 1568 _sessionManager.sessionTimeout = timeout; |
1569 } | 1569 } |
1570 | 1570 |
1571 void _handleRequest(HttpRequest request, HttpResponse response) { | 1571 void _handleRequest(HttpRequest request, HttpResponse response) { |
1572 for (int i = 0; i < _handlers.length; i++) { | 1572 for (int i = 0; i < _handlers.length; i++) { |
1573 if (_handlers[i]._matcher(request)) { | 1573 if (_handlers[i]._matcher(request)) { |
1574 Function handler = _handlers[i]._handler; | 1574 Function handler = _handlers[i]._handler; |
1575 try { | 1575 try { |
1576 handler(request, response); | 1576 handler(request, response); |
1577 } catch (e) { | 1577 } catch (e) { |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1791 } | 1791 } |
1792 | 1792 |
1793 void _onHeaderReceived(String name, String value) { | 1793 void _onHeaderReceived(String name, String value) { |
1794 _headers.add(name, value); | 1794 _headers.add(name, value); |
1795 } | 1795 } |
1796 | 1796 |
1797 void _handleUnauthorized() { | 1797 void _handleUnauthorized() { |
1798 | 1798 |
1799 void retryRequest(_Credentials cr) { | 1799 void retryRequest(_Credentials cr) { |
1800 if (cr != null) { | 1800 if (cr != null) { |
1801 if (cr.scheme == _AuthenticationScheme.DIGEST) { | |
1802 cr.nonce = header.parameters["nonce"]; | |
1803 cr.algorithm = header.parameters["algorithm"]; | |
1804 cr.qop = header.parameters["qop"]; | |
1805 } | |
1806 // Drain body and retry. | 1801 // Drain body and retry. |
1807 // TODO(sjgesse): Support digest. | 1802 // TODO(sgjesse): Support digest. |
1808 if (cr.scheme == _AuthenticationScheme.BASIC) { | 1803 if (cr.scheme == _AuthenticationScheme.BASIC) { |
1809 inputStream.onData = inputStream.read; | 1804 inputStream.onData = inputStream.read; |
1810 inputStream.onClosed = _connection.retry; | 1805 inputStream.onClosed = _connection.retry; |
1811 return; | 1806 return; |
1812 } | 1807 } |
1813 } | 1808 } |
1814 | 1809 |
1815 // Fall through to here to perform normal response handling if | 1810 // Fall through to here to perform normal response handling if |
1816 // there is no sensible authorization handling. | 1811 // there is no sensible authorization handling. |
1817 if (_connection._onResponse != null) { | 1812 if (_connection._onResponse != null) { |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2232 } | 2227 } |
2233 | 2228 |
2234 HttpClientConnection getUrl(Uri url) => _openUrl("GET", url); | 2229 HttpClientConnection getUrl(Uri url) => _openUrl("GET", url); |
2235 | 2230 |
2236 HttpClientConnection post(String host, int port, String path) { | 2231 HttpClientConnection post(String host, int port, String path) { |
2237 return open("POST", host, port, path); | 2232 return open("POST", host, port, path); |
2238 } | 2233 } |
2239 | 2234 |
2240 HttpClientConnection postUrl(Uri url) => _openUrl("POST", url); | 2235 HttpClientConnection postUrl(Uri url) => _openUrl("POST", url); |
2241 | 2236 |
2242 set authenticate(bool f(Uri url, String scheme, String realm)) { | 2237 set authenticate(Future<bool> f(Uri url, String scheme, String realm)) { |
2243 _authenticate = f; | 2238 _authenticate = f; |
2244 } | 2239 } |
2245 | 2240 |
2246 void addCredentials( | 2241 void addCredentials( |
2247 Uri url, String realm, HttpClientCredentials cr) { | 2242 Uri url, String realm, HttpClientCredentials cr) { |
2248 credentials.add(new _Credentials(url, realm, cr)); | 2243 credentials.add(new _Credentials(url, realm, cr)); |
2249 } | 2244 } |
2250 | 2245 |
2251 set findProxy(String f(Uri uri)) => _findProxy = f; | 2246 set findProxy(String f(Uri uri)) => _findProxy = f; |
2252 | 2247 |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2544 String realm; | 2539 String realm; |
2545 HttpClientCredentials credentials; | 2540 HttpClientCredentials credentials; |
2546 | 2541 |
2547 // Digest specific fields. | 2542 // Digest specific fields. |
2548 String nonce; | 2543 String nonce; |
2549 String algorithm; | 2544 String algorithm; |
2550 String qop; | 2545 String qop; |
2551 } | 2546 } |
2552 | 2547 |
2553 | 2548 |
2554 class _HttpClientCredentials implements HttpClientCredentials { | 2549 abstract class _HttpClientCredentials implements HttpClientCredentials { |
2555 abstract _AuthenticationScheme get scheme; | 2550 _AuthenticationScheme get scheme; |
2556 abstract void authorize(HttpClientRequest request); | 2551 void authorize(HttpClientRequest request); |
2557 } | 2552 } |
2558 | 2553 |
2559 | 2554 |
2560 class _HttpClientBasicCredentials implements HttpClientBasicCredentials { | 2555 class _HttpClientBasicCredentials implements HttpClientBasicCredentials { |
2561 _HttpClientBasicCredentials(this.username, | 2556 _HttpClientBasicCredentials(this.username, |
2562 this.password); | 2557 this.password); |
2563 | 2558 |
2564 _AuthenticationScheme get scheme => _AuthenticationScheme.BASIC; | 2559 _AuthenticationScheme get scheme => _AuthenticationScheme.BASIC; |
2565 | 2560 |
2566 void authorize(_Credentials _, HttpClientRequest request) { | 2561 void authorize(_Credentials _, HttpClientRequest request) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2600 | 2595 |
2601 | 2596 |
2602 class _RedirectInfo implements RedirectInfo { | 2597 class _RedirectInfo implements RedirectInfo { |
2603 const _RedirectInfo(int this.statusCode, | 2598 const _RedirectInfo(int this.statusCode, |
2604 String this.method, | 2599 String this.method, |
2605 Uri this.location); | 2600 Uri this.location); |
2606 final int statusCode; | 2601 final int statusCode; |
2607 final String method; | 2602 final String method; |
2608 final Uri location; | 2603 final Uri location; |
2609 } | 2604 } |
OLD | NEW |