This project is read-only.

About Retry broken Downloads

Jun 15, 2009 at 11:55 AM
Edited Jun 15, 2009 at 11:57 AM

FileModule.cs

Old : Retry broken Downloads  not support:


 

if (request.Method != "Headers" && response.Status != HttpStatusCode.NotModified){
	byte[] buffer = new byte[8192];
	int bytesRead = stream.Read(buffer, 0, 8192);
	while (bytesRead > 0){
		response.SendBody(buffer, 0, bytesRead);
		bytesRead = stream.Read(buffer, 0, 8192);
	}
}

New: Retry broken Downloads  support:


if (!string.IsNullOrEmpty(request.Headers["Range"])) {//Range
    string RangeValue = request.Headers["Range"];
    //eg. bytes=335872-162761623(Unit Always"bytes"?????)
    long from = 0;
    long to = 0;
    try {
        int pos1 = RangeValue.IndexOf('=');
        int pos2 = RangeValue.IndexOf('-');
        from = int.Parse(RangeValue.Substring(pos1 + 1, pos2 - pos1 - 1));
        to = int.Parse(RangeValue.Substring(pos2 + 1, RangeValue.Length - pos2 - 1));
    } catch {
        throw new Exception("Pares Range Value Exception.");
    }
    response.Status = HttpStatusCode.PartialContent;
    response.ContentLength = to - from;
    response.AddHeader("Accept-Ranges", "bytes");
    response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", from, to, stream.Length));
    response.SendHeaders();

    if (request.Method != "Headers" && response.Status != HttpStatusCode.NotModified) {
        byte[] buffer = new byte[8192];
        //Move to From 
        stream.Position = from;
        int bytesRead = stream.Read(buffer, 0, 8192);
        while (bytesRead > 0) {
            response.SendBody(buffer, 0, bytesRead);
            if (stream.Position + 8192 < to) {
                bytesRead = stream.Read(buffer, 0, 8192);
            } else if (to - stream.Position > 0) {//The last packet
                bytesRead = stream.Read(buffer, 0, (int)(to - stream.Position));
            } else {//Out of Range 
                bytesRead = -1;
            }
        }
    }
} else {//!Range
    response.ContentLength = stream.Length;
    //tell Client Retry broken Downloads Support
    response.AddHeader("Accept-Ranges", "bytes");
    response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", 0, stream.Length, stream.Length));
    response.SendHeaders();

    if (request.Method != "Headers" && response.Status != HttpStatusCode.NotModified) {
        byte[] buffer = new byte[8192];
        int bytesRead = stream.Read(buffer, 0, 8192);
        while (bytesRead > 0) {
            response.SendBody(buffer, 0, bytesRead);
            bytesRead = stream.Read(buffer, 0, 8192);
        }
    }
}

 

It's my Way, Basic Test Pass.