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

Side by Side Diff: sdk/lib/uri/encode_decode.dart

Issue 11645041: Fix URI encoding/decoding of + and space (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years 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 | tests/standalone/io/url_encoding_test.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 part of dart.uri; 5 part of dart.uri;
6 6
7 /** 7 /**
8 * Javascript-like URI encode/decode functions. 8 * Javascript-like URI encode/decode functions.
9 * The documentation here borrows heavily from the original Javascript 9 * The documentation here borrows heavily from the original Javascript
10 * doumentation on MDN at: 10 * doumentation on MDN at:
11 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects 11 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects
12 */ 12 */
13 13
14 /** 14 /**
15 * A JavaScript-like URI encoder. Encodes Uniform Resource Identifier [uri] 15 * A JavaScript-like URI encoder. Encodes Uniform Resource Identifier [uri]
16 * by replacing each instance of certain characters by one, two, three, or four 16 * by replacing each instance of certain characters by one, two, three, or four
17 * escape sequences representing the UTF-8 encoding of the character (will 17 * escape sequences representing the UTF-8 encoding of the character (will
18 * only be four escape sequences for characters composed of two "surrogate" 18 * only be four escape sequences for characters composed of two "surrogate"
19 * characters). This assumes that [uri] is a complete URI, so does not encode 19 * characters). This assumes that [uri] is a complete URI, so does not encode
20 * reserved characters that have special meaning in the URI: [:#;,/?:@&=+\$:] 20 * reserved characters that have special meaning in the URI: [:#;,/?:@&=+\$:]
21 * It returns the escaped URI. 21 * It returns the escaped URI.
22 */ 22 */
23 String encodeUri(String uri) { 23 String encodeUri(String uri) {
24 return _uriEncode( 24 return _uriEncode(
25 "-_.!~*'()#;,/?:@&=+\$0123456789" 25 "-_.!~*'()#;,/?:@&=\$0123456789"
26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", uri); 26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", uri);
27 } 27 }
28 28
29 /** 29 /**
30 * An implementation of JavaScript's decodeURIComponent function. 30 * An implementation of JavaScript's decodeURIComponent function.
31 * Decodes a Uniform Resource Identifier [uri] previously created by 31 * Decodes a Uniform Resource Identifier [uri] previously created by
32 * encodeURI or by a similar routine. It replaces each escape sequence 32 * encodeURI or by a similar routine. It replaces each escape sequence
33 * in [uri] with the character that it represents. It does not decode 33 * in [uri] with the character that it represents. It does not decode
34 * escape sequences that could not have been introduced by encodeURI. 34 * escape sequences that could not have been introduced by encodeURI.
35 * It returns the unescaped URI. 35 * It returns the unescaped URI.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 /** 73 /**
74 * This is the internal implementation of JavaScript's encodeURI function. 74 * This is the internal implementation of JavaScript's encodeURI function.
75 * It encodes all characters in the string [text] except for those 75 * It encodes all characters in the string [text] except for those
76 * that appear in [canonical], and returns the escaped string. 76 * that appear in [canonical], and returns the escaped string.
77 */ 77 */
78 String _uriEncode(String canonical, String text) { 78 String _uriEncode(String canonical, String text) {
79 final String hex = '0123456789ABCDEF'; 79 final String hex = '0123456789ABCDEF';
80 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v&0xf]}'; 80 var byteToHex = (int v) => '%${hex[v >> 4]}${hex[v&0xf]}';
81 StringBuffer result = new StringBuffer(); 81 StringBuffer result = new StringBuffer();
82 for (int i = 0; i < text.length; i++) { 82 for (int i = 0; i < text.length; i++) {
83 if (canonical.indexOf(text[i]) >= 0) { 83 if (canonical.indexOf(text[i]) >= 0) {
Lasse Reichstein Nielsen 2012/12/20 23:24:32 Ick, that looks slow - indexOf is linear in the ca
Søren Gjesse 2012/12/21 08:51:19 Created a new change to address this.
84 result.add(text[i]); 84 result.add(text[i]);
85 } else if (text[i] == " ") {
86 result.add("+");
85 } else { 87 } else {
86 int ch = text.charCodeAt(i); 88 int ch = text.charCodeAt(i);
87 if (ch >= 0xD800 && ch < 0xDC00) { 89 if (ch >= 0xD800 && ch < 0xDC00) {
88 // Low surrogate. We expect a next char high surrogate. 90 // Low surrogate. We expect a next char high surrogate.
89 ++i; 91 ++i;
90 int nextCh = text.length == i ? 0 : text.charCodeAt(i); 92 int nextCh = text.length == i ? 0 : text.charCodeAt(i);
91 if (nextCh >= 0xDC00 && nextCh < 0xE000) { 93 if (nextCh >= 0xDC00 && nextCh < 0xE000) {
92 // convert the pair to a U+10000 codepoint 94 // convert the pair to a U+10000 codepoint
93 ch = 0x10000 + ((ch-0xD800) << 10) + (nextCh - 0xDC00); 95 ch = 0x10000 + ((ch-0xD800) << 10) + (nextCh - 0xDC00);
94 } else { 96 } else {
(...skipping 26 matching lines...) Expand all
121 /** 123 /**
122 * A JavaScript-like decodeURI function. It unescapes the string [text] and 124 * A JavaScript-like decodeURI function. It unescapes the string [text] and
123 * returns the unescaped string. 125 * returns the unescaped string.
124 */ 126 */
125 String _uriDecode(String text) { 127 String _uriDecode(String text) {
126 StringBuffer result = new StringBuffer(); 128 StringBuffer result = new StringBuffer();
127 List<int> codepoints = new List<int>(); 129 List<int> codepoints = new List<int>();
128 for (int i = 0; i < text.length;) { 130 for (int i = 0; i < text.length;) {
129 String ch = text[i]; 131 String ch = text[i];
130 if (ch != '%') { 132 if (ch != '%') {
131 result.add(ch); 133 if (ch == '+') {
134 result.add(" ");
135 } else {
136 result.add(ch);
137 }
132 i++; 138 i++;
133 } else { 139 } else {
134 codepoints.clear(); 140 codepoints.clear();
135 while (ch == '%') { 141 while (ch == '%') {
136 if (++i > text.length - 2) { 142 if (++i > text.length - 2) {
137 throw new ArgumentError('Truncated URI'); 143 throw new ArgumentError('Truncated URI');
138 } 144 }
139 codepoints.add(_hexCharPairToByte(text, i)); 145 codepoints.add(_hexCharPairToByte(text, i));
140 i += 2; 146 i += 2;
141 if (i == text.length) 147 if (i == text.length)
142 break; 148 break;
143 ch = text[i]; 149 ch = text[i];
144 } 150 }
145 result.add(decodeUtf8(codepoints)); 151 result.add(decodeUtf8(codepoints));
146 } 152 }
147 } 153 }
148 return result.toString(); 154 return result.toString();
149 } 155 }
150 156
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/url_encoding_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698