Permalink
Browse files

Default to CONNECT proxy for https requests

This is a more secure default than `:http`.
  • Loading branch information...
1 parent 210d5eb commit 27bac89c924b89e4ae09fe7e47d7e8c807d2e2c6 @ConradIrwin ConradIrwin committed Jun 11, 2013
Showing with 32 additions and 7 deletions.
  1. +2 −2 lib/em-http/http_connection.rb
  2. +13 −2 lib/em-http/http_connection_options.rb
  3. +17 −3 spec/connectify_proxy_spec.rb
@@ -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
@@ -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]
@@ -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
@@ -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 {

0 comments on commit 27bac89

Please sign in to comment.