Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect through a web proxy more directly #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.14
0.1.17
37 changes: 34 additions & 3 deletions lib/twitter/json_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [],
Expand All @@ -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
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the same condition twice, accidental duplication, or is this intentional?

data << "Host: #{request.host}"
else
data << "Host: #{@options[:host]}"
end

data << 'Accept: */*'
data << "User-Agent: #{@options[:user_agent]}" if @options[:user_agent]

Expand Down
14 changes: 14 additions & 0 deletions spec/twitter/json_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion twitter-stream.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down