文件 Uri 方案和相关文件

发布于 2024-12-11 17:19:12 字数 140 浏览 0 评论 0原文

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

示例路径为 ./.bashrc。完整的 uri 看起来如何? file://./.bashrc 对我来说似乎很奇怪。

Assume that the scheme for a uri is "file". Also assume that the path starts with .

An example path is ./.bashrc. How would the full uri look? file://./.bashrc appears odd to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小情绪 2024-12-18 17:19:13

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

In short, a file URL takes the form of:

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

or you can omit the host (but not the slash):

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

but not this:

file://file_at_current_dir [no way]

nor this:

file://./file_at_current_dir [no way]

I just confirmed that via Python's urllib2.urlopen()

More detail from 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
小清晰的声音 2024-12-18 17:19:13

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

It's impossible to use full file: URI with '.' or '..' segments in path without root part of that path. Whether you use 'file://./.bashrc' or 'file:///./.bashrc' these paths will have no sense. If you want to use a relative link, use it without protocol/authority part:

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

If you want to use full URI, you must tell a root relative to which your relative path is:

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

According to 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 describes even an algorithm of removing these "." and ".." from URI.

月亮是我掰弯的 2024-12-18 17:19:13

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

In a terminal you could type "file://$PWD/.bashrc" using "$PWD" to refer to the current directory.

腹黑女流氓 2024-12-18 17:19:13

您不应在 file: 后添加双斜杠。正确的形式是

'file:.bashrc'

参见 RFC 3986path-rootless定义

You should not put double slash after file:. Correct form is

'file:.bashrc'

See RFC 3986, path-rootless definition

咿呀咿呀哟 2024-12-18 17:19:13

我不知道你的用例。

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

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

I don't know your use case.

I have a similar need in my node code, so when I need a file url relative to my working directory I create a url like so ...

const url = "file://" + process.cwd() + "/" + ".bashrc";
无风消散 2024-12-18 17:19:13

URI 始终是绝对的(除非它们是相对 URI,这是一种没有模式的不同野兽)。这是因为它们是一种服务器-客户端技术,引用服务器的工作目录是没有意义的。话又说回来,引用文件系统在服务器-客户端上下文中也没有意义

URIs are always absolute (unless they're relative URIs, which is a different beast without a schema). That comes from them being a server-client technology where referencing the server's working directory doesn't make sense. Then again, referencing the file system doesn't make sense in a server-client context either ????. Nevertheless, RFC 8089 permits only absolute paths:

The path component represents the absolute path to the file in the file system.

However, if I were to postulate a non-standard extension, I would choose the following syntax:

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

The explanation is that RFC 8089 specifies non-local paths file://<FQDN of host>/path and local paths file:/path, file://localhost/path, and file:///path. Since we're almost certainly trying to specify a local relative path (ie, accessible by "local file system APIs"), and because a . is not a FQDN or even a hostname, the simple file: scheme + scheme-sepecific-part URI syntax makes the most sense.

ゃ懵逼小萝莉 2024-12-18 17:19:13

在 unix shell 脚本中,我设法做到这一点:

file://`pwd`/relative-path

在您的特定情况下:

file://`pwd`/.bashrc

In a unix shell script I managed to go with this:

file://`pwd`/relative-path

In your particular case:

file://`pwd`/.bashrc
星星的轨迹 2024-12-18 17:19:13

有一个解决方法可能会有所帮助。

如果在开发时您只能指定文件的相对路径,但需要 URL(需要知道绝对路径),请使用如下代码 (Java):

new File("relative/path/to/file").toURI().toURL();

这样您就可以获得仍然指向相对路径中的文件的 URL 。它仍然显示在 URL 中,但可以打开。如果您想删除像“..”这样的组件,请使用中间的规范路径。这是一个对我有用的例子:

public static void main(String[] args) throws MalformedURLException, IOException {
  File f = new File("../SimResolver/pom.xml");
  System.out.println(f);
  System.out.println(f.getAbsolutePath());
  System.out.println(f.getCanonicalPath());
  System.out.println(f.toURI());     
  System.out.println(f.toURI().toURL());
  System.out.println(f.getCanonicalFile().toURI());     
  System.out.println(f.getCanonicalFile().toURI().toURL());
  
  InputStream in = f.toURI().toURL().openStream();
  byte[] buffer = new byte[8192];

  int read = in.read(buffer);
  while (read >= 0) {
    System.out.write(buffer, 0, read);
    read = in.read(buffer);
  }
}

There is a workaround that might help.

If at development time you can only specify a relative path to the file but need a URL (that requires to know the absolute path) use code like this (Java):

new File("relative/path/to/file").toURI().toURL();

With that you get a URL still pointing to the file in a relative path. It still shows in the URL but it can be opened. If you want to get rid of components like ".." use the canoncial path inbetween. Here is an example that works for me:

public static void main(String[] args) throws MalformedURLException, IOException {
  File f = new File("../SimResolver/pom.xml");
  System.out.println(f);
  System.out.println(f.getAbsolutePath());
  System.out.println(f.getCanonicalPath());
  System.out.println(f.toURI());     
  System.out.println(f.toURI().toURL());
  System.out.println(f.getCanonicalFile().toURI());     
  System.out.println(f.getCanonicalFile().toURI().toURL());
  
  InputStream in = f.toURI().toURL().openStream();
  byte[] buffer = new byte[8192];

  int read = in.read(buffer);
  while (read >= 0) {
    System.out.write(buffer, 0, read);
    read = in.read(buffer);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文