我正在使用 Delphi 10.3 Rio 重新编码一个旧的 Delphi XE 程序。它使用 TIdHTTPProxyServer Indy 组件监听 127.0.0.1:80。
with IdHTTPProxyServer.Bindings.Add do begin
IP := '127.0.0.1';
Port := 80;
end;
IdHTTPProxyServer.Active := True;
为了测试,我在 hosts 文件中添加了 127.0.0.1 localtest123.com 和 127.0.0.1 www.localtest123.com 并禁用了 DNS 缓存服务。然后在多个浏览器中我请求http://localtest123.com/和http://www.localtest123.com/。使用 OutputDebugString() 我可以看到连接被接受,但随后引发“未知协议”错误。
我在 IdHTTPProxyServer.pas 中的 TIdHTTPProxyServer.CommandPassThrough 过程中调试了异常。似乎 LURI.Protocol 是一个空字符串,这就是引发 RSHTTPUnknownProtocol 的原因。
LContext := TIdHTTPProxyServerContext(ASender.Context);
LContext.FCommand := ASender.CommandHandler.Command; //<-'GET'
LContext.FTarget := ASender.Params.Strings[0]; //<-'/'
LContext.FOutboundClient := TIdTCPClient.Create(nil);
try
LURI := TIdURI.Create(LContext.Target); //<-'/'
try
TIdTCPClient(LContext.FOutboundClient).Host := LURI.Host; //<-''
if LURI.Port <> '' then begin //<-''
TIdTCPClient(LContext.FOutboundClient).Port := IndyStrToInt(LURI.Port, 80);
end
else if TextIsSame(LURI.Protocol, 'http') then begin //<-'' {do not localize}
TIdTCPClient(LContext.FOutboundClient).Port := IdPORT_HTTP;
end
else if TextIsSame(LURI.Protocol, 'https') then begin //<-'' {do not localize}
TIdTCPClient(LContext.FOutboundClient).Port := IdPORT_https;
end else begin
raise EIdException.Create(RSHTTPUnknownProtocol);
end;
我可能遗漏了一些东西,但 TIdHTTPProxyServer 无需太多代码就可以工作,所以我不得不就这个异常寻求帮助。提前致谢!