From a4b51166496d916aae166cea1d8fc7faf8a262e0 Mon Sep 17 00:00:00 2001 From: Andrew Burnheimer Date: Sun, 2 Dec 2012 16:26:24 -0500 Subject: [PATCH 1/3] Connect through a web proxy more directly To resolve https://github.com/voloko/twitter-stream/issues/35 seen where EventMachine.bind_connect would fail to create a connection through a Squid web proxy. At worst, this allows an alternative to what bgreenlee proposed in https://github.com/voloko/twitter-stream/issues/2. --- VERSION | 2 +- lib/twitter/json_stream.rb | 37 +++++++++++++++++++++++++++++--- spec/twitter/json_stream_spec.rb | 14 ++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index f1f1232..c34958a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.14 \ No newline at end of file +0.1.15 diff --git a/lib/twitter/json_stream.rb b/lib/twitter/json_stream.rb index 27f4b4d..794f7ef 100644 --- a/lib/twitter/json_stream.rb +++ b/lib/twitter/json_stream.rb @@ -34,6 +34,9 @@ class JSONStream < EventMachine::Connection :user_agent => 'TwitterStream', :timeout => 0, :proxy => ENV['HTTP_PROXY'], + :proxy_type => ENV['PROXY_TYPE'], # try setting to 'direct' if + # just setting HTTP_PROXY + # doesn't work :auth => nil, :oauth => {}, :filters => [], @@ -58,8 +61,29 @@ def self.connect options = {} if options[:proxy] proxy_uri = URI.parse(options[:proxy]) - host = proxy_uri.host - port = proxy_uri.port + + case options[:proxy_type] + when 'direct' + full_url = "http#{'s' if options[:ssl]}://#{options[:host]}" + if (options[:ssl] == true && options[:port] != 443 ) || + (options[:ssl] == false && options[:port] != 80 ) + full_url << ":#{options[:port]}" + end + full_url << options[:path].to_s + options[:path] = full_url + + options[:host] = host = proxy_uri.host + options[:port] = port = proxy_uri.port + options[:ssl] = proxy_uri.scheme == "https" + + # With a direct proxied-connection, subsequent requests can be + # issued in a more typical way. + options[:proxy] = nil + + else + host = proxy_uri.host + port = proxy_uri.port + end end connection = EventMachine.connect host, port, self, options @@ -257,7 +281,14 @@ def send_request end data << "#{@options[:method]} #{request_uri} HTTP/1.1" - data << "Host: #{@options[:host]}" + + request = URI.parse(request_uri) + if (request.class == URI::HTTPS) || (request.class == URI::HTTPS) + data << "Host: #{request.host}" + else + data << "Host: #{@options[:host]}" + end + data << 'Accept: */*' data << "User-Agent: #{@options[:user_agent]}" if @options[:user_agent] diff --git a/spec/twitter/json_stream_spec.rb b/spec/twitter/json_stream_spec.rb index bc66a32..9a82eb7 100644 --- a/spec/twitter/json_stream_spec.rb +++ b/spec/twitter/json_stream_spec.rb @@ -64,10 +64,24 @@ def receive_data data opts[:host].should == 'stream.twitter.com' opts[:port].should == 443 opts[:proxy].should == 'http://my-proxy:8080' + URI.parse(opts[:path]).should be_an_instance_of URI::Generic end stream = JSONStream.connect(:proxy => "http://my-proxy:8080") {} end + it "should connect to the proxy directly if called for" do + EM.should_receive(:connect).with do |host, port, handler, opts| + host.should == 'my-proxy' + port.should == 8080 + opts[:host].should == 'my-proxy' + opts[:port].should == 8080 + opts[:proxy].should == nil + URI.parse(opts[:path]).should be_an_instance_of URI::HTTPS + end + stream = JSONStream.connect(:proxy => "http://my-proxy:8080", + :'proxy_type' => 'direct') {} + end + it "should not trigger SSL until connection is established" do connection = stub('connection') EM.should_receive(:connect).and_return(connection) From 28c5e9c3783117c53ec52769e951621c24e1ff03 Mon Sep 17 00:00:00 2001 From: Andrew Burnheimer Date: Sun, 2 Dec 2012 16:45:40 -0500 Subject: [PATCH 2/3] Boost version over what is up at rubygems --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index c34958a..44a7df2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.15 +0.1.17 \ No newline at end of file From cd4ac3fd0b5724f59240412a535273f1cf43cd00 Mon Sep 17 00:00:00 2001 From: Andrew Burnheimer Date: Mon, 3 Dec 2012 00:19:59 -0500 Subject: [PATCH 3/3] Track gemspec to VERSION file --- twitter-stream.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twitter-stream.gemspec b/twitter-stream.gemspec index 4624628..af42d8b 100644 --- a/twitter-stream.gemspec +++ b/twitter-stream.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = %q{twitter-stream} - s.version = "0.1.15" + s.version = File.read('VERSION') s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Vladimir Kolesnikov"]