OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _HttpUtils { | 7 class _HttpUtils { |
8 static String decodeUrlEncodedString(String urlEncoded) { | 8 static String decodeUrlEncodedString(String urlEncoded) { |
9 // First check the string for any encoding. | 9 // First check the string for any encoding. |
10 int index = 0; | 10 int index = 0; |
11 bool encoded = false; | 11 bool encoded = false; |
12 while (!encoded && index < urlEncoded.length) { | 12 while (!encoded && index < urlEncoded.length) { |
13 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%"; | 13 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%"; |
14 index++; | 14 index++; |
15 } | 15 } |
16 if (!encoded) return urlEncoded; | 16 if (!encoded) return urlEncoded; |
17 index--; | 17 index--; |
18 | 18 |
19 // Start decoding from the first encoded character. | 19 // Start decoding from the first encoded character. |
20 List<int> bytes = new List<int>(); | 20 List<int> bytes = new List<int>(); |
21 for (int i = 0; i < index; i++) bytes.add(urlEncoded.charCodeAt(i)); | 21 for (int i = 0; i < index; i++) bytes.add(urlEncoded.codeUnitAt(i)); |
22 for (int i = index; i < urlEncoded.length; i++) { | 22 for (int i = index; i < urlEncoded.length; i++) { |
23 if (urlEncoded[i] == "+") { | 23 if (urlEncoded[i] == "+") { |
24 bytes.add(32); | 24 bytes.add(32); |
25 } else if (urlEncoded[i] == "%") { | 25 } else if (urlEncoded[i] == "%") { |
26 if (urlEncoded.length - i < 2) { | 26 if (urlEncoded.length - i < 2) { |
27 throw new HttpException("Invalid URL encoding"); | 27 throw new HttpException("Invalid URL encoding"); |
28 } | 28 } |
29 int byte = 0; | 29 int byte = 0; |
30 for (int j = 0; j < 2; j++) { | 30 for (int j = 0; j < 2; j++) { |
31 var charCode = urlEncoded.charCodeAt(i + j + 1); | 31 var charCode = urlEncoded.codeUnitAt(i + j + 1); |
32 if (0x30 <= charCode && charCode <= 0x39) { | 32 if (0x30 <= charCode && charCode <= 0x39) { |
33 byte = byte * 16 + charCode - 0x30; | 33 byte = byte * 16 + charCode - 0x30; |
34 } else { | 34 } else { |
35 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66). | 35 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66). |
36 charCode |= 0x20; | 36 charCode |= 0x20; |
37 if (0x61 <= charCode && charCode <= 0x66) { | 37 if (0x61 <= charCode && charCode <= 0x66) { |
38 byte = byte * 16 + charCode - 0x57; | 38 byte = byte * 16 + charCode - 0x57; |
39 } else { | 39 } else { |
40 throw new ArgumentError("Invalid URL encoding"); | 40 throw new ArgumentError("Invalid URL encoding"); |
41 } | 41 } |
42 } | 42 } |
43 } | 43 } |
44 bytes.add(byte); | 44 bytes.add(byte); |
45 i += 2; | 45 i += 2; |
46 } else { | 46 } else { |
47 bytes.add(urlEncoded.charCodeAt(i)); | 47 bytes.add(urlEncoded.codeUnitAt(i)); |
48 } | 48 } |
49 } | 49 } |
50 return decodeUtf8(bytes); | 50 return decodeUtf8(bytes); |
51 } | 51 } |
52 | 52 |
53 static Map<String, String> splitQueryString(String queryString) { | 53 static Map<String, String> splitQueryString(String queryString) { |
54 Map<String, String> result = new Map<String, String>(); | 54 Map<String, String> result = new Map<String, String>(); |
55 int currentPosition = 0; | 55 int currentPosition = 0; |
56 while (currentPosition < queryString.length) { | 56 while (currentPosition < queryString.length) { |
57 int position = queryString.indexOf("=", currentPosition); | 57 int position = queryString.indexOf("=", currentPosition); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 222 |
223 int weekday = expectWeekday(); | 223 int weekday = expectWeekday(); |
224 int day; | 224 int day; |
225 int month; | 225 int month; |
226 int year; | 226 int year; |
227 int hours; | 227 int hours; |
228 int minutes; | 228 int minutes; |
229 int seconds; | 229 int seconds; |
230 if (format == formatAsctime) { | 230 if (format == formatAsctime) { |
231 month = expectMonth(" "); | 231 month = expectMonth(" "); |
232 if (date.charCodeAt(index) == SP) index++; | 232 if (date.codeUnitAt(index) == SP) index++; |
233 day = expectNum(" "); | 233 day = expectNum(" "); |
234 hours = expectNum(":"); | 234 hours = expectNum(":"); |
235 minutes = expectNum(":"); | 235 minutes = expectNum(":"); |
236 seconds = expectNum(" "); | 236 seconds = expectNum(" "); |
237 year = expectNum(""); | 237 year = expectNum(""); |
238 } else { | 238 } else { |
239 expect(" "); | 239 expect(" "); |
240 day = expectNum(format == formatRfc1123 ? " " : "-"); | 240 day = expectNum(format == formatRfc1123 ? " " : "-"); |
241 month = expectMonth(format == formatRfc1123 ? " " : "-"); | 241 month = expectMonth(format == formatRfc1123 ? " " : "-"); |
242 year = expectNum(" "); | 242 year = expectNum(" "); |
(...skipping 15 matching lines...) Expand all Loading... |
258 | 258 |
259 void error() { | 259 void error() { |
260 throw new HttpException("Invalid cookie date $date"); | 260 throw new HttpException("Invalid cookie date $date"); |
261 } | 261 } |
262 | 262 |
263 bool isEnd() { | 263 bool isEnd() { |
264 return position == date.length; | 264 return position == date.length; |
265 } | 265 } |
266 | 266 |
267 bool isDelimiter(String s) { | 267 bool isDelimiter(String s) { |
268 int char = s.charCodeAt(0); | 268 int char = s.codeUnitAt(0); |
269 if (char == 0x09) return true; | 269 if (char == 0x09) return true; |
270 if (char >= 0x20 && char <= 0x2F) return true; | 270 if (char >= 0x20 && char <= 0x2F) return true; |
271 if (char >= 0x3B && char <= 0x40) return true; | 271 if (char >= 0x3B && char <= 0x40) return true; |
272 if (char >= 0x5B && char <= 0x60) return true; | 272 if (char >= 0x5B && char <= 0x60) return true; |
273 if (char >= 0x7B && char <= 0x7E) return true; | 273 if (char >= 0x7B && char <= 0x7E) return true; |
274 return false; | 274 return false; |
275 } | 275 } |
276 | 276 |
277 bool isNonDelimiter(String s) { | 277 bool isNonDelimiter(String s) { |
278 int char = s.charCodeAt(0); | 278 int char = s.codeUnitAt(0); |
279 if (char >= 0x00 && char <= 0x08) return true; | 279 if (char >= 0x00 && char <= 0x08) return true; |
280 if (char >= 0x0A && char <= 0x1F) return true; | 280 if (char >= 0x0A && char <= 0x1F) return true; |
281 if (char >= 0x30 && char <= 0x39) return true; // Digit | 281 if (char >= 0x30 && char <= 0x39) return true; // Digit |
282 if (char == 0x3A) return true; // ':' | 282 if (char == 0x3A) return true; // ':' |
283 if (char >= 0x41 && char <= 0x5A) return true; // Alpha | 283 if (char >= 0x41 && char <= 0x5A) return true; // Alpha |
284 if (char >= 0x61 && char <= 0x7A) return true; // Alpha | 284 if (char >= 0x61 && char <= 0x7A) return true; // Alpha |
285 if (char >= 0x7F && char <= 0xFF) return true; // Alpha | 285 if (char >= 0x7F && char <= 0xFF) return true; // Alpha |
286 return false; | 286 return false; |
287 } | 287 } |
288 | 288 |
289 bool isDigit(String s) { | 289 bool isDigit(String s) { |
290 int char = s.charCodeAt(0); | 290 int char = s.codeUnitAt(0); |
291 if (char > 0x2F && char < 0x3A) return true; | 291 if (char > 0x2F && char < 0x3A) return true; |
292 return false; | 292 return false; |
293 } | 293 } |
294 | 294 |
295 int getMonth(String month) { | 295 int getMonth(String month) { |
296 if (month.length < 3) return -1; | 296 if (month.length < 3) return -1; |
297 return monthsLowerCase.indexOf(month.substring(0, 3)); | 297 return monthsLowerCase.indexOf(month.substring(0, 3)); |
298 } | 298 } |
299 | 299 |
300 int toInt(String s) { | 300 int toInt(String s) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 int hour = toInt(timeList[0]); | 352 int hour = toInt(timeList[0]); |
353 int minute = toInt(timeList[1]); | 353 int minute = toInt(timeList[1]); |
354 int second = toInt(timeList[2]); | 354 int second = toInt(timeList[2]); |
355 if (hour > 23) error(); | 355 if (hour > 23) error(); |
356 if (minute > 59) error(); | 356 if (minute > 59) error(); |
357 if (second > 59) error(); | 357 if (second > 59) error(); |
358 | 358 |
359 return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0); | 359 return new DateTime.utc(year, month, dayOfMonth, hour, minute, second, 0); |
360 } | 360 } |
361 } | 361 } |
OLD | NEW |