Browse files

Merge branch 'em-connectify' of git://github.com/ConradIrwin/em-http-…

…request
  • Loading branch information...
2 parents 1c10ce9 + 2dfd9d6 commit b8f26b3eec29a753cea979c7be22d3ea5b55ba7e @igrigorik igrigorik committed Jun 16, 2013
View
1 em-http-request.gemspec
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.add_dependency "addressable", ">= 2.2.3"
s.add_dependency "http_parser.rb", ">= 0.6.0.beta.2"
s.add_dependency "em-socksify"
+ s.add_dependency "em-connectify", ">= 0.2"
s.add_dependency "cookiejar"
s.add_development_dependency "rspec"
View
1 lib/em-http.rb
@@ -1,5 +1,6 @@
require 'eventmachine'
require 'em-socksify'
+require 'em-connectify'
require 'addressable/uri'
require 'http/parser'
View
5 lib/em-http/http_connection.rb
@@ -35,6 +35,7 @@ def unbind(reason=nil)
class HttpConnection
include HTTPMethods
include Socksify
+ include Connectify
attr_reader :deferred
attr_accessor :error, :connopts, :uri, :conn
@@ -148,8 +149,10 @@ 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.connect_proxy?
+ connectify(client.req.uri.host, client.req.uri.port, *@connopts.proxy[:authorization]) { start }
else
start
end
View
15 lib/em-http/http_connection_options.rb
@@ -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] == :http || @proxy[:type].nil?) && @https
+ end
+
+ def socks_proxy?
+ @proxy && (@proxy[:type] == :socks5)
+ end
end
View
37 spec/connectify_proxy_spec.rb
@@ -0,0 +1,37 @@
+require 'helper'
+
+requires_connection do
+ requires_port(3128) do
+ describe EventMachine::HttpRequest do
+
+ let(:http_proxy) { {:proxy => { :host => '127.0.0.1', :port => 3128, :type => :http }} }
+ let(:default_proxy) { {:proxy => { :host => '127.0.0.1', :port => 3128 }} }
+
+ it "should use CONNECT proxy for HTTPS requests when :http specified" do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('https://ipjson.herokuapp.com/', http_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 for HTTPS when no type specified" do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('https://ipjson.herokuapp.com/', default_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
+ end
+ end
+end

0 comments on commit b8f26b3

Please sign in to comment.