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

Side by Side Diff: runtime/bin/http_utils.dart

Issue 10391135: Re-apply "Fix URL decoding in HTTP library" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « lib/compiler/implementation/lib/io.dart ('k') | runtime/bin/io.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 void invalidEscape() { 7 // First check the string for any encoding.
8 // TODO(sgjesse): Handle the error. 8 int index = 0;
9 bool encoded = false;
10 while (!encoded && index < urlEncoded.length) {
11 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%";
12 index++;
9 } 13 }
14 if (!encoded) return urlEncoded;
15 index--;
10 16
11 StringBuffer result = new StringBuffer(); 17 // Start decoding from the first encoded character.
12 for (int ii = 0; urlEncoded.length > ii; ++ii) { 18 List<int> bytes = new List<int>();
13 if ('+' == urlEncoded[ii]) { 19 for (int i = 0; i < index; i++) bytes.add(urlEncoded.charCodeAt(i));
14 result.add(' '); 20 for (int i = index; i < urlEncoded.length; i++) {
15 } else if ('%' == urlEncoded[ii] && 21 if (urlEncoded[i] == "+") {
16 urlEncoded.length - 2 > ii) { 22 bytes.add(32);
17 try { 23 } else if (urlEncoded[i] == "%") {
18 int charCode = 24 if (urlEncoded.length - i < 2) {
19 Math.parseInt('0x' + urlEncoded.substring(ii + 1, ii + 3)); 25 throw new HttpException("Invalid URL encoding");
20 if (charCode <= 0x7f) { 26 }
21 result.add(new String.fromCharCodes([charCode])); 27 int byte = 0;
22 ii += 2; 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;
23 } else { 36 } else {
24 invalidEscape(); 37 throw new HttpException("Invalid URL encoding");
25 return '';
26 } 38 }
27 } catch (BadNumberFormatException ignored) {
28 invalidEscape();
29 return '';
30 } 39 }
40 bytes.add(byte);
41 i += 2;
31 } else { 42 } else {
32 result.add(urlEncoded[ii]); 43 bytes.add(urlEncoded.charCodeAt(i));
33 } 44 }
34 } 45 }
35 return result.toString(); 46 return decodeUtf8(bytes);
36 } 47 }
37 48
38 static Map<String, String> splitQueryString(String queryString) { 49 static Map<String, String> splitQueryString(String queryString) {
39 Map<String, String> result = new Map<String, String>(); 50 Map<String, String> result = new Map<String, String>();
40 int currentPosition = 0; 51 int currentPosition = 0;
41 while (currentPosition < queryString.length) { 52 while (currentPosition < queryString.length) {
42 int position = queryString.indexOf("=", currentPosition); 53 int position = queryString.indexOf("=", currentPosition);
43 if (position == -1) { 54 if (position == -1) {
44 break; 55 break;
45 } 56 }
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 minutes = expectNum(":"); 232 minutes = expectNum(":");
222 seconds = expectNum(" "); 233 seconds = expectNum(" ");
223 expect("GMT"); 234 expect("GMT");
224 } 235 }
225 expectEnd(); 236 expectEnd();
226 TimeZone utc = new TimeZone.utc(); 237 TimeZone utc = new TimeZone.utc();
227 return new Date.withTimeZone( 238 return new Date.withTimeZone(
228 year, month + 1, day, hours, minutes, seconds, 0, utc); 239 year, month + 1, day, hours, minutes, seconds, 0, utc);
229 } 240 }
230 } 241 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/lib/io.dart ('k') | runtime/bin/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698