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

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

Issue 2626013004: Fix invalid URIs generated using Uri constructor with clever paths. (Closed)
Patch Set: Fix typo, name boolean parameter (address comments). Created 3 years, 11 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 | « no previous file | tests/corelib/uri_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 // Frequently used character codes. 7 // Frequently used character codes.
8 const int _SPACE = 0x20; 8 const int _SPACE = 0x20;
9 const int _PERCENT = 0x25; 9 const int _PERCENT = 0x25;
10 const int _PLUS = 0x2B; 10 const int _PLUS = 0x2B;
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 * 372 *
373 * The host string is case-insensitive. 373 * The host string is case-insensitive.
374 * The returned host name is canonicalized to lower-case 374 * The returned host name is canonicalized to lower-case
375 * with upper-case percent-escapes. 375 * with upper-case percent-escapes.
376 */ 376 */
377 String get host; 377 String get host;
378 378
379 /** 379 /**
380 * Returns the port part of the authority component. 380 * Returns the port part of the authority component.
381 * 381 *
382 * Returns the defualt port if there is no port number in the authority 382 * Returns the default port if there is no port number in the authority
383 * component. That's 80 for http, 443 for https, and 0 for everything else. 383 * component. That's 80 for http, 443 for https, and 0 for everything else.
384 */ 384 */
385 int get port; 385 int get port;
386 386
387 /** 387 /**
388 * Returns the path component. 388 * Returns the path component.
389 * 389 *
390 * The returned path is encoded. To get direct access to the decoded 390 * The returned path is encoded. To get direct access to the decoded
391 * path use [pathSegments]. 391 * path use [pathSegments].
392 * 392 *
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 498
499 /** 499 /**
500 * Returns whether the URI has an absolute path (starting with '/'). 500 * Returns whether the URI has an absolute path (starting with '/').
501 */ 501 */
502 bool get hasAbsolutePath; 502 bool get hasAbsolutePath;
503 503
504 /** 504 /**
505 * Returns the origin of the URI in the form scheme://host:port for the 505 * Returns the origin of the URI in the form scheme://host:port for the
506 * schemes http and https. 506 * schemes http and https.
507 * 507 *
508 * It is an error if the scheme is not "http" or "https". 508 * It is an error if the scheme is not "http" or "https", or if the host name
509 * is missing or empty.
509 * 510 *
510 * See: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin 511 * See: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin
511 */ 512 */
512 String get origin; 513 String get origin;
513 514
514 /** 515 /**
515 * Returns the file path from a file URI. 516 * Returns the file path from a file URI.
516 * 517 *
517 * The returned path has either Windows or non-Windows 518 * The returned path has either Windows or non-Windows
518 * semantics. 519 * semantics.
(...skipping 944 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 port = _makePort(port, scheme); 1464 port = _makePort(port, scheme);
1464 bool isFile = (scheme == "file"); 1465 bool isFile = (scheme == "file");
1465 if (host == null && 1466 if (host == null &&
1466 (userInfo.isNotEmpty || port != null || isFile)) { 1467 (userInfo.isNotEmpty || port != null || isFile)) {
1467 host = ""; 1468 host = "";
1468 } 1469 }
1469 bool hasAuthority = (host != null); 1470 bool hasAuthority = (host != null);
1470 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments, 1471 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments,
1471 scheme, hasAuthority); 1472 scheme, hasAuthority);
1472 if (scheme.isEmpty && host == null && !path.startsWith('/')) { 1473 if (scheme.isEmpty && host == null && !path.startsWith('/')) {
1473 path = _normalizeRelativePath(path, scheme.isNotEmpty || host != null); 1474 bool allowScheme = scheme.isNotEmpty || host != null;
1475 path = _normalizeRelativePath(path, allowScheme);
1474 } else { 1476 } else {
1475 path = _removeDotSegments(path); 1477 path = _removeDotSegments(path);
1476 } 1478 }
1477 if (host == null && path.startsWith("//")) { 1479 if (host == null && path.startsWith("//")) {
1478 host = ""; 1480 host = "";
1479 } 1481 }
1480 return new _Uri._internal(scheme, userInfo, host, port, 1482 return new _Uri._internal(scheme, userInfo, host, port,
1481 path, query, fragment); 1483 path, query, fragment);
1482 } 1484 }
1483 1485
(...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after
2513 2515
2514 bool get hasQuery => _query != null; 2516 bool get hasQuery => _query != null;
2515 2517
2516 bool get hasFragment => _fragment != null; 2518 bool get hasFragment => _fragment != null;
2517 2519
2518 bool get hasEmptyPath => _path.isEmpty; 2520 bool get hasEmptyPath => _path.isEmpty;
2519 2521
2520 bool get hasAbsolutePath => _path.startsWith('/'); 2522 bool get hasAbsolutePath => _path.startsWith('/');
2521 2523
2522 String get origin { 2524 String get origin {
2523 if (scheme == "" || _host == null || _host == "") { 2525 if (scheme == "") {
2524 throw new StateError("Cannot use origin without a scheme: $this"); 2526 throw new StateError("Cannot use origin without a scheme: $this");
2525 } 2527 }
2526 if (scheme != "http" && scheme != "https") { 2528 if (scheme != "http" && scheme != "https") {
2527 throw new StateError( 2529 throw new StateError(
2528 "Origin is only applicable schemes http and https: $this"); 2530 "Origin is only applicable schemes http and https: $this");
2529 } 2531 }
2532 if (_host == null || _host == "") {
2533 throw new StateError(
2534 "A $scheme: URI should have a non-empty host name: $this");
2535 }
2530 if (_port == null) return "$scheme://$_host"; 2536 if (_port == null) return "$scheme://$_host";
2531 return "$scheme://$_host:$_port"; 2537 return "$scheme://$_host:$_port";
2532 } 2538 }
2533 2539
2534 String toFilePath({bool windows}) { 2540 String toFilePath({bool windows}) {
2535 if (scheme != "" && scheme != "file") { 2541 if (scheme != "" && scheme != "file") {
2536 throw new UnsupportedError( 2542 throw new UnsupportedError(
2537 "Cannot extract a file path from a $scheme URI"); 2543 "Cannot extract a file path from a $scheme URI");
2538 } 2544 }
2539 if (query != "") { 2545 if (query != "") {
(...skipping 1563 matching lines...) Expand 10 before | Expand all | Expand 10 after
4103 } 4109 }
4104 String get path =>_uri.substring(_pathStart, _queryStart); 4110 String get path =>_uri.substring(_pathStart, _queryStart);
4105 String get query => (_queryStart < _fragmentStart) ? 4111 String get query => (_queryStart < _fragmentStart) ?
4106 _uri.substring(_queryStart + 1, _fragmentStart) : ""; 4112 _uri.substring(_queryStart + 1, _fragmentStart) : "";
4107 String get fragment => (_fragmentStart < _uri.length) ? 4113 String get fragment => (_fragmentStart < _uri.length) ?
4108 _uri.substring(_fragmentStart + 1) : ""; 4114 _uri.substring(_fragmentStart + 1) : "";
4109 4115
4110 String get origin { 4116 String get origin {
4111 // Check original behavior - W3C spec is wonky! 4117 // Check original behavior - W3C spec is wonky!
4112 bool isHttp = _isHttp; 4118 bool isHttp = _isHttp;
4113 if (_schemeEnd < 0 || _hostStart == _portStart) { 4119 if (_schemeEnd < 0) {
4114 throw new StateError("Cannot use origin without a scheme: $this"); 4120 throw new StateError("Cannot use origin without a scheme: $this");
4115 } 4121 }
4116 if (!isHttp && !_isHttps) { 4122 if (!isHttp && !_isHttps) {
4117 throw new StateError( 4123 throw new StateError(
4118 "Origin is only applicable schemes http and https: $this"); 4124 "Origin is only applicable schemes http and https: $this");
4119 } 4125 }
4126 if (_hostStart == _portStart) {
4127 throw new StateError(
4128 "A $scheme: URI should have a non-empty host name: $this");
4129 }
4120 if (_hostStart == _schemeEnd + 3) { 4130 if (_hostStart == _schemeEnd + 3) {
4121 return _uri.substring(0, _pathStart); 4131 return _uri.substring(0, _pathStart);
4122 } 4132 }
4123 // Need to drop anon-empty userInfo. 4133 // Need to drop anon-empty userInfo.
4124 return _uri.substring(0, _schemeEnd + 3) + 4134 return _uri.substring(0, _schemeEnd + 3) +
4125 _uri.substring(_hostStart, _pathStart); 4135 _uri.substring(_hostStart, _pathStart);
4126 } 4136 }
4127 4137
4128 List<String> get pathSegments { 4138 List<String> get pathSegments {
4129 int start = _pathStart; 4139 int start = _pathStart;
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
4496 int delta = (text.codeUnitAt(start + 4) ^ _COLON) * 3; 4506 int delta = (text.codeUnitAt(start + 4) ^ _COLON) * 3;
4497 delta |= text.codeUnitAt(start) ^ 0x64 /*d*/; 4507 delta |= text.codeUnitAt(start) ^ 0x64 /*d*/;
4498 delta |= text.codeUnitAt(start + 1) ^ 0x61 /*a*/; 4508 delta |= text.codeUnitAt(start + 1) ^ 0x61 /*a*/;
4499 delta |= text.codeUnitAt(start + 2) ^ 0x74 /*t*/; 4509 delta |= text.codeUnitAt(start + 2) ^ 0x74 /*t*/;
4500 delta |= text.codeUnitAt(start + 3) ^ 0x61 /*a*/; 4510 delta |= text.codeUnitAt(start + 3) ^ 0x61 /*a*/;
4501 return delta; 4511 return delta;
4502 } 4512 }
4503 4513
4504 /// Helper function returning the length of a string, or `0` for `null`. 4514 /// Helper function returning the length of a string, or `0` for `null`.
4505 int _stringOrNullLength(String s) => (s == null) ? 0 : s.length; 4515 int _stringOrNullLength(String s) => (s == null) ? 0 : s.length;
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/uri_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698