Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Unified Diff: runtime/bin/http_utils.dart

Issue 9602011: Add handling of HTTP header "Expires" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixed accidental edit Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/bin/http_utils.dart
diff --git a/runtime/bin/http_utils.dart b/runtime/bin/http_utils.dart
index 1e5d84c0d12e176d24731f9da713456c0f829563..6c9703a9b4e50f3f7fe89b71e8d600f1e09b3cda 100644
--- a/runtime/bin/http_utils.dart
+++ b/runtime/bin/http_utils.dart
@@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
class _HttpUtils {
+
Anders Johnsen 2012/03/06 10:41:01 Whitespace
Søren Gjesse 2012/03/06 12:01:16 Done.
static String decodeUrlEncodedString(String urlEncoded) {
void invalidEscape() {
// TODO(sgjesse): Handle the error.
@@ -59,4 +60,175 @@ class _HttpUtils {
}
return result;
}
+
+ // From RFC 2616 section "3.3.1 Full Date"
+ // HTTP-date = rfc1123-date | rfc850-date | asctime-date
+ // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
+ // rfc850-date = weekday "," SP date2 SP time SP "GMT"
+ // asctime-date = wkday SP date3 SP time SP 4DIGIT
+ // date1 = 2DIGIT SP month SP 4DIGIT
+ // ; day month year (e.g., 02 Jun 1982)
+ // date2 = 2DIGIT "-" month "-" 2DIGIT
+ // ; day-month-year (e.g., 02-Jun-82)
+ // date3 = month SP ( 2DIGIT | ( SP 1DIGIT ))
+ // ; month day (e.g., Jun 2)
+ // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ // ; 00:00:00 - 23:59:59
+ // wkday = "Mon" | "Tue" | "Wed"
+ // | "Thu" | "Fri" | "Sat" | "Sun"
+ // weekday = "Monday" | "Tuesday" | "Wednesday"
+ // | "Thursday" | "Friday" | "Saturday" | "Sunday"
+ // month = "Jan" | "Feb" | "Mar" | "Apr"
+ // | "May" | "Jun" | "Jul" | "Aug"
+ // | "Sep" | "Oct" | "Nov" | "Dec"
+
+ // Format as RFC 1123 date.
+ static String formatDate(Date date) {
+ List wkday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
+ List month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ Date d = date.changeTimeZone(new TimeZone.utc());
+ StringBuffer sb = new StringBuffer();
+ sb.add(wkday[d.weekday]);
+ sb.add(", ");
+ sb.add(d.day.toString());
+ sb.add(" ");
+ sb.add(month[d.month - 1]);
+ sb.add(" ");
+ sb.add(d.year.toString());
+ d.hours < 9 ? sb.add(" 0") : sb.add(" ");
+ sb.add(d.hours.toString());
+ d.minutes < 9 ? sb.add(":0") : sb.add(":");
+ sb.add(d.minutes.toString());
+ d.seconds < 9 ? sb.add(":0") : sb.add(":");
+ sb.add(d.seconds.toString());
+ sb.add(" GMT");
+ return sb.toString();
+ }
+
+ static Date parseDate(String date) {
+ final int SP = 32;
+ List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
+ List weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday",
+ "Friday", "Saturday", "Sunday"];
+ List months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ final int formatRfc1123 = 0;
+ final int formatRfc850 = 1;
+ final int formatAsctime = 2;
+
+ int index = 0;
+ String tmp;
+ int format;
+
+ void expect(String s) {
+ if (date.length - index < s.length) {
+ throw new HttpException("Invalid HTTP date $date");
+ }
+ String tmp = date.substring(index, index + s.length);
+ if (tmp != s) {
+ throw new HttpException("Invalid HTTP date $date");
+ }
+ index += s.length;
+ }
+
+ int expectWeekday() {
+ // The formatting of the weekday signals the format of the date string.
+ int pos = date.indexOf(",", index);
+ if (pos == -1) {
+ int pos = date.indexOf(" ", index);
+ if (pos == -1) throw new HttpException("Invalid HTTP date $date");
+ tmp = date.substring(index, pos);
+ index = pos + 1;
+ for (int i = 0; i < wkdays.length; i++) {
Anders Johnsen 2012/03/06 10:41:01 wkdays.indexOf(tmp)?
Søren Gjesse 2012/03/06 12:01:16 I forgot about that API, thanks.
+ if (wkdays[i] == tmp) {
+ format = formatAsctime;
+ return i;
+ }
+ }
Anders Johnsen 2012/03/06 10:41:01 Is it an error if we end here? Maybe move the exce
Søren Gjesse 2012/03/06 12:01:16 Done.
+ } else {
+ tmp = date.substring(index, pos);
+ index = pos + 1;
+ for (int i = 0; i < wkdays.length; i++) {
Anders Johnsen 2012/03/06 10:41:01 wkdays.indexOf(tmp)?
Søren Gjesse 2012/03/06 12:01:16 Done.
+ if (wkdays[i] == tmp) {
+ format = formatRfc1123;
+ return i;
+ }
+ }
+ for (int i = 0; i < weekdays.length; i++) {
Anders Johnsen 2012/03/06 10:41:01 weekdays.indexOf(tmp)?
Søren Gjesse 2012/03/06 12:01:16 Done.
+ if (weekdays[i] == tmp) {
+ format = formatRfc850;
+ return i;
+ }
+ }
+ throw new HttpException("Invalid HTTP date $date");
+ }
+ }
+
+ int expectMonth(String separator) {
+ int pos = date.indexOf(separator, index);
+ if (pos - index != 3) throw new HttpException("Invalid HTTP date $date");
+ tmp = date.substring(index, pos);
+ index = pos + 1;
+ for (int i = 0; i < months.length; i++) {
Anders Johnsen 2012/03/06 10:41:01 months.indexOf(tmp)?
Søren Gjesse 2012/03/06 12:01:16 Done.
+ if (months[i] == tmp) return i;
+ }
+ throw new HttpException("Invalid HTTP date $date");
+ }
+
+ int expectNum(String separator) {
+ int pos;
+ if (separator.length > 0) {
+ pos = date.indexOf(separator, index);
+ } else {
+ pos = date.length;
+ }
+ String tmp = date.substring(index, pos);
+ index = pos + separator.length;
+ try {
+ int value = Math.parseInt(tmp);
+ return value;
+ } catch (BadNumberFormatException e) {
+ throw new HttpException("Invalid HTTP date $date");
+ }
+ }
+
+ void expectEnd() {
+ if (index != date.length) {
+ throw new HttpException("Invalid HTTP date $date");
+ }
+ }
+
+ int weekday = expectWeekday();
+ int day;
+ int month;
+ int year;
+ int hours;
+ int minutes;
+ int seconds;
+ if (format == formatAsctime) {
+ month = expectMonth(" ");
+ if (date.charCodeAt(index) == SP) index++;
+ day = expectNum(" ");
+ hours = expectNum(":");
+ minutes = expectNum(":");
+ seconds = expectNum(" ");
+ year = expectNum("");
+ } else {
+ expect(" ");
+ day = expectNum(format == formatRfc1123 ? " " : "-");
+ month = expectMonth(format == formatRfc1123 ? " " : "-");
+ year = expectNum(" ");
+ hours = expectNum(":");
+ minutes = expectNum(":");
+ seconds = expectNum(" ");
+ expect("GMT");
+ }
+ expectEnd();
+ TimeZone utc = new TimeZone.utc();
+ return new Date.withTimeZone(
+ year, month + 1, day, hours, minutes, seconds, 0, utc);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698