OLD | NEW |
1 // Copyright (c) 2011, 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 /** | 5 /** |
6 * A parsed URI, inspired by: | 6 * A parsed URI, inspired by: |
7 * http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html | 7 * http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html |
8 */ | 8 */ |
9 class Uri { | 9 class Uri extends uri.Uri { |
10 /** | 10 /** |
11 * Parses a URL query string into a map. Because you can have multiple values | 11 * Parses a URL query string into a map. Because you can have multiple values |
12 * for the same parameter name, each parameter name maps to a list of | 12 * for the same parameter name, each parameter name maps to a list of |
13 * values. For example, '?a=b&c=d&a=e' would be parsed as | 13 * values. For example, '?a=b&c=d&a=e' would be parsed as |
14 * [{'a':['b','e'],'c':['d']}]. | 14 * [{'a':['b','e'],'c':['d']}]. |
15 */ | 15 */ |
16 // TODO(jmesserly): consolidate with new Uri.fromString(...) | 16 // TODO(jmesserly): consolidate with new Uri.fromString(...) |
17 static Map<String, List<String>> parseQuery(String queryString) { | 17 static Map<String, List<String>> parseQuery(String queryString) { |
18 final queryParams = new Map<String, List<String>>(); | 18 final queryParams = new Map<String, List<String>>(); |
19 if (queryString.startsWith('?')) { | 19 if (queryString.startsWith('?')) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 if (component == null) return component; | 66 if (component == null) return component; |
67 | 67 |
68 return component.replaceAll('%3A', ':') | 68 return component.replaceAll('%3A', ':') |
69 .replaceAll('%2F', '/') | 69 .replaceAll('%2F', '/') |
70 .replaceAll('%3F', '?') | 70 .replaceAll('%3F', '?') |
71 .replaceAll('%3D', '=') | 71 .replaceAll('%3D', '=') |
72 .replaceAll('%26', '&') | 72 .replaceAll('%26', '&') |
73 .replaceAll('%20', ' '); | 73 .replaceAll('%20', ' '); |
74 } | 74 } |
75 | 75 |
76 String scheme; | 76 Uri.fromString(String uri) : super.fromString(uri); |
77 String userInfo; | |
78 String domain; | |
79 int port; | |
80 String path; | |
81 String query; | |
82 String fragment; | |
83 | |
84 Uri.fromString(String uri) { | |
85 final m = _splitRe.firstMatch(uri); | |
86 | |
87 scheme = _decodeOrEmpty(m[_COMPONENT_SCHEME]); | |
88 userInfo = _decodeOrEmpty(m[_COMPONENT_USER_INFO]); | |
89 domain = _decodeOrEmpty(m[_COMPONENT_DOMAIN]); | |
90 port = _parseIntOrZero(m[_COMPONENT_PORT]); | |
91 path = _decodeOrEmpty(m[_COMPONENT_PATH]); | |
92 query = _decodeOrEmpty(m[_COMPONENT_QUERY_DATA]); | |
93 fragment = _decodeOrEmpty(m[_COMPONENT_FRAGMENT]); | |
94 } | |
95 | |
96 static String _decodeOrEmpty(String val) { | |
97 // TODO(jmesserly): use Uri.decodeComponent when available | |
98 //return val ? Uri.decodeComponent(val) : ''; | |
99 return val != null ? val : ''; | |
100 } | |
101 | |
102 static int _parseIntOrZero(String val) { | |
103 if (val !== null && val != '') { | |
104 return Math.parseInt(val); | |
105 } else { | |
106 return 0; | |
107 } | |
108 } | |
109 | |
110 // NOTE: This code was ported from: closure-library/closure/goog/uri/utils.js | |
111 static RegExp _splitReLazy; | |
112 | |
113 static RegExp get _splitRe() { | |
114 if (_splitReLazy == null) { | |
115 _splitReLazy = new RegExp( | |
116 '^' + | |
117 '(?:' + | |
118 '([^:/?#.]+)' + // scheme - ignore special characters | |
119 // used by other URL parts such as :, | |
120 // ?, /, #, and . | |
121 ':)?' + | |
122 '(?://' + | |
123 '(?:([^/?#]*)@)?' + // userInfo | |
124 '([\\w\\d\\-\\u0100-\\uffff.%]*)' + | |
125 // domain - restrict to letters, | |
126 // digits, dashes, dots, percent | |
127 // escapes, and unicode characters. | |
128 '(?::([0-9]+))?' + // port | |
129 ')?' + | |
130 '([^?#]+)?' + // path | |
131 '(?:\\?([^#]*))?' + // query | |
132 '(?:#(.*))?' + // fragment | |
133 '\$'); | |
134 } | |
135 return _splitReLazy; | |
136 } | |
137 | |
138 static final _COMPONENT_SCHEME = 1; | |
139 static final _COMPONENT_USER_INFO = 2; | |
140 static final _COMPONENT_DOMAIN = 3; | |
141 static final _COMPONENT_PORT = 4; | |
142 static final _COMPONENT_PATH = 5; | |
143 static final _COMPONENT_QUERY_DATA = 6; | |
144 static final _COMPONENT_FRAGMENT = 7; | |
145 } | 77 } |
OLD | NEW |