OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library http_parser.media_type; | 5 library http_parser.media_type; |
6 | 6 |
7 import 'package:collection/collection.dart'; | 7 import 'package:collection/collection.dart'; |
8 import 'package:string_scanner/string_scanner.dart'; | 8 import 'package:string_scanner/string_scanner.dart'; |
9 | 9 |
10 // All of the following regular expressions come from section 2.2 of the HTTP | 10 // All of the following regular expressions come from section 2.2 of the HTTP |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 scanner.expect(_token); | 66 scanner.expect(_token); |
67 var attribute = scanner.lastMatch[0]; | 67 var attribute = scanner.lastMatch[0]; |
68 scanner.expect('='); | 68 scanner.expect('='); |
69 | 69 |
70 var value; | 70 var value; |
71 if (scanner.scan(_token)) { | 71 if (scanner.scan(_token)) { |
72 value = scanner.lastMatch[0]; | 72 value = scanner.lastMatch[0]; |
73 } else { | 73 } else { |
74 scanner.expect(_quotedString); | 74 scanner.expect(_quotedString); |
75 var quotedString = scanner.lastMatch[0]; | 75 var quotedString = scanner.lastMatch[0]; |
76 value = quotedString.substring(1, quotedString.length - 1). | 76 value = quotedString |
77 replaceAllMapped(_quotedPair, (match) => match[1]); | 77 .substring(1, quotedString.length - 1) |
| 78 .replaceAllMapped(_quotedPair, (match) => match[1]); |
78 } | 79 } |
79 | 80 |
80 scanner.scan(_whitespace); | 81 scanner.scan(_whitespace); |
81 parameters[attribute] = value; | 82 parameters[attribute] = value; |
82 } | 83 } |
83 | 84 |
84 scanner.expectDone(); | 85 scanner.expectDone(); |
85 return new MediaType(type, subtype, parameters); | 86 return new MediaType(type, subtype, parameters); |
86 } on FormatException catch (error) { | 87 } on FormatException catch (error) { |
87 throw new FormatException( | 88 throw new FormatException( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 133 } |
133 | 134 |
134 return new MediaType(type, subtype, parameters); | 135 return new MediaType(type, subtype, parameters); |
135 } | 136 } |
136 | 137 |
137 /// Converts the media type to a string. | 138 /// Converts the media type to a string. |
138 /// | 139 /// |
139 /// This will produce a valid HTTP media type. | 140 /// This will produce a valid HTTP media type. |
140 String toString() { | 141 String toString() { |
141 var buffer = new StringBuffer() | 142 var buffer = new StringBuffer() |
142 ..write(type) | 143 ..write(type) |
143 ..write("/") | 144 ..write("/") |
144 ..write(subtype); | 145 ..write(subtype); |
145 | 146 |
146 parameters.forEach((attribute, value) { | 147 parameters.forEach((attribute, value) { |
147 buffer.write("; $attribute="); | 148 buffer.write("; $attribute="); |
148 if (_nonToken.hasMatch(value)) { | 149 if (_nonToken.hasMatch(value)) { |
149 buffer | 150 buffer |
150 ..write('"') | 151 ..write('"') |
151 ..write(value.replaceAllMapped( | 152 ..write( |
152 _escapedChar, (match) => "\\" + match[0])) | 153 value.replaceAllMapped(_escapedChar, (match) => "\\" + match[0])) |
153 ..write('"'); | 154 ..write('"'); |
154 } else { | 155 } else { |
155 buffer.write(value); | 156 buffer.write(value); |
156 } | 157 } |
157 }); | 158 }); |
158 | 159 |
159 return buffer.toString(); | 160 return buffer.toString(); |
160 } | 161 } |
161 } | 162 } |
OLD | NEW |