Skip to content

Commit

Permalink
Add proxy support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Greenlee committed Apr 9, 2010
1 parent 550efa2 commit 6a06df7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/twitter/json_stream.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'eventmachine'
require 'em/buftok'
require 'uri'

module Twitter
class JSONStream < EventMachine::Connection
Expand Down Expand Up @@ -28,20 +29,31 @@ class JSONStream < EventMachine::Connection
:ssl => false,
:auth => 'test:test',
:user_agent => 'TwitterStream',
:timeout => 0
:timeout => 0,
:proxy => ENV['HTTP_PROXY']
}

attr_accessor :code
attr_accessor :headers
attr_accessor :nf_last_reconnect
attr_accessor :af_last_reconnect
attr_accessor :reconnect_retries
attr_accessor :proxy

def self.connect options = {}
options[:port] = 443 if options[:ssl] && !options.has_key?(:port)
options = DEFAULT_OPTIONS.merge(options)

connection = EventMachine.connect options[:host], options[:port], self, options
host = options[:host]
port = options[:port]

if options[:proxy]
proxy_uri = URI.parse(options[:proxy])
host = proxy_uri.host
port = proxy_uri.port
end

connection = EventMachine.connect host, port, self, options
connection.start_tls if options[:ssl]
connection
end
Expand All @@ -53,6 +65,7 @@ def initialize options = {}
@af_last_reconnect = nil
@reconnect_retries = 0
@immediate_reconnect = false
@proxy = URI.parse(options[:proxy]) if options[:proxy]
end

def each_item &block
Expand Down Expand Up @@ -160,10 +173,18 @@ def reset_state

def send_request
data = []
data << "#{@options[:method]} #{@options[:path]} HTTP/1.1"
request_uri = @options[:path]
if @proxy
# proxies need the request to be for the full url
request_uri = "http#{'s' if @options[:ssl]}://#{@options[:host]}:#{@options[:port]}#{request_uri}"
end
data << "#{@options[:method]} #{request_uri} HTTP/1.1"
data << "Host: #{@options[:host]}"
data << "User-agent: #{@options[:user_agent]}" if @options[:user_agent]
data << "Authorization: Basic " + [@options[:auth]].pack('m').delete("\r\n")
if @proxy && @proxy.user
data << "Proxy-Authorization: Basic " + ["#{@proxy.user}:#{@proxy.password}"].pack('m').delete("\r\n")
end
if @options[:method] == 'POST'
data << "Content-type: #{@options[:content_type]}"
data << "Content-length: #{@options[:content].length}"
Expand Down
10 changes: 10 additions & 0 deletions spec/twitter/json_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
stream = JSONStream.connect {}
end

it "should connect to the proxy if provided" do
EM.should_receive(:connect).with do |host, port, handler, opts|
host.should == 'my-proxy'
port.should == 8080
opts[:host].should == 'stream.twitter.com'
opts[:port].should == 80
opts[:proxy].should == 'http://my-proxy:8080'
end
stream = JSONStream.connect(:proxy => "http://my-proxy:8080") {}
end
end

Host = "127.0.0.1"
Expand Down

0 comments on commit 6a06df7

Please sign in to comment.