| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.io; | |
| 6 | |
| 7 /** | |
| 8 * Utility functions for working with dates with HTTP specific date | |
| 9 * formats. | |
| 10 */ | |
| 11 class HttpDate { | |
| 12 // From RFC-2616 section "3.3.1 Full Date", | |
| 13 // http://tools.ietf.org/html/rfc2616#section-3.3.1 | |
| 14 // | |
| 15 // HTTP-date = rfc1123-date | rfc850-date | asctime-date | |
| 16 // rfc1123-date = wkday "," SP date1 SP time SP "GMT" | |
| 17 // rfc850-date = weekday "," SP date2 SP time SP "GMT" | |
| 18 // asctime-date = wkday SP date3 SP time SP 4DIGIT | |
| 19 // date1 = 2DIGIT SP month SP 4DIGIT | |
| 20 // ; day month year (e.g., 02 Jun 1982) | |
| 21 // date2 = 2DIGIT "-" month "-" 2DIGIT | |
| 22 // ; day-month-year (e.g., 02-Jun-82) | |
| 23 // date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) | |
| 24 // ; month day (e.g., Jun 2) | |
| 25 // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT | |
| 26 // ; 00:00:00 - 23:59:59 | |
| 27 // wkday = "Mon" | "Tue" | "Wed" | |
| 28 // | "Thu" | "Fri" | "Sat" | "Sun" | |
| 29 // weekday = "Monday" | "Tuesday" | "Wednesday" | |
| 30 // | "Thursday" | "Friday" | "Saturday" | "Sunday" | |
| 31 // month = "Jan" | "Feb" | "Mar" | "Apr" | |
| 32 // | "May" | "Jun" | "Jul" | "Aug" | |
| 33 // | "Sep" | "Oct" | "Nov" | "Dec" | |
| 34 | |
| 35 /** | |
| 36 * Format a date according to | |
| 37 * [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"), | |
| 38 * e.g. `Thu, 1 Jan 1970 00:00:00 GMT`. | |
| 39 */ | |
| 40 static String format(DateTime date) { | |
| 41 const List wkday = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
| 42 const List month = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 43 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | |
| 44 | |
| 45 DateTime d = date.toUtc(); | |
| 46 StringBuffer sb = new StringBuffer(); | |
| 47 sb.write(wkday[d.weekday - 1]); | |
| 48 sb.write(", "); | |
| 49 sb.write(d.day.toString()); | |
| 50 sb.write(" "); | |
| 51 sb.write(month[d.month - 1]); | |
| 52 sb.write(" "); | |
| 53 sb.write(d.year.toString()); | |
| 54 sb.write(d.hour < 9 ? " 0" : " "); | |
| 55 sb.write(d.hour.toString()); | |
| 56 sb.write(d.minute < 9 ? ":0" : ":"); | |
| 57 sb.write(d.minute.toString()); | |
| 58 sb.write(d.second < 9 ? ":0" : ":"); | |
| 59 sb.write(d.second.toString()); | |
| 60 sb.write(" GMT"); | |
| 61 return sb.toString(); | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Parse a date string in either of the formats | |
| 66 * [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"), | |
| 67 * [RFC-850](http://tools.ietf.org/html/rfc850 "RFC-850") or | |
| 68 * ANSI C's asctime() format. These formats are listed here. | |
| 69 * | |
| 70 * Thu, 1 Jan 1970 00:00:00 GMT | |
| 71 * Thursday, 1-Jan-1970 00:00:00 GMT | |
| 72 * Thu Jan 1 00:00:00 1970 | |
| 73 * | |
| 74 * For more information see [RFC-2616 section 3.1.1] | |
| 75 * (http://tools.ietf.org/html/rfc2616#section-3.3.1 | |
| 76 * "RFC-2616 section 3.1.1"). | |
| 77 */ | |
| 78 static DateTime parse(String date) { | |
| 79 final int SP = 32; | |
| 80 const List wkdays = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
| 81 const List weekdays = const ["Monday", "Tuesday", "Wednesday", "Thursday", | |
| 82 "Friday", "Saturday", "Sunday"]; | |
| 83 const List months = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 84 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | |
| 85 const List wkdaysLowerCase = | |
| 86 const ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; | |
| 87 const List weekdaysLowerCase = const ["monday", "tuesday", "wednesday", | |
| 88 "thursday", "friday", "saturday", | |
| 89 "sunday"]; | |
| 90 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may", | |
| 91 "jun", "jul", "aug", "sep", "oct", | |
| 92 "nov", "dec"]; | |
| 93 | |
| 94 final int formatRfc1123 = 0; | |
| 95 final int formatRfc850 = 1; | |
| 96 final int formatAsctime = 2; | |
| 97 | |
| 98 int index = 0; | |
| 99 String tmp; | |
| 100 int format; | |
| 101 | |
| 102 void expect(String s) { | |
| 103 if (date.length - index < s.length) { | |
| 104 throw new HttpException("Invalid HTTP date $date"); | |
| 105 } | |
| 106 String tmp = date.substring(index, index + s.length); | |
| 107 if (tmp != s) { | |
| 108 throw new HttpException("Invalid HTTP date $date"); | |
| 109 } | |
| 110 index += s.length; | |
| 111 } | |
| 112 | |
| 113 int expectWeekday() { | |
| 114 int weekday; | |
| 115 // The formatting of the weekday signals the format of the date string. | |
| 116 int pos = date.indexOf(",", index); | |
| 117 if (pos == -1) { | |
| 118 int pos = date.indexOf(" ", index); | |
| 119 if (pos == -1) throw new HttpException("Invalid HTTP date $date"); | |
| 120 tmp = date.substring(index, pos); | |
| 121 index = pos + 1; | |
| 122 weekday = wkdays.indexOf(tmp); | |
| 123 if (weekday != -1) { | |
| 124 format = formatAsctime; | |
| 125 return weekday; | |
| 126 } | |
| 127 } else { | |
| 128 tmp = date.substring(index, pos); | |
| 129 index = pos + 1; | |
| 130 weekday = wkdays.indexOf(tmp); | |
| 131 if (weekday != -1) { | |
| 132 format = formatRfc1123; | |
| 133 return weekday; | |
| 134 } | |
| 135 weekday = weekdays.indexOf(tmp); | |
| 136 if (weekday != -1) { | |
| 137 format = formatRfc850; | |
| 138 return weekday; | |
| 139 } | |
| 140 } | |
| 141 throw new HttpException("Invalid HTTP date $date"); | |
| 142 } | |
| 143 | |
| 144 int expectMonth(String separator) { | |
| 145 int pos = date.indexOf(separator, index); | |
| 146 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); | |
| 147 tmp = date.substring(index, pos); | |
| 148 index = pos + 1; | |
| 149 int month = months.indexOf(tmp); | |
| 150 if (month != -1) return month; | |
| 151 throw new HttpException("Invalid HTTP date $date"); | |
| 152 } | |
| 153 | |
| 154 int expectNum(String separator) { | |
| 155 int pos; | |
| 156 if (separator.length > 0) { | |
| 157 pos = date.indexOf(separator, index); | |
| 158 } else { | |
| 159 pos = date.length; | |
| 160 } | |
| 161 String tmp = date.substring(index, pos); | |
| 162 index = pos + separator.length; | |
| 163 try { | |
| 164 int value = int.parse(tmp); | |
| 165 return value; | |
| 166 } on FormatException catch (e) { | |
| 167 throw new HttpException("Invalid HTTP date $date"); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void expectEnd() { | |
| 172 if (index != date.length) { | |
| 173 throw new HttpException("Invalid HTTP date $date"); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 int weekday = expectWeekday(); | |
| 178 int day; | |
| 179 int month; | |
| 180 int year; | |
| 181 int hours; | |
| 182 int minutes; | |
| 183 int seconds; | |
| 184 if (format == formatAsctime) { | |
| 185 month = expectMonth(" "); | |
| 186 if (date.codeUnitAt(index) == SP) index++; | |
| 187 day = expectNum(" "); | |
| 188 hours = expectNum(":"); | |
| 189 minutes = expectNum(":"); | |
| 190 seconds = expectNum(" "); | |
| 191 year = expectNum(""); | |
| 192 } else { | |
| 193 expect(" "); | |
| 194 day = expectNum(format == formatRfc1123 ? " " : "-"); | |
| 195 month = expectMonth(format == formatRfc1123 ? " " : "-"); | |
| 196 year = expectNum(" "); | |
| 197 hours = expectNum(":"); | |
| 198 minutes = expectNum(":"); | |
| 199 seconds = expectNum(" "); | |
| 200 expect("GMT"); | |
| 201 } | |
| 202 expectEnd(); | |
| 203 return new DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 class _HttpUtils { | |
| 208 static String decodeUrlEncodedString( | |
| 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", | |
| 314 "jun", "jul", "aug", "sep", "oct", | |
| 315 "nov", "dec"]; | |
| 316 | |
| 317 int position = 0; | |
| 318 | |
| 319 void error() { | |
| 320 throw new HttpException("Invalid cookie date $date"); | |
| 321 } | |
| 322 | |
| 323 bool isEnd() { | |
| 324 return position == date.length; | |
| 325 } | |
| 326 | |
| 327 bool isDelimiter(String s) { | |
| 328 int char = s.codeUnitAt(0); | |
| 329 if (char == 0x09) return true; | |
| 330 if (char >= 0x20 && char <= 0x2F) return true; | |
| 331 if (char >= 0x3B && char <= 0x40) return true; | |
| 332 if (char >= 0x5B && char <= 0x60) return true; | |
| 333 if (char >= 0x7B && char <= 0x7E) return true; | |
| 334 return false; | |
| 335 } | |
| 336 | |
| 337 bool isNonDelimiter(String s) { | |
| 338 int char = s.codeUnitAt(0); | |
| 339 if (char >= 0x00 && char <= 0x08) return true; | |
| 340 if (char >= 0x0A && char <= 0x1F) return true; | |
| 341 if (char >= 0x30 && char <= 0x39) return true; // Digit | |
| 342 if (char == 0x3A) return true; // ':' | |
| 343 if (char >= 0x41 && char <= 0x5A) return true; // Alpha | |
| 344 if (char >= 0x61 && char <= 0x7A) return true; // Alpha | |
| 345 if (char >= 0x7F && char <= 0xFF) return true; // Alpha | |
| 346 return false; | |
| 347 } | |
| 348 | |
| 349 bool isDigit(String s) { | |
| 350 int char = s.codeUnitAt(0); | |
| 351 if (char > 0x2F && char < 0x3A) return true; | |
| 352 return false; | |
| 353 } | |
| 354 | |
| 355 int getMonth(String month) { | |
| 356 if (month.length < 3) return -1; | |
| 357 return monthsLowerCase.indexOf(month.substring(0, 3)); | |
| 358 } | |
| 359 | |
| 360 int toInt(String s) { | |
| 361 int index = 0; | |
| 362 for (; index < s.length && isDigit(s[index]); index++); | |
| 363 return int.parse(s.substring(0, index)); | |
| 364 } | |
| 365 | |
| 366 var tokens = []; | |
| 367 while (!isEnd()) { | |
| 368 while (!isEnd() && isDelimiter(date[position])) position++; | |
| 369 int start = position; | |
| 370 while (!isEnd() && isNonDelimiter(date[position])) position++; | |
| 371 tokens.add(date.substring(start, position).toLowerCase()); | |
| 372 while (!isEnd() && isDelimiter(date[position])) position++; | |
| 373 } | |
| 374 | |
| 375 String timeStr; | |
| 376 String dayOfMonthStr; | |
| 377 String monthStr; | |
| 378 String yearStr; | |
| 379 | |
| 380 for (var token in tokens) { | |
| 381 if (token.length < 1) continue; | |
| 382 if (timeStr == null && token.length >= 5 && isDigit(token[0]) && | |
| 383 (token[1] == ":" || (isDigit(token[1]) && token[2] == ":"))) { | |
| 384 timeStr = token; | |
| 385 } else if (dayOfMonthStr == null && isDigit(token[0])) { | |
| 386 dayOfMonthStr = token; | |
| 387 } else if (monthStr == null && getMonth(token) >= 0) { | |
| 388 monthStr = token; | |
| 389 } else if (yearStr == null && token.length >= 2 && | |
| 390 isDigit(token[0]) && isDigit(token[1])) { | |
| 391 yearStr = token; | |
| 392 } | |
| 393 } | |
| 394 | |
| 395 if (timeStr == null || dayOfMonthStr == null || | |
| 396 monthStr == null || yearStr == null) { | |
| 397 error(); | |
| 398 } | |
| 399 | |
| 400 int year = toInt(yearStr); | |
| 401 if (year >= 70 && year <= 99) year += 1900; | |
| 402 else if (year >= 0 && year <= 69) year += 2000; | |
| 403 if (year < 1601) error(); | |
| 404 | |
| 405 int dayOfMonth = toInt(dayOfMonthStr); | |
| 406 if (dayOfMonth < 1 || dayOfMonth > 31) error(); | |
| 407 | |
| 408 int month = getMonth(monthStr) + 1; | |
| 409 | |
| 410 var timeList = timeStr.split(":"); | |
| 411 if (timeList.length != 3) error(); | |
| 412 int hour = toInt(timeList[0]); | |
| 413 int minute = toInt(timeList[1]); | |
| 414 int second = toInt(timeList[2]); | |
| 415 if (hour > 23) error(); | |
| 416 if (minute > 59) error(); | |
| 417 if (second > 59) error(); | |
| 418 | |
| 419 return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0); | |
| 420 } | |
| 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 } | |
| OLD | NEW |