aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Net/Handlers/StaticFileHandler.cs
blob: 3967d15c35db96ce98a7118db692b61185e6ae81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Kernel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MediaBrowser.Common.Net.Handlers
{
    /// <summary>
    /// Represents an http handler that serves static content
    /// </summary>
    public class StaticFileHandler : BaseHandler<IKernel>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StaticFileHandler" /> class.
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        public StaticFileHandler(IKernel kernel)
        {
            Initialize(kernel);
        }

        /// <summary>
        /// The _path
        /// </summary>
        private string _path;
        /// <summary>
        /// Gets or sets the path to the static resource
        /// </summary>
        /// <value>The path.</value>
        public string Path
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_path))
                {
                    return _path;
                }

                return QueryString["path"];
            }
            set
            {
                _path = value;
            }
        }

        /// <summary>
        /// Gets or sets the last date modified of the resource
        /// </summary>
        /// <value>The last date modified.</value>
        public DateTime? LastDateModified { get; set; }

        /// <summary>
        /// Gets or sets the content type of the resource
        /// </summary>
        /// <value>The type of the content.</value>
        public string ContentType { get; set; }

        /// <summary>
        /// Gets or sets the content type of the resource
        /// </summary>
        /// <value>The etag.</value>
        public Guid Etag { get; set; }

        /// <summary>
        /// Gets or sets the source stream of the resource
        /// </summary>
        /// <value>The source stream.</value>
        public Stream SourceStream { get; set; }

        /// <summary>
        /// Shoulds the compress response.
        /// </summary>
        /// <param name="contentType">Type of the content.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        private bool ShouldCompressResponse(string contentType)
        {
            // It will take some work to support compression with byte range requests
            if (IsRangeRequest)
            {
                return false;
            }

            // Don't compress media
            if (contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            // Don't compress images
            if (contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Gets or sets the duration of the cache.
        /// </summary>
        /// <value>The duration of the cache.</value>
        public TimeSpan? CacheDuration { get; set; }

        /// <summary>
        /// Gets the total length of the content.
        /// </summary>
        /// <param name="responseInfo">The response info.</param>
        /// <returns>System.Nullable{System.Int64}.</returns>
        protected override long? GetTotalContentLength(ResponseInfo responseInfo)
        {
            // If we're compressing the response, content length must be the compressed length, which we don't know
            if (responseInfo.CompressResponse && ClientSupportsCompression)
            {
                return null;
            }

            return SourceStream.Length;
        }

        /// <summary>
        /// Gets the response info.
        /// </summary>
        /// <returns>Task{ResponseInfo}.</returns>
        protected override Task<ResponseInfo> GetResponseInfo()
        {
            var info = new ResponseInfo
            {
                ContentType = ContentType ?? MimeTypes.GetMimeType(Path),
                Etag = Etag,
                DateLastModified = LastDateModified
            };

            if (SourceStream == null && !string.IsNullOrEmpty(Path))
            {
                // FileShare must be ReadWrite in case someone else is currently writing to it.
                SourceStream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
            }

            info.CompressResponse = ShouldCompressResponse(info.ContentType);

            info.SupportsByteRangeRequests = !info.CompressResponse || !ClientSupportsCompression;

            if (!info.DateLastModified.HasValue && !string.IsNullOrWhiteSpace(Path))
            {
                info.DateLastModified = File.GetLastWriteTimeUtc(Path);
            }

            if (CacheDuration.HasValue)
            {
                info.CacheDuration = CacheDuration.Value;
            }

            if (SourceStream == null && string.IsNullOrEmpty(Path))
            {
                throw new ResourceNotFoundException();
            }

            return Task.FromResult(info);
        }

        /// <summary>
        /// Writes the response to output stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="responseInfo">The response info.</param>
        /// <param name="totalContentLength">Total length of the content.</param>
        /// <returns>Task.</returns>
        protected override Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? totalContentLength)
        {
            if (IsRangeRequest && totalContentLength.HasValue)
            {
                var requestedRange = RequestedRanges.First();

                // If the requested range is "0-", we can optimize by just doing a stream copy
                if (!requestedRange.Value.HasValue)
                {
                    return ServeCompleteRangeRequest(requestedRange, stream, totalContentLength.Value);
                }

                // This will have to buffer a portion of the content into memory
                return ServePartialRangeRequest(requestedRange.Key, requestedRange.Value.Value, stream, totalContentLength.Value);
            }

            return SourceStream.CopyToAsync(stream);
        }

        /// <summary>
        /// Disposes the response stream.
        /// </summary>
        protected override void DisposeResponseStream()
        {
            if (SourceStream != null)
            {
                SourceStream.Dispose();
            }

            base.DisposeResponseStream();
        }

        /// <summary>
        /// Handles a range request of "bytes=0-"
        /// This will serve the complete content and add the content-range header
        /// </summary>
        /// <param name="requestedRange">The requested range.</param>
        /// <param name="responseStream">The response stream.</param>
        /// <param name="totalContentLength">Total length of the content.</param>
        /// <returns>Task.</returns>
        private Task ServeCompleteRangeRequest(KeyValuePair<long, long?> requestedRange, Stream responseStream, long totalContentLength)
        {
            var rangeStart = requestedRange.Key;
            var rangeEnd = totalContentLength - 1;
            var rangeLength = 1 + rangeEnd - rangeStart;

            // Content-Length is the length of what we're serving, not the original content
            HttpListenerContext.Response.ContentLength64 = rangeLength;
            HttpListenerContext.Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);

            if (rangeStart > 0)
            {
                SourceStream.Position = rangeStart;
            }

            return SourceStream.CopyToAsync(responseStream);
        }

        /// <summary>
        /// Serves a partial range request
        /// </summary>
        /// <param name="rangeStart">The range start.</param>
        /// <param name="rangeEnd">The range end.</param>
        /// <param name="responseStream">The response stream.</param>
        /// <param name="totalContentLength">Total length of the content.</param>
        /// <returns>Task.</returns>
        private async Task ServePartialRangeRequest(long rangeStart, long rangeEnd, Stream responseStream, long totalContentLength)
        {
            var rangeLength = 1 + rangeEnd - rangeStart;

            // Content-Length is the length of what we're serving, not the original content
            HttpListenerContext.Response.ContentLength64 = rangeLength;
            HttpListenerContext.Response.Headers["Content-Range"] = string.Format("bytes {0}-{1}/{2}", rangeStart, rangeEnd, totalContentLength);

            SourceStream.Position = rangeStart;

            // Fast track to just copy the stream to the end
            if (rangeEnd == totalContentLength - 1)
            {
                await SourceStream.CopyToAsync(responseStream).ConfigureAwait(false);
            }
            else
            {
                // Read the bytes we need
                var buffer = new byte[Convert.ToInt32(rangeLength)];
                await SourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                await responseStream.WriteAsync(buffer, 0, Convert.ToInt32(rangeLength)).ConfigureAwait(false);
            }
        }
    }
}