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

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

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

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

Note You need to log in before you can comment on or make changes to this bug.