Project

General

Profile

Actions

Bug #6057

closed

URI - Nonsensical Behavior

Added by sgonyea (Scott Gonyea) about 12 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
ruby -v:
1.9.3-p125
[ruby-core:42789]

Description

The behavior of Ruby's URI is confusing, to say the least.

URI("http://foo.com/bar") + "baz"
# => #<URI::HTTP:0x710713de URL:http://foo.com/baz> 

URI.join("http://foo.com/baz", "bar")
# => #<URI::HTTP:0x29fd5dc4 URL:http://foo.com/bar>

I could at least understand this behavior being the case if we were adding "/bar" (although it'd still be non-intuitive). That URI throws away its current path makes it a tedious library. Net::HTTP expects it as an input, yet as you interact with URI, you are constantly massaging it into and out of string form.

In my opinion, URI should model the behavior of Pathname.


Files

smime.p7s (6.22 KB) smime.p7s Anonymous, 02/22/2012 08:59 PM

Related issues 2 (0 open2 closed)

Related to Ruby master - Bug #15276: URI.join behaviour is unintuitiveClosedActions
Has duplicate Ruby master - Bug #10075: URI#join needs documentation of its behaviorClosedzzak (zzak _)07/19/2014Actions

Updated by sgonyea (Scott Gonyea) about 12 years ago

Edit: Moronic comment redacted.

Updated by sgonyea (Scott Gonyea) about 12 years ago

I'd be happy to write this improved functionality. But would it be welcome?

Updated by sgonyea (Scott Gonyea) about 12 years ago

Another example:

URI.join("http://foo.com/baz", "bar", "bim")

=> #<URI::HTTP:0x1005412e URL:http://foo.com/bim>

Also, if we did model the behavior of Pathname, then joining of "/baz" would reset the path which I do think is acceptable and good behavior.

Updated by duerst (Martin Dürst) about 12 years ago

I have only done a few quick tests, but it seems that the '+' operator implements relative resolution (see http://tools.ietf.org/html/rfc3986#section-5.2). For URIs, this makes a lot of sense.

Updated by sgonyea (Scott Gonyea) about 12 years ago

Ah. That's the issue.

1.9.3-p125 :016 > uri = URI("http://google.com/foo/bar/")
=> #<URI::HTTP:0x007fd97a891038 URL:http://google.com/foo/bar/>
1.9.3-p125 :017 > uri + "baz"
=> #<URI::HTTP:0x007fd97a887ab0 URL:http://google.com/foo/bar/baz>

If the URI's path itself has a trailing "/" then it will append the new path.

Likewise:

1.9.3-p125 :023 > URI.join("http://google.com/foo/", "bar", "baz")
=> #<URI::HTTP:0x007fd97b8c7308 URL:http://google.com/foo/baz>
1.9.3-p125 :024 > URI.join("http://google.com/foo/", "bar/", "baz")
=> #<URI::HTTP:0x007fd97b8ba928 URL:http://google.com/foo/bar/baz>
1.9.3-p125 :025 >

How very unintuitive :(. I guess the next question would be if URI.join actually needs to follow the merge rules to the letter, but I imagine that argument is moot :).

Updated by Anonymous about 12 years ago

In my opinion, URI should model the behavior of Pathname.

I totally agree.

Additionally I absolutely dislike using global methods instead of class
factory methods. I think it isn't at all obvious and it poisons the
global namespace.

URI.parse(args) over Kernel#URI(args)

Updated by drbrain (Eric Hodel) about 12 years ago

Changing the behavior of URI#+ from the current will break many libraries (including mechanize) that depend upon the current behavior of relative resolution.

The current behavior is frequently used to combine, for example, an anchor href with the current location to generate a new, correct URI if the href is a relative URI like "foo" or "../foo", a complete path like "/foo" or a complete URI like "http://other.example/bar".

Updated by Anonymous about 12 years ago

Scott Gonyea writes:

Issue #6057 has been updated by Scott Gonyea.

I'd be happy to write this improved functionality. But would it be welcome?

Is there anyone who actually likes this behavior?

Actions #9

Updated by ko1 (Koichi Sasada) about 12 years ago

  • Assignee set to akira (akira yamada)
Actions #10

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned
Actions #11

Updated by mame (Yusuke Endoh) over 5 years ago

  • Description updated (diff)
Actions #12

Updated by mame (Yusuke Endoh) over 5 years ago

  • Has duplicate Bug #10075: URI#join needs documentation of its behavior added
Actions #13

Updated by mame (Yusuke Endoh) over 5 years ago

  • Related to Bug #15276: URI.join behaviour is unintuitive added

Updated by mame (Yusuke Endoh) over 5 years ago

  • Status changed from Assigned to Closed

This ticket was duplicated by Bug #10075: URI#join needs documentation of its behavior, and that ticket has been already closed with documentation improvement. So I'm closing this.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0