File URI scheme
Encyclopedia
The file URI scheme is a URI scheme
URI scheme
In the field of computer networking, a URI scheme is the top level of the Uniform Resource Identifier naming structure. All URIs and absolute URI references are formed with a scheme name, followed by a colon character , and the remainder of the URI called the scheme-specific part...

 specified in RFC 1630 and RFC 1738, typically used to retrieve files from within one's own computer.

Format

A file URL 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. If host is omitted, it is taken to be "localhost", the machine from which the URL is being interpreted. Note that when omitting host you do not omit the slash ("file:///foo.txt" is okay, while "file://foo.txt" is not, although some interpreters manage to handle the latter).

Meaning of slash character

The slash character (/), depending on its position, is used in different meanings in a file URL.
  • The // after the file: is part of the general syntax of URLs. (The double slash // should always appear in a file URL according to the specification, but in practice many Web browser
    Web browser
    A web browser is a software application for retrieving, presenting, and traversing information resources on the World Wide Web. An information resource is identified by a Uniform Resource Identifier and may be a web page, image, video, or other piece of content...

    s allow you to omit it, in some cases at least.)
  • The single slash between host and path is part of the syntax of file URLs.
  • And the slashes in path separate directory names in a hierarchical system of directories and subdirectories. In this usage, the slash is a general, system-independent way of separating the parts, and in a particular host system it might be used as such in a pathname (as in Unix systems).

Unix

Here are two Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 examples pointing to the same /etc/fstab file:
file://localhost/etc/fstab
file:///etc/fstab

Windows

Here are some examples which may be accepted by some applications on Windows systems, referring to the same, local file c:\WINDOWS\clock.avi
file://localhost/c|/WINDOWS/clock.avi
file:///c|/WINDOWS/clock.avi
file://localhost/c:/WINDOWS/clock.avi
Here is the correct URI as understood by the Windows Shell API:
file:///c:/WINDOWS/clock.avi

For a local file, the last is the most obvious and human-readable, and also the canonical one understood by the operating system http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx.

Windows

On Microsoft Windows systems, the normal colon (:) after a device letter has sometimes been replaced by a vertical bar (|) in file URLs. This reflected the original URL syntax, which made the colon a reserved character in a path part.

Since Internet Explorer 4
Internet Explorer 4
Microsoft Internet Explorer 4 is a graphical web browser released in September 1997 by Microsoft, primarily for Microsoft Windows, but also with versions available for Apple Mac OS, Solaris, and HP-UX and marketed as "The Web the Way You Want It".It was one of the main participants of the first...

, file URIs have been standardised on Windows, and should follow the following scheme. This applies to all applications which use URLMON or SHLWAPI for parsing, fetching or binding to URIs. To convert a path to a URL, use UrlCreateFromPath, and to convert a URL to a path, use PathCreateFromUrl. http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx

To access a file "the file.txt", the following might be used.

For a network location:
file://hostname/path/to/the%20file.txt
Or for a local file, the hostname is omitted, but the slash is not (note the third slash):
file:///c:/path/to/the%20file.txt

This is not the same as providing the string "localhost" or the dot "." in place of the hostname. The string "localhost" will attempt to access the file as \\localhost\c:\path\to\the file.txt, which will not work since the colon is not allowed in a share name. The dot "." results in the string being passed as \\.\c:\path\to\the file.txt, which will work for local files, but not shares on the local system. For example file://./sharename/path/to/the%20file.txt will not work, because it will result in sharename being interpreted as part of the DOSDEVICES namespace, not as a network share.

The following outline roughly describes the requirements.
  • The colon should be used, and should not be replaced with a vertical bar.
  • Forward slashes should be used to delimit paths. note that while DOS requires backslashes, the Win32 API will accept either a forward or back slash.
  • Characters such as the hash (#) or question mark (?) which are part of the filename should be percent-encoded
    Percent-encoding
    Percent-encoding, also known as URL encoding, is a mechanism for encoding information in a Uniform Resource Identifier under certain circumstances. Although it is known as URL encoding it is, in fact, used more generally within the main Uniform Resource Identifier set, which includes both Uniform...

    .
  • Characters which are not allowed in URIs, but which are allowed in filenames, must also be percent-encoded. For example, any of "{}`^ " and all control characters. In the example above, the space in the filename is encoded as %20.
  • Characters which are allowed in both URIs and filenames must NOT be percent-encoded.
  • Must not use legacy ACP encodings. (ACP code pages are specified by DOS CHCP or Windows Control Panel language setting.)
  • Unicode characters outside of the ASCII
    ASCII
    The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

     range must be UTF-8
    UTF-8
    UTF-8 is a multibyte character encoding for Unicode. Like UTF-16 and UTF-32, UTF-8 can represent every character in the Unicode character set. Unlike them, it is backward-compatible with ASCII and avoids the complications of endianness and byte order marks...

     encoded, and those UTF-8 encodings must be percent-encoded.


Use the provided functions if you can. If you must create a URL programmatically and you cannot access SHLWAPI.dll (for example from script, or another programming environment where the equivalent functions are not available) the above outline will help.

Legacy URLs

To aid the installed base of legacy applications, the PathCreateFromUrl recognises certain URLs which do not meet these criteria, and treats them uniformly. These are called "legacy" file URLs as opposed to "healthy" file URLs. http://blogs.msdn.com/freeassociations/archive/2005/05/19/420059.aspx

In the past, A variety of other applications have used other systems. Some added an additional two slashes. For example, \\remotehost\share\dir\file.txt, would become file:////remotehost/share/dir/file.txt instead of the "healthy" file://remotehost/share/dir/file.txt.

Web pages

File URLs are rarely used in Web page
Web page
A web page or webpage is a document or information resource that is suitable for the World Wide Web and can be accessed through a web browser and displayed on a monitor or mobile device. This information is usually in HTML or XHTML format, and may provide navigation to other web pages via hypertext...

s on the Internet, since they make the assumption that such a file exists on the client's computer. The host specifier can be used to retrieve a file from an external source, although no specific file-retrieval protocol is specified; and using it should result in a message that informs the user that no mechanism to access that machine is available.

Mozilla

Mozilla browsers refuse to follow file URLs on a page that it has fetched with the HTTP protocol (i.e. the page's own URL is an http URL). When you click on such a link, nothing happens. The purpose is security: to prevent a remote page from executing a program on the visitor's computer. File links work on Mozilla on pages that are local files on the user's disk. It is not a surprise that Firefox 3.5.3 does nothing when requesting a file on a remote host:
file:////remotehost/share/dir/file.txt
However, it successfully opens:
file://localhost///remotehost/share/dir/file.txt
or its equivalent:
file://///remotehost/share/dir/file.txt
as it is referred to as a file on the local host.

Mozilla browsers can be configured to override this security restriction as detailed in Mozillazine's "Links to Local Pages Don't Work".

Mozilla browsers also treat file URLs similarly to the Gopher protocol in the way a directory is represented textually (i.e. the source) and graphically.

Internet Explorer

Internet Explorer browsers, prior to version 7, will attempt to access file: URLs even if they reside on pages fetched over HTTP.

Other

The original Web browser, WorldWideWeb
WorldWideWeb
WorldWideWeb, later renamed to Nexus to avoid confusion between the software and the World Wide Web, was the first web browser and editor. When it was written, WorldWideWeb was the only way to view the Web....

, provided editing of resources in file space http://www.w3.org/People/Berners-Lee/WorldWideWeb.html. Amaya
Amaya (web browser)
Amaya is a free and open source WYSIWYG web authoring tool with browsing abilities, created by a structured editor project at the INRIA, a French national research institution, and later adopted by the World Wide Web Consortium . Amaya is used as a testbed for web standards and replaced the Arena...

still has this ability.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK