Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -847,17 +847,20 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr
IEnumerable<string> links;
if (response.Headers.TryGetValues("Link", out links))
{
foreach(string link in links.FirstOrDefault().Split(","))
foreach (string linkHeader in links)
{
Match match = Regex.Match(link, pattern);
if (match.Success)
foreach (string link in linkHeader.Split(","))
{
string url = match.Groups["url"].Value;
string rel = match.Groups["rel"].Value;
if (url != String.Empty && rel != String.Empty && !_relationLink.ContainsKey(rel))
Match match = Regex.Match(link, pattern);
if (match.Success)
{
Uri absoluteUri = new Uri(requestUri, url);
_relationLink.Add(rel, absoluteUri.AbsoluteUri.ToString());
string url = match.Groups["url"].Value;
string rel = match.Groups["rel"].Value;
if (url != String.Empty && rel != String.Empty && !_relationLink.ContainsKey(rel))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if match.Success is true enough to exclude an exception. It seems safer to check String.IsNullOrEmpty() for url and rel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if match.Success

In this instance, it probably is. We are assured a string for both link and pattern so long as we don't mess with the pattern Regex.Match will never except and it true it the 2 indexes will exist in matches.

I agree on the String.IsNullOrEmpty() but the point of this PR is to address the Multiple Link Header issue, not to change this logic. Should that be done in a separate PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc says that there is no null.

Closed.

{
Uri absoluteUri = new Uri(requestUri, url);
_relationLink.Add(rel, absoluteUri.AbsoluteUri.ToString());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,24 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
$result.Output.RelationLink["last"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5"
}

# Test pending support for multiple header capable server on Linux/macOS see issue #4639
It "Validate Invoke-WebRequest returns valid RelationLink property with absolute uris if Multiple Link Headers are present" -Pending:$(!$IsWindows){
$headers = @{
Link =
'<http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=1>; rel="self"',
'<http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=2>; rel="next"',
'<http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5>; rel="last"'
} | ConvertTo-Json -Compress
$headers = [uri]::EscapeDataString($headers)
$uri = "http://localhost:8080/PowerShell?test=response&contenttype=text/plain&output=OK&headers=$headers"
$command = "Invoke-WebRequest -Uri '$uri'"
$result = ExecuteWebCommand -command $command
$result.Output.RelationLink.Count | Should BeExactly 3
$result.Output.RelationLink["self"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=1"
$result.Output.RelationLink["next"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=2"
$result.Output.RelationLink["last"] | Should BeExactly "http://localhost:8080/PowerShell?test=linkheader&maxlinks=5&linknumber=5"
}

It "Validate Invoke-WebRequest quietly ignores invalid Link Headers in RelationLink property: <type>" -TestCases @(
@{ type = "noUrl" }
@{ type = "malformed" }
Expand Down