Mik*_*lor 10 delphi http indy httpserver microsoft-bits
我们正在尝试使用TIdHTTPServer组件为我们的软件编写更新服务器.目前我们正在提供一个XML文件,列出可用的更新及其文件版本等.当客户端程序找到更新版本时,它应该开始使用BITS下载它.
现在这是我们遇到问题的地方,我们的程序正在请求XML文件并看到有可用的更新.然后它创建一个BITS作业来下载它,但是BITS一直报告下载失败.我们可以使用相同的URL和IE/Firefox/Chrome下载文件.
所以我的问题:
TIdHTTPServer与BITS兼容吗?
我问这个,因为我发现有一些工作的下载要求.
BITS下载的HTTP要求
BITS支持HTTP和HTTPS下载和上传,并要求服务器支持HTTP/1.1协议.对于下载,HTTP服务器的Head方法必须返回文件大小,其Get方法必须支持Content-Range和Content-Length标头.因此,除非ASP,ISAPI或CGI脚本支持Content-Range和Content-Length标头,否则BITS仅传输静态文件内容并在尝试传输动态内容时生成错误.
BITS可以使用HTTP/1.0服务器,只要它符合Head和Get方法要求即可.
要支持下载文件范围,服务器必须支持以下要求:
允许MIME标头包含标准的Content-Range和Content-Type标头,以及最多180个字节的其他标头.在HTTP标头和第一个边界字符串之间最多允许两个CR/LF.
刚发现indy中的一个错误,在使用范围请求时阻止文件传输超过2.1GB.
这里是
IdHTTPHeaderInfo.pas
aprox line 770
procedure TIdEntityRange.SetText(const AValue: String);
var
LValue, S: String;
begin
LValue := Trim(AValue);
if LValue <> '' then
begin
S := Fetch(LValue, '-'); {do not localize}
if S <> '' then begin
FStartPos := StrToIntDef(S, -1);
FEndPos := StrToIntDef(Fetch(LValue), -1);
FSuffixLength := -1;
end else begin
FStartPos := -1;
FEndPos := -1;
FSuffixLength := StrToIntDef(Fetch(LValue), -1);
end;
end else begin
FStartPos := -1;
FEndPos := -1;
FSuffixLength := -1;
end;
end;
Run Code Online (Sandbox Code Playgroud)
这应该是
procedure TIdEntityRange.SetText(const AValue: String);
var
LValue, S: String;
begin
LValue := Trim(AValue);
if LValue <> '' then
begin
S := Fetch(LValue, '-'); {do not localize}
if S <> '' then begin
FStartPos := StrToInt64Def(S, -1);
FEndPos := StrToInt64Def(Fetch(LValue), -1);
FSuffixLength := -1;
end else begin
FStartPos := -1;
FEndPos := -1;
FSuffixLength := StrToInt64Def(Fetch(LValue), -1);
end;
end else begin
FStartPos := -1;
FEndPos := -1;
FSuffixLength := -1;
end;
end;
Run Code Online (Sandbox Code Playgroud)
一个让雷米来修理
所以这个问题的答案是:
是的,TIdHTTPServer 是位兼容的。
但前提是您准备好自己做这项工作。
正如 @Rob Kennedy 和我自己所建议的,可以读取标头并使用请求的范围将数据发送回,一次一个块。
OnCommandGet
这是我在活动中所做的一个例子
procedure TForm3.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Ranges : TIdEntityRanges;
DataChunk: TMemoryStream;
ReqFile: TFileStream;
ChunkLength: Int64;
Directory, FileName: string;
begin
Directory := 'H:';
case ARequestInfo.Ranges.Count of
0:
begin
//serve file normally
end;
1:
begin
//serve range of bytes specified for file
filename := Directory + ARequestInfo.Document;
if FileExists(FileName) then
begin
ReqFile := TFileStream.Create(FileName, fmOpenRead);
try
ChunkLength := Succ(ARequestInfo.Ranges.Ranges[0].EndPos - ARequestInfo.Ranges.Ranges[0].StartPos);
if ChunkLength > ReqFile.Size then
ChunkLength := ReqFile.Size;
DataChunk := TMemoryStream.Create;
DataChunk.Posistion := ARequestInfo.Ranges.Ranges[0].StartPos;
DataChunk.CopyFrom(ReqFile, ChunkLength);
AResponseInfo.ContentStream := DataChunk;
AResponseInfo.ContentType := IdHTTPServer1.MIMETable.GetFileMIMEType(FileName);
AResponseInfo.ContentRangeUnits := ARequestInfo.Ranges.Units;
AResponseInfo.ContentRangeStart := ARequestInfo.Ranges.Ranges[0].StartPos;
AResponseInfo.ContentRangeEnd := ARequestInfo.Ranges.Ranges[0].StartPos + Pred(ChunkLength);
AResponseInfo.ContentRangeInstanceLength := ReqFile.Size;
AResponseInfo.ResponseNo := 206;
finally
ReqFile.Free;
end;
end
else
AResponseInfo.ResponseNo := 404;
end
else
begin
//serve the file as multipart/byteranges
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
这还没有完成,但它展示了响应 BITS 范围请求的基础知识。最重要的是它有效。
对代码的任何评论将不胜感激,建设性的批评总是受欢迎的。