URI.join broken?


#1

Hi,

I have been looking at the documentation and the source of URI.join and
can’t see what I have misunderstood.

http://ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html#M009245

I except URI.join(“http://test.de/test1”, “test2”) in an URI which looks
like http://test.de/test1/test2 and not like http://test.de/test2. And
from reading the documentation I even expect that
URI.join(“http://test.de/”, “test1”, “test2”) results in
http://test.de/test1/test2 and not http://test.de/test2

So, that do I misunderstand about the URI class? :slight_smile: I am using Ruby
1.8.4 from Darwinports on Mac OS X.

Best regards,
Oliver


#2

On 09/04/06, Oliver A. removed_email_address@domain.invalid wrote:

I except URI.join(“http://test.de/test1”, “test2”) in an URI which looks
like http://test.de/test1/test2 and not like http://test.de/test2. And
from reading the documentation I even expect that
URI.join(“http://test.de/”, “test1”, “test2”) results in
http://test.de/test1/test2 and not http://test.de/test2

That’s a strange result. It works as expected for me, also using 1.8.4
via Darwin Ports on OS X:

URI.join(“http://test.de/test1”, “test2”).to_s #=>
http://test.de/test2

I don’t think you’re missing anything: you are getting anomalous
output. Are you testing it with the same input data?

Paul.


#3

Paul B. wrote:

On 09/04/06, Oliver A. removed_email_address@domain.invalid wrote:

I except URI.join(“http://test.de/test1”, “test2”) in an URI which looks
like http://test.de/test1/test2 and not like http://test.de/test2. And
from reading the documentation I even expect that
URI.join(“http://test.de/”, “test1”, “test2”) results in
http://test.de/test1/test2 and not http://test.de/test2

That’s a strange result. It works as expected for me, also using 1.8.4
via Darwin Ports on OS X:

URI.join(“http://test.de/test1”, “test2”).to_s #=>
http://test.de/test2

Can you explain to me why this is correct? I would expect
http://test.de/test1/test2 as the result of your call.

Oliver


#4

On Apr 9, 2006, at 6:22 PM, Oliver A. wrote:

Can you explain to me why this is correct? I would expect
http://test.de/test1/test2 as the result of your call.

Oliver

Consider a web browser:

If you are at the site

http://example.com/a

and you click a link whose href is b, its relative and you go to
http://example.com/b

If however the webs server lets you browse directories
and you are at http://example.com/dir/ and you click a link a, you’ll
end up at

http://example.com/dir/a

URL/URIs aren’t quite the same as file paths


#5

Can you explain to me why this is correct? I would expect
http://test.de/test1/test2 as the result of your call.

Sorry, I misread your original statement, which might make my reply
confusing.

You are getting the correct results, as others have said.

It works just like in a browser:
http://example.com/one + two => http://example.com/two
http://example.com/one/ + two => http://example.com/one/two
http://example.com/one + /two => http://example.com/two
http://example.com/one/ + /two => http://example.com/two

Paul.


#6

On Apr 10, 2006, at 12:22 AM, Oliver A. wrote:

Paul B. wrote:

URI.join(“http://test.de/test1”, “test2”).to_s #=>
http://test.de/test2

Can you explain to me why this is correct? I would expect
http://test.de/test1/test2 as the result of your call.

Think of it this way –

You are in your web-browser on http://test.de/test1. There is a link
to “test2”:

<a href="test2">test2</a>

Would you expect it to go to http://test.de/test1/test2? No, it would
go to http://test.de/test2.

You want:

URI.join(“http://test.de/test1/”, “test2”)
^
URI.join(“http://test.de/”, “test1/”, “test2”)
^

– Daniel


#7

Paul B. wrote:

Can you explain to me why this is correct? I would expect
http://test.de/test1/test2 as the result of your call.

Sorry, I misread your original statement, which might make my reply
confusing.

You are getting the correct results, as others have said.

It works just like in a browser:
http://example.com/one + two => http://example.com/two
http://example.com/one/ + two => http://example.com/one/two
http://example.com/one + /two => http://example.com/two
http://example.com/one/ + /two => http://example.com/two

Thanks Guys,

now I know at least the logic behind URI.join. I am not sure, if it is
the method of choice for my problem, but I have to think about it and
may be I come back with another question.

Oliver