Skip to content

Commit

Permalink
Default to CONNECT proxy for https requests
Browse files Browse the repository at this point in the history
This is a more secure default than `:http`.
  • Loading branch information
ConradIrwin committed Jun 11, 2013
1 parent 210d5eb commit 27bac89
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/em-http/http_connection.rb
Expand Up @@ -149,9 +149,9 @@ def receive_data(data)
def connection_completed
@peer = @conn.get_peername

if @connopts.proxy && @connopts.proxy[:type] == :socks5
if @connopts.socks_proxy?
socksify(client.req.uri.host, client.req.uri.port, *@connopts.proxy[:authorization]) { start }
elsif @connopts.proxy && @connopts.proxy[:type] == :connect
elsif @connopts.connect_proxy?
connectify(client.req.uri.host, client.req.uri.port, *@connopts.proxy[:authorization]) { start }
else
start
Expand Down
15 changes: 13 additions & 2 deletions lib/em-http/http_connection_options.rb
Expand Up @@ -18,7 +18,8 @@ def initialize(uri, options)
end

uri = uri.kind_of?(Addressable::URI) ? uri : Addressable::URI::parse(uri.to_s)
uri.port = (uri.scheme == "https" ? (uri.port || 443) : (uri.port || 80))
@https = uri.scheme == "https"
uri.port ||= (@https ? 443 : 80)

if proxy = options[:proxy]
@host = proxy[:host]
Expand All @@ -29,5 +30,15 @@ def initialize(uri, options)
end
end

def http_proxy?; @proxy && [nil, :http].include?(@proxy[:type]); end
def http_proxy?
@proxy && (@proxy[:type] == :http || @proxy[:type].nil? && !@https)
end

def connect_proxy?
@proxy && (@proxy[:type] == :connect || @proxy[:type].nil? && @https)
end

def socks_proxy?
@proxy && (@proxy[:type] == :socks5)
end
end
20 changes: 17 additions & 3 deletions spec/connectify_proxy_spec.rb
Expand Up @@ -4,11 +4,25 @@
requires_port(3128) do
describe EventMachine::HttpRequest do

let(:proxy) { {:proxy => { :host => '127.0.0.1', :port => 3128, :type => :connect }} }
let(:connect_proxy) { {:proxy => { :host => '127.0.0.1', :port => 3128, :type => :connect }} }
let(:default_proxy) { {:proxy => { :host => '127.0.0.1', :port => 3128, }} }

it "should use CONNECT proxy" do
it "should use CONNECT proxy when specified" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://jsonip.com/', proxy).get
http = EventMachine::HttpRequest.new('http://jsonip.com/', connect_proxy).get

http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response.should match('173.230.151.99')
EventMachine.stop
}
}
end

it "should use CONNECT proxy by default for HTTPS requests" do
EventMachine.run {
http = EventMachine::HttpRequest.new('https://ipjson.herokuapp.com/', default_proxy).get

http.errback { failed(http) }
http.callback {
Expand Down

0 comments on commit 27bac89

Please sign in to comment.