OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _HttpIncoming extends Stream<List<int>> { | 7 class _HttpIncoming extends Stream<List<int>> { |
8 final int _transferLength; | 8 final int _transferLength; |
9 final Completer _dataCompleter = new Completer(); | 9 final Completer _dataCompleter = new Completer(); |
10 Stream<List<int>> _stream; | 10 Stream<List<int>> _stream; |
(...skipping 17 matching lines...) Expand all Loading... |
28 // the length of the massage body is not known due to transfer | 28 // the length of the massage body is not known due to transfer |
29 // codings. | 29 // codings. |
30 int get transferLength => _transferLength; | 30 int get transferLength => _transferLength; |
31 | 31 |
32 _HttpIncoming(_HttpHeaders this.headers, | 32 _HttpIncoming(_HttpHeaders this.headers, |
33 int this._transferLength, | 33 int this._transferLength, |
34 Stream<List<int>> this._stream) { | 34 Stream<List<int>> this._stream) { |
35 } | 35 } |
36 | 36 |
37 StreamSubscription<List<int>> listen(void onData(List<int> event), | 37 StreamSubscription<List<int>> listen(void onData(List<int> event), |
38 {void onError(AsyncError error), | 38 {void onError(error), |
39 void onDone(), | 39 void onDone(), |
40 bool unsubscribeOnError}) { | 40 bool unsubscribeOnError}) { |
41 return _stream.listen(onData, | 41 return _stream.listen(onData, |
42 onError: onError, | 42 onError: onError, |
43 onDone: onDone, | 43 onDone: onDone, |
44 unsubscribeOnError: unsubscribeOnError); | 44 unsubscribeOnError: unsubscribeOnError); |
45 } | 45 } |
46 | 46 |
47 // Is completed once all data have been received. | 47 // Is completed once all data have been received. |
48 Future get dataDone => _dataCompleter.future; | 48 Future get dataDone => _dataCompleter.future; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 _session = _httpServer._sessionManager.getSession(sessionId); | 99 _session = _httpServer._sessionManager.getSession(sessionId); |
100 if (_session != null) { | 100 if (_session != null) { |
101 _session._markSeen(); | 101 _session._markSeen(); |
102 break; | 102 break; |
103 } | 103 } |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 StreamSubscription<List<int>> listen(void onData(List<int> event), | 108 StreamSubscription<List<int>> listen(void onData(List<int> event), |
109 {void onError(AsyncError error), | 109 {void onError(error), |
110 void onDone(), | 110 void onDone(), |
111 bool unsubscribeOnError}) { | 111 bool unsubscribeOnError}) { |
112 return _incoming.listen(onData, | 112 return _incoming.listen(onData, |
113 onError: onError, | 113 onError: onError, |
114 onDone: onDone, | 114 onDone: onDone, |
115 unsubscribeOnError: unsubscribeOnError); | 115 unsubscribeOnError: unsubscribeOnError); |
116 } | 116 } |
117 | 117 |
118 Map<String, String> get queryParameters { | 118 Map<String, String> get queryParameters { |
119 if (_queryParameters == null) { | 119 if (_queryParameters == null) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 .then((request) { | 224 .then((request) { |
225 request._responseRedirects.addAll(this.redirects); | 225 request._responseRedirects.addAll(this.redirects); |
226 request._responseRedirects.add(new _RedirectInfo(statusCode, | 226 request._responseRedirects.add(new _RedirectInfo(statusCode, |
227 method, | 227 method, |
228 url)); | 228 url)); |
229 return request.close(); | 229 return request.close(); |
230 }); | 230 }); |
231 } | 231 } |
232 | 232 |
233 StreamSubscription<List<int>> listen(void onData(List<int> event), | 233 StreamSubscription<List<int>> listen(void onData(List<int> event), |
234 {void onError(AsyncError error), | 234 {void onError(error), |
235 void onDone(), | 235 void onDone(), |
236 bool unsubscribeOnError}) { | 236 bool unsubscribeOnError}) { |
237 var stream = _incoming; | 237 var stream = _incoming; |
238 if (headers.value(HttpHeaders.CONTENT_ENCODING) == "gzip") { | 238 if (headers.value(HttpHeaders.CONTENT_ENCODING) == "gzip") { |
239 stream = stream.transform(new ZLibInflater()); | 239 stream = stream.transform(new ZLibInflater()); |
240 } | 240 } |
241 return stream.listen(onData, | 241 return stream.listen(onData, |
242 onError: onError, | 242 onError: onError, |
243 onDone: onDone, | 243 onDone: onDone, |
244 unsubscribeOnError: unsubscribeOnError); | 244 unsubscribeOnError: unsubscribeOnError); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 | 435 |
436 void writeCharCode(int charCode) { | 436 void writeCharCode(int charCode) { |
437 _dataSink.writeCharCode(charCode); | 437 _dataSink.writeCharCode(charCode); |
438 } | 438 } |
439 | 439 |
440 void add(List<int> data) { | 440 void add(List<int> data) { |
441 if (data.length == 0) return; | 441 if (data.length == 0) return; |
442 _dataSink.add(data); | 442 _dataSink.add(data); |
443 } | 443 } |
444 | 444 |
445 void addError(AsyncError error) { | 445 void addError(error) { |
446 _dataSink.addError(error); | 446 _dataSink.addError(error); |
447 } | 447 } |
448 | 448 |
449 Future<T> addStream(Stream<List<int>> stream) { | 449 Future<T> addStream(Stream<List<int>> stream) { |
450 return _dataSink.addStream(stream); | 450 return _dataSink.addStream(stream); |
451 } | 451 } |
452 | 452 |
453 Future close() { | 453 Future close() { |
454 return _dataSink.close(); | 454 return _dataSink.close(); |
455 } | 455 } |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
870 } else { | 870 } else { |
871 future = new Future<HttpClientResponse>.immediate(response); | 871 future = new Future<HttpClientResponse>.immediate(response); |
872 } | 872 } |
873 future.then( | 873 future.then( |
874 (v) => _responseCompleter.complete(v), | 874 (v) => _responseCompleter.complete(v), |
875 onError: (e) { | 875 onError: (e) { |
876 _responseCompleter.completeError(e); | 876 _responseCompleter.completeError(e); |
877 }); | 877 }); |
878 } | 878 } |
879 | 879 |
880 void _onError(AsyncError error) { | 880 void _onError(error) { |
881 _responseCompleter.completeError(error); | 881 _responseCompleter.completeError(error); |
882 } | 882 } |
883 | 883 |
884 void _writeHeader() { | 884 void _writeHeader() { |
885 var buffer = new _BufferList(); | 885 var buffer = new _BufferList(); |
886 writeSP() => buffer.add(const [_CharCode.SP]); | 886 writeSP() => buffer.add(const [_CharCode.SP]); |
887 writeCRLF() => buffer.add(const [_CharCode.CR, _CharCode.LF]); | 887 writeCRLF() => buffer.add(const [_CharCode.CR, _CharCode.LF]); |
888 | 888 |
889 buffer.add(method.codeUnits); | 889 buffer.add(method.codeUnits); |
890 writeSP(); | 890 writeSP(); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 class _ContentLengthValidator | 969 class _ContentLengthValidator |
970 extends StreamEventTransformer<List<int>, List<int>> { | 970 extends StreamEventTransformer<List<int>, List<int>> { |
971 final int expectedContentLength; | 971 final int expectedContentLength; |
972 int _bytesWritten = 0; | 972 int _bytesWritten = 0; |
973 | 973 |
974 _ContentLengthValidator(int this.expectedContentLength); | 974 _ContentLengthValidator(int this.expectedContentLength); |
975 | 975 |
976 void handleData(List<int> data, EventSink<List<int>> sink) { | 976 void handleData(List<int> data, EventSink<List<int>> sink) { |
977 _bytesWritten += data.length; | 977 _bytesWritten += data.length; |
978 if (_bytesWritten > expectedContentLength) { | 978 if (_bytesWritten > expectedContentLength) { |
979 sink.addError(new AsyncError(new HttpException( | 979 sink.addError(new HttpException( |
980 "Content size exceeds specified contentLength. " | 980 "Content size exceeds specified contentLength. " |
981 "$_bytesWritten bytes written while expected " | 981 "$_bytesWritten bytes written while expected " |
982 "$expectedContentLength. " | 982 "$expectedContentLength. " |
983 "[${new String.fromCharCodes(data)}]"))); | 983 "[${new String.fromCharCodes(data)}]")); |
984 sink.close(); | 984 sink.close(); |
985 } else { | 985 } else { |
986 sink.add(data); | 986 sink.add(data); |
987 } | 987 } |
988 } | 988 } |
989 | 989 |
990 void handleDone(EventSink<List<int>> sink) { | 990 void handleDone(EventSink<List<int>> sink) { |
991 if (_bytesWritten < expectedContentLength) { | 991 if (_bytesWritten < expectedContentLength) { |
992 sink.addError(new AsyncError(new HttpException( | 992 sink.addError(new HttpException( |
993 "Content size below specified contentLength. " | 993 "Content size below specified contentLength. " |
994 " $_bytesWritten bytes written while expected " | 994 " $_bytesWritten bytes written while expected " |
995 "$expectedContentLength."))); | 995 "$expectedContentLength.")); |
996 } | 996 } |
997 sink.close(); | 997 sink.close(); |
998 } | 998 } |
999 } | 999 } |
1000 | 1000 |
1001 | 1001 |
1002 // Extends StreamConsumer as this is an internal type, only used to pipe to. | 1002 // Extends StreamConsumer as this is an internal type, only used to pipe to. |
1003 class _HttpOutgoing implements StreamConsumer<List<int>> { | 1003 class _HttpOutgoing implements StreamConsumer<List<int>> { |
1004 final Completer _doneCompleter = new Completer(); | 1004 final Completer _doneCompleter = new Completer(); |
1005 final StreamConsumer _consumer; | 1005 final StreamConsumer _consumer; |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1377 port, | 1377 port, |
1378 sendClientCertificate: true) | 1378 sendClientCertificate: true) |
1379 : Socket.connect(host, port)) | 1379 : Socket.connect(host, port)) |
1380 .then((socket) { | 1380 .then((socket) { |
1381 socket.setOption(SocketOption.TCP_NODELAY, true); | 1381 socket.setOption(SocketOption.TCP_NODELAY, true); |
1382 var connection = new _HttpClientConnection(key, socket, this); | 1382 var connection = new _HttpClientConnection(key, socket, this); |
1383 _activeConnections.add(connection); | 1383 _activeConnections.add(connection); |
1384 return new _ConnnectionInfo(connection, proxy); | 1384 return new _ConnnectionInfo(connection, proxy); |
1385 }, onError: (error) { | 1385 }, onError: (error) { |
1386 // Continue with next proxy. | 1386 // Continue with next proxy. |
1387 return connect(error.error); | 1387 return connect(error); |
1388 }); | 1388 }); |
1389 } | 1389 } |
1390 return connect(new HttpException("No proxies given")); | 1390 return connect(new HttpException("No proxies given")); |
1391 } | 1391 } |
1392 | 1392 |
1393 _Credentials _findCredentials(Uri url, [_AuthenticationScheme scheme]) { | 1393 _Credentials _findCredentials(Uri url, [_AuthenticationScheme scheme]) { |
1394 // Look for credentials. | 1394 // Look for credentials. |
1395 _Credentials cr = | 1395 _Credentials cr = |
1396 _credentials.fold(null, (_Credentials prev, _Credentials value) { | 1396 _credentials.fold(null, (_Credentials prev, _Credentials value) { |
1397 if (value.applies(url, scheme)) { | 1397 if (value.applies(url, scheme)) { |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1585 return new _HttpServer._(socket, true); | 1585 return new _HttpServer._(socket, true); |
1586 }); | 1586 }); |
1587 } | 1587 } |
1588 | 1588 |
1589 _HttpServer._(this._serverSocket, this._closeServer); | 1589 _HttpServer._(this._serverSocket, this._closeServer); |
1590 | 1590 |
1591 _HttpServer.listenOn(ServerSocket this._serverSocket) | 1591 _HttpServer.listenOn(ServerSocket this._serverSocket) |
1592 : _closeServer = false; | 1592 : _closeServer = false; |
1593 | 1593 |
1594 StreamSubscription<HttpRequest> listen(void onData(HttpRequest event), | 1594 StreamSubscription<HttpRequest> listen(void onData(HttpRequest event), |
1595 {void onError(AsyncError error), | 1595 {void onError(error), |
1596 void onDone(), | 1596 void onDone(), |
1597 bool unsubscribeOnError}) { | 1597 bool unsubscribeOnError}) { |
1598 _serverSocket.listen( | 1598 _serverSocket.listen( |
1599 (Socket socket) { | 1599 (Socket socket) { |
1600 socket.setOption(SocketOption.TCP_NODELAY, true); | 1600 socket.setOption(SocketOption.TCP_NODELAY, true); |
1601 // Accept the client connection. | 1601 // Accept the client connection. |
1602 _HttpConnection connection = new _HttpConnection(socket, this); | 1602 _HttpConnection connection = new _HttpConnection(socket, this); |
1603 _connections.add(connection); | 1603 _connections.add(connection); |
1604 }, | 1604 }, |
1605 onError: _controller.addError, | 1605 onError: _controller.addError, |
(...skipping 25 matching lines...) Expand all Loading... |
1631 } | 1631 } |
1632 | 1632 |
1633 set sessionTimeout(int timeout) { | 1633 set sessionTimeout(int timeout) { |
1634 _sessionManager.sessionTimeout = timeout; | 1634 _sessionManager.sessionTimeout = timeout; |
1635 } | 1635 } |
1636 | 1636 |
1637 void _handleRequest(HttpRequest request) { | 1637 void _handleRequest(HttpRequest request) { |
1638 _controller.add(request); | 1638 _controller.add(request); |
1639 } | 1639 } |
1640 | 1640 |
1641 void _handleError(AsyncError error) { | 1641 void _handleError(error) { |
1642 if (!closed) _controller.addError(error); | 1642 if (!closed) _controller.addError(error); |
1643 } | 1643 } |
1644 | 1644 |
1645 void _connectionClosed(_HttpConnection connection) { | 1645 void _connectionClosed(_HttpConnection connection) { |
1646 _connections.remove(connection); | 1646 _connections.remove(connection); |
1647 } | 1647 } |
1648 | 1648 |
1649 _HttpSessionManager get _sessionManager { | 1649 _HttpSessionManager get _sessionManager { |
1650 // Lazy init. | 1650 // Lazy init. |
1651 if (_sessionManagerInstance == null) { | 1651 if (_sessionManagerInstance == null) { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1788 } | 1788 } |
1789 | 1789 |
1790 | 1790 |
1791 class _DetachedSocket extends Stream<List<int>> implements Socket { | 1791 class _DetachedSocket extends Stream<List<int>> implements Socket { |
1792 final Stream<List<int>> _incoming; | 1792 final Stream<List<int>> _incoming; |
1793 final Socket _socket; | 1793 final Socket _socket; |
1794 | 1794 |
1795 _DetachedSocket(this._socket, this._incoming); | 1795 _DetachedSocket(this._socket, this._incoming); |
1796 | 1796 |
1797 StreamSubscription<List<int>> listen(void onData(List<int> event), | 1797 StreamSubscription<List<int>> listen(void onData(List<int> event), |
1798 {void onError(AsyncError error), | 1798 {void onError(error), |
1799 void onDone(), | 1799 void onDone(), |
1800 bool unsubscribeOnError}) { | 1800 bool unsubscribeOnError}) { |
1801 return _incoming.listen(onData, | 1801 return _incoming.listen(onData, |
1802 onError: onError, | 1802 onError: onError, |
1803 onDone: onDone, | 1803 onDone: onDone, |
1804 unsubscribeOnError: unsubscribeOnError); | 1804 unsubscribeOnError: unsubscribeOnError); |
1805 } | 1805 } |
1806 | 1806 |
1807 Encoding get encoding => _socket.encoding; | 1807 Encoding get encoding => _socket.encoding; |
1808 | 1808 |
1809 void set encoding(Encoding value) { | 1809 void set encoding(Encoding value) { |
1810 _socket.encoding = value; | 1810 _socket.encoding = value; |
1811 } | 1811 } |
1812 | 1812 |
1813 void write(Object obj) => _socket.write(obj); | 1813 void write(Object obj) => _socket.write(obj); |
1814 | 1814 |
1815 void writeln([Object obj = ""]) => _socket.writeln(obj); | 1815 void writeln([Object obj = ""]) => _socket.writeln(obj); |
1816 | 1816 |
1817 void writeCharCode(int charCode) => _socket.writeCharCode(charCode); | 1817 void writeCharCode(int charCode) => _socket.writeCharCode(charCode); |
1818 | 1818 |
1819 void writeAll(Iterable objects, [String separator = ""]) { | 1819 void writeAll(Iterable objects, [String separator = ""]) { |
1820 _socket.writeAll(objects, separator); | 1820 _socket.writeAll(objects, separator); |
1821 } | 1821 } |
1822 | 1822 |
1823 void add(List<int> bytes) => _socket.add(bytes); | 1823 void add(List<int> bytes) => _socket.add(bytes); |
1824 | 1824 |
1825 void addError(AsyncError error) => _socket.addError(error); | 1825 void addError(error) => _socket.addError(error); |
1826 | 1826 |
1827 Future<Socket> addStream(Stream<List<int>> stream) { | 1827 Future<Socket> addStream(Stream<List<int>> stream) { |
1828 return _socket.addStream(stream); | 1828 return _socket.addStream(stream); |
1829 } | 1829 } |
1830 | 1830 |
1831 void destroy() => _socket.destroy(); | 1831 void destroy() => _socket.destroy(); |
1832 | 1832 |
1833 Future close() => _socket.close(); | 1833 Future close() => _socket.close(); |
1834 | 1834 |
1835 Future<Socket> get done => _socket.done; | 1835 Future<Socket> get done => _socket.done; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1986 | 1986 |
1987 | 1987 |
1988 class _RedirectInfo implements RedirectInfo { | 1988 class _RedirectInfo implements RedirectInfo { |
1989 const _RedirectInfo(int this.statusCode, | 1989 const _RedirectInfo(int this.statusCode, |
1990 String this.method, | 1990 String this.method, |
1991 Uri this.location); | 1991 Uri this.location); |
1992 final int statusCode; | 1992 final int statusCode; |
1993 final String method; | 1993 final String method; |
1994 final Uri location; | 1994 final Uri location; |
1995 } | 1995 } |
OLD | NEW |