【问题标题】:dart HTMLrequest default to index.html does not workdart HTMLrequest 默认为 index.html 不起作用
【发布时间】:2014-06-05 17:00:10
【问题描述】:

我有一个简单的 Dart HTTPServer 正在运行,它为virDir.serveRequest(request); 提供请求,它适用于 URL 192.168.1.200:8080/index.html,但如果我使用 192.168.1.200:8080192.168.1.200:8080/ 则提供 404 Not Found。我可能天真,尽管默认是自动的。顺便说一句,这对我来说是全新的。

我没有注意到 HTMLServer 中的任何默认设置,这是如何实现的?

(编辑)

我已经能够检测到使用默认值并计算出正确的文件名,但不明白如何将其传递给浏览器:

_processRequest(newPath, request) { // new path is index.html
  File file = new File(newPath);
  file.exists().then((found) {
    if(found) {
      file.openRead().pipe(request.response);  // probably dies here
      }
    else {
     _send404(request.response);
    }
   });
}

[编辑]

仍然无法提供 index.html 文件。我尝试使用 VirtualDirector.serveFile() 但在尝试处理默认 index.html 文件时无法使其工作。我一直在尝试效仿。

final HTTP_ROOT_PATH = Platform.script.resolve('../web').toFilePath();
final virDir = new VirtualDirectory(HTTP_ROOT_PATH)
  ..jailRoot = false  // process links will work
  ..followLinks = true
  ..allowDirectoryListing = true;

var dir = new Directory(virDir.root);
var indexUri = new Uri.file(dir.path).resolve('/index.html');
virDir.serveFile(new File(indexUri.toFilePath()), request);

当我运行它并打印 indexUri.toFilePath() 时,输出是 '/index.html'

我的代码位于 /srv/fireimager/bin 和 /srv/fireimager/web 中,后者是虚拟目录根目录。当我检测到用户没有在 url 中指定 /index.html 时,它不起作用,没有发出错误,并且 javascript 控制台没有显示任何内容,因此没有任何内容提供给浏览器。

我显然不明白如何使用 VirtualDirectly.serveFiler。

【问题讨论】:

标签: dart dart-io


【解决方案1】:

您的默认页面或 index.html 不是由您的浏览器而是由服务器假定的。

您要做的是检查您的请求以什么结尾。在我的服务器中,我检查它是否以“/”结尾。如果它确实以“/”结尾,我打开并发送我的默认页面。

此外,您需要确保检查您的请求,以确保它没有尝试访问它不应该访问的区域。检查路径段中的“..”。如果有,您可能想终止连接。

【讨论】:

  • 如果服务器访问请求时用户输入192.168.1.200:8080' is it modified to 192.168.1.200:8080/'?
  • 根据我的经验,是的 / 是自动附加的。
  • 当您在浏览器上发出请求时,所有请求都以 / 开头。
猜你喜欢
  • 2021-07-12
  • 2015-02-19
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
相关资源
最近更新 更多