Transfer-Encoding: Chunked - Inaccurate and sometimes incomplete using HttpRequestStreamReader
VAchris opened this issue · comments
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.
This issue was moved to dotnet/aspnetcore#3573