HTTPnet icon indicating copy to clipboard operation
HTTPnet copied to clipboard

Disposing response body stream

Open messani opened this issue 7 years ago • 1 comments

In your example files are sent to client this way: context.HttpContext.Response.Body = File.OpenRead(filename);

But Stream returned by function OpenRead is never disposed explicitly. It is not possible to write to the file until Stream is disposed later by garbage collector.

I think that it is possible to dispose it after call to ResponseWriter.WriteAsync in file HttpSessionHandler.cs. Something like this:

try
{
    var httpContext = new HttpContext(httpRequest, httpResponse, _clientSession, this);
    await _clientSession.HandleHttpRequestAsync(httpContext);

    if (httpContext.Response != null)
    {
        await ResponseWriter.WriteAsync(httpContext.Response, _clientSession.CancellationToken);
        HttpNetTrace.Verbose(nameof(HttpSessionHandler), "Response '{0}' sent to '{1}'.", httpContext.Response.StatusCode, _clientSession.Client.Identifier);
    }

    if (httpContext.CloseConnection)
    {
        _clientSession.Close();
    }
}
finally
{
    httpResponse.Body?.Dispose();
}

messani avatar Mar 21 '18 13:03 messani

Hi, thanks for reporting this issue. I wil fix this in the next version. Best regards Christian

chkr1011 avatar Apr 12 '18 22:04 chkr1011