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

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/io/http_date.dart

Issue 2698353003: unfork DDC's copy of most SDK libraries (Closed)
Patch Set: revert core_patch Created 3 years, 9 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 unified diff | Download patch
OLDNEW
(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 ..write(wkday[d.weekday - 1])
48 ..write(", ")
49 ..write(d.day <= 9 ? "0" : "")
50 ..write(d.day.toString())
51 ..write(" ")
52 ..write(month[d.month - 1])
53 ..write(" ")
54 ..write(d.year.toString())
55 ..write(d.hour <= 9 ? " 0" : " ")
56 ..write(d.hour.toString())
57 ..write(d.minute <= 9 ? ":0" : ":")
58 ..write(d.minute.toString())
59 ..write(d.second <= 9 ? ":0" : ":")
60 ..write(d.second.toString())
61 ..write(" GMT");
62 return sb.toString();
63 }
64
65 /**
66 * Parse a date string in either of the formats
67 * [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"),
68 * [RFC-850](http://tools.ietf.org/html/rfc850 "RFC-850") or
69 * ANSI C's asctime() format. These formats are listed here.
70 *
71 * Thu, 1 Jan 1970 00:00:00 GMT
72 * Thursday, 1-Jan-1970 00:00:00 GMT
73 * Thu Jan 1 00:00:00 1970
74 *
75 * For more information see [RFC-2616 section
76 * 3.1.1](http://tools.ietf.org/html/rfc2616#section-3.3.1
77 * "RFC-2616 section 3.1.1").
78 */
79 static DateTime parse(String date) {
80 final int SP = 32;
81 const List wkdays = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
82 const List weekdays = const ["Monday", "Tuesday", "Wednesday", "Thursday",
83 "Friday", "Saturday", "Sunday"];
84 const List months = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
85 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
86 const List wkdaysLowerCase =
87 const ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
88 const List weekdaysLowerCase = const ["monday", "tuesday", "wednesday",
89 "thursday", "friday", "saturday",
90 "sunday"];
91 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may",
92 "jun", "jul", "aug", "sep", "oct",
93 "nov", "dec"];
94
95 final int formatRfc1123 = 0;
96 final int formatRfc850 = 1;
97 final int formatAsctime = 2;
98
99 int index = 0;
100 String tmp;
101 int format;
102
103 void expect(String s) {
104 if (date.length - index < s.length) {
105 throw new HttpException("Invalid HTTP date $date");
106 }
107 String tmp = date.substring(index, index + s.length);
108 if (tmp != s) {
109 throw new HttpException("Invalid HTTP date $date");
110 }
111 index += s.length;
112 }
113
114 int expectWeekday() {
115 int weekday;
116 // The formatting of the weekday signals the format of the date string.
117 int pos = date.indexOf(",", index);
118 if (pos == -1) {
119 int pos = date.indexOf(" ", index);
120 if (pos == -1) throw new HttpException("Invalid HTTP date $date");
121 tmp = date.substring(index, pos);
122 index = pos + 1;
123 weekday = wkdays.indexOf(tmp);
124 if (weekday != -1) {
125 format = formatAsctime;
126 return weekday;
127 }
128 } else {
129 tmp = date.substring(index, pos);
130 index = pos + 1;
131 weekday = wkdays.indexOf(tmp);
132 if (weekday != -1) {
133 format = formatRfc1123;
134 return weekday;
135 }
136 weekday = weekdays.indexOf(tmp);
137 if (weekday != -1) {
138 format = formatRfc850;
139 return weekday;
140 }
141 }
142 throw new HttpException("Invalid HTTP date $date");
143 }
144
145 int expectMonth(String separator) {
146 int pos = date.indexOf(separator, index);
147 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date");
148 tmp = date.substring(index, pos);
149 index = pos + 1;
150 int month = months.indexOf(tmp);
151 if (month != -1) return month;
152 throw new HttpException("Invalid HTTP date $date");
153 }
154
155 int expectNum(String separator) {
156 int pos;
157 if (separator.length > 0) {
158 pos = date.indexOf(separator, index);
159 } else {
160 pos = date.length;
161 }
162 String tmp = date.substring(index, pos);
163 index = pos + separator.length;
164 try {
165 int value = int.parse(tmp);
166 return value;
167 } on FormatException catch (e) {
168 throw new HttpException("Invalid HTTP date $date");
169 }
170 }
171
172 void expectEnd() {
173 if (index != date.length) {
174 throw new HttpException("Invalid HTTP date $date");
175 }
176 }
177
178 int weekday = expectWeekday();
179 int day;
180 int month;
181 int year;
182 int hours;
183 int minutes;
184 int seconds;
185 if (format == formatAsctime) {
186 month = expectMonth(" ");
187 if (date.codeUnitAt(index) == SP) index++;
188 day = expectNum(" ");
189 hours = expectNum(":");
190 minutes = expectNum(":");
191 seconds = expectNum(" ");
192 year = expectNum("");
193 } else {
194 expect(" ");
195 day = expectNum(format == formatRfc1123 ? " " : "-");
196 month = expectMonth(format == formatRfc1123 ? " " : "-");
197 year = expectNum(" ");
198 hours = expectNum(":");
199 minutes = expectNum(":");
200 seconds = expectNum(" ");
201 expect("GMT");
202 }
203 expectEnd();
204 return new DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0);
205 }
206
207 // Parse a cookie date string.
208 static DateTime _parseCookieDate(String date) {
209 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may",
210 "jun", "jul", "aug", "sep", "oct",
211 "nov", "dec"];
212
213 int position = 0;
214
215 void error() {
216 throw new HttpException("Invalid cookie date $date");
217 }
218
219 bool isEnd() => position == date.length;
220
221 bool isDelimiter(String s) {
222 int char = s.codeUnitAt(0);
223 if (char == 0x09) return true;
224 if (char >= 0x20 && char <= 0x2F) return true;
225 if (char >= 0x3B && char <= 0x40) return true;
226 if (char >= 0x5B && char <= 0x60) return true;
227 if (char >= 0x7B && char <= 0x7E) return true;
228 return false;
229 }
230
231 bool isNonDelimiter(String s) {
232 int char = s.codeUnitAt(0);
233 if (char >= 0x00 && char <= 0x08) return true;
234 if (char >= 0x0A && char <= 0x1F) return true;
235 if (char >= 0x30 && char <= 0x39) return true; // Digit
236 if (char == 0x3A) return true; // ':'
237 if (char >= 0x41 && char <= 0x5A) return true; // Alpha
238 if (char >= 0x61 && char <= 0x7A) return true; // Alpha
239 if (char >= 0x7F && char <= 0xFF) return true; // Alpha
240 return false;
241 }
242
243 bool isDigit(String s) {
244 int char = s.codeUnitAt(0);
245 if (char > 0x2F && char < 0x3A) return true;
246 return false;
247 }
248
249 int getMonth(String month) {
250 if (month.length < 3) return -1;
251 return monthsLowerCase.indexOf(month.substring(0, 3));
252 }
253
254 int toInt(String s) {
255 int index = 0;
256 for (; index < s.length && isDigit(s[index]); index++);
257 return int.parse(s.substring(0, index));
258 }
259
260 var tokens = [];
261 while (!isEnd()) {
262 while (!isEnd() && isDelimiter(date[position])) position++;
263 int start = position;
264 while (!isEnd() && isNonDelimiter(date[position])) position++;
265 tokens.add(date.substring(start, position).toLowerCase());
266 while (!isEnd() && isDelimiter(date[position])) position++;
267 }
268
269 String timeStr;
270 String dayOfMonthStr;
271 String monthStr;
272 String yearStr;
273
274 for (var token in tokens) {
275 if (token.length < 1) continue;
276 if (timeStr == null && token.length >= 5 && isDigit(token[0]) &&
277 (token[1] == ":" || (isDigit(token[1]) && token[2] == ":"))) {
278 timeStr = token;
279 } else if (dayOfMonthStr == null && isDigit(token[0])) {
280 dayOfMonthStr = token;
281 } else if (monthStr == null && getMonth(token) >= 0) {
282 monthStr = token;
283 } else if (yearStr == null && token.length >= 2 &&
284 isDigit(token[0]) && isDigit(token[1])) {
285 yearStr = token;
286 }
287 }
288
289 if (timeStr == null || dayOfMonthStr == null ||
290 monthStr == null || yearStr == null) {
291 error();
292 }
293
294 int year = toInt(yearStr);
295 if (year >= 70 && year <= 99) year += 1900;
296 else if (year >= 0 && year <= 69) year += 2000;
297 if (year < 1601) error();
298
299 int dayOfMonth = toInt(dayOfMonthStr);
300 if (dayOfMonth < 1 || dayOfMonth > 31) error();
301
302 int month = getMonth(monthStr) + 1;
303
304 var timeList = timeStr.split(":");
305 if (timeList.length != 3) error();
306 int hour = toInt(timeList[0]);
307 int minute = toInt(timeList[1]);
308 int second = toInt(timeList[2]);
309 if (hour > 23) error();
310 if (minute > 59) error();
311 if (second > 59) error();
312
313 return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0);
314 }
315 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/tool/input_sdk/lib/io/http.dart ('k') | pkg/dev_compiler/tool/input_sdk/lib/io/http_headers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698