HttpWebRequest.AddRange generates incorrect header if called twice #113690
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
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):
Expected behavior
It should print:
Actual behavior
It prints:
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 callwr.Headers.Add("Range", "bytes=10-20,50-60");
).Configuration
Also tested with:
Other information
In .NET Framework,
AddRange
https://referencesource.microsoft.com/#System/net/System/Net/HttpWebRequest.cs,02f08fa9123e2ff5 callsSetAddVerified
https://referencesource.microsoft.com/#System/net/System/Net/WebHeaderCollection.cs,d91ebc3a42cb6026, which adds the new range value to anArrayList
of ranges.In .NET,
AddRange
trims off the range specifier (if there is a current Range value) then overwrites the existing header value:runtime/src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs
Lines 1543 to 1554 in 300485d
The text was updated successfully, but these errors were encountered: