diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer')
4 files changed, 76 insertions, 62 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs index 9997cfbdb..e13e27d5a 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -102,14 +102,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer return result; } - private bool SupportsCompression - { - get - { - return true; - } - } - /// <summary> /// Gets the optimized result. /// </summary> @@ -127,7 +119,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer throw new ArgumentNullException("result"); } - var optimizedResult = SupportsCompression ? requestContext.ToOptimizedResult(result) : result; + var optimizedResult = requestContext.ToOptimizedResult(result); if (responseHeaders != null) { @@ -471,7 +463,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer { Throttle = options.Throttle, ThrottleLimit = options.ThrottleLimit, - MinThrottlePosition = options.MinThrottlePosition + MinThrottlePosition = options.MinThrottlePosition, + ThrottleCallback = options.ThrottleCallback, + OnComplete = options.OnComplete }; } @@ -488,39 +482,20 @@ namespace MediaBrowser.Server.Implementations.HttpServer { Throttle = options.Throttle, ThrottleLimit = options.ThrottleLimit, - MinThrottlePosition = options.MinThrottlePosition + MinThrottlePosition = options.MinThrottlePosition, + ThrottleCallback = options.ThrottleCallback, + OnComplete = options.OnComplete }; } string content; - long originalContentLength = 0; using (var stream = await factoryFn().ConfigureAwait(false)) { - using (var memoryStream = new MemoryStream()) - { - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; - - originalContentLength = memoryStream.Length; - - using (var reader = new StreamReader(memoryStream)) - { - content = await reader.ReadToEndAsync().ConfigureAwait(false); - } - } - } - - if (!SupportsCompression) - { - responseHeaders["Content-Length"] = originalContentLength.ToString(UsCulture); - - if (isHeadRequest) + using (var reader = new StreamReader(stream)) { - return GetHttpResult(new byte[] { }, contentType); + content = await reader.ReadToEndAsync().ConfigureAwait(false); } - - return new HttpResult(content, contentType); } var contents = content.Compress(requestedCompressionType); diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index 657545069..7a143c8d9 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -27,6 +27,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer public bool Throttle { get; set; } public long ThrottleLimit { get; set; } public long MinThrottlePosition; + public Func<long, long, long> ThrottleCallback { get; set; } + public Action OnComplete { get; set; } /// <summary> /// The _options @@ -167,7 +169,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer { responseStream = new ThrottledStream(responseStream, ThrottleLimit) { - MinThrottlePosition = MinThrottlePosition + MinThrottlePosition = MinThrottlePosition, + ThrottleCallback = ThrottleCallback }; } var task = WriteToAsync(responseStream); @@ -182,22 +185,32 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// <returns>Task.</returns> private async Task WriteToAsync(Stream responseStream) { - // Headers only - if (IsHeadRequest) + try { - return; - } + // Headers only + if (IsHeadRequest) + { + return; + } - using (var source = SourceStream) - { - // If the requested range is "0-", we can optimize by just doing a stream copy - if (RangeEnd >= TotalContentLength - 1) + using (var source = SourceStream) { - await source.CopyToAsync(responseStream).ConfigureAwait(false); + // If the requested range is "0-", we can optimize by just doing a stream copy + if (RangeEnd >= TotalContentLength - 1) + { + await source.CopyToAsync(responseStream).ConfigureAwait(false); + } + else + { + await CopyToAsyncInternal(source, responseStream, Convert.ToInt32(RangeLength), CancellationToken.None).ConfigureAwait(false); + } } - else + } + finally + { + if (OnComplete != null) { - await CopyToAsyncInternal(source, responseStream, Convert.ToInt32(RangeLength), CancellationToken.None).ConfigureAwait(false); + OnComplete(); } } } diff --git a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs index 28fc094f7..1ca5f1204 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs @@ -39,6 +39,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer public bool Throttle { get; set; } public long ThrottleLimit { get; set; } public long MinThrottlePosition; + public Func<long, long, long> ThrottleCallback { get; set; } + public Action OnComplete { get; set; } /// <summary> /// Initializes a new instance of the <see cref="StreamWriter" /> class. @@ -85,7 +87,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer { responseStream = new ThrottledStream(responseStream, ThrottleLimit) { - MinThrottlePosition = MinThrottlePosition + MinThrottlePosition = MinThrottlePosition, + ThrottleCallback = ThrottleCallback }; } var task = WriteToAsync(responseStream); @@ -113,6 +116,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer throw; } + finally + { + if (OnComplete != null) + { + OnComplete(); + } + } } } } diff --git a/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs b/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs index 067e53571..4bde30dac 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs @@ -15,6 +15,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// </summary> public const long Infinite = 0; + public Func<long, long, long> ThrottleCallback { get; set; } + #region Private members /// <summary> /// The base stream. @@ -278,22 +280,42 @@ namespace MediaBrowser.Server.Implementations.HttpServer } #endregion - #region Protected methods - /// <summary> - /// Throttles for the specified buffer size in bytes. - /// </summary> - /// <param name="bufferSizeInBytes">The buffer size in bytes.</param> - protected void Throttle(int bufferSizeInBytes) + private bool ThrottleCheck(int bufferSizeInBytes) { if (_bytesWritten < MinThrottlePosition) { - return; + return false; } // Make sure the buffer isn't empty. if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) { - return; + return false; + } + + if (ThrottleCallback != null) + { + var val = ThrottleCallback(_maximumBytesPerSecond, _bytesWritten); + + if (val == 0) + { + return false; + } + } + + return true; + } + + #region Protected methods + /// <summary> + /// Throttles for the specified buffer size in bytes. + /// </summary> + /// <param name="bufferSizeInBytes">The buffer size in bytes.</param> + protected void Throttle(int bufferSizeInBytes) + { + if (!ThrottleCheck(bufferSizeInBytes)) + { + return ; } _byteCount += bufferSizeInBytes; @@ -332,13 +354,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer protected async Task ThrottleAsync(int bufferSizeInBytes, CancellationToken cancellationToken) { - if (_bytesWritten < MinThrottlePosition) - { - return; - } - - // Make sure the buffer isn't empty. - if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) + if (!ThrottleCheck(bufferSizeInBytes)) { return; } |
