4

What is the correct way to handle Content-Length when writing body content in an Owin middleware?

Currently we're using

context.Response.Write(data); // data is a string.

In most cases it works, but in some (IIS host on Win 2012 R2) the Content-Length gets a value of 0.

I thought that setting the Content-Length header, or the option to use Transfer-Encoding: chuncked was up to the host to handle, but it looks like I'm missing something.

What is the correct way to handle the Content-Length when writing body content to an OWIN Response stream?

We've also discussed the bug in a github issue.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • It is primarily a host concern. The application may choose to set content-length if it knows exactly what it will be, but otherwise the app should let the server choose to set content-length or chunked. – Tratcher Aug 22 '15 at 14:32
  • It's not clear what would cause content-length to be set to 0. Perhaps there's an exception after the write and the response gets reset? But in that case you should be getting a 500. Try running it under the debugger and see if there are any first chance exceptions. – Tratcher Aug 22 '15 at 14:35

1 Answers1

0

It is primarily a host concern. The application may choose to set content-length if it knows exactly what it will be, but otherwise the app should let the server choose to set content-length or chunked.

Tratcher
  • 5,929
  • 34
  • 44