Indy Http Server带有恢复支持

3

我已经编写了一个Indy HTTP服务器,我想提供支持恢复的文件下载。当我使用IDM下载文件时,下载是单线程的:

enter image description here

请注意,恢复功能,但当我暂停并恢复下载时,它从头开始。

我的Indy Http服务器如下:

void __fastcall TfrmMain::httpServerCommandGet(TIdContext *AContext,
    TIdHTTPRequestInfo *ARequestInfo, TIdHTTPResponseInfo *AResponseInfo)
{
    Beep(1000, 100);
    string fileName = ExtractFileDir(Application->ExeName) + ARequestInfo->Document;
    fileName = fileName.replace("/", "\\");

    TFileStream *stream = new TFileStream(fileName, fmOpenRead | fmShareDenyNone);

    int start = 0, end = 0;
    string range = ARequestInfo->Range;
    if(!range.empty())
    {
        int dash_pos = range.find("-");
        start = range.substr(0, dash_pos).intValue();
        end = range.substr(dash_pos + 1).intValue();
        if(end == 0) // Endless Range: start-
            end = stream->Size;
    }
    else
    {
        start = 0;
        end = stream->Size;
    }
    OutputDebugStringW(string("ctx=[] start=[] end=[]") <<
        IntToHex((int)AContext, 8) << start << end);

    stream->Seek((__int64)start, soBeginning);
    AResponseInfo->ContentStream = stream;
    AResponseInfo->FreeContentStream = true;
    AResponseInfo->ContentLength = stream->Size;
    if(!range.empty())
    {
        AResponseInfo->ContentRangeStart = start;
        AResponseInfo->ContentRangeEnd = end;
        AResponseInfo->ResponseNo = 206;
        AResponseInfo->ContentRangeInstanceLength = end + 1;
        AResponseInfo->ContentLength = end - start + 1;
        AResponseInfo->AcceptRanges = "bytes";
    }
    AResponseInfo->WriteHeader();
    AResponseInfo->WriteContent();
}

非常感谢您的帮助。

1个回答

5
IdCustomHTTPServer单元中有一个TIdHTTPRangeStream帮助类,专门用于此目的。
如果客户端请求范围下载,请创建TIdHTTPRangeStream实例,并将其传递给您打算使用的TStream和客户端请求的范围,然后将其分配为要发送的ContentStreamTIdHTTPRangeStream还有一个ResponseCode属性,您需要将其分配给响应的ResponseNo属性。
例如:
void __fastcall TfrmMain::httpServerCommandGet(TIdContext *AContext, TIdHTTPRequestInfo *ARequestInfo, TIdHTTPResponseInfo *AResponseInfo)
{
    // create file stream ...

    // THTTPServer parses the ranges for you
    if (ARequestInfo->Ranges->Count > 0)
    {
        if (ARequestInfo->Ranges->Count > 1)
        {
            AResponseInfo->ResponseNo = 416;
            return;
        }

        TIdEntityRange *range = ARequestInfo->Ranges->Range[0];

        TIdHTTPRangeStream *rstream = new TIdHTTPRangeStream(stream, range->StartPos, range->EndPos, true);

        AResponseInfo->ResponseNo = rstream->ResponseCode;
        AResponseInfo->ContentRangeStart = rstream->RangeStart;
        AResponseInfo->ContentRangeEnd = rstream->RangeEnd;
        AResponseInfo->ContentStream = rstream;
        AResponseInfo->AcceptRanges = "bytes";
    }
    else
    {
        AResponseInfo->ContentStream = stream;
    }

    // no need to seek the target stream manually
    // no need to set the ContentLength manually
    // no need to call WriteHeader() and WriteContent() manually
    //
    // TIdHTTPServer handles all that for you internally!
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接