Skip to content

Commit

Permalink
2006-08-30 Gonzalo Paniagua Javier <gonzalo@ximian.com>
Browse files Browse the repository at this point in the history
	* 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
  • Loading branch information
gonzalop committed Aug 31, 2006
1 parent 1de2da2 commit 0c180c5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
7 changes: 7 additions & 0 deletions mcs/class/System/System.Net/ChangeLog
@@ -1,3 +1,10 @@
2006-08-30 Gonzalo Paniagua Javier <gonzalo@ximian.com>

* 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 <gonzalo@ximian.com>

* RequestStream.cs: make sure we return 0 when no more bytes left.
Expand Down
4 changes: 2 additions & 2 deletions mcs/class/System/System.Net/HttpConnection.cs
Expand Up @@ -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;
Expand Down
41 changes: 25 additions & 16 deletions mcs/class/System/System.Net/RequestStream.cs
Expand Up @@ -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) :
Expand All @@ -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) :
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 0c180c5

Please sign in to comment.