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 // 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]; |
11 // Bytes for "HTTP/1.". | 11 // Bytes for "HTTP/1.". |
12 static const HTTP1DOT = const [72, 84, 84, 80, 47, 49, 46]; | 12 static const HTTP1DOT = const [72, 84, 84, 80, 47, 49, 46]; |
13 // Bytes for "HTTP/1.0". | 13 // Bytes for "HTTP/1.0". |
14 static const HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; | 14 static const HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; |
15 // Bytes for "HTTP/1.1". | 15 // Bytes for "HTTP/1.1". |
16 static const HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; | 16 static const HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; |
17 | 17 |
18 static const bool T = true; | 18 static const bool T = true; |
19 static const bool F = false; | 19 static const bool F = false; |
20 // Loopup-map for the following characters: '()<>@,;:\\"/[]?={} \t'. | 20 // Loopup-map for the following characters: '()<>@,;:\\"/[]?={} \t'. |
21 static const SEPARATOR_MAP = const [ | 21 static const SEPARATOR_MAP = const [ |
22 F,F,F,F,F,F,F,F,F,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,F,T,F,F, | 22 F, F, F, F, F, F, F, F, F, T, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
23 F,F,F,T,T,F,F,T,F,F,T,F,F,F,F,F,F,F,F,F,F,T,T,T,T,T,T,T,F,F,F,F,F,F,F,F,F, | 23 F, F, F, F, F, F, F, F, T, F, T, F, F, F, F, F, T, T, F, F, T, F, F, T, // |
24 F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,T,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F, | 24 F, F, F, F, F, F, F, F, F, F, T, T, T, T, T, T, T, F, F, F, F, F, F, F, // |
25 F,F,F,F,F,F,F,F,F,F,F,F,T,F,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F, | 25 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T, T, T, F, F, // |
26 F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F, | 26 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
27 F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F, | 27 F, F, F, T, F, T, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
28 F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F]; | 28 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
| 29 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
| 30 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
| 31 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, // |
| 32 F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F |
| 33 ]; |
29 } | 34 } |
30 | 35 |
31 | |
32 // Frequently used character codes. | 36 // Frequently used character codes. |
33 class _CharCode { | 37 class _CharCode { |
34 static const int HT = 9; | 38 static const int HT = 9; |
35 static const int LF = 10; | 39 static const int LF = 10; |
36 static const int CR = 13; | 40 static const int CR = 13; |
37 static const int SP = 32; | 41 static const int SP = 32; |
38 static const int AMPERSAND = 38; | 42 static const int AMPERSAND = 38; |
39 static const int COMMA = 44; | 43 static const int COMMA = 44; |
40 static const int DASH = 45; | 44 static const int DASH = 45; |
41 static const int SLASH = 47; | 45 static const int SLASH = 47; |
42 static const int ZERO = 48; | 46 static const int ZERO = 48; |
43 static const int ONE = 49; | 47 static const int ONE = 49; |
44 static const int COLON = 58; | 48 static const int COLON = 58; |
45 static const int SEMI_COLON = 59; | 49 static const int SEMI_COLON = 59; |
46 static const int EQUAL = 61; | 50 static const int EQUAL = 61; |
47 } | 51 } |
48 | 52 |
49 | |
50 // States of the HTTP parser state machine. | 53 // States of the HTTP parser state machine. |
51 class _State { | 54 class _State { |
52 static const int START = 0; | 55 static const int START = 0; |
53 static const int METHOD_OR_RESPONSE_HTTP_VERSION = 1; | 56 static const int METHOD_OR_RESPONSE_HTTP_VERSION = 1; |
54 static const int RESPONSE_HTTP_VERSION = 2; | 57 static const int RESPONSE_HTTP_VERSION = 2; |
55 static const int REQUEST_LINE_METHOD = 3; | 58 static const int REQUEST_LINE_METHOD = 3; |
56 static const int REQUEST_LINE_URI = 4; | 59 static const int REQUEST_LINE_URI = 4; |
57 static const int REQUEST_LINE_HTTP_VERSION = 5; | 60 static const int REQUEST_LINE_HTTP_VERSION = 5; |
58 static const int REQUEST_LINE_ENDING = 6; | 61 static const int REQUEST_LINE_ENDING = 6; |
59 static const int RESPONSE_LINE_STATUS_CODE = 7; | 62 static const int RESPONSE_LINE_STATUS_CODE = 7; |
(...skipping 29 matching lines...) Expand all Loading... |
89 static const int HTTP11 = 2; | 92 static const int HTTP11 = 2; |
90 } | 93 } |
91 | 94 |
92 // States of the HTTP parser state machine. | 95 // States of the HTTP parser state machine. |
93 class _MessageType { | 96 class _MessageType { |
94 static const int UNDETERMINED = 0; | 97 static const int UNDETERMINED = 0; |
95 static const int REQUEST = 1; | 98 static const int REQUEST = 1; |
96 static const int RESPONSE = 0; | 99 static const int RESPONSE = 0; |
97 } | 100 } |
98 | 101 |
99 | |
100 /** | 102 /** |
101 * The _HttpDetachedStreamSubscription takes a subscription and some extra data, | 103 * The _HttpDetachedStreamSubscription takes a subscription and some extra data, |
102 * and makes it possible to "inject" the data in from of other data events | 104 * and makes it possible to "inject" the data in from of other data events |
103 * from the subscription. | 105 * from the subscription. |
104 * | 106 * |
105 * It does so by overriding pause/resume, so that once the | 107 * It does so by overriding pause/resume, so that once the |
106 * _HttpDetachedStreamSubscription is resumed, it'll deliver the data before | 108 * _HttpDetachedStreamSubscription is resumed, it'll deliver the data before |
107 * resuming the underlaying subscription. | 109 * resuming the underlaying subscription. |
108 */ | 110 */ |
109 class _HttpDetachedStreamSubscription implements StreamSubscription<List<int>> { | 111 class _HttpDetachedStreamSubscription implements StreamSubscription<List<int>> { |
110 StreamSubscription<List<int>> _subscription; | 112 StreamSubscription<List<int>> _subscription; |
111 List<int> _injectData; | 113 List<int> _injectData; |
112 bool _isCanceled = false; | 114 bool _isCanceled = false; |
113 int _pauseCount = 1; | 115 int _pauseCount = 1; |
114 Function _userOnData; | 116 Function _userOnData; |
115 bool _scheduled = false; | 117 bool _scheduled = false; |
116 | 118 |
117 _HttpDetachedStreamSubscription(this._subscription, | 119 _HttpDetachedStreamSubscription( |
118 this._injectData, | 120 this._subscription, this._injectData, this._userOnData); |
119 this._userOnData); | |
120 | 121 |
121 bool get isPaused => _subscription.isPaused; | 122 bool get isPaused => _subscription.isPaused; |
122 | 123 |
123 Future<T> asFuture<T>([T futureValue]) => | 124 Future<T> asFuture<T>([T futureValue]) => |
124 _subscription.asFuture<T>(futureValue); | 125 _subscription.asFuture<T>(futureValue); |
125 | 126 |
126 Future cancel() { | 127 Future cancel() { |
127 _isCanceled = true; | 128 _isCanceled = true; |
128 _injectData = null; | 129 _injectData = null; |
129 return _subscription.cancel(); | 130 return _subscription.cancel(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 // To ensure that 'subscription.isPaused' is false, we resume the | 175 // To ensure that 'subscription.isPaused' is false, we resume the |
175 // subscription here. This is fine as potential events are delayed. | 176 // subscription here. This is fine as potential events are delayed. |
176 _subscription.resume(); | 177 _subscription.resume(); |
177 if (_userOnData != null) { | 178 if (_userOnData != null) { |
178 _userOnData(data); | 179 _userOnData(data); |
179 } | 180 } |
180 }); | 181 }); |
181 } | 182 } |
182 } | 183 } |
183 | 184 |
184 | |
185 class _HttpDetachedIncoming extends Stream<List<int>> { | 185 class _HttpDetachedIncoming extends Stream<List<int>> { |
186 final StreamSubscription<List<int>> subscription; | 186 final StreamSubscription<List<int>> subscription; |
187 final List<int> bufferedData; | 187 final List<int> bufferedData; |
188 | 188 |
189 _HttpDetachedIncoming(this.subscription, this.bufferedData); | 189 _HttpDetachedIncoming(this.subscription, this.bufferedData); |
190 | 190 |
191 StreamSubscription<List<int>> listen(void onData(List<int> event), | 191 StreamSubscription<List<int>> listen(void onData(List<int> event), |
192 {Function onError, | 192 {Function onError, void onDone(), bool cancelOnError}) { |
193 void onDone(), | |
194 bool cancelOnError}) { | |
195 if (subscription != null) { | 193 if (subscription != null) { |
196 subscription | 194 subscription |
197 ..onData(onData) | 195 ..onData(onData) |
198 ..onError(onError) | 196 ..onError(onError) |
199 ..onDone(onDone); | 197 ..onDone(onDone); |
200 if (bufferedData == null) { | 198 if (bufferedData == null) { |
201 return subscription..resume(); | 199 return subscription..resume(); |
202 } | 200 } |
203 return new _HttpDetachedStreamSubscription(subscription, | 201 return new _HttpDetachedStreamSubscription( |
204 bufferedData, | 202 subscription, bufferedData, onData) |
205 onData) | |
206 ..resume(); | 203 ..resume(); |
207 } else { | 204 } else { |
208 // TODO(26379): add test for this branch. | 205 // TODO(26379): add test for this branch. |
209 return new Stream<List<int>>.fromIterable([bufferedData]) | 206 return new Stream<List<int>>.fromIterable([bufferedData]).listen(onData, |
210 .listen(onData, | 207 onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
211 onError: onError, | |
212 onDone: onDone, | |
213 cancelOnError: cancelOnError); | |
214 } | 208 } |
215 } | 209 } |
216 } | 210 } |
217 | 211 |
218 | |
219 /** | 212 /** |
220 * HTTP parser which parses the data stream given to [consume]. | 213 * HTTP parser which parses the data stream given to [consume]. |
221 * | 214 * |
222 * If an HTTP parser error occours, the parser will signal an error to either | 215 * If an HTTP parser error occours, the parser will signal an error to either |
223 * the current _HttpIncoming or the _parser itself. | 216 * the current _HttpIncoming or the _parser itself. |
224 * | 217 * |
225 * The connection upgrades (e.g. switching from HTTP/1.1 to the | 218 * The connection upgrades (e.g. switching from HTTP/1.1 to the |
226 * WebSocket protocol) is handled in a special way. If connection | 219 * WebSocket protocol) is handled in a special way. If connection |
227 * upgrade is specified in the headers, then on the callback to | 220 * upgrade is specified in the headers, then on the callback to |
228 * [:responseStart:] the [:upgrade:] property on the [:HttpParser:] | 221 * [:responseStart:] the [:upgrade:] property on the [:HttpParser:] |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 _pauseStateChanged(); | 290 _pauseStateChanged(); |
298 }, | 291 }, |
299 onCancel: () { | 292 onCancel: () { |
300 if (_socketSubscription != null) { | 293 if (_socketSubscription != null) { |
301 _socketSubscription.cancel(); | 294 _socketSubscription.cancel(); |
302 } | 295 } |
303 }); | 296 }); |
304 _reset(); | 297 _reset(); |
305 } | 298 } |
306 | 299 |
307 | |
308 StreamSubscription<_HttpIncoming> listen(void onData(_HttpIncoming event), | 300 StreamSubscription<_HttpIncoming> listen(void onData(_HttpIncoming event), |
309 {Function onError, | 301 {Function onError, void onDone(), bool cancelOnError}) { |
310 void onDone(), | |
311 bool cancelOnError}) { | |
312 return _controller.stream.listen(onData, | 302 return _controller.stream.listen(onData, |
313 onError: onError, | 303 onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
314 onDone: onDone, | |
315 cancelOnError: cancelOnError); | |
316 } | 304 } |
317 | 305 |
318 void listenToStream(Stream<List<int>> stream) { | 306 void listenToStream(Stream<List<int>> stream) { |
319 // Listen to the stream and handle data accordingly. When a | 307 // Listen to the stream and handle data accordingly. When a |
320 // _HttpIncoming is created, _dataPause, _dataResume, _dataDone is | 308 // _HttpIncoming is created, _dataPause, _dataResume, _dataDone is |
321 // given to provide a way of controlling the parser. | 309 // given to provide a way of controlling the parser. |
322 // TODO(ajohnsen): Remove _dataPause, _dataResume and _dataDone and clean up | 310 // TODO(ajohnsen): Remove _dataPause, _dataResume and _dataDone and clean up |
323 // how the _HttpIncoming signals the parser. | 311 // how the _HttpIncoming signals the parser. |
324 _socketSubscription = stream.listen( | 312 _socketSubscription = |
325 _onData, | 313 stream.listen(_onData, onError: _controller.addError, onDone: _onDone); |
326 onError: _controller.addError, | |
327 onDone: _onDone); | |
328 } | 314 } |
329 | 315 |
330 void _parse() { | 316 void _parse() { |
331 try { | 317 try { |
332 _doParse(); | 318 _doParse(); |
333 } catch (e, s) { | 319 } catch (e, s) { |
334 _state = _State.FAILURE; | 320 _state = _State.FAILURE; |
335 _reportError(e, s); | 321 _reportError(e, s); |
336 } | 322 } |
337 } | 323 } |
338 | 324 |
339 // Process end of headers. Returns true if the parser should stop | 325 // Process end of headers. Returns true if the parser should stop |
340 // parsing and return. This will be in case of either an upgrade | 326 // parsing and return. This will be in case of either an upgrade |
341 // request or a request or response with an empty body. | 327 // request or a request or response with an empty body. |
342 bool _headersEnd() { | 328 bool _headersEnd() { |
343 _headers._mutable = false; | 329 _headers._mutable = false; |
344 | 330 |
345 _transferLength = _headers.contentLength; | 331 _transferLength = _headers.contentLength; |
346 // Ignore the Content-Length header if Transfer-Encoding | 332 // Ignore the Content-Length header if Transfer-Encoding |
347 // is chunked (RFC 2616 section 4.4) | 333 // is chunked (RFC 2616 section 4.4) |
348 if (_chunked) _transferLength = -1; | 334 if (_chunked) _transferLength = -1; |
349 | 335 |
350 // If a request message has neither Content-Length nor | 336 // If a request message has neither Content-Length nor |
351 // Transfer-Encoding the message must not have a body (RFC | 337 // Transfer-Encoding the message must not have a body (RFC |
352 // 2616 section 4.3). | 338 // 2616 section 4.3). |
353 if (_messageType == _MessageType.REQUEST && | 339 if (_messageType == _MessageType.REQUEST && |
354 _transferLength < 0 && | 340 _transferLength < 0 && |
355 _chunked == false) { | 341 _chunked == false) { |
356 _transferLength = 0; | 342 _transferLength = 0; |
357 } | 343 } |
358 if (_connectionUpgrade) { | 344 if (_connectionUpgrade) { |
359 _state = _State.UPGRADED; | 345 _state = _State.UPGRADED; |
360 _transferLength = 0; | 346 _transferLength = 0; |
361 } | 347 } |
362 _createIncoming(_transferLength); | 348 _createIncoming(_transferLength); |
363 if (_requestParser) { | 349 if (_requestParser) { |
364 _incoming.method = | 350 _incoming.method = new String.fromCharCodes(_method); |
365 new String.fromCharCodes(_method); | |
366 _incoming.uri = | 351 _incoming.uri = |
367 Uri.parse( | 352 Uri.parse(new String.fromCharCodes(_uri_or_reason_phrase)); |
368 new String.fromCharCodes(_uri_or_reason_phrase)); | |
369 } else { | 353 } else { |
370 _incoming.statusCode = _statusCode; | 354 _incoming.statusCode = _statusCode; |
371 _incoming.reasonPhrase = | 355 _incoming.reasonPhrase = new String.fromCharCodes(_uri_or_reason_phrase); |
372 new String.fromCharCodes(_uri_or_reason_phrase); | |
373 } | 356 } |
374 _method.clear(); | 357 _method.clear(); |
375 _uri_or_reason_phrase.clear(); | 358 _uri_or_reason_phrase.clear(); |
376 if (_connectionUpgrade) { | 359 if (_connectionUpgrade) { |
377 _incoming.upgraded = true; | 360 _incoming.upgraded = true; |
378 _parserCalled = false; | 361 _parserCalled = false; |
379 var tmp = _incoming; | 362 var tmp = _incoming; |
380 _closeIncoming(); | 363 _closeIncoming(); |
381 _controller.add(tmp); | 364 _controller.add(tmp); |
382 return true; | 365 return true; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 void _doParse() { | 399 void _doParse() { |
417 assert(!_parserCalled); | 400 assert(!_parserCalled); |
418 _parserCalled = true; | 401 _parserCalled = true; |
419 if (_state == _State.CLOSED) { | 402 if (_state == _State.CLOSED) { |
420 throw new HttpException("Data on closed connection"); | 403 throw new HttpException("Data on closed connection"); |
421 } | 404 } |
422 if (_state == _State.FAILURE) { | 405 if (_state == _State.FAILURE) { |
423 throw new HttpException("Data on failed connection"); | 406 throw new HttpException("Data on failed connection"); |
424 } | 407 } |
425 while (_buffer != null && | 408 while (_buffer != null && |
426 _index < _buffer.length && | 409 _index < _buffer.length && |
427 _state != _State.FAILURE && | 410 _state != _State.FAILURE && |
428 _state != _State.UPGRADED) { | 411 _state != _State.UPGRADED) { |
429 // Depending on _incoming, we either break on _bodyPaused or _paused. | 412 // Depending on _incoming, we either break on _bodyPaused or _paused. |
430 if ((_incoming != null && _bodyPaused) || | 413 if ((_incoming != null && _bodyPaused) || |
431 (_incoming == null && _paused)) { | 414 (_incoming == null && _paused)) { |
432 _parserCalled = false; | 415 _parserCalled = false; |
433 return; | 416 return; |
434 } | 417 } |
435 int byte = _buffer[_index++]; | 418 int byte = _buffer[_index++]; |
436 switch (_state) { | 419 switch (_state) { |
437 case _State.START: | 420 case _State.START: |
438 if (byte == _Const.HTTP[0]) { | 421 if (byte == _Const.HTTP[0]) { |
(...skipping 12 matching lines...) Expand all Loading... |
451 _state = _State.REQUEST_LINE_METHOD; | 434 _state = _State.REQUEST_LINE_METHOD; |
452 } | 435 } |
453 break; | 436 break; |
454 | 437 |
455 case _State.METHOD_OR_RESPONSE_HTTP_VERSION: | 438 case _State.METHOD_OR_RESPONSE_HTTP_VERSION: |
456 if (_httpVersionIndex < _Const.HTTP.length && | 439 if (_httpVersionIndex < _Const.HTTP.length && |
457 byte == _Const.HTTP[_httpVersionIndex]) { | 440 byte == _Const.HTTP[_httpVersionIndex]) { |
458 // Continue parsing HTTP version. | 441 // Continue parsing HTTP version. |
459 _httpVersionIndex++; | 442 _httpVersionIndex++; |
460 } else if (_httpVersionIndex == _Const.HTTP.length && | 443 } else if (_httpVersionIndex == _Const.HTTP.length && |
461 byte == _CharCode.SLASH) { | 444 byte == _CharCode.SLASH) { |
462 // HTTP/ parsed. As method is a token this cannot be a | 445 // HTTP/ parsed. As method is a token this cannot be a |
463 // method anymore. | 446 // method anymore. |
464 _httpVersionIndex++; | 447 _httpVersionIndex++; |
465 if (_requestParser) { | 448 if (_requestParser) { |
466 throw new HttpException("Invalid request line"); | 449 throw new HttpException("Invalid request line"); |
467 } | 450 } |
468 _state = _State.RESPONSE_HTTP_VERSION; | 451 _state = _State.RESPONSE_HTTP_VERSION; |
469 } else { | 452 } else { |
470 // Did not parse HTTP version. Expect method instead. | 453 // Did not parse HTTP version. Expect method instead. |
471 for (int i = 0; i < _httpVersionIndex; i++) { | 454 for (int i = 0; i < _httpVersionIndex; i++) { |
(...skipping 11 matching lines...) Expand all Loading... |
483 } | 466 } |
484 } | 467 } |
485 break; | 468 break; |
486 | 469 |
487 case _State.RESPONSE_HTTP_VERSION: | 470 case _State.RESPONSE_HTTP_VERSION: |
488 if (_httpVersionIndex < _Const.HTTP1DOT.length) { | 471 if (_httpVersionIndex < _Const.HTTP1DOT.length) { |
489 // Continue parsing HTTP version. | 472 // Continue parsing HTTP version. |
490 _expect(byte, _Const.HTTP1DOT[_httpVersionIndex]); | 473 _expect(byte, _Const.HTTP1DOT[_httpVersionIndex]); |
491 _httpVersionIndex++; | 474 _httpVersionIndex++; |
492 } else if (_httpVersionIndex == _Const.HTTP1DOT.length && | 475 } else if (_httpVersionIndex == _Const.HTTP1DOT.length && |
493 byte == _CharCode.ONE) { | 476 byte == _CharCode.ONE) { |
494 // HTTP/1.1 parsed. | 477 // HTTP/1.1 parsed. |
495 _httpVersion = _HttpVersion.HTTP11; | 478 _httpVersion = _HttpVersion.HTTP11; |
496 _persistentConnection = true; | 479 _persistentConnection = true; |
497 _httpVersionIndex++; | 480 _httpVersionIndex++; |
498 } else if (_httpVersionIndex == _Const.HTTP1DOT.length && | 481 } else if (_httpVersionIndex == _Const.HTTP1DOT.length && |
499 byte == _CharCode.ZERO) { | 482 byte == _CharCode.ZERO) { |
500 // HTTP/1.0 parsed. | 483 // HTTP/1.0 parsed. |
501 _httpVersion = _HttpVersion.HTTP10; | 484 _httpVersion = _HttpVersion.HTTP10; |
502 _persistentConnection = false; | 485 _persistentConnection = false; |
503 _httpVersionIndex++; | 486 _httpVersionIndex++; |
504 } else if (_httpVersionIndex == _Const.HTTP1DOT.length + 1) { | 487 } else if (_httpVersionIndex == _Const.HTTP1DOT.length + 1) { |
505 _expect(byte, _CharCode.SP); | 488 _expect(byte, _CharCode.SP); |
506 // HTTP version parsed. | 489 // HTTP version parsed. |
507 _state = _State.RESPONSE_LINE_STATUS_CODE; | 490 _state = _State.RESPONSE_LINE_STATUS_CODE; |
508 } else { | 491 } else { |
509 throw new HttpException("Invalid response line"); | 492 throw new HttpException("Invalid response line"); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 } | 584 } |
602 break; | 585 break; |
603 | 586 |
604 case _State.RESPONSE_LINE_ENDING: | 587 case _State.RESPONSE_LINE_ENDING: |
605 _expect(byte, _CharCode.LF); | 588 _expect(byte, _CharCode.LF); |
606 _messageType == _MessageType.RESPONSE; | 589 _messageType == _MessageType.RESPONSE; |
607 if (_statusCode < 100 || _statusCode > 599) { | 590 if (_statusCode < 100 || _statusCode > 599) { |
608 throw new HttpException("Invalid response status code"); | 591 throw new HttpException("Invalid response status code"); |
609 } else { | 592 } else { |
610 // Check whether this response will never have a body. | 593 // Check whether this response will never have a body. |
611 if (_statusCode <= 199 || _statusCode == 204 || | 594 if (_statusCode <= 199 || |
| 595 _statusCode == 204 || |
612 _statusCode == 304) { | 596 _statusCode == 304) { |
613 _noMessageBody = true; | 597 _noMessageBody = true; |
614 } | 598 } |
615 } | 599 } |
616 _state = _State.HEADER_START; | 600 _state = _State.HEADER_START; |
617 break; | 601 break; |
618 | 602 |
619 case _State.HEADER_START: | 603 case _State.HEADER_START: |
620 _headers = new _HttpHeaders(version); | 604 _headers = new _HttpHeaders(version); |
621 if (byte == _CharCode.CR) { | 605 if (byte == _CharCode.CR) { |
622 _state = _State.HEADER_ENDING; | 606 _state = _State.HEADER_ENDING; |
623 } else if (byte == _CharCode.LF) { | 607 } else if (byte == _CharCode.LF) { |
624 _state = _State.HEADER_ENDING; | 608 _state = _State.HEADER_ENDING; |
625 _index--; // Make the new state see the LF again. | 609 _index--; // Make the new state see the LF again. |
626 } else { | 610 } else { |
627 // Start of new header field. | 611 // Start of new header field. |
628 _headerField.add(_toLowerCaseByte(byte)); | 612 _headerField.add(_toLowerCaseByte(byte)); |
629 _state = _State.HEADER_FIELD; | 613 _state = _State.HEADER_FIELD; |
630 } | 614 } |
631 break; | 615 break; |
632 | 616 |
633 case _State.HEADER_FIELD: | 617 case _State.HEADER_FIELD: |
634 if (byte == _CharCode.COLON) { | 618 if (byte == _CharCode.COLON) { |
635 _state = _State.HEADER_VALUE_START; | 619 _state = _State.HEADER_VALUE_START; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 } else { | 658 } else { |
675 String headerField = new String.fromCharCodes(_headerField); | 659 String headerField = new String.fromCharCodes(_headerField); |
676 String headerValue = new String.fromCharCodes(_headerValue); | 660 String headerValue = new String.fromCharCodes(_headerValue); |
677 if (headerField == "transfer-encoding" && | 661 if (headerField == "transfer-encoding" && |
678 _caseInsensitiveCompare("chunked".codeUnits, _headerValue)) { | 662 _caseInsensitiveCompare("chunked".codeUnits, _headerValue)) { |
679 _chunked = true; | 663 _chunked = true; |
680 } | 664 } |
681 if (headerField == "connection") { | 665 if (headerField == "connection") { |
682 List<String> tokens = _tokenizeFieldValue(headerValue); | 666 List<String> tokens = _tokenizeFieldValue(headerValue); |
683 for (int i = 0; i < tokens.length; i++) { | 667 for (int i = 0; i < tokens.length; i++) { |
684 if (_caseInsensitiveCompare("upgrade".codeUnits, | 668 if (_caseInsensitiveCompare( |
685 tokens[i].codeUnits)) { | 669 "upgrade".codeUnits, tokens[i].codeUnits)) { |
686 _connectionUpgrade = true; | 670 _connectionUpgrade = true; |
687 } | 671 } |
688 _headers._add(headerField, tokens[i]); | 672 _headers._add(headerField, tokens[i]); |
689 } | 673 } |
690 } else { | 674 } else { |
691 _headers._add(headerField, headerValue); | 675 _headers._add(headerField, headerValue); |
692 } | 676 } |
693 _headerField.clear(); | 677 _headerField.clear(); |
694 _headerValue.clear(); | 678 _headerValue.clear(); |
695 | 679 |
696 if (byte == _CharCode.CR) { | 680 if (byte == _CharCode.CR) { |
697 _state = _State.HEADER_ENDING; | 681 _state = _State.HEADER_ENDING; |
698 } else if (byte == _CharCode.LF) { | 682 } else if (byte == _CharCode.LF) { |
699 _state = _State.HEADER_ENDING; | 683 _state = _State.HEADER_ENDING; |
700 _index--; // Make the new state see the LF again. | 684 _index--; // Make the new state see the LF again. |
701 } else { | 685 } else { |
702 // Start of new header field. | 686 // Start of new header field. |
703 _headerField.add(_toLowerCaseByte(byte)); | 687 _headerField.add(_toLowerCaseByte(byte)); |
704 _state = _State.HEADER_FIELD; | 688 _state = _State.HEADER_FIELD; |
705 } | 689 } |
706 } | 690 } |
707 break; | 691 break; |
708 | 692 |
709 case _State.HEADER_ENDING: | 693 case _State.HEADER_ENDING: |
710 _expect(byte, _CharCode.LF); | 694 _expect(byte, _CharCode.LF); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 case _State.BODY: | 749 case _State.BODY: |
766 // The body is not handled one byte at a time but in blocks. | 750 // The body is not handled one byte at a time but in blocks. |
767 _index--; | 751 _index--; |
768 int dataAvailable = _buffer.length - _index; | 752 int dataAvailable = _buffer.length - _index; |
769 if (_remainingContent >= 0 && dataAvailable > _remainingContent) { | 753 if (_remainingContent >= 0 && dataAvailable > _remainingContent) { |
770 dataAvailable = _remainingContent; | 754 dataAvailable = _remainingContent; |
771 } | 755 } |
772 // Always present the data as a view. This way we can handle all | 756 // Always present the data as a view. This way we can handle all |
773 // cases like this, and the user will not experince different data | 757 // cases like this, and the user will not experince different data |
774 // typed (which could lead to polymorphic user code). | 758 // typed (which could lead to polymorphic user code). |
775 List<int> data = new Uint8List.view(_buffer.buffer, | 759 List<int> data = new Uint8List.view( |
776 _buffer.offsetInBytes + _index, | 760 _buffer.buffer, _buffer.offsetInBytes + _index, dataAvailable); |
777 dataAvailable); | |
778 _bodyController.add(data); | 761 _bodyController.add(data); |
779 if (_remainingContent != -1) { | 762 if (_remainingContent != -1) { |
780 _remainingContent -= data.length; | 763 _remainingContent -= data.length; |
781 } | 764 } |
782 _index += data.length; | 765 _index += data.length; |
783 if (_remainingContent == 0) { | 766 if (_remainingContent == 0) { |
784 if (!_chunked) { | 767 if (!_chunked) { |
785 _reset(); | 768 _reset(); |
786 _closeIncoming(); | 769 _closeIncoming(); |
787 } else { | 770 } else { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 void _onDone() { | 807 void _onDone() { |
825 // onDone cancles the subscription. | 808 // onDone cancles the subscription. |
826 _socketSubscription = null; | 809 _socketSubscription = null; |
827 if (_state == _State.CLOSED || _state == _State.FAILURE) return; | 810 if (_state == _State.CLOSED || _state == _State.FAILURE) return; |
828 | 811 |
829 if (_incoming != null) { | 812 if (_incoming != null) { |
830 if (_state != _State.UPGRADED && | 813 if (_state != _State.UPGRADED && |
831 !(_state == _State.START && !_requestParser) && | 814 !(_state == _State.START && !_requestParser) && |
832 !(_state == _State.BODY && !_chunked && _transferLength == -1)) { | 815 !(_state == _State.BODY && !_chunked && _transferLength == -1)) { |
833 _bodyController.addError( | 816 _bodyController.addError( |
834 new HttpException("Connection closed while receiving data")); | 817 new HttpException("Connection closed while receiving data")); |
835 } | 818 } |
836 _closeIncoming(true); | 819 _closeIncoming(true); |
837 _controller.close(); | 820 _controller.close(); |
838 return; | 821 return; |
839 } | 822 } |
840 // If the connection is idle the HTTP stream is closed. | 823 // If the connection is idle the HTTP stream is closed. |
841 if (_state == _State.START) { | 824 if (_state == _State.START) { |
842 if (!_requestParser) { | 825 if (!_requestParser) { |
843 _reportError(new HttpException( | 826 _reportError(new HttpException( |
844 "Connection closed before full header was received")); | 827 "Connection closed before full header was received")); |
845 } | 828 } |
846 _controller.close(); | 829 _controller.close(); |
847 return; | 830 return; |
848 } | 831 } |
849 | 832 |
850 if (_state == _State.UPGRADED) { | 833 if (_state == _State.UPGRADED) { |
851 _controller.close(); | 834 _controller.close(); |
852 return; | 835 return; |
853 } | 836 } |
854 | 837 |
855 if (_state < _State.FIRST_BODY_STATE) { | 838 if (_state < _State.FIRST_BODY_STATE) { |
856 _state = _State.FAILURE; | 839 _state = _State.FAILURE; |
857 // Report the error through the error callback if any. Otherwise | 840 // Report the error through the error callback if any. Otherwise |
858 // throw the error. | 841 // throw the error. |
859 _reportError(new HttpException( | 842 _reportError(new HttpException( |
860 "Connection closed before full header was received")); | 843 "Connection closed before full header was received")); |
861 _controller.close(); | 844 _controller.close(); |
862 return; | 845 return; |
863 } | 846 } |
864 | 847 |
865 if (!_chunked && _transferLength == -1) { | 848 if (!_chunked && _transferLength == -1) { |
866 _state = _State.CLOSED; | 849 _state = _State.CLOSED; |
867 } else { | 850 } else { |
868 _state = _State.FAILURE; | 851 _state = _State.FAILURE; |
869 // Report the error through the error callback if any. Otherwise | 852 // Report the error through the error callback if any. Otherwise |
870 // throw the error. | 853 // throw the error. |
871 _reportError(new HttpException( | 854 _reportError( |
872 "Connection closed before full body was received")); | 855 new HttpException("Connection closed before full body was received")); |
873 } | 856 } |
874 _controller.close(); | 857 _controller.close(); |
875 } | 858 } |
876 | 859 |
877 String get version { | 860 String get version { |
878 switch (_httpVersion) { | 861 switch (_httpVersion) { |
879 case _HttpVersion.HTTP10: | 862 case _HttpVersion.HTTP10: |
880 return "1.0"; | 863 return "1.0"; |
881 case _HttpVersion.HTTP11: | 864 case _HttpVersion.HTTP11: |
882 return "1.1"; | 865 return "1.1"; |
883 } | 866 } |
884 return null; | 867 return null; |
885 } | 868 } |
886 | 869 |
887 int get messageType => _messageType; | 870 int get messageType => _messageType; |
888 int get transferLength => _transferLength; | 871 int get transferLength => _transferLength; |
889 bool get upgrade => _connectionUpgrade && _state == _State.UPGRADED; | 872 bool get upgrade => _connectionUpgrade && _state == _State.UPGRADED; |
890 bool get persistentConnection => _persistentConnection; | 873 bool get persistentConnection => _persistentConnection; |
891 | 874 |
892 void set isHead(bool value) { | 875 void set isHead(bool value) { |
893 if (value) _noMessageBody = true; | 876 if (value) _noMessageBody = true; |
894 } | 877 } |
895 | 878 |
896 _HttpDetachedIncoming detachIncoming() { | 879 _HttpDetachedIncoming detachIncoming() { |
897 // Simulate detached by marking as upgraded. | 880 // Simulate detached by marking as upgraded. |
898 _state = _State.UPGRADED; | 881 _state = _State.UPGRADED; |
899 return new _HttpDetachedIncoming(_socketSubscription, | 882 return new _HttpDetachedIncoming(_socketSubscription, readUnparsedData()); |
900 readUnparsedData()); | |
901 } | 883 } |
902 | 884 |
903 List<int> readUnparsedData() { | 885 List<int> readUnparsedData() { |
904 if (_buffer == null) return null; | 886 if (_buffer == null) return null; |
905 if (_index == _buffer.length) return null; | 887 if (_index == _buffer.length) return null; |
906 var result = _buffer.sublist(_index); | 888 var result = _buffer.sublist(_index); |
907 _releaseBuffer(); | 889 _releaseBuffer(); |
908 return result; | 890 return result; |
909 } | 891 } |
910 | 892 |
(...skipping 24 matching lines...) Expand all Loading... |
935 void _releaseBuffer() { | 917 void _releaseBuffer() { |
936 _buffer = null; | 918 _buffer = null; |
937 _index = null; | 919 _index = null; |
938 } | 920 } |
939 | 921 |
940 static bool _isTokenChar(int byte) { | 922 static bool _isTokenChar(int byte) { |
941 return byte > 31 && byte < 128 && !_Const.SEPARATOR_MAP[byte]; | 923 return byte > 31 && byte < 128 && !_Const.SEPARATOR_MAP[byte]; |
942 } | 924 } |
943 | 925 |
944 static bool _isValueChar(int byte) { | 926 static bool _isValueChar(int byte) { |
945 return (byte > 31 && byte < 128) || (byte == _CharCode.SP) || | 927 return (byte > 31 && byte < 128) || |
| 928 (byte == _CharCode.SP) || |
946 (byte == _CharCode.HT); | 929 (byte == _CharCode.HT); |
947 } | 930 } |
948 | 931 |
949 static List<String> _tokenizeFieldValue(String headerValue) { | 932 static List<String> _tokenizeFieldValue(String headerValue) { |
950 List<String> tokens = new List<String>(); | 933 List<String> tokens = new List<String>(); |
951 int start = 0; | 934 int start = 0; |
952 int index = 0; | 935 int index = 0; |
953 while (index < headerValue.length) { | 936 while (index < headerValue.length) { |
954 if (headerValue[index] == ",") { | 937 if (headerValue[index] == ",") { |
955 tokens.add(headerValue.substring(start, index)); | 938 tokens.add(headerValue.substring(start, index)); |
(...skipping 26 matching lines...) Expand all Loading... |
982 } | 965 } |
983 | 966 |
984 int _expect(int val1, int val2) { | 967 int _expect(int val1, int val2) { |
985 if (val1 != val2) { | 968 if (val1 != val2) { |
986 throw new HttpException("Failed to parse HTTP"); | 969 throw new HttpException("Failed to parse HTTP"); |
987 } | 970 } |
988 } | 971 } |
989 | 972 |
990 int _expectHexDigit(int byte) { | 973 int _expectHexDigit(int byte) { |
991 if (0x30 <= byte && byte <= 0x39) { | 974 if (0x30 <= byte && byte <= 0x39) { |
992 return byte - 0x30; // 0 - 9 | 975 return byte - 0x30; // 0 - 9 |
993 } else if (0x41 <= byte && byte <= 0x46) { | 976 } else if (0x41 <= byte && byte <= 0x46) { |
994 return byte - 0x41 + 10; // A - F | 977 return byte - 0x41 + 10; // A - F |
995 } else if (0x61 <= byte && byte <= 0x66) { | 978 } else if (0x61 <= byte && byte <= 0x66) { |
996 return byte - 0x61 + 10; // a - f | 979 return byte - 0x61 + 10; // a - f |
997 } else { | 980 } else { |
998 throw new HttpException("Failed to parse HTTP"); | 981 throw new HttpException("Failed to parse HTTP"); |
999 } | 982 } |
1000 } | 983 } |
1001 | 984 |
1002 void _createIncoming(int transferLength) { | 985 void _createIncoming(int transferLength) { |
1003 assert(_incoming == null); | 986 assert(_incoming == null); |
1004 assert(_bodyController == null); | 987 assert(_bodyController == null); |
1005 assert(!_bodyPaused); | 988 assert(!_bodyPaused); |
1006 var incoming; | 989 var incoming; |
(...skipping 18 matching lines...) Expand all Loading... |
1025 _pauseStateChanged(); | 1008 _pauseStateChanged(); |
1026 }, | 1009 }, |
1027 onCancel: () { | 1010 onCancel: () { |
1028 if (incoming != _incoming) return; | 1011 if (incoming != _incoming) return; |
1029 if (_socketSubscription != null) { | 1012 if (_socketSubscription != null) { |
1030 _socketSubscription.cancel(); | 1013 _socketSubscription.cancel(); |
1031 } | 1014 } |
1032 _closeIncoming(true); | 1015 _closeIncoming(true); |
1033 _controller.close(); | 1016 _controller.close(); |
1034 }); | 1017 }); |
1035 incoming = _incoming = new _HttpIncoming( | 1018 incoming = _incoming = |
1036 _headers, transferLength, _bodyController.stream); | 1019 new _HttpIncoming(_headers, transferLength, _bodyController.stream); |
1037 _bodyPaused = true; | 1020 _bodyPaused = true; |
1038 _pauseStateChanged(); | 1021 _pauseStateChanged(); |
1039 } | 1022 } |
1040 | 1023 |
1041 void _closeIncoming([bool closing = false]) { | 1024 void _closeIncoming([bool closing = false]) { |
1042 // Ignore multiple close (can happen in re-entrance). | 1025 // Ignore multiple close (can happen in re-entrance). |
1043 if (_incoming == null) return; | 1026 if (_incoming == null) return; |
1044 var tmp = _incoming; | 1027 var tmp = _incoming; |
1045 tmp.close(closing); | 1028 tmp.close(closing); |
1046 _incoming = null; | 1029 _incoming = null; |
(...skipping 17 matching lines...) Expand all Loading... |
1064 } | 1047 } |
1065 } | 1048 } |
1066 | 1049 |
1067 void _reportError(error, [stackTrace]) { | 1050 void _reportError(error, [stackTrace]) { |
1068 if (_socketSubscription != null) _socketSubscription.cancel(); | 1051 if (_socketSubscription != null) _socketSubscription.cancel(); |
1069 _state = _State.FAILURE; | 1052 _state = _State.FAILURE; |
1070 _controller.addError(error, stackTrace); | 1053 _controller.addError(error, stackTrace); |
1071 _controller.close(); | 1054 _controller.close(); |
1072 } | 1055 } |
1073 } | 1056 } |
OLD | NEW |