文件Uri方案和相关文件

时间:2011-10-22 04:34:17

标签: file uri relative-path url-scheme file-uri

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

示例路径是'./.bashrc'。 fulluri看起来怎么样? 'file://./.bashrc'对我来说很奇怪。

6 个答案:

答案 0 :(得分:58)

简而言之,文件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

答案 1 :(得分:22)

使用完整文件是不可能的: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的“..”。

答案 2 :(得分:14)

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

答案 3 :(得分:8)

您不应在file:之后加上双斜杠。正确的格式是

'file:.bashrc'

请参见Optimizing the viewpath-rootless定义

答案 4 :(得分:3)

我不知道你的用例。

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

const url = "file://" + process.cwd() + "/" + ".bashrc";

答案 5 :(得分:1)

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

file://`pwd`/relative-path

在您的特定情况下:

file://`pwd`/.bashrc