From 35f40993b2b85efc6fbb677d373b337aebfe0465 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 22 Oct 2014 01:06:19 -0400 Subject: update http server --- .../HttpServer/RangeRequestWriter.cs | 59 +++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs') diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index 7a143c8d9..cdd4c6d7c 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -173,9 +173,64 @@ namespace MediaBrowser.Server.Implementations.HttpServer ThrottleCallback = ThrottleCallback }; } - var task = WriteToAsync(responseStream); + WriteToInternal(responseStream); + } + + /// + /// Writes to async. + /// + /// The response stream. + /// Task. + private void WriteToInternal(Stream responseStream) + { + try + { + // 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) + { + source.CopyTo(responseStream); + } + else + { + CopyToInternal(source, responseStream, Convert.ToInt32(RangeLength)); + } + } + } + finally + { + if (OnComplete != null) + { + OnComplete(); + } + } + } - Task.WaitAll(task); + private void CopyToInternal(Stream source, Stream destination, int copyLength) + { + const int bufferSize = 81920; + var array = new byte[bufferSize]; + int count; + while ((count = source.Read(array, 0, array.Length)) != 0) + { + var bytesToCopy = Math.Min(count, copyLength); + + destination.Write(array, 0, bytesToCopy); + + copyLength -= bytesToCopy; + + if (copyLength <= 0) + { + break; + } + } } /// -- cgit v1.2.3