[System] Don't send response on empty stream write. Fixes #47549
authorMarek Safar <marek.safar@gmail.com>
Wed, 23 Nov 2016 16:40:00 +0000 (17:40 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 23 Nov 2016 16:40:49 +0000 (17:40 +0100)
mcs/class/System/System.Net/ResponseStream.cs
mcs/class/System/Test/System.Net/HttpListenerRequestTest.cs

index 03993e989590fdf086917fa523d314b1dfa6d68d..81e1d6f67d4be3308554d97eb4f01a2ecc97bafa 100644 (file)
@@ -143,6 +143,8 @@ namespace System.Net {
                {
                        if (disposed)
                                throw new ObjectDisposedException (GetType ().ToString ());
+                       if (count == 0)
+                               return;
 
                        byte [] bytes = null;
                        MemoryStream ms = GetHeaders (false);
index 18dedcae32dbe27dbc0f7863ae84c0b22c0b7130..2f24f73ec0d0efc50f2572ae0a8f235ec5c17e2c 100644 (file)
@@ -34,6 +34,7 @@ using System.Net.NetworkInformation;
 using System.Net.Sockets;
 using System.Text;
 using System.Collections.Generic;
+using System.Threading.Tasks;
 
 using NUnit.Framework;
 
@@ -254,12 +255,38 @@ namespace MonoTests.System.Net
                        var request = (HttpWebRequest) WebRequest.Create (rawUrl);
                        request.GetResponseAsync ();
 
-                       if(!contextTask.Wait (1000))
-                               Assert.Fail ("Timeout");
+                       Assert.IsTrue (contextTask.Wait (1000));
 
                        Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);
 
                        listener.Close ();
                }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void EmptyWrite ()
+               {
+                       var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
+
+                       HttpListener listener = new HttpListener ();
+                       listener.Prefixes.Add (prefix);
+                       listener.Start ();
+
+                       Task.Run (() => {
+                               var context = listener.GetContext ();
+
+                               var s = context.Response.OutputStream;
+                               s.Write (new byte[10], 0, 0);
+                               return;
+                       });
+
+                       var request = (HttpWebRequest)WebRequest.Create (prefix);
+                       var rsp = request.GetResponseAsync ();
+                       Assert.IsFalse (rsp.Wait (1000), "Don't send on empty write");
+
+                       listener.Close ();
+               }
        }
 }