Chromium Code Reviews| Index: runtime/bin/http_utils.dart |
| diff --git a/runtime/bin/http_utils.dart b/runtime/bin/http_utils.dart |
| index 96ce2213bff0727059ae618523ce3a84bd307c1a..f283128dfe11b52307c78470c2f2779baf5b63d0 100644 |
| --- a/runtime/bin/http_utils.dart |
| +++ b/runtime/bin/http_utils.dart |
| @@ -117,7 +117,7 @@ class _HttpUtils { |
| return sb.toString(); |
| } |
| - static Date parseDate(String date, [bool strict = true]) { |
| + static Date parseDate(String date) { |
| final int SP = 32; |
| List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
|
Mads Ager (google)
2012/09/04 08:41:14
Not your code, but these could be:
const List wkd
Anders Johnsen
2012/09/05 08:44:29
Done.
|
| List weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", |
| @@ -143,14 +143,8 @@ class _HttpUtils { |
| throw new HttpException("Invalid HTTP date $date"); |
| } |
| String tmp = date.substring(index, index + s.length); |
| - if (strict) { |
| - if (tmp != s) { |
| - throw new HttpException("Invalid HTTP date $date"); |
| - } |
| - } else { |
| - if (tmp.toLowerCase() != s.toLowerCase()) { |
| - throw new HttpException("Invalid HTTP date $date"); |
| - } |
| + if (tmp != s) { |
| + throw new HttpException("Invalid HTTP date $date"); |
| } |
| index += s.length; |
| } |
| @@ -164,8 +158,7 @@ class _HttpUtils { |
| if (pos == -1) throw new HttpException("Invalid HTTP date $date"); |
| tmp = date.substring(index, pos); |
| index = pos + 1; |
| - weekday = strict ? wkdays.indexOf(tmp) |
| - : wkdaysLowerCase.indexOf(tmp.toLowerCase()); |
| + weekday = wkdays.indexOf(tmp); |
| if (weekday != -1) { |
| format = formatAsctime; |
| return weekday; |
| @@ -173,14 +166,12 @@ class _HttpUtils { |
| } else { |
| tmp = date.substring(index, pos); |
| index = pos + 1; |
| - weekday = strict ? wkdays.indexOf(tmp) |
| - : wkdaysLowerCase.indexOf(tmp.toLowerCase()); |
| + weekday = wkdays.indexOf(tmp); |
| if (weekday != -1) { |
| format = formatRfc1123; |
| return weekday; |
| } |
| - weekday = strict ? weekdays.indexOf(tmp) |
| - : weekdaysLowerCase.indexOf(tmp.toLowerCase()); |
| + weekday = weekdays.indexOf(tmp); |
| if (weekday != -1) { |
| format = formatRfc850; |
| return weekday; |
| @@ -194,8 +185,7 @@ class _HttpUtils { |
| if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); |
| tmp = date.substring(index, pos); |
| index = pos + 1; |
| - int month = strict ? months.indexOf(tmp) |
| - : monthsLowerCase.indexOf(tmp.toLowerCase()); |
| + int month = months.indexOf(tmp); |
| if (month != -1) return month; |
| throw new HttpException("Invalid HTTP date $date"); |
| } |
| @@ -252,4 +242,111 @@ class _HttpUtils { |
| return new Date( |
| year, month + 1, day, hours, minutes, seconds, 0, isUtc: true); |
| } |
| + |
| + static Date parseCookieDate(String date) { |
| + List monthsLowerCase = ["jan", "feb", "mar", "apr", "may", "jun", |
|
Mads Ager (google)
2012/09/04 08:41:14
Similarly, this could be explicitly marked as a co
Anders Johnsen
2012/09/05 08:44:29
Done.
|
| + "jul", "aug", "sep", "oct", "nov", "dec"]; |
| + |
| + int position = 0; |
| + |
| + void error() { |
| + throw new HttpException("Invalid cookie date $date"); |
| + } |
| + |
| + bool isEnd() { |
| + return position == date.length; |
| + } |
| + |
| + bool isDelimiter(String s) { |
| + int char = s.charCodeAt(0); |
| + if (char === 0x09) return true; |
| + if (char >= 0x20 && char <= 0x2F) return true; |
| + if (char >= 0x3B && char <= 0x40) return true; |
| + if (char >= 0x5B && char <= 0x60) return true; |
| + if (char >= 0x7B && char <= 0x7E) return true; |
| + return false; |
| + } |
| + |
| + bool isNonDelimiter(String s) { |
| + int char = s.charCodeAt(0); |
| + if (char >= 0x00 && char <= 0x08) return true; |
| + if (char >= 0x0A && char <= 0x1F) return true; |
| + if (char >= 0x30 && char <= 0x39) return true; // Digit |
| + if (char == 0x3A) return true; // ':' |
| + if (char >= 0x41 && char <= 0x5A) return true; // Alpha |
| + if (char >= 0x61 && char <= 0x7A) return true; // Alpha |
| + if (char >= 0x7F && char <= 0xFF) return true; // Alpha |
| + return false; |
| + } |
| + |
| + bool isDigit(String s) { |
| + int char = s.charCodeAt(0); |
| + if (char > 0x2F && char < 0x3A) return true; |
| + return false; |
| + } |
| + |
| + int getMonth(String month) { |
| + if (month.length < 3) return -1; |
| + return monthsLowerCase.indexOf(month.substring(0, 3)); |
| + } |
| + |
| + int toInt(String s) { |
| + int index = 0; |
| + for (; index < s.length && isDigit(s[index]); index++); |
| + return parseInt(s.substring(0, index)); |
| + } |
| + |
| + var tokens = []; |
| + while (!isEnd()) { |
| + while (!isEnd() && isDelimiter(date[position])) position++; |
| + int start = position; |
| + while (!isEnd() && isNonDelimiter(date[position])) position++; |
| + tokens.add(date.substring(start, position).toLowerCase()); |
| + while (!isEnd() && isDelimiter(date[position])) position++; |
| + } |
| + |
| + String timeStr; |
| + String dayOfMonthStr; |
| + String monthStr; |
| + String yearStr; |
| + |
| + for (var token in tokens) { |
| + if (token.length < 1) continue; |
| + if (timeStr === null && token.length >= 5 && isDigit(token[0]) && |
| + (token[1] == ":" || (isDigit(token[1]) && token[2] == ":"))) { |
| + timeStr = token; |
| + } else if (dayOfMonthStr === null && isDigit(token[0])) { |
| + dayOfMonthStr = token; |
| + } else if (monthStr === null && getMonth(token) >= 0) { |
| + monthStr = token; |
| + } else if (yearStr === null && token.length >= 2 && |
| + isDigit(token[0]) && isDigit(token[1])) { |
| + yearStr = token; |
| + } |
| + } |
| + |
| + if (timeStr === null || dayOfMonthStr === null || |
| + monthStr === null || yearStr === null) error(); |
|
Mads Ager (google)
2012/09/04 08:41:14
Since the condition alone covers more than one lin
Anders Johnsen
2012/09/05 08:44:29
Done.
|
| + |
| + int year = toInt(yearStr); |
| + if (year >= 70 && year <= 99) year += 1900; |
| + else if (year >= 0 && year <= 69) year += 2000; |
| + if (year < 1601) error(); |
| + |
| + int dayOfMonth = toInt(dayOfMonthStr); |
| + if (dayOfMonth < 1 || dayOfMonth > 31) error(); |
| + |
| + int month = getMonth(monthStr) + 1; |
| + |
| + var timeList = timeStr.split(":"); |
| + if (timeList.length !== 3) error(); |
| + int hour = toInt(timeList[0]); |
| + int minute = toInt(timeList[1]); |
| + int second = toInt(timeList[2]); |
| + if (hour > 23) error(); |
| + if (minute > 59) error(); |
| + if (second > 59) error(); |
| + |
| + return new Date(year, month, dayOfMonth, hour, minute, second, 0, true); |
| + } |
| } |