From 0c180c51add128f15e7cd3926102212a7363db9c Mon Sep 17 00:00:00 2001 From: Gonzalo Paniagua Javier Date: Thu, 31 Aug 2006 00:05:22 +0000 Subject: [PATCH] 2006-08-30 Gonzalo Paniagua Javier * HttpConnection.cs: when the content length is available, keep it updated after synchronous read. Renamed 'available' to 'remaining_body'. * RequestStream.cs: when creating the stream, pass the remaining length starting from the offset in the buffer. svn path=/branches/mono-1-1-17/mcs/; revision=64585 --- mcs/class/System/System.Net/ChangeLog | 7 ++++ mcs/class/System/System.Net/HttpConnection.cs | 4 +- mcs/class/System/System.Net/RequestStream.cs | 41 +++++++++++-------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/mcs/class/System/System.Net/ChangeLog b/mcs/class/System/System.Net/ChangeLog index 0807ff3faa34c..e96bca5f5af93 100644 --- a/mcs/class/System/System.Net/ChangeLog +++ b/mcs/class/System/System.Net/ChangeLog @@ -1,3 +1,10 @@ +2006-08-30 Gonzalo Paniagua Javier + + * HttpConnection.cs: when the content length is available, keep it + updated after synchronous read. Renamed 'available' to 'remaining_body'. + * RequestStream.cs: when creating the stream, pass the remaining length + starting from the offset in the buffer. + 2006-08-30 Gonzalo Paniagua Javier * RequestStream.cs: make sure we return 0 when no more bytes left. diff --git a/mcs/class/System/System.Net/HttpConnection.cs b/mcs/class/System/System.Net/HttpConnection.cs index 7069797719500..c776105f15570 100644 --- a/mcs/class/System/System.Net/HttpConnection.cs +++ b/mcs/class/System/System.Net/HttpConnection.cs @@ -108,9 +108,9 @@ public RequestStream GetRequestStream (bool chunked, long contentlength) if (chunked) { this.chunked = true; context.Response.SendChunked = true; - i_stream = new ChunkedInputStream (context, sock, buffer, position, length); + i_stream = new ChunkedInputStream (context, sock, buffer, position, length - position); } else { - i_stream = new RequestStream (sock, buffer, position, length, contentlength); + i_stream = new RequestStream (sock, buffer, position, length - position, contentlength); } } return i_stream; diff --git a/mcs/class/System/System.Net/RequestStream.cs b/mcs/class/System/System.Net/RequestStream.cs index 35a84a309cf36..12d1afad4a061 100644 --- a/mcs/class/System/System.Net/RequestStream.cs +++ b/mcs/class/System/System.Net/RequestStream.cs @@ -35,7 +35,7 @@ class RequestStream : NetworkStream byte [] buffer; int offset; int length; - long available; + long remaining_body; bool disposed; internal RequestStream (Socket sock, byte [] buffer, int offset, int length) : @@ -44,7 +44,7 @@ class RequestStream : NetworkStream this.buffer = buffer; this.offset = offset; this.length = length; - this.available = -1; + this.remaining_body = -1; } internal RequestStream (Socket sock, byte [] buffer, int offset, int length, long contentlength) : @@ -53,7 +53,7 @@ class RequestStream : NetworkStream this.buffer = buffer; this.offset = offset; this.length = length; - this.available = contentlength; + this.remaining_body = contentlength; } public override bool CanRead { @@ -105,20 +105,27 @@ int FillFromBuffer (byte [] buffer, int off, int count) if (off > len - count) throw new ArgumentException ("Reading would overrun buffer"); - if (this.available == 0) + if (this.remaining_body == 0) return -1; if (this.length == 0) return 0; int size = Math.Min (this.length, count); - if (this.available > 0) - size = (int) Math.Min (size, this.available); + if (this.remaining_body > 0) + size = (int) Math.Min (size, this.remaining_body); + + if (this.offset > this.buffer.Length - size) { + size = Math.Min (size, this.buffer.Length - this.offset); + } + if (size == 0) + return 0; + Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size); this.offset += size; this.length -= size; - if (this.available > 0) - available -= size; + if (this.remaining_body > 0) + remaining_body -= size; return size; } @@ -127,15 +134,17 @@ public override int Read ([In,Out] byte[] buffer, int offset, int count) if (disposed) throw new ObjectDisposedException (typeof (RequestStream).ToString ()); - // Avoid reading past the end of the request to allow - // for HTTP pipelining + // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0 int nread = FillFromBuffer (buffer, offset, count); - if (nread == -1) // No more bytes available (Content-Length) + if (nread == -1) { // No more bytes available (Content-Length) return 0; - if (nread > 0) + } else if (nread > 0) { return nread; + } nread = base.Read (buffer, offset, count); + if (nread > 0 && remaining_body > 0) + remaining_body -= nread; return nread; } @@ -160,8 +169,8 @@ public override int Read ([In,Out] byte[] buffer, int offset, int count) // Avoid reading past the end of the request to allow // for HTTP pipelining - if (available != -1 && count > available) - count = (int) Math.Min (Int32.MaxValue, available); + if (remaining_body >= 0 && count > remaining_body) + count = (int) Math.Min (Int32.MaxValue, remaining_body); return base.BeginRead (buffer, offset, count, cback, state); } @@ -182,8 +191,8 @@ public override int EndRead (IAsyncResult ares) // Close on exception? int nread = base.EndRead (ares); - if (available != -1) - available -= nread; + if (remaining_body > 0 && nread > 0) + remaining_body -= nread; return nread; }