Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpWebRequest.AddRange generates incorrect header if called twice #113690

Open
bgrainger opened this issue Mar 19, 2025 · 2 comments · May be fixed by #113720
Open

HttpWebRequest.AddRange generates incorrect header if called twice #113690

bgrainger opened this issue Mar 19, 2025 · 2 comments · May be fixed by #113720
Labels
area-System.Net bug good first issue Issue should be easy to implement, good for first-time contributors in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@bgrainger
Copy link
Contributor

Description

While HttpWebRequest is a deprecated API, it is still in use by legacy code being ported from .NET Framework to .NET 9.

Under .NET, the HttpWebRequest.AddRange method fails to generate a valid header if called twice.

Reproduction Steps

Run the following code on .NET (I've tested with 6.0 and later):

var wr = HttpWebRequest.CreateHttp("http://example.com");
wr.AddRange(10, 20);
wr.AddRange(50, 60);
Console.WriteLine(wr.Headers["Range"]);

Expected behavior

It should print:

bytes=10-20,50-60

Actual behavior

It prints:

50-60

Note that this is an invalid header value because it's missing the bytes= range specifier.

Regression?

This works as expected under .NET Framework 4.7.2.

Known Workarounds

As a workaround, construct the full Range: header yourself (and call wr.Headers.Add("Range", "bytes=10-20,50-60");).

Configuration

.NET SDK:
 Version:           10.0.100-preview.2.25164.34
 Commit:            374efa6cf4
 Workload version:  10.0.100-manifests.d1fec061
 MSBuild version:   17.14.0-preview-25155-01+d0cb70a0d

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\10.0.100-preview.2.25164.34\

Also tested with:

{
  "sdk": {
    "version": "6.0.100",
    "rollForward": "latestFeature"
  }
}
.NET SDK (reflecting any global.json):
 Version:   6.0.428
 Commit:    ef6f5ce48c

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.428\

Host:
  Version:      10.0.0-preview.2.25163.2
  Architecture: x64
  Commit:       c81f403737

Other information

In .NET Framework, AddRange https://referencesource.microsoft.com/#System/net/System/Net/HttpWebRequest.cs,02f08fa9123e2ff5 calls SetAddVerified https://referencesource.microsoft.com/#System/net/System/Net/WebHeaderCollection.cs,d91ebc3a42cb6026, which adds the new range value to an ArrayList of ranges.

In .NET, AddRange trims off the range specifier (if there is a current Range value) then overwrites the existing header value:

if (!string.Equals(curRange.Substring(0, curRange.IndexOf('=')), rangeSpecifier, StringComparison.OrdinalIgnoreCase))
{
return false;
}
curRange = string.Empty;
}
curRange += from.ToString();
if (to != null)
{
curRange += "-" + to;
}
_webHeaderCollection[HttpKnownHeaderNames.Range] = curRange;

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 19, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@ManickaP
Copy link
Member

This seems like straightforward fix, would you like to put up a PR for it?

@ManickaP ManickaP added bug and removed untriaged New issue has not been triaged by the area owner labels Mar 19, 2025
@ManickaP ManickaP added this to the 10.0.0 milestone Mar 19, 2025
@ManickaP ManickaP added the good first issue Issue should be easy to implement, good for first-time contributors label Mar 19, 2025
@bgrainger bgrainger linked a pull request Mar 20, 2025 that will close this issue
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Net bug good first issue Issue should be easy to implement, good for first-time contributors in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants