HttpAbstractions icon indicating copy to clipboard operation
HttpAbstractions copied to clipboard

Transfer-Encoding: Chunked - Inaccurate and sometimes incomplete using HttpRequestStreamReader

Open VAchris opened this issue 8 years ago • 1 comments

https://github.com/aspnet/HttpAbstractions/blob/bfa183747f6fb528087554c3d6ec58ef05f1c10a/src/Microsoft.AspNetCore.WebUtilities/HttpRequestStreamReader.cs#L322

Ok, so lets set the stage. I have an ApiController that accepts a POST request. I will always expect the POST to include the http headers Transfer-Encoding: Chunked and Content-Type: application/json. The POST body will typically contain a large JSON file that is sent from a central gateway (similar to a DMZ) that sits between the LAN and WAN.

The central gateway doesn't store and forward; it just forwards using chunked transfer encoding to my receiving ApiController which writes the POST body to a file.

Issue: How do I detect the true EOF when chunked transfer encoding is used?

What I've tried:

var streamReader = new HttpRequestStreamReader(Request.Body, Encoding.UTF8); if(streamReader.Peek() >= 0) { ... } However there are times when .Peek() will return -1 even though only a small portion of the POST body transfer is complete.

I've also tried a do while loop using .Read() however I get similar results to .Peek()

var postBody = streamReader.ReadToEnd(); This works however I can't and shouldn't read the entire contents; I need to be able to stream the POST body and immediately write to a file, especially for large POSTs.

Interestingly: .Peek() and .Read() consistently both return -1 after 8,192 bytes have been read from the stream.

VAchris avatar Aug 15 '18 11:08 VAchris

This issue was moved to aspnet/Home#3573

Tratcher avatar Oct 01 '18 16:10 Tratcher