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

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

Issue 1224263009: Do "path normalization" when creating a URI. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comments. Created 5 years, 5 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
« no previous file with comments | « CHANGELOG.md ('k') | tests/corelib/uri_normalize_path_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.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * A parsed URI, such as a URL. 8 * A parsed URI, such as a URL.
9 * 9 *
10 * **See also:** 10 * **See also:**
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 char = uri.codeUnitAt(index); 374 char = uri.codeUnitAt(index);
375 if (char == _QUESTION || char == _NUMBER_SIGN) { 375 if (char == _QUESTION || char == _NUMBER_SIGN) {
376 break; 376 break;
377 } 377 }
378 char = EOI; 378 char = EOI;
379 } 379 }
380 state = NOT_IN_PATH; 380 state = NOT_IN_PATH;
381 } 381 }
382 382
383 assert(state == NOT_IN_PATH); 383 assert(state == NOT_IN_PATH);
384 bool isFile = (scheme == "file"); 384 bool hasAuthority = (host != null);
385 bool ensureLeadingSlash = host != null; 385 path = _makePath(uri, pathStart, index, null, scheme, hasAuthority);
386 path = _makePath(uri, pathStart, index, null, ensureLeadingSlash, isFile);
387 386
388 if (char == _QUESTION) { 387 if (char == _QUESTION) {
389 int numberSignIndex = -1; 388 int numberSignIndex = -1;
390 for (int i = index + 1; i < end; i++) { 389 for (int i = index + 1; i < end; i++) {
391 if (uri.codeUnitAt(i) == _NUMBER_SIGN) { 390 if (uri.codeUnitAt(i) == _NUMBER_SIGN) {
392 numberSignIndex = i; 391 numberSignIndex = i;
393 break; 392 break;
394 } 393 }
395 } 394 }
396 if (numberSignIndex < 0) { 395 if (numberSignIndex < 0) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 // Special case this constructor for backwards compatibility. 503 // Special case this constructor for backwards compatibility.
505 if (query == "") query = null; 504 if (query == "") query = null;
506 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters); 505 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters);
507 fragment = _makeFragment(fragment, 0, _stringOrNullLength(fragment)); 506 fragment = _makeFragment(fragment, 0, _stringOrNullLength(fragment));
508 port = _makePort(port, scheme); 507 port = _makePort(port, scheme);
509 bool isFile = (scheme == "file"); 508 bool isFile = (scheme == "file");
510 if (host == null && 509 if (host == null &&
511 (userInfo.isNotEmpty || port != null || isFile)) { 510 (userInfo.isNotEmpty || port != null || isFile)) {
512 host = ""; 511 host = "";
513 } 512 }
514 bool ensureLeadingSlash = host != null; 513 bool hasAuthority = (host != null);
515 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments, 514 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments,
516 ensureLeadingSlash, isFile); 515 scheme, hasAuthority);
516 if (scheme.isEmpty && host == null && !path.startsWith('/')) {
517 path = _normalizeRelativePath(path);
518 } else {
519 path = _removeDotSegments(path);
520 }
517 return new Uri._internal(scheme, userInfo, host, port, 521 return new Uri._internal(scheme, userInfo, host, port,
518 path, query, fragment); 522 path, query, fragment);
519 } 523 }
520 524
521 /** 525 /**
522 * Creates a new `http` URI from authority, path and query. 526 * Creates a new `http` URI from authority, path and query.
523 * 527 *
524 * Examples: 528 * Examples:
525 * 529 *
526 * ``` 530 * ```
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 } 950 }
947 } 951 }
948 if (host != null) { 952 if (host != null) {
949 host = _makeHost(host, 0, host.length, false); 953 host = _makeHost(host, 0, host.length, false);
950 } else if (this.hasAuthority) { 954 } else if (this.hasAuthority) {
951 host = this.host; 955 host = this.host;
952 } else if (userInfo.isNotEmpty || port != null || isFile) { 956 } else if (userInfo.isNotEmpty || port != null || isFile) {
953 host = ""; 957 host = "";
954 } 958 }
955 959
956 bool ensureLeadingSlash = (host != null); 960 bool hasAuthority = host != null;
957 if (path != null || pathSegments != null) { 961 if (path != null || pathSegments != null) {
958 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments, 962 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments,
959 ensureLeadingSlash, isFile); 963 scheme, hasAuthority);
960 } else { 964 } else {
961 path = this.path; 965 path = this.path;
962 if ((isFile || (ensureLeadingSlash && !path.isEmpty)) && 966 if ((isFile || (hasAuthority && !path.isEmpty)) &&
963 !path.startsWith('/')) { 967 !path.startsWith('/')) {
964 path = "/$path"; 968 path = "/" + path;
965 } 969 }
966 } 970 }
967 971
968 if (query != null || queryParameters != null) { 972 if (query != null || queryParameters != null) {
969 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters); 973 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters);
970 } else if (this.hasQuery) { 974 } else if (this.hasQuery) {
971 query = this.query; 975 query = this.query;
972 } 976 }
973 977
974 if (fragment != null) { 978 if (fragment != null) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 * calls that would mutate it. 1022 * calls that would mutate it.
1019 */ 1023 */
1020 Map<String, String> get queryParameters { 1024 Map<String, String> get queryParameters {
1021 if (_queryParameters == null) { 1025 if (_queryParameters == null) {
1022 _queryParameters = new UnmodifiableMapView(splitQueryString(query)); 1026 _queryParameters = new UnmodifiableMapView(splitQueryString(query));
1023 } 1027 }
1024 return _queryParameters; 1028 return _queryParameters;
1025 } 1029 }
1026 1030
1027 /** 1031 /**
1028 * Returns an URI where the path has been normalized. 1032 * Returns a URI where the path has been normalized.
1029 * 1033 *
1030 * A normalized path does not contain `.` segments or non-leading `..` 1034 * A normalized path does not contain `.` segments or non-leading `..`
1031 * segments. 1035 * segments.
1032 * Only a relative path may contain leading `..` segments, 1036 * Only a relative path with no scheme or authority may contain
1037 * leading `..` segments,
1033 * a path that starts with `/` will also drop any leading `..` segments. 1038 * a path that starts with `/` will also drop any leading `..` segments.
1034 * 1039 *
1035 * This uses the same normalization strategy as [resolveUri], as specified by 1040 * This uses the same normalization strategy as `new Uri().resolve(this)`.
1036 * RFC 3986.
1037 * 1041 *
1038 * Does not change any part of the URI except the path. 1042 * Does not change any part of the URI except the path.
1043 *
1044 * The default implementation of `Uri` always normalizes paths, so calling
1045 * this function has no effect.
1039 */ 1046 */
1040 Uri normalizePath() { 1047 Uri normalizePath() {
1041 String path = _removeDotSegments(_path); 1048 String path = _normalizePath(_path, scheme, hasAuthority);
1042 if (identical(path, _path)) return this; 1049 if (identical(path, _path)) return this;
1043 return this.replace(path: path); 1050 return this.replace(path: path);
1044 } 1051 }
1045 1052
1046 static int _makePort(int port, String scheme) { 1053 static int _makePort(int port, String scheme) {
1047 // Perform scheme specific normalization. 1054 // Perform scheme specific normalization.
1048 if (port != null && port == _defaultPort(scheme)) return null; 1055 if (port != null && port == _defaultPort(scheme)) return null;
1049 return port; 1056 return port;
1050 } 1057 }
1051 1058
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 * Validates scheme characters and does case-normalization. 1178 * Validates scheme characters and does case-normalization.
1172 * 1179 *
1173 * Schemes are converted to lower case. They cannot contain escapes. 1180 * Schemes are converted to lower case. They cannot contain escapes.
1174 */ 1181 */
1175 static String _makeScheme(String scheme, int start, int end) { 1182 static String _makeScheme(String scheme, int start, int end) {
1176 if (start == end) return ""; 1183 if (start == end) return "";
1177 final int firstCodeUnit = scheme.codeUnitAt(start); 1184 final int firstCodeUnit = scheme.codeUnitAt(start);
1178 if (!_isAlphabeticCharacter(firstCodeUnit)) { 1185 if (!_isAlphabeticCharacter(firstCodeUnit)) {
1179 _fail(scheme, start, "Scheme not starting with alphabetic character"); 1186 _fail(scheme, start, "Scheme not starting with alphabetic character");
1180 } 1187 }
1181 bool allLowercase = firstCodeUnit >= _LOWER_CASE_A; 1188 bool containsUpperCase = false;
1182 for (int i = start; i < end; i++) { 1189 for (int i = start; i < end; i++) {
1183 final int codeUnit = scheme.codeUnitAt(i); 1190 final int codeUnit = scheme.codeUnitAt(i);
1184 if (!_isSchemeCharacter(codeUnit)) { 1191 if (!_isSchemeCharacter(codeUnit)) {
1185 _fail(scheme, i, "Illegal scheme character"); 1192 _fail(scheme, i, "Illegal scheme character");
1186 } 1193 }
1187 if (codeUnit < _LOWER_CASE_A || codeUnit > _LOWER_CASE_Z) { 1194 if (_UPPER_CASE_A <= codeUnit && codeUnit <= _UPPER_CASE_Z) {
1188 allLowercase = false; 1195 containsUpperCase = true;
1189 } 1196 }
1190 } 1197 }
1191 scheme = scheme.substring(start, end); 1198 scheme = scheme.substring(start, end);
1192 if (!allLowercase) scheme = scheme.toLowerCase(); 1199 if (containsUpperCase) scheme = scheme.toLowerCase();
1193 return scheme; 1200 return scheme;
1194 } 1201 }
1195 1202
1196 static String _makeUserInfo(String userInfo, int start, int end) { 1203 static String _makeUserInfo(String userInfo, int start, int end) {
1197 if (userInfo == null) return ""; 1204 if (userInfo == null) return "";
1198 return _normalize(userInfo, start, end, _userinfoTable); 1205 return _normalize(userInfo, start, end, _userinfoTable);
1199 } 1206 }
1200 1207
1201 static String _makePath(String path, int start, int end, 1208 static String _makePath(String path, int start, int end,
1202 Iterable<String> pathSegments, 1209 Iterable<String> pathSegments,
1203 bool ensureLeadingSlash, 1210 String scheme,
1204 bool isFile) { 1211 bool hasAuthority) {
1212 bool isFile = (scheme == "file");
1213 bool ensureLeadingSlash = isFile || hasAuthority;
1205 if (path == null && pathSegments == null) return isFile ? "/" : ""; 1214 if (path == null && pathSegments == null) return isFile ? "/" : "";
1206 if (path != null && pathSegments != null) { 1215 if (path != null && pathSegments != null) {
1207 throw new ArgumentError('Both path and pathSegments specified'); 1216 throw new ArgumentError('Both path and pathSegments specified');
1208 } 1217 }
1209 var result; 1218 var result;
1210 if (path != null) { 1219 if (path != null) {
1211 result = _normalize(path, start, end, _pathCharOrSlashTable); 1220 result = _normalize(path, start, end, _pathCharOrSlashTable);
1212 } else { 1221 } else {
1213 result = pathSegments.map((s) => _uriEncode(_pathCharTable, s)).join("/"); 1222 result = pathSegments.map((s) => _uriEncode(_pathCharTable, s)).join("/");
1214 } 1223 }
1215 if (result.isEmpty) { 1224 if (result.isEmpty) {
1216 if (isFile) return "/"; 1225 if (isFile) return "/";
1217 } else if ((isFile || ensureLeadingSlash) && 1226 } else if (ensureLeadingSlash && !result.startsWith('/')) {
1218 result.codeUnitAt(0) != _SLASH) { 1227 result = "/" + result;
1219 return "/$result";
1220 } 1228 }
1229 result = _normalizePath(result, scheme, hasAuthority);
1221 return result; 1230 return result;
1222 } 1231 }
1223 1232
1233 /// Performs path normalization (remove dot segments) on a path.
1234 ///
1235 /// If the URI has neither scheme nor authority, it's considered a
1236 /// "pure path" and normalization won't remove leading ".." segments.
1237 /// Otherwise it follows the RFC 3986 "remove dot segments" algorithm.
1238 static String _normalizePath(String path, String scheme, bool hasAuthority) {
1239 if (scheme.isEmpty && !hasAuthority && !path.startsWith('/')) {
1240 return _normalizeRelativePath(path);
1241 }
1242 return _removeDotSegments(path);
1243 }
1244
1224 static String _makeQuery(String query, int start, int end, 1245 static String _makeQuery(String query, int start, int end,
1225 Map<String, String> queryParameters) { 1246 Map<String, String> queryParameters) {
1226 if (query == null && queryParameters == null) return null; 1247 if (query == null && queryParameters == null) return null;
1227 if (query != null && queryParameters != null) { 1248 if (query != null && queryParameters != null) {
1228 throw new ArgumentError('Both query and queryParameters specified'); 1249 throw new ArgumentError('Both query and queryParameters specified');
1229 } 1250 }
1230 if (query != null) return _normalize(query, start, end, _queryCharTable); 1251 if (query != null) return _normalize(query, start, end, _queryCharTable);
1231 1252
1232 var result = new StringBuffer(); 1253 var result = new StringBuffer();
1233 var first = true; 1254 var first = true;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1422 static bool _isGeneralDelimiter(int ch) { 1443 static bool _isGeneralDelimiter(int ch) {
1423 return ch <= _RIGHT_BRACKET && 1444 return ch <= _RIGHT_BRACKET &&
1424 ((_genDelimitersTable[ch >> 4] & (1 << (ch & 0x0f))) != 0); 1445 ((_genDelimitersTable[ch >> 4] & (1 << (ch & 0x0f))) != 0);
1425 } 1446 }
1426 1447
1427 /** 1448 /**
1428 * Returns whether the URI is absolute. 1449 * Returns whether the URI is absolute.
1429 */ 1450 */
1430 bool get isAbsolute => scheme != "" && fragment == ""; 1451 bool get isAbsolute => scheme != "" && fragment == "";
1431 1452
1432 String _merge(String base, String reference) { 1453 String _mergePaths(String base, String reference) {
1433 if (base.isEmpty) return "/$reference";
1434 // Optimize for the case: absolute base, reference beginning with "../". 1454 // Optimize for the case: absolute base, reference beginning with "../".
1435 int backCount = 0; 1455 int backCount = 0;
1436 int refStart = 0; 1456 int refStart = 0;
1437 // Count number of "../" at beginning of reference. 1457 // Count number of "../" at beginning of reference.
1438 while (reference.startsWith("../", refStart)) { 1458 while (reference.startsWith("../", refStart)) {
1439 refStart += 3; 1459 refStart += 3;
1440 backCount++; 1460 backCount++;
1441 } 1461 }
1442 1462
1443 // Drop last segment - everything after last '/' of base. 1463 // Drop last segment - everything after last '/' of base.
(...skipping 12 matching lines...) Expand all
1456 (delta == 2 || base.codeUnitAt(newEnd + 2) == _DOT)) { 1476 (delta == 2 || base.codeUnitAt(newEnd + 2) == _DOT)) {
1457 break; 1477 break;
1458 } 1478 }
1459 baseEnd = newEnd; 1479 baseEnd = newEnd;
1460 backCount--; 1480 backCount--;
1461 } 1481 }
1462 return base.replaceRange(baseEnd + 1, null, 1482 return base.replaceRange(baseEnd + 1, null,
1463 reference.substring(refStart - 3 * backCount)); 1483 reference.substring(refStart - 3 * backCount));
1464 } 1484 }
1465 1485
1466 bool _hasDotSegments(String path) { 1486 /// Make a guess at whether a path contains a `..` or `.` segment.
1467 if (path.length > 0 && path.codeUnitAt(0) == _DOT) return true; 1487 ///
1488 /// This is a primitive test that can cause false positives.
1489 /// It's only used to avoid a more expensive operation in the case where
1490 /// it's not necessary.
1491 static bool _mayContainDotSegments(String path) {
1492 if (path.startsWith('.')) return true;
1468 int index = path.indexOf("/."); 1493 int index = path.indexOf("/.");
1469 return index != -1; 1494 return index != -1;
1470 } 1495 }
1471 1496
1472 String _removeDotSegments(String path) { 1497 /// Removes '.' and '..' segments from a path.
1473 if (!_hasDotSegments(path)) return path; 1498 ///
1499 /// Follows the RFC 2986 "remove dot segments" algorithm.
1500 /// This algorithm is only used on paths of URIs with a scheme,
1501 /// and it treats the path as if it is absolute (leading '..' are removed).
1502 static String _removeDotSegments(String path) {
1503 if (!_mayContainDotSegments(path)) return path;
1504 assert(path.isNotEmpty); // An empty path would not have dot segments.
1474 List<String> output = []; 1505 List<String> output = [];
1475 bool appendSlash = false; 1506 bool appendSlash = false;
1476 for (String segment in path.split("/")) { 1507 for (String segment in path.split("/")) {
1477 appendSlash = false; 1508 appendSlash = false;
1478 if (segment == "..") { 1509 if (segment == "..") {
1479 if (!output.isEmpty && 1510 if (output.isNotEmpty) {
1480 ((output.length != 1) || (output[0] != ""))) output.removeLast(); 1511 output.removeLast();
1512 if (output.isEmpty) {
1513 output.add("");
1514 }
1515 }
1481 appendSlash = true; 1516 appendSlash = true;
1482 } else if ("." == segment) { 1517 } else if ("." == segment) {
1483 appendSlash = true; 1518 appendSlash = true;
1484 } else { 1519 } else {
1485 output.add(segment); 1520 output.add(segment);
1486 } 1521 }
1487 } 1522 }
1488 if (appendSlash) output.add(""); 1523 if (appendSlash) output.add("");
1489 return output.join("/"); 1524 return output.join("/");
1490 } 1525 }
1491 1526
1527 /// Removes all `.` segments and any non-leading `..` segments.
1528 ///
1529 /// Removing the ".." from a "bar/foo/.." sequence results in "bar/"
1530 /// (trailing "/"). If the entire path is removed (because it contains as
1531 /// many ".." segments as real segments), the result is "./".
1532 /// This is different from an empty string, which represents "no path",
1533 /// when you resolve it against a base URI with a path with a non-empty
1534 /// final segment.
1535 static String _normalizeRelativePath(String path) {
1536 assert(!path.startsWith('/')); // Only get called for relative paths.
1537 if (!_mayContainDotSegments(path)) return path;
1538 assert(path.isNotEmpty); // An empty path would not have dot segments.
1539 List<String> output = [];
1540 bool appendSlash = false;
1541 for (String segment in path.split("/")) {
1542 appendSlash = false;
1543 if (".." == segment) {
1544 if (!output.isEmpty && output.last != "..") {
1545 output.removeLast();
1546 appendSlash = true;
1547 } else {
1548 output.add("..");
1549 }
1550 } else if ("." == segment) {
1551 appendSlash = true;
1552 } else {
1553 output.add(segment);
1554 }
1555 }
1556 if (output.isEmpty || (output.length == 1 && output[0].isEmpty)) {
1557 return "./";
1558 }
1559 if (appendSlash || output.last == '..') output.add("");
1560 return output.join("/");
1561 }
1562
1492 /** 1563 /**
1493 * Resolve [reference] as an URI relative to `this`. 1564 * Resolve [reference] as an URI relative to `this`.
1494 * 1565 *
1495 * First turn [reference] into a URI using [Uri.parse]. Then resolve the 1566 * First turn [reference] into a URI using [Uri.parse]. Then resolve the
1496 * resulting URI relative to `this`. 1567 * resulting URI relative to `this`.
1497 * 1568 *
1498 * Returns the resolved URI. 1569 * Returns the resolved URI.
1499 * 1570 *
1500 * See [resolveUri] for details. 1571 * See [resolveUri] for details.
1501 */ 1572 */
1502 Uri resolve(String reference) { 1573 Uri resolve(String reference) {
1503 return resolveUri(Uri.parse(reference)); 1574 return resolveUri(Uri.parse(reference));
1504 } 1575 }
1505 1576
1506 /** 1577 /**
1507 * Resolve [reference] as an URI relative to `this`. 1578 * Resolve [reference] as an URI relative to `this`.
1508 * 1579 *
1509 * Returns the resolved URI. 1580 * Returns the resolved URI.
1510 * 1581 *
1511 * The algorithm for resolving a reference is described in 1582 * The algorithm "Transform Reference" for resolving a reference is
1512 * [RFC-3986 Section 5] 1583 * described in [RFC-3986 Section 5]
1513 * (http://tools.ietf.org/html/rfc3986#section-5 "RFC-1123"). 1584 * (http://tools.ietf.org/html/rfc3986#section-5 "RFC-1123").
1585 *
1586 * Updated to handle the case where the base URI is just a relative path -
1587 * that is: when it has no scheme or authority and the path does not start
1588 * with a slash.
1589 * In that case, the paths are combined without removing leading "..", and
1590 * an empty path is not converted to "/".
1514 */ 1591 */
1515 Uri resolveUri(Uri reference) { 1592 Uri resolveUri(Uri reference) {
1516 // From RFC 3986. 1593 // From RFC 3986.
1517 String targetScheme; 1594 String targetScheme;
1518 String targetUserInfo = ""; 1595 String targetUserInfo = "";
1519 String targetHost; 1596 String targetHost;
1520 int targetPort; 1597 int targetPort;
1521 String targetPath; 1598 String targetPath;
1522 String targetQuery; 1599 String targetQuery;
1523 if (reference.scheme.isNotEmpty) { 1600 if (reference.scheme.isNotEmpty) {
(...skipping 10 matching lines...) Expand all
1534 } else { 1611 } else {
1535 targetScheme = this.scheme; 1612 targetScheme = this.scheme;
1536 if (reference.hasAuthority) { 1613 if (reference.hasAuthority) {
1537 targetUserInfo = reference.userInfo; 1614 targetUserInfo = reference.userInfo;
1538 targetHost = reference.host; 1615 targetHost = reference.host;
1539 targetPort = _makePort(reference.hasPort ? reference.port : null, 1616 targetPort = _makePort(reference.hasPort ? reference.port : null,
1540 targetScheme); 1617 targetScheme);
1541 targetPath = _removeDotSegments(reference.path); 1618 targetPath = _removeDotSegments(reference.path);
1542 if (reference.hasQuery) targetQuery = reference.query; 1619 if (reference.hasQuery) targetQuery = reference.query;
1543 } else { 1620 } else {
1621 targetUserInfo = this._userInfo;
1622 targetHost = this._host;
1623 targetPort = this._port;
1544 if (reference.path == "") { 1624 if (reference.path == "") {
1545 targetPath = this._path; 1625 targetPath = this._path;
1546 if (reference.hasQuery) { 1626 if (reference.hasQuery) {
1547 targetQuery = reference.query; 1627 targetQuery = reference.query;
1548 } else { 1628 } else {
1549 targetQuery = this._query; 1629 targetQuery = this._query;
1550 } 1630 }
1551 } else { 1631 } else {
1552 if (reference.path.startsWith("/")) { 1632 if (reference.hasAbsolutePath) {
1553 targetPath = _removeDotSegments(reference.path); 1633 targetPath = _removeDotSegments(reference.path);
1554 } else { 1634 } else {
1555 targetPath = _removeDotSegments(_merge(this._path, reference.path)); 1635 // This is the RFC 3986 behavior for merging.
1636 if (this.hasEmptyPath) {
1637 if (!this.hasScheme && !this.hasAuthority) {
1638 // Keep the path relative if no scheme or authority.
1639 targetPath = reference.path;
1640 } else {
1641 // Add path normalization on top of RFC algorithm.
1642 targetPath = _removeDotSegments("/" + reference.path);
1643 }
1644 } else {
1645 var mergedPath = _mergePaths(this._path, reference.path);
1646 if (this.hasScheme || this.hasAuthority || this.hasAbsolutePath) {
1647 targetPath = _removeDotSegments(mergedPath);
1648 } else {
1649 // Non-RFC 3986 beavior. If both base and reference are relative
1650 // path, allow the merged path to start with "..".
1651 // The RFC only specifies the case where the base has a scheme.
1652 targetPath = _normalizeRelativePath(mergedPath);
1653 }
1654 }
1556 } 1655 }
1557 if (reference.hasQuery) targetQuery = reference.query; 1656 if (reference.hasQuery) targetQuery = reference.query;
1558 } 1657 }
1559 targetUserInfo = this._userInfo;
1560 targetHost = this._host;
1561 targetPort = this._port;
1562 } 1658 }
1563 } 1659 }
1564 String fragment = reference.hasFragment ? reference.fragment : null; 1660 String fragment = reference.hasFragment ? reference.fragment : null;
1565 return new Uri._internal(targetScheme, 1661 return new Uri._internal(targetScheme,
1566 targetUserInfo, 1662 targetUserInfo,
1567 targetHost, 1663 targetHost,
1568 targetPort, 1664 targetPort,
1569 targetPath, 1665 targetPath,
1570 targetQuery, 1666 targetQuery,
1571 fragment); 1667 fragment);
1572 } 1668 }
1573 1669
1574 /** 1670 /**
1671 * Returns whether the URI has a [scheme] component.
1672 */
1673 bool get hasScheme => scheme.isNotEmpty;
1674
1675 /**
1575 * Returns whether the URI has an [authority] component. 1676 * Returns whether the URI has an [authority] component.
1576 */ 1677 */
1577 bool get hasAuthority => _host != null; 1678 bool get hasAuthority => _host != null;
1578 1679
1579 /** 1680 /**
1580 * Returns whether the URI has an explicit port. 1681 * Returns whether the URI has an explicit port.
1581 * 1682 *
1582 * If the port number is the default port number 1683 * If the port number is the default port number
1583 * (zero for unrecognized schemes, with http (80) and https (443) being 1684 * (zero for unrecognized schemes, with http (80) and https (443) being
1584 * recognized), 1685 * recognized),
1585 * then the port is made implicit and omitted from the URI. 1686 * then the port is made implicit and omitted from the URI.
1586 */ 1687 */
1587 bool get hasPort => _port != null; 1688 bool get hasPort => _port != null;
1588 1689
1589 /** 1690 /**
1590 * Returns whether the URI has a query part. 1691 * Returns whether the URI has a query part.
1591 */ 1692 */
1592 bool get hasQuery => _query != null; 1693 bool get hasQuery => _query != null;
1593 1694
1594 /** 1695 /**
1595 * Returns whether the URI has a fragment part. 1696 * Returns whether the URI has a fragment part.
1596 */ 1697 */
1597 bool get hasFragment => _fragment != null; 1698 bool get hasFragment => _fragment != null;
1598 1699
1599 /** 1700 /**
1701 * Returns whether the URI has an empty path.
1702 */
1703 bool get hasEmptyPath => _path.isEmpty;
1704
1705 /**
1706 * Returns whether the URI has an absolute path (starting with '/').
1707 */
1708 bool get hasAbsolutePath => _path.startsWith('/');
1709
1710 /**
1600 * Returns the origin of the URI in the form scheme://host:port for the 1711 * Returns the origin of the URI in the form scheme://host:port for the
1601 * schemes http and https. 1712 * schemes http and https.
1602 * 1713 *
1603 * It is an error if the scheme is not "http" or "https". 1714 * It is an error if the scheme is not "http" or "https".
1604 * 1715 *
1605 * See: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin 1716 * See: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin
1606 */ 1717 */
1607 String get origin { 1718 String get origin {
1608 if (scheme == "" || _host == null || _host == "") { 1719 if (scheme == "" || _host == null || _host == "") {
1609 throw new StateError("Cannot use origin without a scheme: $this"); 1720 throw new StateError("Cannot use origin without a scheme: $this");
(...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after
2470 0xafff, // 0x30 - 0x3f 1111111111110101 2581 0xafff, // 0x30 - 0x3f 1111111111110101
2471 // @ABCDEFGHIJKLMNO 2582 // @ABCDEFGHIJKLMNO
2472 0xffff, // 0x40 - 0x4f 1111111111111111 2583 0xffff, // 0x40 - 0x4f 1111111111111111
2473 // PQRSTUVWXYZ _ 2584 // PQRSTUVWXYZ _
2474 0x87ff, // 0x50 - 0x5f 1111111111100001 2585 0x87ff, // 0x50 - 0x5f 1111111111100001
2475 // abcdefghijklmno 2586 // abcdefghijklmno
2476 0xfffe, // 0x60 - 0x6f 0111111111111111 2587 0xfffe, // 0x60 - 0x6f 0111111111111111
2477 // pqrstuvwxyz ~ 2588 // pqrstuvwxyz ~
2478 0x47ff]; // 0x70 - 0x7f 1111111111100010 2589 0x47ff]; // 0x70 - 0x7f 1111111111100010
2479 } 2590 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | tests/corelib/uri_normalize_path_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698