86

假设 uri 的方案是“文件”。还假设路径以“。”开头。

一个示例路径是“./.bashrc”。fulluri 的外观如何?'file://./.bashrc' 对我来说很奇怪。

4

7 回答 7

86

简而言之,文件 URL 采用以下形式:

file://localhost/absolute/path/to/file [ok]

或者你可以省略主机(但不是斜杠):

file:///absolute/path/to/file [ok]

但不是这个:

file://file_at_current_dir [no way]

也不是这个:

file://./file_at_current_dir [no way]

我刚刚通过 Python 的 urllib2.urlopen() 确认了这一点

来自http://en.wikipedia.org/wiki/File_URI_scheme的更多详细信息:

"file:///foo.txt" is okay, while "file://foo.txt" is not,
although some interpreters manage to handle the latter
于 2013-03-04T02:40:30.727 回答
25

不可能使用完整的文件:带有 '.' 的 URI 或“..”路径中的段,没有该路径的根部分。无论您使用 'file://./.bashrc' 还是 'file:///./.bashrc' 这些路径都没有意义。如果要使用相对链接,请在没有协议/权限部分的情况下使用它:

<a href="./.bashrc">link</a>

如果你想使用完整的 URI,你必须告诉一个根相对你的相对路径是:

<a href="file:///home/kindrik/./.bashrc">link</a>

根据RFC 3986

The path segments "." and "..", also known as dot-segments, are
defined for relative reference within the path name hierarchy.  They
are intended for use at the beginning of a relative-path reference
(Section 4.2) to indicate relative position within the hierarchical
tree of names.  This is similar to their role within some operating
systems' file directory structures to indicate the current directory
and parent directory, respectively.  However, unlike in a file
system, these dot-segments are only interpreted within the URI path
hierarchy and are removed as part of the resolution process (Section
5.2).

The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2).  However, some
deployed implementations incorrectly assume that reference resolution
is not necessary when the reference is already a URI and thus fail to
remove dot-segments when they occur in non-relative paths.  URI
normalizers should remove dot-segments by applying the
remove_dot_segments algorithm to the path, as described in Section 5.2.4.

The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2) 

RFC 3986 甚至描述了删除这些“.”的算法。和来自 URI 的“..”。

于 2013-10-06T00:49:24.497 回答
20

在终端中,您可以使用“$PWD”键入“file://$PWD/.bashrc”来引用当前目录。

于 2014-12-14T17:07:29.233 回答
19

你不应该在file:. 正确的形式是

'file:.bashrc'

参见RFC 3986path-rootless定义

于 2018-10-31T12:06:15.987 回答
6

我不知道你的用例。

我的节点代码也有类似的需求,所以当我需要一个相对于我的工作目录的文件 url 时,我会创建一个这样的 url ...

const url = "file://" + process.cwd() + "/" + ".bashrc";
于 2018-04-18T12:59:51.567 回答
1

在一个 unix shell 脚本中,我设法做到了:

file://`pwd`/relative-path

在您的特定情况下:

file://`pwd`/.bashrc
于 2018-11-03T16:20:59.673 回答
0

URI 始终是绝对的(除非它们是相对 URI,这是没有模式的不同野兽)。这是因为它们是一种服务器-客户端技术,在这种技术中引用服务器的工作目录没有意义。再说一次,在服务器-客户端上下文中引用文件系统也没有意义。尽管如此,RFC 8089只允许绝对路径:

路径组件表示文件系统中文件的绝对路径。

但是,如果我要假设一个非标准扩展,我会选择以下语法:

file:file.txt
file:./file.txt

解释是 RFC 8089 指定了非本地路径file://<FQDN of host>/path和本地路径file:/path, file://localhost/path, 和file:///path. 由于我们几乎肯定会尝试指定本地相对路径(即,可通过“本地文件系统 API”访问),并且因为 a.不是 FQDN 甚至不是主机名,简单的file:方案 + 方案特定部分 URI 语法使得最有意义的。

于 2021-11-19T08:34:53.807 回答