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

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

Issue 11659009: Reapply "Fix URI encoding/decoding of + and space"" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated tests 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
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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 /** 132 /**
131 * A JavaScript-like decodeURI function. It unescapes the string [text] and 133 * A JavaScript-like decodeURI function. It unescapes the string [text] and
132 * returns the unescaped string. 134 * returns the unescaped string.
133 */ 135 */
134 String _uriDecode(String text) { 136 String _uriDecode(String text) {
135 StringBuffer result = new StringBuffer(); 137 StringBuffer result = new StringBuffer();
136 List<int> codepoints = new List<int>(); 138 List<int> codepoints = new List<int>();
137 for (int i = 0; i < text.length;) { 139 for (int i = 0; i < text.length;) {
138 String ch = text[i]; 140 String ch = text[i];
139 if (ch != '%') { 141 if (ch != '%') {
140 result.add(ch); 142 if (ch == '+') {
143 result.add(" ");
144 } else {
145 result.add(ch);
146 }
141 i++; 147 i++;
142 } else { 148 } else {
143 codepoints.clear(); 149 codepoints.clear();
144 while (ch == '%') { 150 while (ch == '%') {
145 if (++i > text.length - 2) { 151 if (++i > text.length - 2) {
146 throw new ArgumentError('Truncated URI'); 152 throw new ArgumentError('Truncated URI');
147 } 153 }
148 codepoints.add(_hexCharPairToByte(text, i)); 154 codepoints.add(_hexCharPairToByte(text, i));
149 i += 2; 155 i += 2;
150 if (i == text.length) 156 if (i == text.length)
151 break; 157 break;
152 ch = text[i]; 158 ch = text[i];
153 } 159 }
154 result.add(decodeUtf8(codepoints)); 160 result.add(decodeUtf8(codepoints));
155 } 161 }
156 } 162 }
157 return result.toString(); 163 return result.toString();
158 } 164 }
159 165
OLDNEW
« no previous file with comments | « pkg/oauth2/test/authorization_code_grant_test.dart ('k') | tests/standalone/io/url_encoding_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698