internal Interop.HttpApi.HTTP_FLAGS ComputeHeaders()
{
Interop.HttpApi.HTTP_FLAGS flags = Interop.HttpApi.HTTP_FLAGS.NONE;
if (NetEventSource.IsEnabled) NetEventSource.Info(this);
Debug.Assert(!ComputedHeaders, "ComputedHeaders is true.");
_responseState = ResponseState.ComputedHeaders;
ComputeCoreHeaders();
if (NetEventSource.IsEnabled) NetEventSource.Info(this,
$"flags: {flags} _boundaryType: {_boundaryType} _contentLength: {_contentLength} _keepAlive: {_keepAlive}");
if (_boundaryType == BoundaryType.None)
{
if (HttpListenerRequest.ProtocolVersion.Minor == 0)
{
_keepAlive = false;
}
else
{
_boundaryType = BoundaryType.Chunked;
}
if (CanSendResponseBody(_httpContext.Response.StatusCode))
{
_contentLength = -1;
}
else
{
ContentLength64 = 0;
}
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"flags:{flags} _BoundaryType:{_boundaryType} _contentLength:{_contentLength} _keepAlive: {_keepAlive}");
if (_boundaryType == BoundaryType.ContentLength)
{
Headers[HttpResponseHeader.ContentLength] = _contentLength.ToString("D", NumberFormatInfo.InvariantInfo);
if (_contentLength == 0)
{
flags = Interop.HttpApi.HTTP_FLAGS.NONE;
}
}
else if (_boundaryType == BoundaryType.Chunked)
{
Headers[HttpResponseHeader.TransferEncoding] = "chunked";
}
else if (_boundaryType == BoundaryType.None)
{
flags = Interop.HttpApi.HTTP_FLAGS.NONE; // seems like HTTP_SEND_RESPONSE_FLAG_MORE_DATA but this hangs the app;
}
else
{
_keepAlive = false;
}
if (!_keepAlive)
{
Headers.Add(HttpResponseHeader.Connection, "close");
if (flags == Interop.HttpApi.HTTP_FLAGS.NONE)
{
flags = Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_DISCONNECT;
}
}
else
{
if (HttpListenerRequest.ProtocolVersion.Minor == 0)
{
Headers[HttpResponseHeader.KeepAlive] = "true";
}
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"flags:{flags} _BoundaryType:{_boundaryType} _contentLength:{_contentLength} _keepAlive: {_keepAlive}");
return flags;
}