Skip to content

Commit 9d06371

Browse files
author
Frank Robijn
committed
Corrected logic for combining base + relative URI.
1 parent 624ba0a commit 9d06371

File tree

2 files changed

+83
-61
lines changed

2 files changed

+83
-61
lines changed

Tests/HttpUnitTests/UriUnitTests.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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.
63

7-
using nanoFramework.TestFramework;
84
using System;
5+
using nanoFramework.TestFramework;
96

107
namespace HttpUnitTests
118
{
@@ -90,10 +87,12 @@ public void UriCtor_Should_Not_Throw_Exception(string uri)
9087
Assert.IsNotNull(createdUri);
9188
}
9289

93-
[DataRow("http://user:password@www.co5ntoso.com/Home", "Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Index.html?q1=v1&q2=v2#FragmentName")]
94-
[DataRow("http://user:password@www.co5ntoso.com/Home/", "Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Index.html?q1=v1&q2=v2#FragmentName")]
95-
[DataRow("http://user:password@www.co5ntoso.com/Home", "/Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Index.html?q1=v1&q2=v2#FragmentName")]
96-
[DataRow("http://user:password@www.co5ntoso.com/Home/", "/Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Index.html?q1=v1&q2=v2#FragmentName")]
90+
[DataRow("http://user:password@www.co5ntoso.com/Home/Sub?q1=v1&q3=v3#Fragment", "Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Index.html?q1=v1&q2=v2#FragmentName")]
91+
[DataRow("http://user:password@www.co5ntoso.com/Home/Sub", "Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Index.html?q1=v1&q2=v2#FragmentName")]
92+
[DataRow("http://user:password@www.co5ntoso.com/Home/Sub/", "Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Home/Sub/Index.html?q1=v1&q2=v2#FragmentName")]
93+
[DataRow("http://user:password@www.co5ntoso.com/Home/Sub", "/Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Index.html?q1=v1&q2=v2#FragmentName")]
94+
[DataRow("http://user:password@www.co5ntoso.com/Home/Sub/", "/Index.html?q1=v1&q2=v2#FragmentName", "http://user:password@www.co5ntoso.com/Index.html?q1=v1&q2=v2#FragmentName")]
95+
[DataRow("http://user:password@www.co5ntoso.com/Home/Sub/", "http://usr:pwd@www.co6ntoso.com/Index.html?q1=v1&q2=v2#FragmentName", "http://usr:pwd@www.co6ntoso.com/Index.html?q1=v1&q2=v2#FragmentName")]
9796
public void UriWithParamaters(string uri, string paramaters, string correctFullUri)
9897
{
9998
Uri baseUri = new(uri);
@@ -341,7 +340,7 @@ public void UriCtor_Query_Should_Be_Valid(string uriString, string expectedValue
341340
[TestMethod]
342341
[DataRow("https://user:password@www.contoso.com:443/Home/Index.htm?q1=v1&q2=v2#FragmentName", "/Home/Index.htm?q1=v1&q2=v2")]
343342
[DataRow("ftp://user:password@ftp.contoso.com:21/directory/file.txt", "/directory/file.txt")]
344-
[DataRow("\tftp://abc.com ","/")]
343+
[DataRow("\tftp://abc.com ", "/")]
345344
[DataRow("file:////c", "/")]
346345
[DataRow("ldap://[2001:db8::7]/c=GB?objectClass?one", "/c=GB?objectClass?one")]
347346
[DataRow("mailto:user@example.com?subject=Hello&body=World", "?subject=Hello&body=World")]

nanoFramework.System.Net.Http/Http/System.Uri.cs

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
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.
63

74
using System.Diagnostics;
85

@@ -489,42 +486,42 @@ public Uri(string uriString, UriKind kind)
489486
switch (kind)
490487
{
491488
case UriKind.Absolute:
489+
{
490+
if (!ConstructAbsoluteUri(uriString))
492491
{
493-
if (!ConstructAbsoluteUri(uriString))
494-
{
495-
throw new FormatException();
496-
}
497-
break;
492+
throw new FormatException();
498493
}
494+
break;
495+
}
499496

500497
case UriKind.RelativeOrAbsolute:
498+
{
499+
// try first with a absolute
500+
if (ConstructAbsoluteUri(uriString))
501501
{
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-
}
515502
break;
516503
}
517-
518-
// Relative Uri. Store in original string.
519-
case UriKind.Relative:
504+
else
520505
{
521-
// Validates the relative Uri.
506+
// now try with relative
522507
if (!ValidateUriPart(uriString, 0))
523508
{
524509
throw new FormatException();
525510
}
526-
break;
527511
}
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+
}
528525
}
529526

530527
_originalUriString = uriString;
@@ -552,15 +549,41 @@ public Uri(
552549
throw new ArgumentOutOfRangeException();
553550
}
554551

555-
if (!ValidateUriPart(relativeUri, 0))
552+
if (string.IsNullOrEmpty(relativeUri))
553+
{
554+
ConstructAbsoluteUri(baseUri.OriginalString);
555+
}
556+
else
556557
{
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+
}
558586
}
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);
564587
}
565588

566589
/// <summary>
@@ -1335,28 +1358,28 @@ public static bool IsWellFormedUriString(
13351358
switch (uriKind)
13361359
{
13371360
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);
13451363

1346-
return false;
1364+
if (testUri.IsAbsoluteUri)
1365+
{
1366+
return true;
13471367
}
13481368

1369+
return false;
1370+
}
1371+
13491372
case UriKind.Relative:
1373+
{
1374+
Uri testUri = new Uri(uriString, UriKind.Relative);
1375+
if (!testUri.IsAbsoluteUri)
13501376
{
1351-
Uri testUri = new Uri(uriString, UriKind.Relative);
1352-
if (!testUri.IsAbsoluteUri)
1353-
{
1354-
return true;
1355-
}
1356-
1357-
return false;
1377+
return true;
13581378
}
13591379

1380+
return false;
1381+
}
1382+
13601383
default:
13611384
return false;
13621385
}

0 commit comments

Comments
 (0)