Bug 44413 - HttpHeaders.TryAddWithoutValidation behaves differently from .NET
Summary: HttpHeaders.TryAddWithoutValidation behaves differently from .NET
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Net.Http ()
Version: 4.4.2 (C7SR1)
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-09-16 11:28 UTC by Robin Wallin
Modified: 2016-09-16 13:50 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Example of behavior (321 bytes, text/plain)
2016-09-16 11:28 UTC, Robin Wallin
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Robin Wallin 2016-09-16 11:28:49 UTC
Created attachment 17535 [details]
Example of behavior

It seems like Mono's HttpHeaders.TryAddWithoutValidation(string, string) method behaves differently from the .NET version when setting an invalid User-Agent (User-Agent with comma not contained within parenthesis). .NET version "accepts" the value where Mono quietly drops it, even though the method call itself returns "true". From Mono REPL:

csharp> LoadAssembly("System.Net.Http")
csharp> using System.Net.Http;
csharp> var client = new HttpClient();
csharp> client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "some,agent")
true                                                                                                                                                
csharp> client.DefaultRequestHeaders.UserAgent
{  }

From VS2015 C# interactive:

> #r "System.Net.Http.dll"
> using System.Net.Http;
> var client = new HttpClient();
> client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "some,agent")
true
> client.DefaultRequestHeaders.UserAgent
[some,agent]

The documentation, https://msdn.microsoft.com/en-us/library/hh875107(v=vs.110).aspx, is kind of sparse on information so I can't tell which behavior is the correct one, but return value of true implies that both name and value were added, in the Mono case only the name seems to be added.

I enclosed a test case, compiling with:

- Mono: mcs -r:"System.Net.Http" Main.cs 
- .NET: csc -r:"System.Net.Http.dll" Main.cs

Expected output:

user,agent/1.0

Actual output:

<empty>

System info:
  - Mono version: Mono JIT compiler version 4.4.2 (Stable 4.4.2.11/f72fe45 Thu Aug 11 06:03:25 BST 2016)
  - OS: OSX 10.11.6
Comment 1 Marek Safar 2016-09-16 13:50:31 UTC
Fixed in master and Mono 4.8