diff options
3 files changed, 73 insertions, 1 deletions
diff --git a/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs b/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs index abb05921042..05b5fe62681 100644 --- a/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs +++ b/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs @@ -363,6 +363,10 @@ namespace System.Net.Http.Headers first = false; } + // Return null for empty values list + if (first) + return null; + return sb.ToString (); } diff --git a/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs b/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs index ccf9a808c25..087e62474d2 100644 --- a/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs +++ b/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs @@ -30,6 +30,7 @@ using System.Threading; using System.Threading.Tasks; using System.Collections.Specialized; using System.Net.Http.Headers; +using System.Linq; namespace System.Net.Http { @@ -274,7 +275,18 @@ namespace System.Net.Http // Add request headers var headers = wr.Headers; foreach (var header in request.Headers) { - headers.AddValue (header.Key, HttpRequestHeaders.GetSingleHeaderString (header.Key, header.Value)); + var values = header.Value; + if (header.Key == "Transfer-Encoding") { + // Chunked Transfer-Encoding is never set for HttpWebRequest. It's detected + // from ContentLength by HttpWebRequest + values = values.Where (l => l != "chunked"); + } + + var values_formated = HttpRequestHeaders.GetSingleHeaderString (header.Key, values); + if (values_formated == null) + continue; + + headers.AddValue (header.Key, values_formated); } return wr; diff --git a/mcs/class/System.Net.Http/Test/System.Net.Http/HttpClientTest.cs b/mcs/class/System.Net.Http/Test/System.Net.Http/HttpClientTest.cs index a6c69de2bb9..ec049a93210 100644 --- a/mcs/class/System.Net.Http/Test/System.Net.Http/HttpClientTest.cs +++ b/mcs/class/System.Net.Http/Test/System.Net.Http/HttpClientTest.cs @@ -606,6 +606,62 @@ namespace MonoTests.System.Net.Http } } + [Test] + public void Send_Transfer_Encoding_Chunked () + { + bool? failed = null; + + var listener = CreateListener (l => { + var request = l.Request; + + try { + Assert.AreEqual (1, request.Headers.Count, "#1"); + failed = false; + } catch { + failed = true; + } + }); + + try { + var client = new HttpClient (); + client.DefaultRequestHeaders.TransferEncodingChunked = true; + + client.GetAsync (LocalServer).Wait (); + + Assert.AreEqual (false, failed, "#102"); + } finally { + listener.Abort (); + listener.Close (); + } + } + + [Test] + public void Send_Transfer_Encoding_Custom () + { + bool? failed = null; + + var listener = CreateListener (l => { + failed = true; + }); + + try { + var client = new HttpClient (); + client.DefaultRequestHeaders.TransferEncoding.Add (new TransferCodingHeaderValue ("chunked2")); + + var request = new HttpRequestMessage (HttpMethod.Get, LocalServer); + + try { + client.SendAsync (request, HttpCompletionOption.ResponseHeadersRead).Wait (); + Assert.Fail ("#1"); + } catch (AggregateException e) { + Assert.AreEqual (typeof (ProtocolViolationException), e.InnerException.GetType (), "#2"); + } + Assert.IsNull (failed, "#102"); + } finally { + listener.Abort (); + listener.Close (); + } + } [Test] public void Send_Complete_Content () |