| 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Utility functions for working with dates with HTTP specific date | 8 * Utility functions for working with dates with HTTP specific date |
| 9 * formats. | 9 * formats. |
| 10 */ | 10 */ |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 month = expectMonth(format == formatRfc1123 ? " " : "-"); | 195 month = expectMonth(format == formatRfc1123 ? " " : "-"); |
| 196 year = expectNum(" "); | 196 year = expectNum(" "); |
| 197 hours = expectNum(":"); | 197 hours = expectNum(":"); |
| 198 minutes = expectNum(":"); | 198 minutes = expectNum(":"); |
| 199 seconds = expectNum(" "); | 199 seconds = expectNum(" "); |
| 200 expect("GMT"); | 200 expect("GMT"); |
| 201 } | 201 } |
| 202 expectEnd(); | 202 expectEnd(); |
| 203 return new DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0); | 203 return new DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0); |
| 204 } | 204 } |
| 205 } | |
| 206 | 205 |
| 207 class _HttpUtils { | 206 // Parse a cookie date string. |
| 208 static String decodeUrlEncodedString( | 207 static DateTime _parseCookieDate(String date) { |
| 209 String urlEncoded, | |
| 210 {Encoding encoding: Encoding.UTF_8}) { | |
| 211 // First check the string for any encoding. | |
| 212 int index = 0; | |
| 213 bool encoded = false; | |
| 214 while (!encoded && index < urlEncoded.length) { | |
| 215 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%"; | |
| 216 index++; | |
| 217 } | |
| 218 if (!encoded) return urlEncoded; | |
| 219 index--; | |
| 220 | |
| 221 // Start decoding from the first encoded character. | |
| 222 List<int> bytes = new List<int>(); | |
| 223 for (int i = 0; i < index; i++) bytes.add(urlEncoded.codeUnitAt(i)); | |
| 224 for (int i = index; i < urlEncoded.length; i++) { | |
| 225 if (urlEncoded[i] == "+") { | |
| 226 bytes.add(32); | |
| 227 } else if (urlEncoded[i] == "%") { | |
| 228 if (urlEncoded.length - i < 2) { | |
| 229 throw new HttpException("Invalid URL encoding"); | |
| 230 } | |
| 231 int byte = 0; | |
| 232 for (int j = 0; j < 2; j++) { | |
| 233 var charCode = urlEncoded.codeUnitAt(i + j + 1); | |
| 234 if (0x30 <= charCode && charCode <= 0x39) { | |
| 235 byte = byte * 16 + charCode - 0x30; | |
| 236 } else { | |
| 237 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66). | |
| 238 charCode |= 0x20; | |
| 239 if (0x61 <= charCode && charCode <= 0x66) { | |
| 240 byte = byte * 16 + charCode - 0x57; | |
| 241 } else { | |
| 242 throw new ArgumentError("Invalid URL encoding"); | |
| 243 } | |
| 244 } | |
| 245 } | |
| 246 bytes.add(byte); | |
| 247 i += 2; | |
| 248 } else { | |
| 249 bytes.add(urlEncoded.codeUnitAt(i)); | |
| 250 } | |
| 251 } | |
| 252 return _decodeString(bytes, encoding); | |
| 253 } | |
| 254 | |
| 255 static Map<String, String> splitQueryString( | |
| 256 String queryString, | |
| 257 {Encoding encoding: Encoding.UTF_8}) { | |
| 258 Map<String, String> result = new Map<String, String>(); | |
| 259 int currentPosition = 0; | |
| 260 int length = queryString.length; | |
| 261 | |
| 262 while (currentPosition < length) { | |
| 263 | |
| 264 // Find the first equals character between current position and | |
| 265 // the provided end. | |
| 266 int indexOfEquals(int end) { | |
| 267 int index = currentPosition; | |
| 268 while (index < end) { | |
| 269 if (queryString.codeUnitAt(index) == _CharCode.EQUAL) return index; | |
| 270 index++; | |
| 271 } | |
| 272 return -1; | |
| 273 } | |
| 274 | |
| 275 // Find the next separator (either & or ;), see | |
| 276 // http://www.w3.org/TR/REC-html40/appendix/notes.html#ampersands-in-uris | |
| 277 // relating the ; separator. If no separator is found returns | |
| 278 // the length of the query string. | |
| 279 int indexOfSeparator() { | |
| 280 int end = length; | |
| 281 int index = currentPosition; | |
| 282 while (index < end) { | |
| 283 int codeUnit = queryString.codeUnitAt(index); | |
| 284 if (codeUnit == _CharCode.AMPERSAND || | |
| 285 codeUnit == _CharCode.SEMI_COLON) { | |
| 286 return index; | |
| 287 } | |
| 288 index++; | |
| 289 } | |
| 290 return end; | |
| 291 } | |
| 292 | |
| 293 int seppos = indexOfSeparator(); | |
| 294 int equalspos = indexOfEquals(seppos); | |
| 295 String name; | |
| 296 String value; | |
| 297 if (equalspos == -1) { | |
| 298 name = queryString.substring(currentPosition, seppos); | |
| 299 value = ''; | |
| 300 } else { | |
| 301 name = queryString.substring(currentPosition, equalspos); | |
| 302 value = queryString.substring(equalspos + 1, seppos); | |
| 303 } | |
| 304 currentPosition = seppos + 1; // This also works when seppos == length. | |
| 305 if (name == '') continue; | |
| 306 result[_HttpUtils.decodeUrlEncodedString(name, encoding: encoding)] = | |
| 307 _HttpUtils.decodeUrlEncodedString(value, encoding: encoding); | |
| 308 } | |
| 309 return result; | |
| 310 } | |
| 311 | |
| 312 static DateTime parseCookieDate(String date) { | |
| 313 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may", | 208 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may", |
| 314 "jun", "jul", "aug", "sep", "oct", | 209 "jun", "jul", "aug", "sep", "oct", |
| 315 "nov", "dec"]; | 210 "nov", "dec"]; |
| 316 | 211 |
| 317 int position = 0; | 212 int position = 0; |
| 318 | 213 |
| 319 void error() { | 214 void error() { |
| 320 throw new HttpException("Invalid cookie date $date"); | 215 throw new HttpException("Invalid cookie date $date"); |
| 321 } | 216 } |
| 322 | 217 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 if (timeList.length != 3) error(); | 306 if (timeList.length != 3) error(); |
| 412 int hour = toInt(timeList[0]); | 307 int hour = toInt(timeList[0]); |
| 413 int minute = toInt(timeList[1]); | 308 int minute = toInt(timeList[1]); |
| 414 int second = toInt(timeList[2]); | 309 int second = toInt(timeList[2]); |
| 415 if (hour > 23) error(); | 310 if (hour > 23) error(); |
| 416 if (minute > 59) error(); | 311 if (minute > 59) error(); |
| 417 if (second > 59) error(); | 312 if (second > 59) error(); |
| 418 | 313 |
| 419 return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0); | 314 return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0); |
| 420 } | 315 } |
| 421 | |
| 422 // Decode a string with HTTP entities. Returns null if the string ends in the | |
| 423 // middle of a http entity. | |
| 424 static String decodeHttpEntityString(String input) { | |
| 425 int amp = input.lastIndexOf('&'); | |
| 426 if (amp < 0) return input; | |
| 427 int end = input.lastIndexOf(';'); | |
| 428 if (end < amp) return null; | |
| 429 | |
| 430 var buffer = new StringBuffer(); | |
| 431 int offset = 0; | |
| 432 | |
| 433 parse(amp, end) { | |
| 434 switch (input[amp + 1]) { | |
| 435 case '#': | |
| 436 if (input[amp + 2] == 'x') { | |
| 437 buffer.writeCharCode( | |
| 438 int.parse(input.substring(amp + 3, end), radix: 16)); | |
| 439 } else { | |
| 440 buffer.writeCharCode(int.parse(input.substring(amp + 2, end))); | |
| 441 } | |
| 442 break; | |
| 443 | |
| 444 default: | |
| 445 throw new HttpException('Unhandled HTTP entity token'); | |
| 446 } | |
| 447 } | |
| 448 | |
| 449 while ((amp = input.indexOf('&', offset)) >= 0) { | |
| 450 buffer.write(input.substring(offset, amp)); | |
| 451 int end = input.indexOf(';', amp); | |
| 452 parse(amp, end); | |
| 453 offset = end + 1; | |
| 454 } | |
| 455 buffer.write(input.substring(offset)); | |
| 456 return buffer.toString(); | |
| 457 } | |
| 458 } | 316 } |
| OLD | NEW |