|
1 | | -// |
2 | | -// Copyright (c) .NET Foundation and Contributors |
3 | | -// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
4 | | -// See LICENSE file in the project root for full license information. |
5 | | -// |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
6 | 3 |
|
7 | 4 | using System.Diagnostics; |
8 | 5 |
|
@@ -489,42 +486,42 @@ public Uri(string uriString, UriKind kind) |
489 | 486 | switch (kind) |
490 | 487 | { |
491 | 488 | case UriKind.Absolute: |
| 489 | + { |
| 490 | + if (!ConstructAbsoluteUri(uriString)) |
492 | 491 | { |
493 | | - if (!ConstructAbsoluteUri(uriString)) |
494 | | - { |
495 | | - throw new FormatException(); |
496 | | - } |
497 | | - break; |
| 492 | + throw new FormatException(); |
498 | 493 | } |
| 494 | + break; |
| 495 | + } |
499 | 496 |
|
500 | 497 | case UriKind.RelativeOrAbsolute: |
| 498 | + { |
| 499 | + // try first with a absolute |
| 500 | + if (ConstructAbsoluteUri(uriString)) |
501 | 501 | { |
502 | | - // try first with a absolute |
503 | | - if (ConstructAbsoluteUri(uriString)) |
504 | | - { |
505 | | - break; |
506 | | - } |
507 | | - else |
508 | | - { |
509 | | - // now try with relative |
510 | | - if (!ValidateUriPart(uriString, 0)) |
511 | | - { |
512 | | - throw new FormatException(); |
513 | | - } |
514 | | - } |
515 | 502 | break; |
516 | 503 | } |
517 | | - |
518 | | - // Relative Uri. Store in original string. |
519 | | - case UriKind.Relative: |
| 504 | + else |
520 | 505 | { |
521 | | - // Validates the relative Uri. |
| 506 | + // now try with relative |
522 | 507 | if (!ValidateUriPart(uriString, 0)) |
523 | 508 | { |
524 | 509 | throw new FormatException(); |
525 | 510 | } |
526 | | - break; |
527 | 511 | } |
| 512 | + break; |
| 513 | + } |
| 514 | + |
| 515 | + // Relative Uri. Store in original string. |
| 516 | + case UriKind.Relative: |
| 517 | + { |
| 518 | + // Validates the relative Uri. |
| 519 | + if (!ValidateUriPart(uriString, 0)) |
| 520 | + { |
| 521 | + throw new FormatException(); |
| 522 | + } |
| 523 | + break; |
| 524 | + } |
528 | 525 | } |
529 | 526 |
|
530 | 527 | _originalUriString = uriString; |
@@ -552,15 +549,41 @@ public Uri( |
552 | 549 | throw new ArgumentOutOfRangeException(); |
553 | 550 | } |
554 | 551 |
|
555 | | - if (!ValidateUriPart(relativeUri, 0)) |
| 552 | + if (string.IsNullOrEmpty(relativeUri)) |
| 553 | + { |
| 554 | + ConstructAbsoluteUri(baseUri.OriginalString); |
| 555 | + } |
| 556 | + else |
556 | 557 | { |
557 | | - throw new FormatException(); |
| 558 | + // try first with a absolute |
| 559 | + if (!ConstructAbsoluteUri(relativeUri)) |
| 560 | + { |
| 561 | + // now try with relative |
| 562 | + string baseUrl; |
| 563 | + if (relativeUri[0] == '/') |
| 564 | + { |
| 565 | + baseUrl = baseUri.AbsoluteUri.Substring(0, baseUri.AbsoluteUri.Length - baseUri.AbsolutePath.Length - baseUri.Query.Length - baseUri.Fragment.Length); |
| 566 | + } |
| 567 | + else |
| 568 | + { |
| 569 | + baseUrl = baseUri.AbsoluteUri.Substring(0, baseUri.AbsoluteUri.Length - baseUri.Query.Length - baseUri.Fragment.Length); |
| 570 | + if (!baseUrl.EndsWith("/")) |
| 571 | + { |
| 572 | + int idx = baseUrl.LastIndexOf("/"); |
| 573 | + if (idx >= 0) |
| 574 | + { |
| 575 | + baseUrl = baseUrl.Substring(0, idx + 1); |
| 576 | + } |
| 577 | + else |
| 578 | + { |
| 579 | + baseUrl += '/'; |
| 580 | + } |
| 581 | + } |
| 582 | + } |
| 583 | + |
| 584 | + ConstructAbsoluteUri(baseUrl + relativeUri); |
| 585 | + } |
558 | 586 | } |
559 | | - |
560 | | - // We need to have http://myuri/relative and sometimes the / is missing |
561 | | - var baseadj = baseUri.AbsoluteUri.EndsWith("/") ? baseUri.AbsoluteUri : baseUri.AbsoluteUri + "/"; |
562 | | - var relativeadj = relativeUri.StartsWith("/") ? relativeUri.Substring(1) : relativeUri; |
563 | | - ConstructAbsoluteUri(baseadj + relativeadj); |
564 | 587 | } |
565 | 588 |
|
566 | 589 | /// <summary> |
@@ -1335,28 +1358,28 @@ public static bool IsWellFormedUriString( |
1335 | 1358 | switch (uriKind) |
1336 | 1359 | { |
1337 | 1360 | case UriKind.Absolute: |
1338 | | - { |
1339 | | - Uri testUri = new Uri(uriString); |
1340 | | - |
1341 | | - if (testUri.IsAbsoluteUri) |
1342 | | - { |
1343 | | - return true; |
1344 | | - } |
| 1361 | + { |
| 1362 | + Uri testUri = new Uri(uriString); |
1345 | 1363 |
|
1346 | | - return false; |
| 1364 | + if (testUri.IsAbsoluteUri) |
| 1365 | + { |
| 1366 | + return true; |
1347 | 1367 | } |
1348 | 1368 |
|
| 1369 | + return false; |
| 1370 | + } |
| 1371 | + |
1349 | 1372 | case UriKind.Relative: |
| 1373 | + { |
| 1374 | + Uri testUri = new Uri(uriString, UriKind.Relative); |
| 1375 | + if (!testUri.IsAbsoluteUri) |
1350 | 1376 | { |
1351 | | - Uri testUri = new Uri(uriString, UriKind.Relative); |
1352 | | - if (!testUri.IsAbsoluteUri) |
1353 | | - { |
1354 | | - return true; |
1355 | | - } |
1356 | | - |
1357 | | - return false; |
| 1377 | + return true; |
1358 | 1378 | } |
1359 | 1379 |
|
| 1380 | + return false; |
| 1381 | + } |
| 1382 | + |
1360 | 1383 | default: |
1361 | 1384 | return false; |
1362 | 1385 | } |
|
0 commit comments