Skip to content

Commit

Permalink
Fix StreamContent handling in request processing (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Feb 28, 2022
1 parent 9bac68c commit a0ce639
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nanoFramework.System.Net.Http/Http/HttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ HttpResponseMessage CreateResponseMessage(HttpWebResponse wr, HttpRequestMessage
{
RequestMessage = requestMessage,
ReasonPhrase = wr.StatusDescription,
Content = new StreamContent(wr.GetResponseStream())
Content = new StreamContent(wr.GetResponseStream(), (int)wr.ContentLength)
};

var headers = wr.Headers;
Expand Down
22 changes: 18 additions & 4 deletions nanoFramework.System.Net.Http/Http/StreamContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//

using System.IO;
using System.Threading;

namespace System.Net.Http
{
Expand Down Expand Up @@ -50,7 +51,7 @@ public StreamContent(
{
throw new ArgumentOutOfRangeException();
}

_bufferSize = bufferSize;

if (content.CanSeek)
Expand All @@ -76,12 +77,25 @@ protected override void SerializeToStream(Stream stream)
_contentCopied = true;
}

byte[] buffer = new byte[_bufferSize];
// need to read from the stream in batches of 2kB
byte[] buffer = new byte[2048];
int read;
int totalRead = 0;

while ((read = _content.Read(buffer, 0, buffer.Length)) != 0)
while (totalRead < _bufferSize)
{
stream.Write(buffer, 0, read);
read = _content.Read(buffer, 0, buffer.Length);

if (read == 0)
{
// need to let the native layer get more data
Thread.Sleep(10);
}
else
{
totalRead += read;
stream.Write(buffer, 0, read);
}
}
}

Expand Down

0 comments on commit a0ce639

Please sign in to comment.