web-dev-qa-db-ja.com

ファイルURIスキームと相対ファイル

Uriのスキームが「ファイル」であると仮定します。また、パスが「。」で始まると仮定します。

パスの例は「./.bashrc」です。フルーリはどのように見えますか? 「file://./.bashrc」は奇妙に見えます。

56
user592419

要するに、ファイル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
63
RayLuo

完全なファイルを使用することは不可能です: '。'付きの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からの「..」.

22
kinORnirvana

ターミナルでは、「$ PWD」を使用して「file://$PWD/.bashrc」と入力し、現在のディレクトリを参照できます。

14
andz

file:の後に二重スラッシュを入れないでください。正しい形式は

'file:.bashrc'

RFC 3986path-rootlessの定義を参照してください

10
Maxim Reznik

ユースケースがわかりません。

私は私のノードコードにも同様のニーズがありますので、作業ディレクトリに相対的なファイルURLが必要なときは、そのようなURLを作成します...

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

UNIXシェルスクリプトで、私はこれでうまくいくことができました。

file://`pwd`/relative-path

特定の場合:

file://`pwd`/.bashrc
1
1234ru