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 class _HttpUtils { | |
6 static String decodeUrlEncodedString(String urlEncoded) { | |
7 // First check the string for any encoding. | |
8 int index = 0; | |
9 bool encoded = false; | |
10 while (!encoded && index < urlEncoded.length) { | |
11 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%"; | |
12 index++; | |
13 } | |
14 if (!encoded) return urlEncoded; | |
15 index--; | |
16 | |
17 // Start decoding from the first encoded character. | |
18 List<int> bytes = new List<int>(); | |
19 for (int i = 0; i < index; i++) bytes.add(urlEncoded.charCodeAt(i)); | |
20 for (int i = index; i < urlEncoded.length; i++) { | |
21 if (urlEncoded[i] == "+") { | |
22 bytes.add(32); | |
23 } else if (urlEncoded[i] == "%") { | |
24 if (urlEncoded.length - i < 2) { | |
25 throw new HttpException("Invalid URL encoding"); | |
26 } | |
27 int byte = 0; | |
28 for (int j = 0; j < 2; j++) { | |
29 var charCode = urlEncoded.charCodeAt(i + j + 1); | |
30 if (0x30 <= charCode && charCode <= 0x39) { | |
31 byte = byte * 16 + charCode - 0x30; | |
32 } else if (0x41 <= charCode && charCode <= 0x46) { | |
33 byte = byte * 16 + charCode - 0x37; | |
34 } else if (0x61 <= charCode && charCode <= 0x66) { | |
35 byte = byte * 16 + charCode - 0x57; | |
36 } else { | |
37 throw new HttpException("Invalid URL encoding"); | |
38 } | |
39 } | |
40 bytes.add(byte); | |
41 i += 2; | |
42 } else { | |
43 bytes.add(urlEncoded.charCodeAt(i)); | |
44 } | |
45 } | |
46 return decodeUtf8(bytes); | |
47 } | |
48 | |
49 static Map<String, String> splitQueryString(String queryString) { | |
50 Map<String, String> result = new Map<String, String>(); | |
51 int currentPosition = 0; | |
52 while (currentPosition < queryString.length) { | |
53 int position = queryString.indexOf("=", currentPosition); | |
54 if (position == -1) { | |
55 break; | |
56 } | |
57 String name = queryString.substring(currentPosition, position); | |
58 currentPosition = position + 1; | |
59 position = queryString.indexOf("&", currentPosition); | |
60 String value; | |
61 if (position == -1) { | |
62 value = queryString.substring(currentPosition); | |
63 currentPosition = queryString.length; | |
64 } else { | |
65 value = queryString.substring(currentPosition, position); | |
66 currentPosition = position + 1; | |
67 } | |
68 result[_HttpUtils.decodeUrlEncodedString(name)] = | |
69 _HttpUtils.decodeUrlEncodedString(value); | |
70 } | |
71 return result; | |
72 } | |
73 | |
74 // From RFC 2616 section "3.3.1 Full Date" | |
75 // HTTP-date = rfc1123-date | rfc850-date | asctime-date | |
76 // rfc1123-date = wkday "," SP date1 SP time SP "GMT" | |
77 // rfc850-date = weekday "," SP date2 SP time SP "GMT" | |
78 // asctime-date = wkday SP date3 SP time SP 4DIGIT | |
79 // date1 = 2DIGIT SP month SP 4DIGIT | |
80 // ; day month year (e.g., 02 Jun 1982) | |
81 // date2 = 2DIGIT "-" month "-" 2DIGIT | |
82 // ; day-month-year (e.g., 02-Jun-82) | |
83 // date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) | |
84 // ; month day (e.g., Jun 2) | |
85 // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT | |
86 // ; 00:00:00 - 23:59:59 | |
87 // wkday = "Mon" | "Tue" | "Wed" | |
88 // | "Thu" | "Fri" | "Sat" | "Sun" | |
89 // weekday = "Monday" | "Tuesday" | "Wednesday" | |
90 // | "Thursday" | "Friday" | "Saturday" | "Sunday" | |
91 // month = "Jan" | "Feb" | "Mar" | "Apr" | |
92 // | "May" | "Jun" | "Jul" | "Aug" | |
93 // | "Sep" | "Oct" | "Nov" | "Dec" | |
94 | |
95 // Format as RFC 1123 date. | |
96 static String formatDate(Date date) { | |
97 const List wkday = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
98 const List month = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
99 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | |
100 | |
101 Date d = date.toUtc(); | |
102 StringBuffer sb = new StringBuffer(); | |
103 sb.add(wkday[d.weekday - 1]); | |
104 sb.add(", "); | |
105 sb.add(d.day.toString()); | |
106 sb.add(" "); | |
107 sb.add(month[d.month - 1]); | |
108 sb.add(" "); | |
109 sb.add(d.year.toString()); | |
110 d.hour < 9 ? sb.add(" 0") : sb.add(" "); | |
111 sb.add(d.hour.toString()); | |
112 d.minute < 9 ? sb.add(":0") : sb.add(":"); | |
113 sb.add(d.minute.toString()); | |
114 d.second < 9 ? sb.add(":0") : sb.add(":"); | |
115 sb.add(d.second.toString()); | |
116 sb.add(" GMT"); | |
117 return sb.toString(); | |
118 } | |
119 | |
120 static Date parseDate(String date) { | |
121 final int SP = 32; | |
122 const List wkdays = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
123 const List weekdays = const ["Monday", "Tuesday", "Wednesday", "Thursday", | |
124 "Friday", "Saturday", "Sunday"]; | |
125 const List months = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
126 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | |
127 const List wkdaysLowerCase = | |
128 const ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; | |
129 const List weekdaysLowerCase = const ["monday", "tuesday", "wednesday", | |
130 "thursday", "friday", "saturday", | |
131 "sunday"]; | |
132 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may", | |
133 "jun", "jul", "aug", "sep", "oct", | |
134 "nov", "dec"]; | |
135 | |
136 final int formatRfc1123 = 0; | |
137 final int formatRfc850 = 1; | |
138 final int formatAsctime = 2; | |
139 | |
140 int index = 0; | |
141 String tmp; | |
142 int format; | |
143 | |
144 void expect(String s) { | |
145 if (date.length - index < s.length) { | |
146 throw new HttpException("Invalid HTTP date $date"); | |
147 } | |
148 String tmp = date.substring(index, index + s.length); | |
149 if (tmp != s) { | |
150 throw new HttpException("Invalid HTTP date $date"); | |
151 } | |
152 index += s.length; | |
153 } | |
154 | |
155 int expectWeekday() { | |
156 int weekday; | |
157 // The formatting of the weekday signals the format of the date string. | |
158 int pos = date.indexOf(",", index); | |
159 if (pos == -1) { | |
160 int pos = date.indexOf(" ", index); | |
161 if (pos == -1) throw new HttpException("Invalid HTTP date $date"); | |
162 tmp = date.substring(index, pos); | |
163 index = pos + 1; | |
164 weekday = wkdays.indexOf(tmp); | |
165 if (weekday != -1) { | |
166 format = formatAsctime; | |
167 return weekday; | |
168 } | |
169 } else { | |
170 tmp = date.substring(index, pos); | |
171 index = pos + 1; | |
172 weekday = wkdays.indexOf(tmp); | |
173 if (weekday != -1) { | |
174 format = formatRfc1123; | |
175 return weekday; | |
176 } | |
177 weekday = weekdays.indexOf(tmp); | |
178 if (weekday != -1) { | |
179 format = formatRfc850; | |
180 return weekday; | |
181 } | |
182 } | |
183 throw new HttpException("Invalid HTTP date $date"); | |
184 } | |
185 | |
186 int expectMonth(String separator) { | |
187 int pos = date.indexOf(separator, index); | |
188 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); | |
189 tmp = date.substring(index, pos); | |
190 index = pos + 1; | |
191 int month = months.indexOf(tmp); | |
192 if (month != -1) return month; | |
193 throw new HttpException("Invalid HTTP date $date"); | |
194 } | |
195 | |
196 int expectNum(String separator) { | |
197 int pos; | |
198 if (separator.length > 0) { | |
199 pos = date.indexOf(separator, index); | |
200 } else { | |
201 pos = date.length; | |
202 } | |
203 String tmp = date.substring(index, pos); | |
204 index = pos + separator.length; | |
205 try { | |
206 int value = parseInt(tmp); | |
207 return value; | |
208 } on FormatException catch (e) { | |
209 throw new HttpException("Invalid HTTP date $date"); | |
210 } | |
211 } | |
212 | |
213 void expectEnd() { | |
214 if (index != date.length) { | |
215 throw new HttpException("Invalid HTTP date $date"); | |
216 } | |
217 } | |
218 | |
219 int weekday = expectWeekday(); | |
220 int day; | |
221 int month; | |
222 int year; | |
223 int hours; | |
224 int minutes; | |
225 int seconds; | |
226 if (format == formatAsctime) { | |
227 month = expectMonth(" "); | |
228 if (date.charCodeAt(index) == SP) index++; | |
229 day = expectNum(" "); | |
230 hours = expectNum(":"); | |
231 minutes = expectNum(":"); | |
232 seconds = expectNum(" "); | |
233 year = expectNum(""); | |
234 } else { | |
235 expect(" "); | |
236 day = expectNum(format == formatRfc1123 ? " " : "-"); | |
237 month = expectMonth(format == formatRfc1123 ? " " : "-"); | |
238 year = expectNum(" "); | |
239 hours = expectNum(":"); | |
240 minutes = expectNum(":"); | |
241 seconds = expectNum(" "); | |
242 expect("GMT"); | |
243 } | |
244 expectEnd(); | |
245 return new Date.utc(year, month + 1, day, hours, minutes, seconds, 0); | |
246 } | |
247 | |
248 static Date parseCookieDate(String date) { | |
249 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may", | |
250 "jun", "jul", "aug", "sep", "oct", | |
251 "nov", "dec"]; | |
252 | |
253 int position = 0; | |
254 | |
255 void error() { | |
256 throw new HttpException("Invalid cookie date $date"); | |
257 } | |
258 | |
259 bool isEnd() { | |
260 return position == date.length; | |
261 } | |
262 | |
263 bool isDelimiter(String s) { | |
264 int char = s.charCodeAt(0); | |
265 if (char === 0x09) return true; | |
266 if (char >= 0x20 && char <= 0x2F) return true; | |
267 if (char >= 0x3B && char <= 0x40) return true; | |
268 if (char >= 0x5B && char <= 0x60) return true; | |
269 if (char >= 0x7B && char <= 0x7E) return true; | |
270 return false; | |
271 } | |
272 | |
273 bool isNonDelimiter(String s) { | |
274 int char = s.charCodeAt(0); | |
275 if (char >= 0x00 && char <= 0x08) return true; | |
276 if (char >= 0x0A && char <= 0x1F) return true; | |
277 if (char >= 0x30 && char <= 0x39) return true; // Digit | |
278 if (char == 0x3A) return true; // ':' | |
279 if (char >= 0x41 && char <= 0x5A) return true; // Alpha | |
280 if (char >= 0x61 && char <= 0x7A) return true; // Alpha | |
281 if (char >= 0x7F && char <= 0xFF) return true; // Alpha | |
282 return false; | |
283 } | |
284 | |
285 bool isDigit(String s) { | |
286 int char = s.charCodeAt(0); | |
287 if (char > 0x2F && char < 0x3A) return true; | |
288 return false; | |
289 } | |
290 | |
291 int getMonth(String month) { | |
292 if (month.length < 3) return -1; | |
293 return monthsLowerCase.indexOf(month.substring(0, 3)); | |
294 } | |
295 | |
296 int toInt(String s) { | |
297 int index = 0; | |
298 for (; index < s.length && isDigit(s[index]); index++); | |
299 return parseInt(s.substring(0, index)); | |
300 } | |
301 | |
302 var tokens = []; | |
303 while (!isEnd()) { | |
304 while (!isEnd() && isDelimiter(date[position])) position++; | |
305 int start = position; | |
306 while (!isEnd() && isNonDelimiter(date[position])) position++; | |
307 tokens.add(date.substring(start, position).toLowerCase()); | |
308 while (!isEnd() && isDelimiter(date[position])) position++; | |
309 } | |
310 | |
311 String timeStr; | |
312 String dayOfMonthStr; | |
313 String monthStr; | |
314 String yearStr; | |
315 | |
316 for (var token in tokens) { | |
317 if (token.length < 1) continue; | |
318 if (timeStr === null && token.length >= 5 && isDigit(token[0]) && | |
319 (token[1] == ":" || (isDigit(token[1]) && token[2] == ":"))) { | |
320 timeStr = token; | |
321 } else if (dayOfMonthStr === null && isDigit(token[0])) { | |
322 dayOfMonthStr = token; | |
323 } else if (monthStr === null && getMonth(token) >= 0) { | |
324 monthStr = token; | |
325 } else if (yearStr === null && token.length >= 2 && | |
326 isDigit(token[0]) && isDigit(token[1])) { | |
327 yearStr = token; | |
328 } | |
329 } | |
330 | |
331 if (timeStr === null || dayOfMonthStr === null || | |
332 monthStr === null || yearStr === null) { | |
333 error(); | |
334 } | |
335 | |
336 int year = toInt(yearStr); | |
337 if (year >= 70 && year <= 99) year += 1900; | |
338 else if (year >= 0 && year <= 69) year += 2000; | |
339 if (year < 1601) error(); | |
340 | |
341 int dayOfMonth = toInt(dayOfMonthStr); | |
342 if (dayOfMonth < 1 || dayOfMonth > 31) error(); | |
343 | |
344 int month = getMonth(monthStr) + 1; | |
345 | |
346 var timeList = timeStr.split(":"); | |
347 if (timeList.length !== 3) error(); | |
348 int hour = toInt(timeList[0]); | |
349 int minute = toInt(timeList[1]); | |
350 int second = toInt(timeList[2]); | |
351 if (hour > 23) error(); | |
352 if (minute > 59) error(); | |
353 if (second > 59) error(); | |
354 | |
355 return new Date.utc(year, month, dayOfMonth, hour, minute, second, 0); | |
356 } | |
357 } | |
OLD | NEW |