Esempio n. 1
0
 public static Func<Stream, Task> Create(string path, Tuple<long, long> range)
 {
     return stream =>
     {
         var fileBody = new FileBody(path, range);
         return fileBody.Start(stream);
     };
 }
Esempio n. 2
0
 public static BodyDelegate Create(string path, Tuple<long, long> range)
 {
     return (write, flush, end, cancel) =>
     {
         var fileBody = new FileBody(path, range);
         fileBody.Start(write, flush, end, cancel);
     };
 }
Esempio n. 3
0
        private Task Serve(IDictionary<string, object> env, string path)
        {
            Request request = new Request(env);
            Response response = new Response(env);

            var fileInfo = new FileInfo(path);
            var size = fileInfo.Length;
            Tuple<long, long> range;

            if (!RangeHeader.IsValid(request.Headers))
            {
                response.StatusCode = OK;
                range = new Tuple<long, long>(0, size - 1);
            }
            else
            {
                var ranges = RangeHeader.Parse(request.Headers, size);

                if (ranges == null)
                {
                    // Unsatisfiable.  Return error and file size.
                    return Fail(
                        RequestedRangeNotSatisfiable,
                        "Byte range unsatisfiable",
                        "Content-Range", "bytes */" + size)
                        .Invoke(env);
                }

                if (ranges.Count() > 1)
                {
                    // TODO: Support multiple byte ranges.
                    response.StatusCode = OK;
                    range = new Tuple<long, long>(0, size - 1);
                }
                else
                {
                    // Partial content
                    range = ranges.First();
                    response.StatusCode = PartialContent;
                    response.Headers.SetHeader("Content-Range", "bytes " + range.Item1 + "-" + range.Item2 + "/" + size);
                    size = range.Item2 - range.Item1 + 1;
                }
            }

            response.Headers
                .SetHeader("Last-Modified", fileInfo.LastWriteTimeUtc.ToHttpDateString())
                .SetHeader("Content-Type", Mime.MimeType(fileInfo.Extension, "text/plain"))
                .SetHeader("Content-Length", size.ToString(CultureInfo.InvariantCulture));

            if ("HEAD".Equals(request.Method, StringComparison.OrdinalIgnoreCase))
            {
                // Suppress the body.
                return TaskHelpers.Completed();
            }

            FileBody body = new FileBody(path, range);

            SendFileFunc sendFile = env.Get<SendFileFunc>("sendfile.Func");
            if (sendFile != null)
            {
                return body.Start(sendFile);
            }

            return body.Start(response.OutputStream);
        }
Esempio n. 4
0
        private Task Serve(IDictionary <string, object> env, string path)
        {
            Request  request  = new Request(env);
            Response response = new Response(env);

            var fileInfo = new FileInfo(path);
            var size     = fileInfo.Length;
            Tuple <long, long> range;

            if (!RangeHeader.IsValid(request.Headers))
            {
                response.StatusCode = OK;
                range = new Tuple <long, long>(0, size - 1);
            }
            else
            {
                var ranges = RangeHeader.Parse(request.Headers, size);

                if (ranges == null)
                {
                    // Unsatisfiable.  Return error and file size.
                    return(Fail(
                               RequestedRangeNotSatisfiable,
                               "Byte range unsatisfiable",
                               "Content-Range", "bytes */" + size)
                           .Invoke(env));
                }

                if (ranges.Count() > 1)
                {
                    // TODO: Support multiple byte ranges.
                    response.StatusCode = OK;
                    range = new Tuple <long, long>(0, size - 1);
                }
                else
                {
                    // Partial content
                    range = ranges.First();
                    response.StatusCode = PartialContent;
                    response.Headers.SetHeader("Content-Range", "bytes " + range.Item1 + "-" + range.Item2 + "/" + size);
                    size = range.Item2 - range.Item1 + 1;
                }
            }

            response.Headers
            .SetHeader("Last-Modified", fileInfo.LastWriteTimeUtc.ToHttpDateString())
            .SetHeader("Content-Type", Mime.MimeType(fileInfo.Extension, "text/plain"))
            .SetHeader("Content-Length", size.ToString(CultureInfo.InvariantCulture));

            if ("HEAD".Equals(request.Method, StringComparison.OrdinalIgnoreCase))
            {
                // Suppress the body.
                return(TaskHelpers.Completed());
            }

            FileBody body = new FileBody(path, range);

            //TODO: update for current send file spec
            //var req = new Request(env);
            //SendFileFunc sendFile = env.Get<SendFileFunc>("sendfile.Func");
            //if (sendFile != null)
            //{
            //    return body.Start(sendFile);
            //}

            return(body.Start(response.OutputStream));
        }