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

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

Issue 10387082: Fix URL decoding in HTTP library (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fix 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 | « no previous file | 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 - if not no decoding is
Mads Ager (google) 2012/05/14 08:19:26 Remove the ' - if not ...' part?
Søren Gjesse 2012/05/14 09:54:26 Done.
8 // TODO(sgjesse): Handle the error. 8 // required
9 int index = 0;
10 bool encoded = false;
11 while (!encoded && index < urlEncoded.length) {
12 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%";
13 index++;
9 } 14 }
15 if (!encoded) return urlEncoded;
16 index--;
10 17
11 StringBuffer result = new StringBuffer(); 18 // Start decoding from the first encoded character.
12 for (int ii = 0; urlEncoded.length > ii; ++ii) { 19 List<int> bytes = new List<int>();
13 if ('+' == urlEncoded[ii]) { 20 for (int i = 0; i < index; i++) bytes.add(urlEncoded.charCodeAt(i));
14 result.add(' '); 21 for (int i = index; i < urlEncoded.length; i++) {
15 } else if ('%' == urlEncoded[ii] && 22 if (urlEncoded[i] == "+") {
16 urlEncoded.length - 2 > ii) { 23 bytes.add(32);
17 try { 24 } else if (urlEncoded[i] == "%") {
18 int charCode = 25 if (urlEncoded.length - i < 2) {
19 Math.parseInt('0x' + urlEncoded.substring(ii + 1, ii + 3)); 26 throw new HttpException("Invalid URL encoding");
20 if (charCode <= 0x7f) { 27 }
21 result.add(new String.fromCharCodes([charCode])); 28 int byte = 0;
22 ii += 2; 29 for (int j = 0; j < 2; j++) {
30 var charCode = urlEncoded.charCodeAt(i + j + 1);
31 if (0x30 <= charCode && charCode <= 0x39) {
32 byte = byte * 16 + charCode - 0x30;
33 } else if (0x41 <= charCode && charCode <= 0x46) {
34 byte = byte * 16 + charCode - 0x37;
35 } else if (0x61 <= charCode && charCode <= 0x66) {
36 byte = byte * 16 + charCode - 0x57;
23 } else { 37 } else {
24 invalidEscape(); 38 throw new HttpException("Invalid URL encoding");
25 return '';
26 } 39 }
27 } catch (BadNumberFormatException ignored) {
28 invalidEscape();
29 return '';
30 } 40 }
41 bytes.add(byte);
42 i += 2;
31 } else { 43 } else {
32 result.add(urlEncoded[ii]); 44 bytes.add(urlEncoded.charCodeAt(i));
33 } 45 }
34 } 46 }
35 return result.toString(); 47 return decodeUtf8(bytes);
36 } 48 }
37 49
38 static Map<String, String> splitQueryString(String queryString) { 50 static Map<String, String> splitQueryString(String queryString) {
39 Map<String, String> result = new Map<String, String>(); 51 Map<String, String> result = new Map<String, String>();
40 int currentPosition = 0; 52 int currentPosition = 0;
41 while (currentPosition < queryString.length) { 53 while (currentPosition < queryString.length) {
42 int position = queryString.indexOf("=", currentPosition); 54 int position = queryString.indexOf("=", currentPosition);
43 if (position == -1) { 55 if (position == -1) {
44 break; 56 break;
45 } 57 }
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 minutes = expectNum(":"); 233 minutes = expectNum(":");
222 seconds = expectNum(" "); 234 seconds = expectNum(" ");
223 expect("GMT"); 235 expect("GMT");
224 } 236 }
225 expectEnd(); 237 expectEnd();
226 TimeZone utc = new TimeZone.utc(); 238 TimeZone utc = new TimeZone.utc();
227 return new Date.withTimeZone( 239 return new Date.withTimeZone(
228 year, month + 1, day, hours, minutes, seconds, 0, utc); 240 year, month + 1, day, hours, minutes, seconds, 0, utc);
229 } 241 }
230 } 242 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698