Content-Length: 276168 | pFad | https://github.com/sporkmonger/addressable/issues/363

49 `Addressable::URI::InvalidURIError (Hostname not supplied: '')` when using `omit` · Issue #363 · sporkmonger/addressable · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressable::URI::InvalidURIError (Hostname not supplied: '') when using omit #363

Open
noff opened this issue Oct 24, 2019 · 6 comments
Open
Labels

Comments

@noff
Copy link

noff commented Oct 24, 2019

Hello. Here is an example:

Addressable::URI.parse("https://user:password@dev.domain.com").omit(:scheme, :port, :user, :password, :host)
Traceback (most recent call last):
        1: from (irb):22
Addressable::URI::InvalidURIError (Hostname not supplied: '')

It raises only with omit method.

When try to parse URL and use an object, everything works great:

u = Addressable::URI.parse("https://user:password@dev.domain.com").
u.path
=> ""
@dentarg
Copy link
Collaborator

dentarg commented Oct 24, 2019

@noff what version of Addressable are you using?

@noff
Copy link
Author

noff commented Oct 24, 2019

addressable (~> 2.3, >= 2.3.8)

@noff
Copy link
Author

noff commented Oct 24, 2019

Just updated to addressable (2.7.0), same issue

@dentarg
Copy link
Collaborator

dentarg commented Jul 15, 2021

Addressable::URI.parse("https://user:password@dev.domain.com").omit(:scheme, :port, :user, :password, :host)

@noff what do you expect this to return?

This?

irb(main):009:0> Addressable::URI.parse("")
=> #<Addressable::URI:0x168 URI:>

Tested this myself with a few variants, seems to only trigger with username/password in the URL

irb(main):016:0> Addressable::URI.parse("https://foo.com").omit(:scheme)
=> #<Addressable::URI:0x1e0 URI://foo.com>
irb(main):017:0> Addressable::URI.parse("https://foo.com").omit(:scheme, :host)
=> #<Addressable::URI:0x1f4 URI:>
irb(main):018:0> Addressable::URI.parse("https://foo.com").omit(:scheme, :port, :host)
=> #<Addressable::URI:0x208 URI:>
irb(main):019:0> Addressable::URI.parse("https://foo.com").omit(:scheme, :port, :user, :host)
=> #<Addressable::URI:0x21c URI:>
irb(main):020:0> Addressable::URI.parse("https://foo.com").omit(:scheme, :port, :user, :password, :host)
=> #<Addressable::URI:0x230 URI:>
irb(main):021:0> Addressable::URI.parse("https://foo@foo.com").omit(:scheme, :port, :user, :password, :host)
=> #<Addressable::URI:0x244 URI:>
irb(main):022:0> Addressable::URI.parse("https://foo:bar@foo.com").omit(:scheme, :port, :user, :password, :host)
Traceback (most recent call last):
        7: from /Users/dentarg/.rubies/ruby-2.7.3/bin/irb:23:in `<main>'
        6: from /Users/dentarg/.rubies/ruby-2.7.3/bin/irb:23:in `load'
        5: from /Users/dentarg/.rubies/ruby-2.7.3/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
        4: from (irb):22
        3: from /Users/dentarg/.gem/ruby/2.7.3/gems/addressable-2.8.0/lib/addressable/uri.rb:2327:in `omit'
        2: from /Users/dentarg/.gem/ruby/2.7.3/gems/addressable-2.8.0/lib/addressable/uri.rb:2421:in `defer_validation'
        1: from /Users/dentarg/.gem/ruby/2.7.3/gems/addressable-2.8.0/lib/addressable/uri.rb:2483:in `validate'
Addressable::URI::InvalidURIError (Hostname not supplied: '')

I wonder if this is a similar bug? (similar error as in #272)

irb(main):015:0> Addressable::URI.parse("https://foo.com").omit(:host)
Traceback (most recent call last):
        7: from /Users/dentarg/.rubies/ruby-2.7.3/bin/irb:23:in `<main>'
        6: from /Users/dentarg/.rubies/ruby-2.7.3/bin/irb:23:in `load'
        5: from /Users/dentarg/.rubies/ruby-2.7.3/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
        4: from (irb):15
        3: from /Users/dentarg/.gem/ruby/2.7.3/gems/addressable-2.8.0/lib/addressable/uri.rb:2327:in `omit'
        2: from /Users/dentarg/.gem/ruby/2.7.3/gems/addressable-2.8.0/lib/addressable/uri.rb:2421:in `defer_validation'
        1: from /Users/dentarg/.gem/ruby/2.7.3/gems/addressable-2.8.0/lib/addressable/uri.rb:2476:in `validate'
Addressable::URI::InvalidURIError (Absolute URI missing hierarchical segment: 'https:')

@dentarg dentarg changed the title Hostname not supplied Addressable::URI::InvalidURIError (Hostname not supplied: '') when using omit Jul 15, 2021
@noff
Copy link
Author

noff commented Jul 16, 2021

@dentarg
It will be great if omit host won't raise error. Like this:

Addressable::URI.parse("https://foo.com/mypath").omit(:host).path
=> "/mypath"

@sdhull
Copy link

sdhull commented Jan 14, 2025

Looks like the relevant bit of code is here: https://github.com/sporkmonger/addressable/blob/main/lib/addressable/uri.rb#L2484-L2490

I agree that "parsing a URL and then omitting the host should produce a path-only URI"—that makes sense to some degree. However if you're going to retain the scheme, how can you produce a valid URI?

[58] pry(main)> Addressable::URI.parse "https://example.com/foo"
=> #<Addressable::URI:0x15540 URI:https://example.com/foo>
[59] pry(main)> u = _
=> #<Addressable::URI:0x15540 URI:https://example.com/foo>
[60] pry(main)> u.omit(:host)
=> #<Addressable::URI:0x15554 URI:https:/foo>
[61] pry(main)> u.omit(:host).to_s
=> "https:/foo"
[62] pry(main)> u.host = nil
=> nil
[63] pry(main)> u.to_s
=> "https:/foo"

(This is with addressable 2.8.1)

I think it can be surprising to encounter this exception sometimes but when you look at the alternative (path-only URI but with a port? with a user/pw?) this behavior makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/sporkmonger/addressable/issues/363

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy