【问题标题】:File Uri Scheme and Relative Files文件 Uri 方案和相关文件
【发布时间】:2011-12-13 00:47:09
【问题描述】:

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

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

【问题讨论】:

  • 根据维基百科en.wikipedia.org/wiki/Uniform_resource_identifier 显然您可以省略该方案,并在您相对引用时将“./.bashrc”作为uri。然而,这只是一个猜测,我不确定它是否真的是这样工作的。
  • @Tony - 谢谢,这可以很好地在 .docx 文件中进行相对引用 - 只需解压缩,找到“file:///long-absolute-path/relative-path”引用,然后替换与“相对路径”
  • 严格省略前缀并不总是有效,因为 URI 可以包含用百分号编码的特殊字符(例如 %20 = 空格);根据应用程序,您可能需要将转义字符替换为其实际表示。

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


【解决方案1】:

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

【讨论】:

  • 是否也可以使用 file:/absolute/path 或 file:relative (即使这不起作用),因为您可以删除文件协议的权限。
  • @RayLuo 仍然没有回答如何使用“。”的相对文件语法创建 URI 的问题。 ?
  • @cogmission 你没有得到我回答中的第四个例子吗?它明确提到没有办法使用“。”在 URI 中。好吧,你可以,它只是没有任何意义,并且不会达到你可能期望的结果。您也可以参考2nd-highest upvoted answer right in this page。不过,您阅读和弄清楚的时间会更长。
  • §4.2 of RFC3986 说相对引用很好。 Canonical process for resolving the reference 在给定的上下文(“基本 URI”)内。第一个“不可能”是忽略 URI 的结构,绝不是指路径。第二个是文件系统根目录下名为file_at_current_dir 的文件。我use Python in this example 真正强调了构建字符串没有当前目录这一事实。
  • @amcgregor 正如您所引用的,RFC3986 中的相对引用仅适用于给定的上下文,即“基本 URI”。如果它位于使用http://https:// 方案的网页内,则可以满足该条件。但是在 OP 要求的 file:// 方案中,它在语义上取决于调用程序的 CWD 在哪里,即使该调用程序设法解释该 URI。在实践中,您将在哪里使用 file:// uri?如果它是一个 CLI 工具,您可以完全避免 file:// 并回退到老派本地路径。如果它在浏览器中,我们也不能假设它的 CWD。​​span>
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 这也适用于相对路径,“file://$PWD/../parentchilddir/somefile.txt”
    • 正如@kai-dj 在另一个答案中提到的那样,如果$PWD 包含像C:/Users/Joshua Pinter/ 这样的空格,那么路径将无效。需要以某种方式逃脱。
    • 您可以使用变量字符串替换,空格作为转义空格,例如file://${PWD// /\\ }/relative/path
    • 次要注意,反斜杠转义不是正确的形式。这是一个 URI。 Use percent encoding 注意空格字符本身可以优化为+。这有一个附加说明,URI 允许 UTF-8,因此许多 Unicode 字符(例如重音字母、表情符号、§ 等符号)实际上根本不需要编码。另请注意:未编码的 URI 将由用户代理自动编码,因此用作字符串(包含空格)可能是正常的。 (空格是 shell 扩展/进程参数列表构建的问题。)
    【解决方案4】:

    我不知道您的用例。

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

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

    【讨论】:

      【解决方案5】:

      你不应该在file: 后面加上双斜杠。正确的形式是

      'file:.bashrc'
      

      参见RFC 3986path-rootless 定义

      【讨论】:

      • 请参考同一个 RFC,Syntax Components, §3 的定义和 §3.2 权限(描述其相对组成,其中涉及//),然后记下§2 of the RFC defining the file: scheme,它只允许@987654329 @。相对 file: URI 在技术上并不存在,即使某些系统按照惯例允许它们。
      • 这让 PyLD JSON-LD 解析引擎在尝试消除这个超级烦人的错误时很高兴:Invalid JSON-LD syntax; @context @id value must be an absolute IRI, a blank node identifier, or a keyword.。我只是想要一个相对 ID,让我来做吧!
      【解决方案6】:

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

      file://`pwd`/relative-path
      

      在您的特定情况下:

      file://`pwd`/.bashrc
      

      【讨论】:

      • 在 VIM 中呢?
      • @71GA,不知道:/我不知道 vim 足以回答你的问题。
      【解决方案7】:

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2011-02-20
        • 1970-01-01
        • 2012-06-14
        • 2013-07-30
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多