Don't set status code if response is closed

pull/1065/head
Claus Vium 6 years ago
parent 10a0d6bdba
commit 3fa43a1e08

@ -225,7 +225,7 @@ namespace Emby.Server.Implementations.HttpServer
var httpRes = httpReq.Response; var httpRes = httpReq.Response;
if (httpRes.IsClosed) if (httpRes.OriginalResponse.HasStarted)
{ {
return; return;
} }
@ -595,8 +595,6 @@ namespace Emby.Server.Implementations.HttpServer
} }
finally finally
{ {
// TODO response closes automatically after the handler is done, but some functions rely on knowing if it's closed or not
httpRes.IsClosed = true;
stopWatch.Stop(); stopWatch.Stop();
var elapsed = stopWatch.Elapsed; var elapsed = stopWatch.Elapsed;
if (elapsed.TotalMilliseconds > 500) if (elapsed.TotalMilliseconds > 500)

@ -78,7 +78,7 @@ namespace Emby.Server.Implementations.Services
foreach (var requestFilter in actionContext.RequestFilters) foreach (var requestFilter in actionContext.RequestFilters)
{ {
requestFilter.RequestFilter(request, request.Response, requestDto); requestFilter.RequestFilter(request, request.Response, requestDto);
if (request.Response.IsClosed) if (request.Response.OriginalResponse.HasStarted)
{ {
Task.FromResult<object>(null); Task.FromResult<object>(null);
} }

@ -90,7 +90,10 @@ namespace Emby.Server.Implementations.SocketSharp
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "AcceptWebSocketAsync error"); _logger.LogError(ex, "AcceptWebSocketAsync error");
ctx.Response.StatusCode = 500; if (!ctx.Response.HasStarted)
{
ctx.Response.StatusCode = 500;
}
} }
} }

@ -20,22 +20,19 @@ namespace Emby.Server.Implementations.SocketSharp
public partial class WebSocketSharpRequest : IHttpRequest public partial class WebSocketSharpRequest : IHttpRequest
{ {
private readonly HttpRequest request; private readonly HttpRequest request;
private readonly IResponse response;
public WebSocketSharpRequest(HttpRequest httpContext, HttpResponse response, string operationName, ILogger logger) public WebSocketSharpRequest(HttpRequest httpContext, HttpResponse response, string operationName, ILogger logger)
{ {
this.OperationName = operationName; this.OperationName = operationName;
this.request = httpContext; this.request = httpContext;
this.response = new WebSocketSharpResponse(logger, response, this); this.Response = new WebSocketSharpResponse(logger, response);
// HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes[0]); // HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes[0]);
} }
public HttpRequest HttpRequest => request; public HttpRequest HttpRequest => request;
public IResponse Response => response; public IResponse Response { get; }
public IResponse HttpResponse => response;
public string OperationName { get; set; } public string OperationName { get; set; }

@ -16,38 +16,28 @@ namespace Emby.Server.Implementations.SocketSharp
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly HttpResponse _response; public WebSocketSharpResponse(ILogger logger, HttpResponse response)
public WebSocketSharpResponse(ILogger logger, HttpResponse response, IRequest request)
{ {
_logger = logger; _logger = logger;
this._response = response; OriginalResponse = response;
Items = new Dictionary<string, object>();
Request = request;
} }
public IRequest Request { get; private set; } public HttpResponse OriginalResponse { get; }
public Dictionary<string, object> Items { get; private set; }
public object OriginalResponse => _response;
public int StatusCode public int StatusCode
{ {
get => this._response.StatusCode; get => OriginalResponse.StatusCode;
set => this._response.StatusCode = value; set => OriginalResponse.StatusCode = value;
} }
public string StatusDescription { get; set; } public string StatusDescription { get; set; }
public string ContentType public string ContentType
{ {
get => _response.ContentType; get => OriginalResponse.ContentType;
set => _response.ContentType = value; set => OriginalResponse.ContentType = value;
} }
public IHeaderDictionary Headers => _response.Headers;
public void AddHeader(string name, string value) public void AddHeader(string name, string value)
{ {
if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase)) if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
@ -56,22 +46,15 @@ namespace Emby.Server.Implementations.SocketSharp
return; return;
} }
_response.Headers.Add(name, value); OriginalResponse.Headers.Add(name, value);
}
public string GetHeader(string name)
{
return _response.Headers[name];
} }
public void Redirect(string url) public void Redirect(string url)
{ {
_response.Redirect(url); OriginalResponse.Redirect(url);
} }
public Stream OutputStream => _response.Body; public Stream OutputStream => OriginalResponse.Body;
public bool IsClosed { get; set; }
public bool SendChunked { get; set; } public bool SendChunked { get; set; }

@ -101,7 +101,7 @@ namespace MediaBrowser.Model.Services
public interface IResponse public interface IResponse
{ {
IRequest Request { get; } HttpResponse OriginalResponse { get; }
int StatusCode { get; set; } int StatusCode { get; set; }
@ -111,22 +111,10 @@ namespace MediaBrowser.Model.Services
void AddHeader(string name, string value); void AddHeader(string name, string value);
string GetHeader(string name);
void Redirect(string url); void Redirect(string url);
Stream OutputStream { get; } Stream OutputStream { get; }
/// <summary>
/// Gets a value indicating whether this instance is closed.
/// </summary>
bool IsClosed { get; set; }
//Add Metadata to Response
Dictionary<string, object> Items { get; }
IHeaderDictionary Headers { get; }
Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, IFileSystem fileSystem, IStreamHelper streamHelper, CancellationToken cancellationToken); Task TransmitFile(string path, long offset, long count, FileShareMode fileShareMode, IFileSystem fileSystem, IStreamHelper streamHelper, CancellationToken cancellationToken);
bool SendChunked { get; set; } bool SendChunked { get; set; }

Loading…
Cancel
Save