javascript - 德尔福 TIdHTTPServer : not allowed to load local resourse from remote device

标签 javascript delphi remote-access indy httpserver

我正在制作一个 VCL 表单应用程序,在主表单上使用 TIdHTTPServer 并在 IdHTTPServer 过程中使用 CommandGet:

procedure TForm6.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  indexStream: TFileStream;
  path, indexPath: string;
begin
  AResponseInfo.CharSet := 'UTF-8';
  path := GetCurrentDir.Remove(GetCurrentDir.Length - Length('Win32\Debug'));
  ARequestInfo.Document := path + 'scripts/script1.js';
  ARequestInfo.Document := path + 'scripts/script2.js';

  if pos('profile', ARequestInfo.UnparsedParams) > 0 then
  begin
    indexPath := path + 'index.html';  
    AResponseInfo.ContentStream := TFileStream.Create(indexPath, fmOpenReadWrite);
  end;
end;

编辑

procedure TForm6.IdHTTPServerCommandGet(AContext: TIdContext;
   ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  applicationDirectory: string;
begin
  AResponseInfo.CharSet := 'UTF-8';
  AResponseInfo.FreeContentStream := True;
  applicationDirectory := ExtractFilePath(Application.ExeName)
    .Remove(ExtractFilePath(Application.ExeName).Length -
    Length('Win32\Debug') - 1);
  AResponseInfo.ContentStream := TFileStream.Create(applicationDirectory +
     'scripts/script1.js', fmOpenRead or fmShareDenyWrite);
  AResponseInfo.ContentStream := TFileStream.Create(applicationDirectory +
     'scripts/script2.js', fmOpenRead or fmShareDenyWrite);

  if pos('profile&userName=', ARequestInfo.UnparsedParams) > 0 then
  begin
    AResponseInfo.ContentStream :=
       TFileStream.Create(applicationDirectory + 'index.html', fmOpenRead);
  end;
  // other requests
end;

这里是index.html的内容:

<html>
  <head>
    <title>Profile</title>
    <script type="text/javascript" src="[app-path]/scripts/script1.js "></script>
    <script type="text/javascript" src="[app-path]/scripts/script2.js "></script>
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">
  </head>
  <body>
  <!-- Page content -->
  </body>
</html>

当我启动应用程序并访问 http://localhost/?profile&userName=testUser 时它工作正常但是当我启动应用程序并在其他计算机的 chrome 浏览器中输入时 http://{my-ip4-address}/?profile&userName=testUser 我收到两条消息:'不允许加载本地资源:file:///{app_path}/scripts/script1.js','不允许加载本地资源:file:///{app_path}/scripts/script2.js。 HTML 页面的内容是可见的。

编辑

根据评论和回答index.html改为

 <html>
  <head>
    <title>Profile</title>
    <script type="text/javascript" src="/scripts/script1.js "></script>
    <script type="text/javascript" src="/scripts/script2.js "></script>
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">
  </head>
  <body>
  <!-- Page content -->
  </body>
</html>

像这样编辑我的代码我可以访问文件,但问题是 script1.js 的内容与 script2.js 的内容相同。知道如何正确发送文件。

最佳答案

如果要将 HTML 文件提供给其他计算机,切勿使用应用程序路径([app-path] 在您的示例 HTML 代码中)。对于本地独立 HTML 页面,这可能有效,但外部客户端将无法访问该资源。

相反,指定 JavaScript 的路径(绝对路径为 /scripts/script1.js 或相对路径,如 scripts/script1.js,具体取决于虚拟目录结构)并根据Request.Document属性解析为服务器上的文件。

您的示例代码从不读取 Request.Document 属性(甚至为其赋值 - 两次!)。所以服务器不知道客户端真正想要的资源。

您的代码必须将 Document 属性转换为相对于文档根文件夹的路径,例如,该文件夹可能是您的应用程序工作目录的专用子文件夹。然后检查此路径是否指向现有文件。如果它存在,读取它并返回它的内容。如果不存在,则返回错误码。

关于javascript - 德尔福 TIdHTTPServer : not allowed to load local resourse from remote device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755352/

相关文章:

javascript - 我主要使用jquery制作了一个onclick rpg。我在随机选择被敌方机器人攻击的英雄时遇到了一个主要问题

javascript - Emscripten - 有没有办法限制写入标准输出?

delphi - 对于 "Form1.MousePreview:=true"这样的问题有解决方法吗?

使用 hostapd 创建访问点后 SSH 进入 linux

php - 如何在允许另一个用户编辑之前检查用户是否正在编辑表记录,而不是等待发布?

android - AVD 的远程桌面服务器?

javascript - 如何重构这段代码?我有 3 个标签需要在列表中重新选择

javascript - 从 Vue 应用程序外部访问 Vue 方法或事件

delphi - 在运行时检测 GPU 是否支持 Pixel Shader 2.0 (Firemonkey)

delphi - 为什么 FireMonkey TListBox Animation 会阻止 Windows32 平台上的项目取消选择