URI.parse的替代方法,允许主机名包含下划线

我正在使用DMOZ 的url主题列表 ,其中包含一些主机名包含下划线的url。

例如:

608  609 The Outer Heaven 610 Information and image gallery of McFarlane's action figures for Trigun, Akira, Tenchi Muyo and other Japanese Sci-Fi animations. 611 Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures 612  

虽然这个url可以在网络浏览器中使用(或者,至少在我的网站中也是如此:p), 根据标准 , 这是不合法的 :

主机名可能不包含其他字符,例如下划线字符(_),

尝试使用URI.parse解析此类URL时会导致错误:

 [2] pry(main)> require 'uri' => true [3] pry(main)> URI.parse "http://outer_heaven4.tripod.com/index2.htm" URI::InvalidURIError: the scheme http does not accept registry part: outer_heaven4.tripod.com (or bad hostname?) from ~/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:213:in `initialize' 

是否有URI.parse的替代URI.parse我可以使用具有较低严格性而不仅仅是自己滚动?

尝试Addressable :: URI 。 它比RFC更紧密地遵循RFC并且非常灵活。

 require 'addressable/uri' uri = Addressable::URI.parse('http://outer_heaven4.tripod.com/index2.htm') uri.host => "outer_heaven4.tripod.com" 

我已将它用于某些项目,并对此感到满意。 URI变得有点……生锈,需要TLC。 其他人也评论过它:

http://www.cloudspace.com/blog/2009/05/26/replacing-rubys-uri-with-addressable/

几年前在Ruby开发人员中对URI的状态进行了相当多的讨论。 我现在找不到它的链接,但有人建议使用Addressable :: URI作为替代品。 我不知道是否有人加紧接管URI开发,或者现在的情况。 在我自己的代码中,我继续使用URI来处理简单的事情,并在URIcertificate为我做错事时切换到Addressable :: URI。