Giraffe icon indicating copy to clipboard operation
Giraffe copied to clipboard

Never decalre reader with `use` on `ctx.Request.Body`

Open xperiandri opened this issue 2 years ago • 4 comments

this https://github.com/giraffe-fsharp/Giraffe/blob/6a364955f77609e9265c577ddd817c92fefe104c/src/Giraffe/HttpContextExtensions.fs#L197 disposes body, because the reader disposes underlying stream. Which must not be done in any case!

xperiandri avatar Oct 26 '23 18:10 xperiandri

I did not get it, what is the problem with disposing the request body after reading it?

64J0 avatar Oct 26 '23 19:10 64J0

The problem is that Giraffe is not an owner of it. The owner of it is an appropriate ASP.NET core middleware. Giraffe must not dispose the stuff it is not owner of. It leads to unexpected behavior for the consumer.

xperiandri avatar Oct 29 '23 18:10 xperiandri

I see, thanks for the explanation. I agree then!

64J0 avatar Oct 29 '23 18:10 64J0

Looks like the leaveOpen parameter could be specified to allow disposal of the StreamReader without disposing the underlying resource: doc reference

use reader = new StreamReader(ctx.Request.Body, Encoding.UTF8, leaveOpen = true)

Banashek avatar Nov 22 '23 21:11 Banashek