OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// A library for broadly-useful functions and regular expressions for scanning | 5 /// A library for broadly-useful functions and regular expressions for scanning |
6 /// HTTP entities. | 6 /// HTTP entities. |
7 /// | 7 /// |
8 /// Many of the regular expressions come from [section 2.2 of the HTTP | 8 /// Many of the regular expressions come from [section 2.2 of the HTTP |
9 /// spec][spec]. | 9 /// spec][spec]. |
10 /// | 10 /// |
11 /// [spec]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html | 11 /// [spec]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html |
12 import 'package:string_scanner/string_scanner.dart'; | 12 import 'package:string_scanner/string_scanner.dart'; |
| 13 /// HTTP entities. |
| 14 /// |
| 15 /// Many of the regular expressions come from [section 2.2 of the HTTP |
| 16 /// spec][spec]. |
| 17 /// |
| 18 /// [spec]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html |
| 19 |
13 | 20 |
14 /// An HTTP token. | 21 /// An HTTP token. |
15 final token = new RegExp(r'[^()<>@,;:"\\/[\]?={} \t\x00-\x1F\x7F]+'); | 22 final token = new RegExp(r'[^()<>@,;:"\\/[\]?={} \t\x00-\x1F\x7F]+'); |
16 | 23 |
17 /// Linear whitespace. | 24 /// Linear whitespace. |
18 final _lws = new RegExp(r"(?:\r\n)?[ \t]+"); | 25 final _lws = new RegExp(r"(?:\r\n)?[ \t]+"); |
19 | 26 |
20 /// A quoted string. | 27 /// A quoted string. |
21 final _quotedString = new RegExp(r'"(?:[^"\x00-\x1F\x7F]|\\.)*"'); | 28 final _quotedString = new RegExp(r'"(?:[^"\x00-\x1F\x7F]|\\.)*"'); |
22 | 29 |
23 /// A quoted pair. | 30 /// A quoted pair. |
24 final _quotedPair = new RegExp(r'\\(.)'); | 31 final _quotedPair = new RegExp(r'\\(.)'); |
25 | 32 |
26 /// A character that is *not* a valid HTTP token. | 33 /// A character that is *not* a valid HTTP token. |
27 final nonToken = new RegExp(r'[()<>@,;:"\\/\[\]?={} \t\x00-\x1F\x7F]'); | 34 final nonToken = new RegExp(r'[()<>@,;:"\\/\[\]?={} \t\x00-\x1F\x7F]'); |
28 | 35 |
29 /// A regular expression matching any number of [_lws] productions in a row. | 36 /// A regular expression matching any number of [_lws] productions in a row. |
30 final whitespace = new RegExp("(?:${_lws.pattern})*"); | 37 final whitespace = new RegExp("(?:${_lws.pattern})*"); |
31 | 38 |
32 /// Parses a list of elements, as in `1#element` in the HTTP spec. | 39 /// Parses a list of elements, as in `1#element` in the HTTP spec. |
33 /// | 40 /// |
34 /// [scanner] is used to parse the elements, and [parseElement] is used to parse | 41 /// [scanner] is used to parse the elements, and [parseElement] is used to parse |
35 /// each one individually. The values returned by [parseElement] are collected | 42 /// each one individually. The values returned by [parseElement] are collected |
36 /// in a list and returned. | 43 /// in a list and returned. |
37 /// | 44 /// |
38 /// Once this is finished, [scanner] will be at the next non-LWS character in | 45 /// Once this is finished, [scanner] will be at the next non-LWS character in |
39 /// the string, or the end of the string. | 46 /// the string, or the end of the string. |
40 List/*<T>*/ parseList/*<T>*/(StringScanner scanner, /*=T*/ parseElement()) { | 47 List<T> parseList<T>(StringScanner scanner, T parseElement()) { |
41 var result = /*<T>*/ []; | 48 var result = <T>[]; |
42 | 49 |
43 // Consume initial empty values. | 50 // Consume initial empty values. |
44 while (scanner.scan(",")) { | 51 while (scanner.scan(",")) { |
45 scanner.scan(whitespace); | 52 scanner.scan(whitespace); |
46 } | 53 } |
47 | 54 |
48 result.add(parseElement()); | 55 result.add(parseElement()); |
49 scanner.scan(whitespace); | 56 scanner.scan(whitespace); |
50 | 57 |
51 while (scanner.scan(",")) { | 58 while (scanner.scan(",")) { |
(...skipping 14 matching lines...) Expand all Loading... |
66 /// If [name] is passed, it's used to describe the expected value if it's not | 73 /// If [name] is passed, it's used to describe the expected value if it's not |
67 /// found. | 74 /// found. |
68 String expectQuotedString(StringScanner scanner, {String name}) { | 75 String expectQuotedString(StringScanner scanner, {String name}) { |
69 if (name == null) name = "quoted string"; | 76 if (name == null) name = "quoted string"; |
70 scanner.expect(_quotedString, name: name); | 77 scanner.expect(_quotedString, name: name); |
71 var string = scanner.lastMatch[0]; | 78 var string = scanner.lastMatch[0]; |
72 return string | 79 return string |
73 .substring(1, string.length - 1) | 80 .substring(1, string.length - 1) |
74 .replaceAllMapped(_quotedPair, (match) => match[1]); | 81 .replaceAllMapped(_quotedPair, (match) => match[1]); |
75 } | 82 } |
OLD | NEW |