diff options
author | Gert Driesen <drieseng@users.sourceforge.net> | 2009-06-21 23:09:27 +0400 |
---|---|---|
committer | Gert Driesen <drieseng@users.sourceforge.net> | 2009-06-21 23:09:27 +0400 |
commit | e9ff61a03b7f9561f5a6d28fe129ce1dd33c6328 (patch) | |
tree | 8c356bf91b85ab28e9aef5db03255b85ee299f6d /mcs/class/System | |
parent | 2f6c7d8077b3d07eb3feccda165f1a3fdfbb8202 (diff) |
* HttpWebRequestTest.cs: Improved and enabled test for bug #510642.
* WebConnectionStream.cs: Keep count of all bytes written, and perform
write overflow check in both buffered and non-buffered writing (if
not using chunked transfer encoding). Fixes bug #510642.
svn path=/trunk/mcs/; revision=136570
Diffstat (limited to 'mcs/class/System')
-rw-r--r-- | mcs/class/System/System.Net/ChangeLog | 6 | ||||
-rw-r--r-- | mcs/class/System/System.Net/WebConnectionStream.cs | 34 | ||||
-rw-r--r-- | mcs/class/System/Test/System.Net/ChangeLog | 4 | ||||
-rw-r--r-- | mcs/class/System/Test/System.Net/HttpWebRequestTest.cs | 179 |
4 files changed, 199 insertions, 24 deletions
diff --git a/mcs/class/System/System.Net/ChangeLog b/mcs/class/System/System.Net/ChangeLog index d071b5670b8..fbf34a9801e 100644 --- a/mcs/class/System/System.Net/ChangeLog +++ b/mcs/class/System/System.Net/ChangeLog @@ -1,5 +1,11 @@ 2009-06-20 Gert Driesen <drieseng@users.sourceforge.net> + * WebConnectionStream.cs: Keep count of all bytes written, and perform + write overflow check in both buffered and non-buffered writing (if + not using chunked transfer encoding). Fixes bug #510642. + +2009-06-20 Gert Driesen <drieseng@users.sourceforge.net> + * WebClient.cs: Use lowercase hex bytes to match MS. Avoid using zero-length string literal. Fixed exception argument name to match MS. diff --git a/mcs/class/System/System.Net/WebConnectionStream.cs b/mcs/class/System/System.Net/WebConnectionStream.cs index ebc168c5296..c5bad047d35 100644 --- a/mcs/class/System/System.Net/WebConnectionStream.cs +++ b/mcs/class/System/System.Net/WebConnectionStream.cs @@ -46,6 +46,7 @@ namespace System.Net int readBufferSize; int contentLength; int totalRead; + long totalWritten; bool nextReadCalled; int pendingReads; int pendingWrites; @@ -61,7 +62,6 @@ namespace System.Net bool initRead; bool read_eof; bool complete_request_written; - long max_buffer_size; int read_timeout; int write_timeout; @@ -100,13 +100,8 @@ namespace System.Net this.request = request; allowBuffering = request.InternalAllowBuffering; sendChunked = request.SendChunked; - if (allowBuffering) { + if (allowBuffering) writeBuffer = new MemoryStream (); - max_buffer_size = request.ContentLength; - } else { - max_buffer_size = -1; - } - if (sendChunked) pending = new ManualResetEvent (true); } @@ -452,17 +447,11 @@ namespace System.Net } WebAsyncResult result = new WebAsyncResult (cb, state); + if (!sendChunked) + CheckWriteOverflow (request.ContentLength, totalWritten, size); if (allowBuffering) { - if (max_buffer_size >= 0) { - long avail = max_buffer_size - writeBuffer.Length; - if (size > avail) { - if (requestWritten) - throw new ProtocolViolationException ( - "The number of bytes to be written is greater than " + - "the specified ContentLength."); - } - } writeBuffer.Write (buffer, offset, size); + totalWritten += size; if (!sendChunked) { result.SetCompleted (true, 0); result.DoCallback (); @@ -491,9 +480,22 @@ namespace System.Net } result.InnerAsyncResult = cnc.BeginWrite (buffer, offset, size, callback, result); + totalWritten += size; return result; } + static void CheckWriteOverflow (long contentLength, long totalWritten, long size) + { + if (contentLength == -1) + return; + + long avail = contentLength - totalWritten; + if (size > avail) + throw new ProtocolViolationException ( + "The number of bytes to be written is greater than " + + "the specified ContentLength."); + } + public override void EndWrite (IAsyncResult r) { if (r == null) diff --git a/mcs/class/System/Test/System.Net/ChangeLog b/mcs/class/System/Test/System.Net/ChangeLog index a857c66ed51..1ca61e686d0 100644 --- a/mcs/class/System/Test/System.Net/ChangeLog +++ b/mcs/class/System/Test/System.Net/ChangeLog @@ -1,5 +1,9 @@ 2009-06-20 Gert Driesen <drieseng@users.sourceforge.net> + * HttpWebRequestTest.cs: Improved and enabled test for bug #510642. + +2009-06-20 Gert Driesen <drieseng@users.sourceforge.net> + * SocketResponder.cs: Made a little more robust. 2009-06-20 Gert Driesen <drieseng@users.sourceforge.net> diff --git a/mcs/class/System/Test/System.Net/HttpWebRequestTest.cs b/mcs/class/System/Test/System.Net/HttpWebRequestTest.cs index dfab0a31e6f..704914741b0 100644 --- a/mcs/class/System/Test/System.Net/HttpWebRequestTest.cs +++ b/mcs/class/System/Test/System.Net/HttpWebRequestTest.cs @@ -612,36 +612,199 @@ namespace MonoTests.System.Net } [Test] // bug #510642 - [Category ("NotWorking")] public void GetRequestStream_Write_Overflow () { IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 8001); string url = "http://" + IPAddress.Loopback.ToString () + ":8001/test/"; + // buffered, non-chunked using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) { responder.Start (); - HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); + HttpWebRequest req; + Stream rs; + byte [] buffer; + + req = (HttpWebRequest) WebRequest.Create (url); req.ProtocolVersion = HttpVersion.Version11; req.Method = "POST"; - req.Timeout = 200; + req.Timeout = 1000; req.ReadWriteTimeout = 100; req.ContentLength = 2; - Stream rs = req.GetRequestStream (); + rs = req.GetRequestStream (); + rs.WriteByte (0x2c); - byte [] buffer = new byte [] { 0x2a, 0x2c, 0x1d }; + buffer = new byte [] { 0x2a, 0x1d }; try { - rs.Write (buffer, 0, 3); - Assert.Fail ("#1"); + rs.Write (buffer, 0, buffer.Length); + Assert.Fail ("#A1"); + } catch (ProtocolViolationException ex) { + // Bytes to be written to the stream exceed + // Content-Length bytes size specified + Assert.IsNull (ex.InnerException, "#A2"); + Assert.IsNotNull (ex.Message, "#A3"); + } finally { + req.Abort (); + } + + req = (HttpWebRequest) WebRequest.Create (url); + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + + buffer = new byte [] { 0x2a, 0x2c, 0x1d }; + try { + rs.Write (buffer, 0, buffer.Length); + Assert.Fail ("#B1"); + } catch (ProtocolViolationException ex) { + // Bytes to be written to the stream exceed + // Content-Length bytes size specified + Assert.IsNull (ex.InnerException, "#B2"); + Assert.IsNotNull (ex.Message, "#B3"); + } finally { + req.Abort (); + } + } + + // buffered, chunked + using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) { + responder.Start (); + + HttpWebRequest req; + Stream rs; + byte [] buffer; + + /* + req = (HttpWebRequest) WebRequest.Create (url); + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.SendChunked = true; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + rs.WriteByte (0x2c); + + buffer = new byte [] { 0x2a, 0x1d }; + rs.Write (buffer, 0, buffer.Length); + req.Abort (); + */ + + req = (HttpWebRequest) WebRequest.Create (url); + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.SendChunked = true; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + + buffer = new byte [] { 0x2a, 0x2c, 0x1d }; + rs.Write (buffer, 0, buffer.Length); + req.Abort (); + } + + // non-buffered, non-chunked + using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) { + responder.Start (); + + HttpWebRequest req; + Stream rs; + byte [] buffer; + + req = (HttpWebRequest) WebRequest.Create (url); + req.AllowWriteStreamBuffering = false; + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + rs.WriteByte (0x2c); + + buffer = new byte [] { 0x2a, 0x1d }; + try { + rs.Write (buffer, 0, buffer.Length); + Assert.Fail ("#C1"); } catch (ProtocolViolationException ex) { // Bytes to be written to the stream exceed // Content-Length bytes size specified - Assert.IsNull (ex.InnerException, "#2"); + Assert.IsNull (ex.InnerException, "#C2"); Assert.IsNotNull (ex.Message, "#3"); } finally { req.Abort (); } + + req = (HttpWebRequest) WebRequest.Create (url); + req.AllowWriteStreamBuffering = false; + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + + buffer = new byte [] { 0x2a, 0x2c, 0x1d }; + try { + rs.Write (buffer, 0, buffer.Length); + Assert.Fail ("#D1"); + } catch (ProtocolViolationException ex) { + // Bytes to be written to the stream exceed + // Content-Length bytes size specified + Assert.IsNull (ex.InnerException, "#D2"); + Assert.IsNotNull (ex.Message, "#D3"); + } finally { + req.Abort (); + } + } + + // non-buffered, chunked + using (SocketResponder responder = new SocketResponder (ep, new SocketRequestHandler (EchoRequestHandler))) { + responder.Start (); + + HttpWebRequest req; + Stream rs; + byte [] buffer; + + req = (HttpWebRequest) WebRequest.Create (url); + req.AllowWriteStreamBuffering = false; + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.SendChunked = true; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + rs.WriteByte (0x2c); + + buffer = new byte [] { 0x2a, 0x1d }; + rs.Write (buffer, 0, buffer.Length); + req.Abort (); + + req = (HttpWebRequest) WebRequest.Create (url); + req.AllowWriteStreamBuffering = false; + req.ProtocolVersion = HttpVersion.Version11; + req.Method = "POST"; + req.SendChunked = true; + req.Timeout = 1000; + req.ReadWriteTimeout = 100; + req.ContentLength = 2; + + rs = req.GetRequestStream (); + + buffer = new byte [] { 0x2a, 0x2c, 0x1d }; + rs.Write (buffer, 0, buffer.Length); + req.Abort (); } } |