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

faraday does not support ipv6 in URL #589

Open
vanerax opened this issue Jun 7, 2016 · 5 comments
Open

faraday does not support ipv6 in URL #589

vanerax opened this issue Jun 7, 2016 · 5 comments

Comments

@vanerax
Copy link

vanerax commented Jun 7, 2016

Issue:
faraday does not support ipv6 in URL.
For example:
http://[::1]/
http://[::1]:80/

Steps to reproduce:

  1. Prepare a server with ipv6 environment.
  2. Start a HTTP server. (This step can be skipped because the issue is occurred before request is sent)
  3. Run the following sample.
require 'faraday'
conn = Faraday.new(:url => 'http://[::1]:80') do |faraday|
  faraday.request  :url_encoded             # form-encode POST params
  faraday.response :logger                  # log requests to STDOUT
  faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
end
resp = conn.get '/'

Finally it'll throw the exception below.

/usr/local/lib/ruby/2.1.0/net/http.rb:879:in `initialize': getaddrinfo: Name or service not known (Faraday::ConnectionFailed)
        from /usr/local/lib/ruby/2.1.0/net/http.rb:879:in `open'
        from /usr/local/lib/ruby/2.1.0/net/http.rb:879:in `block in connect'
        from /usr/local/lib/ruby/2.1.0/timeout.rb:76:in `timeout'
        from /usr/local/lib/ruby/2.1.0/net/http.rb:878:in `connect'
        from /usr/local/lib/ruby/2.1.0/net/http.rb:863:in `do_start'
        from /usr/local/lib/ruby/2.1.0/net/http.rb:852:in `start'
        from /usr/local/lib/ruby/2.1.0/net/http.rb:1369:in `request'
        from /usr/local/lib/ruby/2.1.0/net/http.rb:1128:in `get'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/adapter/net_http.rb:80:in `perform_request'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/adapter/net_http.rb:40:in `block in call'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/adapter/net_http.rb:87:in `with_net_http_connection'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/adapter/net_http.rb:32:in `call'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/response.rb:8:in `call'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/response/logger.rb:24:in `call'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/request/url_encoded.rb:15:in `call'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/rack_builder.rb:139:in `build_response'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/connection.rb:377:in `run_request'
        from /usr/local/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/connection.rb:140:in `get'
        from f.rb:9:in `<main>'
@zorbash
Copy link
Contributor

zorbash commented Jul 4, 2016

This is an issue of net/http and seems related to https://bugs.ruby-lang.org/issues/3788
and https://bugs.ruby-lang.org/issues/9129

I don't think there's something that should change in Faraday for this issue.
As a workaround you can use an adapter other than the default. I tried excon@0.50.1 and it works.

@sbleon
Copy link
Contributor

sbleon commented Aug 25, 2016

I'm not sure if this is Net::HTTP's fault. It can handle IPv6 addresses. I think the square brackets may be the issue. Net::HTTP doesn't understand an IPv6 address with brackets if it receives it as a hostname:

Net::HTTP.get('[::1]', '/')
SocketError: Failed to open TCP connection to [::1]:80 (getaddrinfo: nodename nor servname provided, or not known)
    from /Users/leon/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:882:in `rescue in block in connect'

But if a URL with square brackets is parsed by URI and then the URI is given to Net::HTTP, it works fine:

Net::HTTP.get(URI('http://[::1]:80'))
=> "<html><body><h1>It works!</h1></body></html>\n"

Net::HTTP also works if no brackets are present:

Net::HTTP.get('::1', '/')
=> "<html><body><h1>It works!</h1></body></html>\n"

Maybe our Net::HTTP adapter needs to do some parsing with URI at some point?

@zorbash
Copy link
Contributor

zorbash commented Sep 7, 2016

I'm willing to investigate this a bit further and submit a PR if necessary.

foca added a commit to foca/faraday that referenced this issue Oct 20, 2016
Fixes lostisland#589.

The problem comes from Net::HTTP not being able to understand brackets
when passed a literal IPv6 address:

    Net::HTTP.start("[::1]") #=> :(
    Net::HTTP.start("::1") #=> :)

URI provides two different methods to extract the network host from the
URI: #host, and #hostname.

The only difference being that `#hostname` will unwrap brackets for IPv6
addresses, which is what we want here.
@giovannism20
Copy link

Is this issue still opened @iMacTia ?

@iMacTia
Copy link
Member

iMacTia commented Jun 2, 2023

It's complicated 😅
See this PR for more details: #621

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

No branches or pull requests

5 participants