sparklemotion / mechanize

Mechanize is a ruby library that makes automated web interaction easy.

Home Page:https://www.rubydoc.info/gems/mechanize/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`set_proxy` prepends value with "http://" regardless of what is passed in

ttilberg opened this issue · comments

Some services require that when you specify a proxy address, it includes the URI protocol (http), while some libraries handle it for you. To better accommodate abstractions, this is desirable that it adds http when needed -- but only if it's needed.

Mechanize is an example that handles it for you, but does so without checking what was passed in, and can create a hard-to-find error, especially if you don't see the value set_proxy returns.

proxy = { address: "http://127.0.0.1", port: 8080}
agent = Mechanize.new
agent.set_proxy proxy[:address], proxy[:port]
# => #<URI::HTTP http://http:8080//127.0.0.1>

agent.get 'https://api.ipify.org'
SocketError: getaddrinfo: Name or service not known
from /home/ttilberg/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/net/http.rb:879:in `initialize'

When you dig into the source, you come across this note:

  ##
  # Sets the proxy address, port, user, and password +addr+ should be a host,
  # with no "http://", +port+ may be a port number, service name or port
  # number string.

It handles the address with this line.

Is there a reason that proxy_uri = URI "http://#{addr}" couldn't or shouldn't be fleshed out better to check for %r(^https?//)

Seems like this issue should be closed now.

Yes, thank you!