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 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A parsed URI, as specified by RFC-3986, http://tools.ietf.org/html/rfc3986. | 8 * A parsed URI, as specified by RFC-3986, http://tools.ietf.org/html/rfc3986. |
9 */ | 9 */ |
10 class Uri { | 10 class Uri { |
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
762 * the individual parts. | 762 * the individual parts. |
763 * | 763 * |
764 * For handling the [path] and [query] components consider using | 764 * For handling the [path] and [query] components consider using |
765 * [pathSegments] and [queryParameters] to get the separated and | 765 * [pathSegments] and [queryParameters] to get the separated and |
766 * decoded component. | 766 * decoded component. |
767 */ | 767 */ |
768 static String decodeComponent(String encodedComponent) { | 768 static String decodeComponent(String encodedComponent) { |
769 return _uriDecode(encodedComponent); | 769 return _uriDecode(encodedComponent); |
770 } | 770 } |
771 | 771 |
772 static String decodeQueryComponent(String encodedComponent) { | 772 static String decodeQueryComponent( |
773 return _uriDecode(encodedComponent, plusToSpace: true); | 773 String encodedComponent, |
774 {String decodeString(List<int> bytes): decodeUtf8}) { | |
Lasse Reichstein Nielsen
2013/07/04 14:56:33
Document the method.
Mention that "decodeString" i
Anders Johnsen
2013/07/05 09:51:28
Done.
| |
775 return _uriDecode(encodedComponent, | |
776 plusToSpace: true, | |
777 decodeString: decodeString); | |
774 } | 778 } |
775 | 779 |
776 /** | 780 /** |
777 * Encode the string [uri] using percent-encoding to make it | 781 * Encode the string [uri] using percent-encoding to make it |
778 * safe for literal use as a full URI. | 782 * safe for literal use as a full URI. |
779 * | 783 * |
780 * All characters except uppercase and lowercase letters, digits and | 784 * All characters except uppercase and lowercase letters, digits and |
781 * the characters `!#$&'()*+,-./:;=?@_~` are percent-encoded. This | 785 * the characters `!#$&'()*+,-./:;=?@_~` are percent-encoded. This |
782 * is the set of characters specified in in ECMA-262 version 5.1 for | 786 * is the set of characters specified in in ECMA-262 version 5.1 for |
783 * the encodeURI function . | 787 * the encodeURI function . |
(...skipping 18 matching lines...) Expand all Loading... | |
802 * Returns the [query] split into a map according to the rules | 806 * Returns the [query] split into a map according to the rules |
803 * specified for FORM post in the | 807 * specified for FORM post in the |
804 * [HTML 4.01 specification section 17.13.4] | 808 * [HTML 4.01 specification section 17.13.4] |
805 * (http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4 | 809 * (http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4 |
806 * "HTML 4.01 section 17.13.4"). Each key and value in the returned | 810 * "HTML 4.01 section 17.13.4"). Each key and value in the returned |
807 * map has been decoded. If the [query] | 811 * map has been decoded. If the [query] |
808 * is the empty string an empty map is returned. | 812 * is the empty string an empty map is returned. |
809 * | 813 * |
810 * Keys in the query string that have no value are mapped to the | 814 * Keys in the query string that have no value are mapped to the |
811 * empty string. | 815 * empty string. |
816 * | |
817 * If [decodeString] is specified, that function will be used to decode the | |
Lasse Reichstein Nielsen
2013/07/04 14:56:33
again "decodeString" -> decode.
It will be used to
Anders Johnsen
2013/07/05 09:51:28
Done.
| |
818 * strings from bytes. Default is a UTF_8 decoder. | |
812 */ | 819 */ |
813 static Map<String, String> splitQueryString(String query) { | 820 static Map<String, String> splitQueryString( |
821 String query, | |
822 {String decodeString(List<int> bytes): decodeUtf8}) { | |
814 return query.split("&").fold({}, (map, element) { | 823 return query.split("&").fold({}, (map, element) { |
815 int index = element.indexOf("="); | 824 int index = element.indexOf("="); |
816 if (index == -1) { | 825 if (index == -1) { |
817 if (element != "") map[decodeQueryComponent(element)] = ""; | 826 if (element != "") map[decodeQueryComponent(element)] = ""; |
818 } else if (index != 0) { | 827 } else if (index != 0) { |
819 var key = element.substring(0, index); | 828 var key = element.substring(0, index); |
820 var value = element.substring(index + 1); | 829 var value = element.substring(index + 1); |
821 map[Uri.decodeQueryComponent(key)] = decodeQueryComponent(value); | 830 map[Uri.decodeQueryComponent(key, decodeString: decodeString)] = |
831 decodeQueryComponent(value, decodeString: decodeString); | |
822 } | 832 } |
823 return map; | 833 return map; |
824 }); | 834 }); |
825 } | 835 } |
826 | 836 |
827 // Frequently used character codes. | 837 // Frequently used character codes. |
828 static const int _PERCENT = 0x25; | 838 static const int _PERCENT = 0x25; |
829 static const int _PLUS = 0x2B; | 839 static const int _PLUS = 0x2B; |
830 static const int _SLASH = 0x2F; | 840 static const int _SLASH = 0x2F; |
831 static const int _ZERO = 0x30; | 841 static const int _ZERO = 0x30; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
894 byte = byte * 16 + charCode - 0x57; | 904 byte = byte * 16 + charCode - 0x57; |
895 } else { | 905 } else { |
896 throw new ArgumentError("Invalid URL encoding"); | 906 throw new ArgumentError("Invalid URL encoding"); |
897 } | 907 } |
898 } | 908 } |
899 } | 909 } |
900 return byte; | 910 return byte; |
901 } | 911 } |
902 | 912 |
903 /** | 913 /** |
904 * A JavaScript-like decodeURI function. It unescapes the string [text] and | 914 * A JavaScript-like decodeURI function. It unescapes the string [text] and |
Lasse Reichstein Nielsen
2013/07/04 14:56:33
This methods needs a new documentation (a standalo
Bill Hesse
2013/07/05 08:52:13
I would make clear that it decodes URI encoding to
Anders Johnsen
2013/07/05 09:51:28
Done.
Anders Johnsen
2013/07/05 09:51:28
Done.
| |
905 * returns the unescaped string. | 915 * returns the unescaped string. |
906 */ | 916 */ |
907 static String _uriDecode(String text, {bool plusToSpace: false}) { | 917 static String _uriDecode(String text, |
918 {bool plusToSpace: false, | |
919 String decodeString(List<int> bytes): decodeUtf8}) { | |
908 StringBuffer result = new StringBuffer(); | 920 StringBuffer result = new StringBuffer(); |
909 List<int> codepoints = new List<int>(); | 921 List<int> codepoints = new List<int>(); |
910 for (int i = 0; i < text.length;) { | 922 for (int i = 0; i < text.length;) { |
911 int ch = text.codeUnitAt(i); | 923 int ch = text.codeUnitAt(i); |
912 if (ch != _PERCENT) { | 924 if (ch != _PERCENT) { |
913 if (plusToSpace && ch == _PLUS) { | 925 if (plusToSpace && ch == _PLUS) { |
914 result.write(" "); | 926 result.write(" "); |
915 } else { | 927 } else { |
916 result.writeCharCode(ch); | 928 result.writeCharCode(ch); |
917 } | 929 } |
918 i++; | 930 i++; |
919 } else { | 931 } else { |
920 codepoints.clear(); | 932 codepoints.clear(); |
921 while (ch == _PERCENT) { | 933 while (ch == _PERCENT) { |
922 if (++i > text.length - 2) { | 934 if (++i > text.length - 2) { |
923 throw new ArgumentError('Truncated URI'); | 935 throw new ArgumentError('Truncated URI'); |
924 } | 936 } |
925 codepoints.add(_hexCharPairToByte(text, i)); | 937 codepoints.add(_hexCharPairToByte(text, i)); |
926 i += 2; | 938 i += 2; |
927 if (i == text.length) break; | 939 if (i == text.length) break; |
928 ch = text.codeUnitAt(i); | 940 ch = text.codeUnitAt(i); |
929 } | 941 } |
930 result.write(decodeUtf8(codepoints)); | 942 result.write(decodeString(codepoints)); |
931 } | 943 } |
932 } | 944 } |
933 return result.toString(); | 945 return result.toString(); |
934 } | 946 } |
935 | 947 |
936 // Tables of char-codes organized as a bit vector of 128 bits where | 948 // Tables of char-codes organized as a bit vector of 128 bits where |
937 // each bit indicate whether a character code on the 0-127 needs to | 949 // each bit indicate whether a character code on the 0-127 needs to |
938 // be escaped or not. | 950 // be escaped or not. |
939 | 951 |
940 // The unreserved characters of RFC 3986. | 952 // The unreserved characters of RFC 3986. |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1120 void clear() { | 1132 void clear() { |
1121 throw new UnsupportedError("Cannot modify an unmodifiable map"); | 1133 throw new UnsupportedError("Cannot modify an unmodifiable map"); |
1122 } | 1134 } |
1123 void forEach(void f(K key, V value)) => _map.forEach(f); | 1135 void forEach(void f(K key, V value)) => _map.forEach(f); |
1124 Iterable<K> get keys => _map.keys; | 1136 Iterable<K> get keys => _map.keys; |
1125 Iterable<V> get values => _map.values; | 1137 Iterable<V> get values => _map.values; |
1126 int get length => _map.length; | 1138 int get length => _map.length; |
1127 bool get isEmpty => _map.isEmpty; | 1139 bool get isEmpty => _map.isEmpty; |
1128 bool get isNotEmpty => _map.isNotEmpty; | 1140 bool get isNotEmpty => _map.isNotEmpty; |
1129 } | 1141 } |
OLD | NEW |