|
| 1 | +namespace Unosquare.Labs.EmbedIO.Modules |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using Swan; |
| 5 | + using System.Collections.Generic; |
| 6 | + using System.Collections.ObjectModel; |
| 7 | + using System.IO; |
| 8 | + using System.Threading; |
| 9 | + using System.Threading.Tasks; |
| 10 | + using Constants; |
| 11 | +#if NET47 |
| 12 | + using System.Net; |
| 13 | +#else |
| 14 | + using Net; |
| 15 | +#endif |
| 16 | + |
| 17 | + public abstract class FileModuleBase |
| 18 | + : WebModuleBase |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// The maximum gzip input length |
| 22 | + /// </summary> |
| 23 | + protected const int MaxGzipInputLength = 4 * 1024 * 1024; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The chunk size for sending files |
| 27 | + /// </summary> |
| 28 | + private const int ChunkSize = 256 * 1024; |
| 29 | + |
| 30 | + private readonly Lazy<Dictionary<string, string>> _mimeTypes = |
| 31 | + new Lazy<Dictionary<string, string>>( |
| 32 | + () => |
| 33 | + new Dictionary<string, string>(Constants.MimeTypes.DefaultMimeTypes, Strings.StandardStringComparer)); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the collection holding the MIME types. |
| 37 | + /// </summary> |
| 38 | + /// <value> |
| 39 | + /// The MIME types. |
| 40 | + /// </value> |
| 41 | + public Lazy<ReadOnlyDictionary<string, string>> MimeTypes |
| 42 | + => |
| 43 | + new Lazy<ReadOnlyDictionary<string, string>>( |
| 44 | + () => new ReadOnlyDictionary<string, string>(_mimeTypes.Value)); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// The default headers |
| 48 | + /// </summary> |
| 49 | + public Dictionary<string, string> DefaultHeaders { get; } = new Dictionary<string, string>(); |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets a value indicating whether [use gzip]. |
| 53 | + /// </summary> |
| 54 | + /// <value> |
| 55 | + /// <c>true</c> if [use gzip]; otherwise, <c>false</c>. |
| 56 | + /// </value> |
| 57 | + public bool UseGzip { get; set; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Writes the file asynchronous. |
| 61 | + /// </summary> |
| 62 | + /// <param name="usingPartial">if set to <c>true</c> [using partial].</param> |
| 63 | + /// <param name="partialHeader">The partial header.</param> |
| 64 | + /// <param name="fileSize">Size of the file.</param> |
| 65 | + /// <param name="context">The context.</param> |
| 66 | + /// <param name="buffer">The buffer.</param> |
| 67 | + /// <param name="ct">The ct.</param> |
| 68 | + /// <returns>A task representing the write action.</returns> |
| 69 | + protected Task WriteFileAsync( |
| 70 | + bool usingPartial, |
| 71 | + string partialHeader, |
| 72 | + long fileSize, |
| 73 | + HttpListenerContext context, |
| 74 | + Stream buffer, |
| 75 | + CancellationToken ct) |
| 76 | + { |
| 77 | + long lowerByteIndex = 0; |
| 78 | + |
| 79 | + if (usingPartial && |
| 80 | + CalculateRange(partialHeader, fileSize, out lowerByteIndex, out var upperByteIndex)) |
| 81 | + { |
| 82 | + if (upperByteIndex > fileSize) |
| 83 | + { |
| 84 | + // invalid partial request |
| 85 | + context.Response.StatusCode = 416; |
| 86 | + context.Response.AddHeader(Headers.ContentRanges, $"bytes */{fileSize}"); |
| 87 | + |
| 88 | + return Task.FromResult(0); |
| 89 | + } |
| 90 | + |
| 91 | + if (upperByteIndex == fileSize) |
| 92 | + { |
| 93 | + context.Response.ContentLength64 = buffer.Length; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + context.Response.StatusCode = 206; |
| 98 | + context.Response.ContentLength64 = upperByteIndex - lowerByteIndex + 1; |
| 99 | + |
| 100 | + context.Response.AddHeader(Headers.ContentRanges, |
| 101 | + $"bytes {lowerByteIndex}-{upperByteIndex}/{fileSize}"); |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + if (UseGzip && |
| 107 | + context.RequestHeader(Headers.AcceptEncoding).Contains(Headers.CompressionGzip) && |
| 108 | + buffer.Length < MaxGzipInputLength && |
| 109 | + |
| 110 | + // Ignore audio/video from compression |
| 111 | + context.Response.ContentType?.StartsWith("audio") == false && |
| 112 | + context.Response.ContentType?.StartsWith("video") == false) |
| 113 | + { |
| 114 | + // Perform compression if available |
| 115 | + buffer = buffer.Compress(); |
| 116 | + context.Response.AddHeader(Headers.ContentEncoding, Headers.CompressionGzip); |
| 117 | + lowerByteIndex = 0; |
| 118 | + } |
| 119 | + |
| 120 | + context.Response.ContentLength64 = buffer.Length; |
| 121 | + } |
| 122 | + |
| 123 | + return WriteToOutputStream(context.Response, buffer, lowerByteIndex, ct); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Sets the default cache headers. |
| 128 | + /// </summary> |
| 129 | + /// <param name="response">The response.</param> |
| 130 | + protected void SetDefaultCacheHeaders(HttpListenerResponse response) |
| 131 | + { |
| 132 | + response.AddHeader(Headers.CacheControl, |
| 133 | + DefaultHeaders.GetValueOrDefault(Headers.CacheControl, "private")); |
| 134 | + response.AddHeader(Headers.Pragma, DefaultHeaders.GetValueOrDefault(Headers.Pragma, string.Empty)); |
| 135 | + response.AddHeader(Headers.Expires, DefaultHeaders.GetValueOrDefault(Headers.Expires, string.Empty)); |
| 136 | + } |
| 137 | + |
| 138 | + private static async Task WriteToOutputStream( |
| 139 | + HttpListenerResponse response, |
| 140 | + Stream buffer, |
| 141 | + long lowerByteIndex, |
| 142 | + CancellationToken ct) |
| 143 | + { |
| 144 | + var streamBuffer = new byte[ChunkSize]; |
| 145 | + long sendData = 0; |
| 146 | + var readBufferSize = ChunkSize; |
| 147 | + |
| 148 | + while (true) |
| 149 | + { |
| 150 | + if (sendData + ChunkSize > response.ContentLength64) readBufferSize = (int)(response.ContentLength64 - sendData); |
| 151 | + |
| 152 | + buffer.Seek(lowerByteIndex + sendData, SeekOrigin.Begin); |
| 153 | + var read = await buffer.ReadAsync(streamBuffer, 0, readBufferSize, ct); |
| 154 | + |
| 155 | + if (read == 0) break; |
| 156 | + |
| 157 | + sendData += read; |
| 158 | + await response.OutputStream.WriteAsync(streamBuffer, 0, readBufferSize, ct); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static bool CalculateRange( |
| 163 | + string partialHeader, |
| 164 | + long fileSize, |
| 165 | + out long lowerByteIndex, |
| 166 | + out long upperByteIndex) |
| 167 | + { |
| 168 | + lowerByteIndex = 0; |
| 169 | + upperByteIndex = 0; |
| 170 | + |
| 171 | + var range = partialHeader.Replace("bytes=", string.Empty).Split('-'); |
| 172 | + |
| 173 | + if (range.Length == 2 && long.TryParse(range[0], out lowerByteIndex) && |
| 174 | + long.TryParse(range[1], out upperByteIndex)) |
| 175 | + { |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + if ((range.Length == 2 && long.TryParse(range[0], out lowerByteIndex) && |
| 180 | + string.IsNullOrWhiteSpace(range[1])) || |
| 181 | + (range.Length == 1 && long.TryParse(range[0], out lowerByteIndex))) |
| 182 | + { |
| 183 | + upperByteIndex = (int)fileSize; |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + if (range.Length == 2 && string.IsNullOrWhiteSpace(range[0]) && |
| 188 | + long.TryParse(range[1], out upperByteIndex)) |
| 189 | + { |
| 190 | + lowerByteIndex = (int)fileSize - upperByteIndex; |
| 191 | + upperByteIndex = (int)fileSize; |
| 192 | + return true; |
| 193 | + } |
| 194 | + |
| 195 | + return false; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments