jgauffin / griffin.networking

Networking library for .NET
http://blog.gauffin.org/2012/05/griffin-networking-a-somewhat-performant-networking-library-for-net/
GNU Lesser General Public License v3.0
108 stars 35 forks source link

Byte ranges not supported #16

Closed ddevault closed 11 years ago

ddevault commented 11 years ago

Please add support for byte ranges (see this document), which will provide better support for HTML5 audio/video.

I've tried to do it myself like this:

        var stream = File.OpenRead(Path.Combine(BaseDirectory, path));
        response.AddHeader("Accept-Ranges", "bytes");
        response.ContentType = HttpServer.GetContentTypeForExtension(Path.GetExtension(path).Substring(1));
        // Handle ranges
        if (request.Headers.Any(h => h.Name == "Range"))
        {
            response.StatusCode = 206;
            response.StatusDescription = "Partial Content";
            var range = request.Headers["Range"].Value;
            var type = range.Remove(range.IndexOf("="));
            if (type != "bytes")
            {
                response.StatusCode = 400;
                response.StatusDescription = "Bad Request";
                return;
            }
            range = range.Substring(range.IndexOf("=") + 1);
            var rangeParts = range.Split('-');
            stream.Seek(int.Parse(rangeParts[0]), SeekOrigin.Begin);
        }
        stream.CopyTo(response.Body);

But it doesn't seem to work.

ddevault commented 11 years ago

Closing this, since I found some example code in the repository that shows that byte ranges are indeed supported. Maybe a good opportunity to work on better documentation?