# URI class bug?

**URL:** <https://www.ruby-forum.com/t/uri-class-bug/105980>\
**Category:** Ruby\
**Created:** [June 5, 2007, 8:35am UTC](https://www.ruby-forum.com/t/uri-class-bug/105980 "2007-06-05T08:35:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 8:35am UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/1 "2007-06-05T08:35:27Z")

</div>

I am using Ruby 1.8.6. I found URI cannot parse URI with “\_” is host.

uri = “http://dr\_gabriele.podomatic.com/enclosure/  
2006-08-03T15\_09\_59-07\_00.m4v”  
URI.parse(uri)

Is there any way to work around that?  
thanks

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 9:01am UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/2 "2007-06-05T09:01:30Z")

</div>

On 6/5/07, Morgan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using Ruby 1.8.6. I found URI cannot parse URI with “\_” is host.
> 
> uri = “http://dr\_gabriele.podomatic.com/enclosure/  
> 2006-08-03T15\_09\_59-07\_00.m4v”  
> URI.parse(uri)
> 
> Is there any way to work around that?  
> thanks

It seems underscores are not allowed in host part of an URI. So it’s  
not a bug. See RFC 2396 (URI), and 1035 (DNS). If you really want it,  
you can open the class and redefine some of the methods and/or  
manually edit URI sources.

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 9:41am UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/3 "2007-06-05T09:41:33Z")

</div>

On Jun 5, 3:00 pm, “Jano S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems underscores are not allowed in host part of an URI. So it’s  
> not a bug. See RFC 2396 (URI), and 1035 (DNS). If you really want it,  
> you can open the class and redefine some of the methods and/or  
> manually edit URI sources.

In RFC 2396, “\_” is taken as “Unreserved Characters”.  
Unreserved characters can be escaped without changing the semantics  
of the URI, but this should not be done unless the URI is being  
used  
in a context that does not allow the unescaped character to appear.

However, URI.escape doesn’t escape “\_”.

require ‘URI’  
original\_uri = “http://dr\_gabriele.podomatic.com/enclosure/”  
uri = URI.escape(original\_uri)  
puts uri == original\_uri

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 11:51am UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/4 "2007-06-05T11:51:01Z")

</div>

On Jun 5, 5:24 pm, “Jano S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > 2006-08-03T15\_09\_59-07\_00.m4v"  
> > > > In RFC 2396, “_" is taken as “Unreserved Characters”.  
> > > > puts uri == original\_uri  
> > > > alpha = lowalpha | upalpha  
> > > > There’s no "_” there. YMMV 😉  
> > > > Thanks a lot for your help.

I am just wandering, internet is a wild world. Wierd non-standard  
stuff is all around. The non-standard host name is a example. Popular  
browser can handle these URLs well.  
Perhaps ruby should be more strong to survive better in such wild  
world 🙂

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 11:24am UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/5 "2007-06-05T11:24:59Z")

</div>

On 6/5/07, Morgan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > thanks  
> > > used  
> > > in a context that does not allow the unescaped character to appear.
> 
> However, URI.escape doesn’t escape “\_”.
> 
> require ‘URI’  
> original\_uri = “http://dr\_gabriele.podomatic.com/enclosure/”  
> uri = URI.escape(original\_uri)  
> puts uri == original\_uri

I’m no expert on DNS, this is what I have found in appendix A:

host = hostname | IPv4address  
hostname = _( domainlabel “.” ) toplabel [“.”]  
domainlabel = alphanum | alphanum ( alphanum | “-” ) alphanum  
toplabel = alpha | alpha ( alphanum | “-” ) alphanum  
IPv4address = 1digit “.” 1digit “.” 1_digit “.” 1\*digit

alphanum = alpha | digit  
alpha = lowalpha | upalpha

lowalpha = “a” | “b” | “c” | “d” | “e” | “f” | “g” | “h” | “i” |  
“j” | “k” | “l” | “m” | “n” | “o” | “p” | “q” | “r” |  
“s” | “t” | “u” | “v” | “w” | “x” | “y” | “z”  
upalpha = “A” | “B” | “C” | “D” | “E” | “F” | “G” | “H” | “I” |  
“J” | “K” | “L” | “M” | “N” | “O” | “P” | “Q” | “R” |  
“S” | “T” | “U” | “V” | “W” | “X” | “Y” | “Z”  
digit = “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” |  
“8” | “9”

There’s no “\_” there. YMMV 😉

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/6 "2007-06-05T13:13:46Z")

</div>

Morgan C. wrote:

> On Jun 5, 5:24 pm, “Jano S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > There’s no “\_” there. YMMV 😉
> 
> Thanks a lot for your help.
> 
> I am just wandering, internet is a wild world. Wierd non-standard  
> stuff is all around. The non-standard host name is a example. Popular  
> browser can handle these URLs well.  
> Perhaps ruby should be more strong to survive better in such wild  
> world 🙂  
> There was mention a few days ago of bringing the URI class up to a more  
> recent RFC compliance (3986, I think). Would that help in this  
> instance?

---

<div class="post-metadata">

**Author:** ![mog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mog](https://www.ruby-forum.com/u/mog)\
**Post date:** [June 5, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/uri-class-bug/105980/7 "2007-06-05T13:14:47Z")

</div>

On 6/5/07, Morgan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > > uri = "http://dr\_gabriele.podomatic.com/enclosure/
> > 
> > > uri = URI.escape(original\_uri)  
> > > alphanum = alpha | digit
> > 
> > There’s no “\_” there. YMMV 😉
> 
> Thanks a lot for your help.
> 
> I am just wandering, internet is a wild world. Wierd non-standard  
> stuff is all around. The non-standard host name is a example. Popular  
> browser can handle these URLs well.  
> Perhaps ruby should be more strong to survive better in such wild  
> world 🙂

If you want to really use underscores, modify lib/1.8/uri/common.rb:  
add the following after line HOSTNAME=… and comment out (prefix with  
#) the original HOSTNAME line.

```
  ALPHA_ = "a-zA-Z_"
  ALNUM_ = "#{ALPHA_}\\d"
  DOMLABEL_ = "(?:[#{ALNUM_}](?:[-#{ALNUM_}]*[#{ALNUM_}])?)"
  TOPLABEL_ = "(?:[#{ALPHA_}](?:[-#{ALNUM_}]*[#{ALNUM_}])?)"
  HOSTNAME = "(?:#{DOMLABEL_}\\.)*#{TOPLABEL_}\\.?"

```

J.
