Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Uri Scheme and Relative Files

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

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

like image 219
user592419 Avatar asked Oct 22 '11 04:10

user592419


People also ask

What is a URI filename?

A file URI takes the form of file://host/path. where host is the fully qualified domain name of the system on which the path is accessible, and path is a hierarchical directory path of the form directory/directory/.../name.

What is a relative file path?

A relative path is a way to specify the location of a directory relative to another directory. For example, suppose your documents are in C:\Sample\Documents and your index is in C:\Sample\Index. The absolute path for the documents would be C:\Sample\Documents.

Is file a URL or URI?

file is a registered URI scheme (for "Host-specific file names"). So yes, file URIs are URLs.

How do I find the URI of a file?

To get File Uri from a absolute path of File you can use DocumentFile. fromFile(new File(path, name)), it's added in Api 22, and returns null for versions below. Show activity on this post. Uri uri = Uri.


Video Answer


2 Answers

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 
like image 120
RayLuo Avatar answered Sep 22 '22 21:09

RayLuo


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.

like image 23
kinORnirvana Avatar answered Sep 25 '22 21:09

kinORnirvana