Bug 44413

Summary: HttpHeaders.TryAddWithoutValidation behaves differently from .NET
Product: [Mono] Class Libraries Reporter: Robin Wallin <robin>
Component: System.Net.HttpAssignee: Marek Safar <masafa>
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: 4.4.2 (C7SR1)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Example of behavior

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")
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")
> client.DefaultRequestHeaders.UserAgent

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:


Actual output:


System info:
  - Mono version: Mono JIT compiler version 4.4.2 (Stable 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