Merge pull request #1942 from MediaBrowser/dev

Dev
This commit is contained in:
Luke 2016-07-14 23:58:07 -04:00 committed by GitHub
commit 7051b6905b
4 changed files with 58 additions and 40 deletions

View File

@ -335,7 +335,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// <param name="httpReq">The HTTP req.</param> /// <param name="httpReq">The HTTP req.</param>
/// <param name="url">The URL.</param> /// <param name="url">The URL.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
protected Task RequestHandler(IHttpRequest httpReq, Uri url) protected async Task RequestHandler(IHttpRequest httpReq, Uri url)
{ {
var date = DateTime.Now; var date = DateTime.Now;
@ -345,7 +345,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
httpRes.StatusCode = 503; httpRes.StatusCode = 503;
httpRes.Close(); httpRes.Close();
return Task.FromResult(true); return ;
} }
var operationName = httpReq.OperationName; var operationName = httpReq.OperationName;
@ -365,13 +365,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase)) string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase))
{ {
httpRes.RedirectToUrl(DefaultRedirectPath); httpRes.RedirectToUrl(DefaultRedirectPath);
return Task.FromResult(true); return;
} }
if (string.Equals(localPath, "/emby", StringComparison.OrdinalIgnoreCase) || if (string.Equals(localPath, "/emby", StringComparison.OrdinalIgnoreCase) ||
string.Equals(localPath, "/mediabrowser", StringComparison.OrdinalIgnoreCase)) string.Equals(localPath, "/mediabrowser", StringComparison.OrdinalIgnoreCase))
{ {
httpRes.RedirectToUrl("emby/" + DefaultRedirectPath); httpRes.RedirectToUrl("emby/" + DefaultRedirectPath);
return Task.FromResult(true); return;
} }
if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase) || if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase) ||
@ -389,35 +389,35 @@ namespace MediaBrowser.Server.Implementations.HttpServer
httpRes.Write("<!doctype html><html><head><title>Emby</title></head><body>Please update your Emby bookmark to <a href=\"" + newUrl + "\">" + newUrl + "</a></body></html>"); httpRes.Write("<!doctype html><html><head><title>Emby</title></head><body>Please update your Emby bookmark to <a href=\"" + newUrl + "\">" + newUrl + "</a></body></html>");
httpRes.Close(); httpRes.Close();
return Task.FromResult(true); return;
} }
} }
if (string.Equals(localPath, "/web", StringComparison.OrdinalIgnoreCase)) if (string.Equals(localPath, "/web", StringComparison.OrdinalIgnoreCase))
{ {
httpRes.RedirectToUrl(DefaultRedirectPath); httpRes.RedirectToUrl(DefaultRedirectPath);
return Task.FromResult(true); return;
} }
if (string.Equals(localPath, "/web/", StringComparison.OrdinalIgnoreCase)) if (string.Equals(localPath, "/web/", StringComparison.OrdinalIgnoreCase))
{ {
httpRes.RedirectToUrl("../" + DefaultRedirectPath); httpRes.RedirectToUrl("../" + DefaultRedirectPath);
return Task.FromResult(true); return;
} }
if (string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase)) if (string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase))
{ {
httpRes.RedirectToUrl(DefaultRedirectPath); httpRes.RedirectToUrl(DefaultRedirectPath);
return Task.FromResult(true); return;
} }
if (string.IsNullOrEmpty(localPath)) if (string.IsNullOrEmpty(localPath))
{ {
httpRes.RedirectToUrl("/" + DefaultRedirectPath); httpRes.RedirectToUrl("/" + DefaultRedirectPath);
return Task.FromResult(true); return;
} }
if (string.Equals(localPath, "/emby/pin", StringComparison.OrdinalIgnoreCase)) if (string.Equals(localPath, "/emby/pin", StringComparison.OrdinalIgnoreCase))
{ {
httpRes.RedirectToUrl("web/pin.html"); httpRes.RedirectToUrl("web/pin.html");
return Task.FromResult(true); return;
} }
if (!string.IsNullOrWhiteSpace(GlobalResponse)) if (!string.IsNullOrWhiteSpace(GlobalResponse))
@ -427,7 +427,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
httpRes.Write(GlobalResponse); httpRes.Write(GlobalResponse);
httpRes.Close(); httpRes.Close();
return Task.FromResult(true); return;
} }
var handler = HttpHandlerFactory.GetHandler(httpReq); var handler = HttpHandlerFactory.GetHandler(httpReq);
@ -443,13 +443,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName(); httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
} }
var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName); try
task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
//Matches Exceptions handled in HttpListenerBase.InitTask()
task.ContinueWith(x =>
{ {
await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName).ConfigureAwait(false);
}
finally
{
httpRes.Close();
var statusCode = httpRes.StatusCode; var statusCode = httpRes.StatusCode;
var duration = DateTime.Now - date; var duration = DateTime.Now - date;
@ -458,13 +458,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
LoggerUtils.LogResponse(_logger, statusCode, urlToLog, remoteIp, duration); LoggerUtils.LogResponse(_logger, statusCode, urlToLog, remoteIp, duration);
} }
}
}, TaskContinuationOptions.None);
return task;
} }
return new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo) throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo);
.AsTaskException();
} }
/// <summary> /// <summary>

View File

@ -4,13 +4,15 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Threading.Tasks;
using ServiceStack;
namespace MediaBrowser.Server.Implementations.HttpServer namespace MediaBrowser.Server.Implementations.HttpServer
{ {
/// <summary> /// <summary>
/// Class StreamWriter /// Class StreamWriter
/// </summary> /// </summary>
public class StreamWriter : IStreamWriter, IHasOptions public class StreamWriter : IStreamWriter, /*IAsyncStreamWriter,*/ IHasOptions
{ {
private ILogger Logger { get; set; } private ILogger Logger { get; set; }
@ -73,24 +75,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
} }
// 256k
private const int BufferSize = 262144;
/// <summary> /// <summary>
/// Writes to. /// Writes to.
/// </summary> /// </summary>
/// <param name="responseStream">The response stream.</param> /// <param name="responseStream">The response stream.</param>
public void WriteTo(Stream responseStream) public void WriteTo(Stream responseStream)
{
WriteToInternal(responseStream);
}
// 256k
private const int BufferSize = 262144;
/// <summary>
/// Writes to async.
/// </summary>
/// <param name="responseStream">The response stream.</param>
/// <returns>Task.</returns>
private void WriteToInternal(Stream responseStream)
{ {
try try
{ {
@ -107,7 +99,36 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
OnError(); OnError();
} }
throw;
}
finally
{
if (OnComplete != null)
{
OnComplete();
}
}
}
public async Task WriteToAsync(Stream responseStream)
{
try
{
using (var src = SourceStream)
{
await src.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Logger.ErrorException("Error streaming data", ex);
if (OnError != null)
{
OnError();
}
throw; throw;
} }
finally finally

View File

@ -73,8 +73,8 @@
<HintPath>..\packages\SimpleInjector.3.2.0\lib\net45\SimpleInjector.dll</HintPath> <HintPath>..\packages\SimpleInjector.3.2.0\lib\net45\SimpleInjector.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="SocketHttpListener, Version=1.0.5955.1537, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="SocketHttpListener, Version=1.0.6039.31236, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SocketHttpListener.1.0.0.30\lib\net45\SocketHttpListener.dll</HintPath> <HintPath>..\packages\SocketHttpListener.1.0.0.33\lib\net45\SocketHttpListener.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />

View File

@ -9,5 +9,5 @@
<package id="morelinq" version="1.4.0" targetFramework="net45" /> <package id="morelinq" version="1.4.0" targetFramework="net45" />
<package id="Patterns.Logging" version="1.0.0.2" targetFramework="net45" /> <package id="Patterns.Logging" version="1.0.0.2" targetFramework="net45" />
<package id="SimpleInjector" version="3.2.0" targetFramework="net45" /> <package id="SimpleInjector" version="3.2.0" targetFramework="net45" />
<package id="SocketHttpListener" version="1.0.0.30" targetFramework="net45" /> <package id="SocketHttpListener" version="1.0.0.33" targetFramework="net45" />
</packages> </packages>