Chromium Code Reviews| 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 class _HttpUtils { | 5 class _HttpUtils { |
| 6 static String decodeUrlEncodedString(String urlEncoded) { | 6 static String decodeUrlEncodedString(String urlEncoded) { |
| 7 // First check the string for any encoding. | 7 void invalidEscape() { |
| 8 int index = 0; | 8 // TODO(sgjesse): Handle the error. |
| 9 bool encoded = false; | |
| 10 while (!encoded && index < urlEncoded.length) { | |
| 11 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%"; | |
| 12 index++; | |
| 13 } | 9 } |
| 14 if (!encoded) return urlEncoded; | |
| 15 index--; | |
| 16 | 10 |
| 17 // Start decoding from the first encoded character. | 11 StringBuffer result = new StringBuffer(); |
| 18 List<int> bytes = new List<int>(); | 12 for (int ii = 0; urlEncoded.length > ii; ++ii) { |
|
Mads Ager (google)
2012/05/14 10:46:08
Why 'ii' instead of 'i'?
| |
| 19 for (int i = 0; i < index; i++) bytes.add(urlEncoded.charCodeAt(i)); | 13 if ('+' == urlEncoded[ii]) { |
| 20 for (int i = index; i < urlEncoded.length; i++) { | 14 result.add(' '); |
| 21 if (urlEncoded[i] == "+") { | 15 } else if ('%' == urlEncoded[ii] && |
| 22 bytes.add(32); | 16 urlEncoded.length - 2 > ii) { |
| 23 } else if (urlEncoded[i] == "%") { | 17 try { |
| 24 if (urlEncoded.length - i < 2) { | 18 int charCode = |
| 25 throw new HttpException("Invalid URL encoding"); | 19 Math.parseInt('0x' + urlEncoded.substring(ii + 1, ii + 3)); |
| 20 if (charCode <= 0x7f) { | |
| 21 result.add(new String.fromCharCodes([charCode])); | |
| 22 ii += 2; | |
| 23 } else { | |
| 24 invalidEscape(); | |
| 25 return ''; | |
| 26 } | |
| 27 } catch (BadNumberFormatException ignored) { | |
| 28 invalidEscape(); | |
| 29 return ''; | |
| 26 } | 30 } |
| 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 { | 31 } else { |
| 43 bytes.add(urlEncoded.charCodeAt(i)); | 32 result.add(urlEncoded[ii]); |
| 44 } | 33 } |
| 45 } | 34 } |
| 46 return decodeUtf8(bytes); | 35 return result.toString(); |
| 47 } | 36 } |
| 48 | 37 |
| 49 static Map<String, String> splitQueryString(String queryString) { | 38 static Map<String, String> splitQueryString(String queryString) { |
| 50 Map<String, String> result = new Map<String, String>(); | 39 Map<String, String> result = new Map<String, String>(); |
| 51 int currentPosition = 0; | 40 int currentPosition = 0; |
| 52 while (currentPosition < queryString.length) { | 41 while (currentPosition < queryString.length) { |
| 53 int position = queryString.indexOf("=", currentPosition); | 42 int position = queryString.indexOf("=", currentPosition); |
| 54 if (position == -1) { | 43 if (position == -1) { |
| 55 break; | 44 break; |
| 56 } | 45 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 minutes = expectNum(":"); | 221 minutes = expectNum(":"); |
| 233 seconds = expectNum(" "); | 222 seconds = expectNum(" "); |
| 234 expect("GMT"); | 223 expect("GMT"); |
| 235 } | 224 } |
| 236 expectEnd(); | 225 expectEnd(); |
| 237 TimeZone utc = new TimeZone.utc(); | 226 TimeZone utc = new TimeZone.utc(); |
| 238 return new Date.withTimeZone( | 227 return new Date.withTimeZone( |
| 239 year, month + 1, day, hours, minutes, seconds, 0, utc); | 228 year, month + 1, day, hours, minutes, seconds, 0, utc); |
| 240 } | 229 } |
| 241 } | 230 } |
| OLD | NEW |