Exemple #1
0
 public Static(IStaticFileServer fileServer, TcpSettings tcpSettings)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _fileServer = fileServer
                   ?? throw new ArgumentNullException(nameof(fileServer));
     _tcpSettings = tcpSettings;
 }
Exemple #2
0
        public static async Task WriteAsync(
            this IStaticFileServer inst,
            IHttpRequest request,
            StaticRangeRequest rangeRequest,
            IHttpResponse response,
            string pathToContentFile,
            int writeBufferSize)
        {
            Validation.RequireValidBufferSize(writeBufferSize);

            // Set headers
            response.Header[HttpKeys.ContentType] =
                inst.GetContentTypeHeader(pathToContentFile);

            // Open the file
            using (var fs = inst.OpenRead(pathToContentFile))
            {
                // The requested range may contains relative values,
                // Convert them to absolute values here, as we knew the
                // content length
                rangeRequest = rangeRequest.ToAbsolute(fs.Length);

                // Set headers (cont)
                var contentLength = rangeRequest.To - rangeRequest.From + 1L;
                response.Header[HttpKeys.ContentLength]
                    = contentLength.Str();
                response.Header[HttpKeys.ContentRange]
                    = rangeRequest.GenerateRangeHeaderValue(fs.Length);

                // Write body (for GET method only)
                if (request.Header.Method == HttpRequestMethod.GET)
                {
                    fs.Position = rangeRequest.From;
                    await fs.CopyToAsync(
                        response.Body,
                        count : contentLength,
                        bufferSize : writeBufferSize);
                }
            }
        }
Exemple #3
0
        public static async Task WriteAsync(
            this IStaticFileServer staticFileServer,
            IHttpRequest request,
            StaticRangeRequest[] ranges,
            IHttpResponse response,
            string pathToContentFile,
            int writeBufferSize)
        {
            var contentType = staticFileServer.GetContentTypeHeader(pathToContentFile);

            using (var fs = staticFileServer.OpenRead(pathToContentFile))
            {
                for (var i = 0; i < ranges.Length; i++)
                {
                    ranges[i] = ranges[i].ToAbsolute(fs.Length);
                }

                // Write header
                var chunks = GenerateChunks(ranges, fs.Length, contentType);
                response.Header[HttpKeys.ContentType]
                    = $"multipart/byteranges; boundary={MultiRangeBoundaryString}";
                response.Header[HttpKeys.ContentLength]
                    = CalculateContentLength(chunks).Str();

                // Write body and footer (GET method only)
                if (request.Header.Method == HttpRequestMethod.GET)
                {
                    for (var i = 0; i < chunks.Count; i++)
                    {
                        await WriteChunkBody(response, writeBufferSize, fs, chunks[i]);
                    }
                    await response.Body.WriteAsync(
                        MultiRangeBoundaryFooterString,
                        writeBufferSize
                        );
                }
            }
        }