An implementation of a socket factory that returns a socket which will tunnel the connection through an HTTP proxy. It allows explicit specification of the user and password, but if none are given it will look in the HTTP_PROXY_USER/HTTP_PROXY_PASSWORD and CONNECT_USER/CONNECT_PASSWORD environment variables as well.
Methods
Public Class methods
Create a new socket factory that tunnels via the given host and port.
[ show source ]
# File lib/net/ssh/proxy/http.rb, line 34
34: def initialize( proxy_host, proxy_port=80, options={} )
35: @proxy_host = proxy_host
36: @proxy_port = proxy_port
37: @options = options
38: end
Public Instance methods
Return a new socket connected to the given host and port via the proxy that was requested when the socket factory was instantiated.
[ show source ]
# File lib/net/ssh/proxy/http.rb, line 42
42: def open( host, port )
43: connect_string = "CONNECT #{host}:#{port} HTTP/1.0"
44:
45: socket = TCPSocket.new( @proxy_host, @proxy_port )
46: socket.puts connect_string
47: socket.puts
48:
49: resp = parse_response( socket )
50:
51: return socket if resp[:code] == 200
52:
53: socket.shutdown
54: raise ConnectError, resp.inspect unless resp[:code] == 407
55:
56: user = proxy_user
57: passwd = proxy_password
58:
59: raise UnauthorizedError, "no proxy user given" unless user
60:
61: auth = resp[:headers]["Proxy-Authenticate"]
62: scheme, parms = auth.split( / /, 2 )
63:
64: case scheme
65: when "Basic"
66: credentials =
67: Base64.encode64( "#{user}:#{passwd}" ).gsub( /\n/, "" )
68: else
69: raise NotImplementedError,
70: "authorization scheme #{scheme.inspect} is not supported"
71: end
72:
73: socket = TCPSocket.new( @proxy_host, @proxy_port )
74: socket.puts connect_string
75: socket.puts "Proxy-Authorization: #{scheme} #{credentials}"
76: socket.puts
77:
78: resp = parse_response( socket )
79:
80: raise ConnectError, resp.inspect if resp[:code] != 200
81:
82: return socket
83: end