Ruby URI module produces illegal file-URI

2019-05-04 10:07发布

问题:

This is a follow-up question to Converting file path to URI.

Consider:

require 'uri'

uri = URI.join('file:///', '/home/user/dir1/dir2/dir3/name.ext')
 => #<URI::Generic:0x0000000263fcc0 URL:file:/home/user/dir1/dir2/dir3/name.ext>

uri.to_s
 => "file:/home/user/dir1/dir2/dir3/name.ext"

Isn't the result illegal? Shoudln't it be "file://home/...", with a double slash?

回答1:

No. file://home/... refers to a file on the host named 'home'. The full syntax is file:///home/..., with three slashes, where the empty host component indicates the local host. However, most URI parsers that recognize the 'file' scheme also accept file:/pathname with only one slash; the lack of doubled slashes means the host component is skipped.



标签: ruby uri