voloko / twitter-stream

Twitter realtime API client
MIT License
233 stars 81 forks source link

Connect through a web proxy more directly #36

Open aburnheimer opened 11 years ago

aburnheimer commented 11 years ago

(First pull request. Apologize for the over-verbosity.)

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 just allows an alternative to what bgreenlee proposed in https://github.com/voloko/twitter-stream/issues/2.

Problem: voloko:twitter-stream (0.1.16), options calling JSONStream::connect

{:auth=>"1234567890abcdef1234567890abcdef12345678:X",
 :host=>"streaming.campfirenow.com",
 :proxy=>"http://proxy.domain.com:3128",
 :path=>"/room/123456/live.json"}

voloko:twitter-stream (0.1.16), options inside of JSONStream::connect

[[:auth, "1234567890abcdef1234567890abcdef12345678:X"],
 [:auto_reconnect, true], [:content, ""],
 [:content_type, "application/x-www-form-urlencoded"],
 [:filters, []], [:host, "streaming.campfirenow.com"],
 [:method, "GET"], [:oauth, {}], [:params, {}],
 [:path, "/room/123456/live.json"], [:port, 443],
 [:proxy, "http://proxy.domain.com:3128"],
 [:ssl, true], [:timeout, 0], [:user_agent, "TwitterStream"]]

voloko:twitter-stream (0.1.16), JSONStream#send_request never gets called because Twitter::JSONStream.connect(options) hangs... eventually dying with "Tried 11 times to connect."

Resolution provided by this request (0.1.17), options calling JSONStream::connect

{:auth=>"1234567890abcdef1234567890abcdef12345678:X",
 :host=>"streaming.campfirenow.com",
 :proxy=>"http://proxy.domain.com:3128",
 :proxy_type=>:direct,
 :path=>"/room/123456/live.json"}

...options inside of JSONStream::connect

[[:auth, "1234567890abcdef1234567890abcdef12345678:X"],
 [:auto_reconnect, true], [:content, ""],
 [:content_type, "application/x-www-form-urlencoded"],
 [:filters, []], [:host, "streaming.campfirenow.com"],
 [:method, "GET"], [:oauth, {}], [:params, {}],
 [:path, "/room/123456/live.json"], [:port, 443],
 [:proxy, "http://proxy.domain.com:3128"],
 [:proxy_type, :direct], [:ssl, true], [:timeout, 0],
 [:user_agent, "TwitterStream"]]

...by the time JSONStream::connect finishes, host, options[:host], port, and options[:port] are overwritten to things that allow EventMachine.bind_connect to work.

JSONStream#send_request is called, and options looks as such at the bottom

[[:auth, "1234567890abcdef1234567890abcdef12345678:X"],
 [:auto_reconnect, true], [:content, ""],
 [:content_type, "application/x-www-form-urlencoded"], [:filters, []],
 [:host, "proxy.domain.com"], [:method, "GET"], [:oauth, {}],
 [:params, {}], [:path, "https://streaming.campfirenow.com/room/123456/live.json"],
 [:port, 3128], [:proxy, nil], [:proxy_type, :direct], [:ssl, false],
 [:timeout, 0], [:user_agent, "TwitterStream"]]

..., headers at bottom of JSONStream#send_request

["GET https://streaming.campfirenow.com/room/123456/live.json HTTP/1.1",
 "Host: streaming.campfirenow.com", "Accept: */*",
 "User-Agent: TwitterStream",
 "Authorization: Basic ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRST",
 "\r\n"]
MarcoSero commented 10 years ago

I can't get the proxy working. Can anyone maybe merge this if it solves the problem?

rud commented 9 years ago

This does not look too scary, what's the thinking, anybody want to see this merged?