9

We have a site that streams text data over http. It is set up in the following way:

  • It doesn't do any server side buffering
  • Content-Type is text/plain
  • Transfer-Encoding is chunked
  • Compression is disabled

When using plain curl or FireFox, the text gets streamed to the browser to the browser from the first byte. But when using Chrome, no text is displayed until 1024 bytes have been sent. After that, everything show up instantly.

Question: is there a way to disable this buffering behavior?

More info: here is a simple ASP.NET page that demonstrates the behavior:

<%@ language=c# %>

<%
    Response.BufferOutput = false;
    Response.ContentType = "text/plain";

    for (int i=0; i<50; i++)
    {
        Response.Write("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567\r\n");
        System.Threading.Thread.Sleep(1000);
    }
%>

With a web.config that disables compression:

<configuration>
    <system.webServer>
        <urlCompression doStaticCompression="false" doDynamicCompression="false"/>
    </system.webServer>
</configuration>

I also have a live repro running at http://bufferingtest.azurewebsites.net/. Just hit it from both Chrome and FireFox to observe the different behavior.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117

2 Answers2

5

Add X-Content-Type-Options: nosniff to your headers and let me know how it goes for you.

According to Mozilla Docs:

The X-Content-Type-Options response HTTP header is a marker used by the server to indicate that the MIME types advertised in the Content-Type headers should not be changed and be followed. This allows to opt-out of MIME type sniffing, or, in other words, it is a way to say that the webmasters knew what they were doing.

Daniel F
  • 13,684
  • 11
  • 87
  • 116
McFlurriez
  • 513
  • 6
  • 17
4

Try switching your Content-Type from text/plain to application/octet-stream.

Check this out, for further reading:

Do I need Content-Type: application/octet-stream for file download?

This fix worked perfectly for me. I had the same issue you're describing, where sending small text chunks encoded as text/plain; charset=UTF-8 caused a delay in Chrome, but not in Firefox. Changing the type to application/octet resolved the issue on Chrome without effecting FireFox performance.

Community
  • 1
  • 1
  • 2
    text/event-stream works, and allows you to preview the response in Chrome without it trying to download. Not sure if that is a real mime type or not but I've seen it used. – user169771 Sep 08 '16 at 15:32
  • `octet-stream` makes the browser download the data into a file. `event-stream` works like a charm. – Daniel F May 11 '19 at 09:25