chkr1011 / HTTPnet

HTTPnet is a high performance .NET library for HTTP and WebSocket based communication. It provides a HTTP/WebSocket server and a powerful processing pipeline for HTTP request and their responses.
MIT License
21 stars 6 forks source link

Disposing response body stream #3

Open messani opened 6 years ago

messani commented 6 years ago

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();
}
chkr1011 commented 6 years ago

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