Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Side by Side Diff: sdk/lib/io/http_parser.dart

Issue 124753002: Code cleanup (mostly io lib and some http lib). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Merge to head. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Global constants. 7 // Global constants.
8 class _Const { 8 class _Const {
9 // Bytes for "HTTP". 9 // Bytes for "HTTP".
10 static const HTTP = const [72, 84, 84, 80]; 10 static const HTTP = const [72, 84, 84, 80];
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 98
99 class _HttpDetachedIncoming extends Stream<List<int>> { 99 class _HttpDetachedIncoming extends Stream<List<int>> {
100 StreamController<List<int>> controller; 100 StreamController<List<int>> controller;
101 final StreamSubscription subscription; 101 final StreamSubscription subscription;
102 102
103 List<int> bufferedData; 103 List<int> bufferedData;
104 bool paused; 104 bool paused;
105 105
106 Completer resumeCompleter; 106 Completer resumeCompleter;
107 107
108 _HttpDetachedIncoming(StreamSubscription this.subscription, 108 _HttpDetachedIncoming(this.subscription, this.bufferedData) {
109 List<int> this.bufferedData) {
110 controller = new StreamController<List<int>>( 109 controller = new StreamController<List<int>>(
111 sync: true, 110 sync: true,
112 onListen: resume, 111 onListen: resume,
113 onPause: pause, 112 onPause: pause,
114 onResume: resume, 113 onResume: resume,
115 onCancel: () => subscription.cancel()); 114 onCancel: () => subscription.cancel());
116 if (subscription == null) { 115 if (subscription == null) {
117 // Socket was already closed. 116 // Socket was already closed.
118 if (bufferedData != null) controller.add(bufferedData); 117 if (bufferedData != null) controller.add(bufferedData);
119 controller.close(); 118 controller.close();
120 } else { 119 } else {
121 pause(); 120 pause();
122 subscription.resume(); 121 subscription
123 subscription.onData(controller.add); 122 ..resume()
124 subscription.onDone(controller.close); 123 ..onData(controller.add)
125 subscription.onError(controller.addError); 124 ..onDone(controller.close)
125 ..onError(controller.addError);
126 } 126 }
127 } 127 }
128 128
129 StreamSubscription<List<int>> listen(void onData(List<int> event), 129 StreamSubscription<List<int>> listen(void onData(List<int> event),
130 {Function onError, 130 {Function onError,
131 void onDone(), 131 void onDone(),
132 bool cancelOnError}) { 132 bool cancelOnError}) {
133 return controller.stream.listen( 133 return controller.stream.listen(
134 onData, 134 onData,
135 onError: onError, 135 onError: onError,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 * there is no more HTTP data. After the upgrade the method 179 * there is no more HTTP data. After the upgrade the method
180 * [:readUnparsedData:] can be used to read any remaining bytes in the 180 * [:readUnparsedData:] can be used to read any remaining bytes in the
181 * HTTP parser which are part of the protocol the connection is 181 * HTTP parser which are part of the protocol the connection is
182 * upgrading to. These bytes cannot be processed by the HTTP parser 182 * upgrading to. These bytes cannot be processed by the HTTP parser
183 * and should be handled according to whatever protocol is being 183 * and should be handled according to whatever protocol is being
184 * upgraded to. 184 * upgraded to.
185 */ 185 */
186 class _HttpParser 186 class _HttpParser
187 extends Stream<_HttpIncoming> 187 extends Stream<_HttpIncoming>
188 implements StreamConsumer<List<int>> { 188 implements StreamConsumer<List<int>> {
189 // State.
190 bool _parserCalled = false;
191
192 // The data that is currently being parsed.
193 Uint8List _buffer;
194 int _index;
195
196 final bool _requestParser;
197 int _state;
198 int _httpVersionIndex;
199 int _messageType;
200 int _statusCode = 0;
201 List _method_or_status_code;
202 List _uri_or_reason_phrase;
203 List _headerField;
204 List _headerValue;
205
206 int _httpVersion;
207 int _transferLength = -1;
208 bool _persistentConnection;
209 bool _connectionUpgrade;
210 bool _chunked;
211
212 bool _noMessageBody;
213 String _responseToMethod; // Indicates the method used for the request.
214 int _remainingContent = -1;
215
216 _HttpHeaders _headers;
217
218 // The current incoming connection.
219 _HttpIncoming _incoming;
220 StreamSubscription _socketSubscription;
221 bool _paused = true;
222 bool _bodyPaused = false;
223 StreamController<_HttpIncoming> _controller;
224 StreamController<List<int>> _bodyController;
189 225
190 factory _HttpParser.requestParser() { 226 factory _HttpParser.requestParser() {
191 return new _HttpParser._(true); 227 return new _HttpParser._(true);
192 } 228 }
193 229
194 factory _HttpParser.responseParser() { 230 factory _HttpParser.responseParser() {
195 return new _HttpParser._(false); 231 return new _HttpParser._(false);
196 } 232 }
197 233
198 _HttpParser._(this._requestParser) { 234 _HttpParser._(this._requestParser) {
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 } 984 }
949 } 985 }
950 } 986 }
951 987
952 void _reportError(error, [stackTrace]) { 988 void _reportError(error, [stackTrace]) {
953 if (_socketSubscription != null) _socketSubscription.cancel(); 989 if (_socketSubscription != null) _socketSubscription.cancel();
954 _state = _State.FAILURE; 990 _state = _State.FAILURE;
955 _controller.addError(error, stackTrace); 991 _controller.addError(error, stackTrace);
956 _controller.close(); 992 _controller.close();
957 } 993 }
958
959 // State.
960 bool _parserCalled = false;
961
962 // The data that is currently being parsed.
963 Uint8List _buffer;
964 int _index;
965
966 final bool _requestParser;
967 int _state;
968 int _httpVersionIndex;
969 int _messageType;
970 int _statusCode = 0;
971 List _method_or_status_code;
972 List _uri_or_reason_phrase;
973 List _headerField;
974 List _headerValue;
975
976 int _httpVersion;
977 int _transferLength = -1;
978 bool _persistentConnection;
979 bool _connectionUpgrade;
980 bool _chunked;
981
982 bool _noMessageBody;
983 String _responseToMethod; // Indicates the method used for the request.
984 int _remainingContent = -1;
985
986 _HttpHeaders _headers;
987
988 // The current incoming connection.
989 _HttpIncoming _incoming;
990 StreamSubscription _socketSubscription;
991 bool _paused = true;
992 bool _bodyPaused = false;
993 StreamController<_HttpIncoming> _controller;
994 StreamController<List<int>> _bodyController;
995 } 994 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698