RejectedSoftware Forums

Sign up

how to return file

Hello,
I'm new to D programing.I want to ask some entry level question: how I can return large file in chunks... did not find any example in github.
I'm trying to make simple file server to serve video files.

thanks for your help in advance..

Re: how to return file

On Thu, 27 Oct 2016 14:11:46 GMT, Desislav Stanchev wrote:

Hello,
I'm new to D programing.I want to ask some entry level question: how I can return large file in chunks... did not find any example in github.
I'm trying to make simple file server to serve video files.

thanks for your help in advance..

The canonical way would be to support/use HTTP ranges. There is a ticket for this, but I didn't yet implement this for vibe.d's file server. If you want to start experimenting with it, you could copy the sendFileImpl function and apply the patch in that ticket. A full version of this requires some more work, but I will try to get to that for the next release.

Re: how to return file

On Thu, 27 Oct 2016 18:10:00 GMT, Sönke Ludwig wrote:

On Thu, 27 Oct 2016 14:11:46 GMT, Desislav Stanchev wrote:

Hello,
I'm new to D programing.I want to ask some entry level question: how I can return large file in chunks... did not find any example in github.
I'm trying to make simple file server to serve video files.

thanks for your help in advance..

The canonical way would be to support/use HTTP ranges. There is a ticket for this, but I didn't yet implement this for vibe.d's file server. If you want to start experimenting with it, you could copy the sendFileImpl function and apply the patch in that ticket. A full version of this requires some more work, but I will try to get to that for the next release.

@Sönke Ludwig thank you for the information! I managed to return video in chunks(with code from the ticket) but I set res.statusCode = 206 within this if statement:

if (auto prange = "Range" in req.headers)
{
    auto range = (*prange).chompPrefix("bytes=");
    auto s = range.split("-");
    rangeStart = s[0].to!ulong();
    rangeEnd = s[1].length ? s[1].to!ulong() : dirent.size;
    res.headers["Content-Length"] = to!string(rangeEnd - rangeStart + 1);
    res.headers["Content-Range"] = "bytes %s-%s/%s".format(rangeStart, rangeEnd, dirent.size);
    res.statusCode = 206; // <- here
}
else
{
    res.headers["Content-Length"] = to!string(dirent.size);
}

Is this correct?