| 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 // Global constants. | 5 // Global constants. |
| 6 class _Const { | 6 class _Const { |
| 7 // Bytes for "HTTP/1.0". | 7 // Bytes for "HTTP/1.0". |
| 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; | 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; |
| 9 // Bytes for "HTTP/1.1". | 9 // Bytes for "HTTP/1.1". |
| 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; | 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // Start of new header field. | 252 // Start of new header field. |
| 253 _headerField.addCharCode(_toLowerCase(byte)); | 253 _headerField.addCharCode(_toLowerCase(byte)); |
| 254 _state = _State.HEADER_FIELD; | 254 _state = _State.HEADER_FIELD; |
| 255 } | 255 } |
| 256 break; | 256 break; |
| 257 | 257 |
| 258 case _State.HEADER_FIELD: | 258 case _State.HEADER_FIELD: |
| 259 if (byte == _CharCode.COLON) { | 259 if (byte == _CharCode.COLON) { |
| 260 _state = _State.HEADER_VALUE_START; | 260 _state = _State.HEADER_VALUE_START; |
| 261 } else { | 261 } else { |
| 262 if (!_isTokenChar(byte)) { |
| 263 throw new HttpParserException("Invalid header field name"); |
| 264 } |
| 262 _headerField.addCharCode(_toLowerCase(byte)); | 265 _headerField.addCharCode(_toLowerCase(byte)); |
| 263 } | 266 } |
| 264 break; | 267 break; |
| 265 | 268 |
| 266 case _State.HEADER_VALUE_START: | 269 case _State.HEADER_VALUE_START: |
| 267 if (byte != _CharCode.SP && byte != _CharCode.HT) { | 270 if (byte == _CharCode.CR) { |
| 271 _state = _State.HEADER_VALUE_FOLDING_OR_ENDING; |
| 272 } else if (byte != _CharCode.SP && byte != _CharCode.HT) { |
| 268 // Start of new header value. | 273 // Start of new header value. |
| 269 _headerValue.addCharCode(byte); | 274 _headerValue.addCharCode(byte); |
| 270 _state = _State.HEADER_VALUE; | 275 _state = _State.HEADER_VALUE; |
| 271 } | 276 } |
| 272 break; | 277 break; |
| 273 | 278 |
| 274 case _State.HEADER_VALUE: | 279 case _State.HEADER_VALUE: |
| 275 if (byte == _CharCode.CR) { | 280 if (byte == _CharCode.CR) { |
| 276 _state = _State.HEADER_VALUE_FOLDING_OR_ENDING; | 281 _state = _State.HEADER_VALUE_FOLDING_OR_ENDING; |
| 277 } else { | 282 } else { |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 _contentLength = -1; | 516 _contentLength = -1; |
| 512 _keepAlive = false; | 517 _keepAlive = false; |
| 513 _connectionClose = false; | 518 _connectionClose = false; |
| 514 _chunked = false; | 519 _chunked = false; |
| 515 | 520 |
| 516 _noMessageBody = false; | 521 _noMessageBody = false; |
| 517 _responseToMethod = null; | 522 _responseToMethod = null; |
| 518 _remainingContent = null; | 523 _remainingContent = null; |
| 519 } | 524 } |
| 520 | 525 |
| 526 bool _isTokenChar(int byte) { |
| 527 return byte > 31 && byte < 128 && _Const.SEPARATORS.indexOf(byte) == -1; |
| 528 } |
| 529 |
| 521 int _toLowerCase(int byte) { | 530 int _toLowerCase(int byte) { |
| 522 final int aCode = "A".charCodeAt(0); | 531 final int aCode = "A".charCodeAt(0); |
| 523 final int zCode = "Z".charCodeAt(0); | 532 final int zCode = "Z".charCodeAt(0); |
| 524 final int delta = "a".charCodeAt(0) - aCode; | 533 final int delta = "a".charCodeAt(0) - aCode; |
| 525 return (aCode <= byte && byte <= zCode) ? byte + delta : byte; | 534 return (aCode <= byte && byte <= zCode) ? byte + delta : byte; |
| 526 } | 535 } |
| 527 | 536 |
| 528 int _expect(int val1, int val2) { | 537 int _expect(int val1, int val2) { |
| 529 if (val1 != val2) { | 538 if (val1 != val2) { |
| 530 throw new HttpParserException("Failed to parse HTTP"); | 539 throw new HttpParserException("Failed to parse HTTP"); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 Function dataEnd; | 578 Function dataEnd; |
| 570 Function error; | 579 Function error; |
| 571 } | 580 } |
| 572 | 581 |
| 573 | 582 |
| 574 class HttpParserException implements Exception { | 583 class HttpParserException implements Exception { |
| 575 const HttpParserException([String this.message = ""]); | 584 const HttpParserException([String this.message = ""]); |
| 576 String toString() => "HttpParserException: $message"; | 585 String toString() => "HttpParserException: $message"; |
| 577 final String message; | 586 final String message; |
| 578 } | 587 } |
| OLD | NEW |