Issue #14035 has been updated by phluid61 (Matthew Kerwin).


The spec for file URIs is [RFC8089](https://tools.ietf.org/html/rfc8089), which allows for file URIs with no authority part (e.g. 'file:/foo').

Neither it nor RFC3986 make any claims about canonical representation of URIs, much as I may have liked it to.

Also, have a look at the ['file-uri' gem](https://rubygems.org/gems/file-uri). 

----------------------------------------
Bug #14035: URI module file representation uses the minimal form instead of the traditional one.
https://bugs.ruby-lang.org/issues/14035#change-67394

* Author: ioggstream (Roberto Polli)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
### Reproduce

execute URI("file:///etc/hosts").to_s 


### I expect

  - URI("file:///etc/hosts").to_s == "file:///etc/hosts"

  - URI to represent file:// paths in canonical form (Eg. see "file:///etc/hosts" in https://tools.ietf.org/html/rfc3986#section-1.1)

### Instead 

  - URI("file:///etc/hosts").to_s == "file:/etc/hosts"

  - URI("file:///etc/hosts").host == nil

### Fix for Ruby 2.4

Adopting the convention

  - file schema to have a blank/empty "" host  

suggested in rfc3986 will fix the issue.

```
   If the URI scheme defines a default for host, then that default
   applies when the host subcomponent is undefined or when the
   registered name is empty (zero length).  For example, the "file" URI
   scheme is defined so that no authority, an empty host, and
   "localhost" all mean the end-user's machine, whereas the "http"
   scheme considers a missing authority or empty host invalid.
```
 

This is a proposed implementation.

```
require 'uri/generic'

module URI
  class FILE < Generic
    def initialize(*args)
      super(*args)
      @host = "" if @host.nil?
    end
  end
  @@schemes['FILE'] = FILE
end
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>