Skip to content
This repository
Browse code

Add cookie support to Transport::Http.

  • Loading branch information...
commit 7a08b405f880476fd12e48fa7811a550d947c519 1 parent 20b41fa
James Coglan jcoglan authored
1  faye.gemspec
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15 15
16 16 s.require_paths = %w[lib]
17 17
  18 + s.add_dependency "cookiejar", ">= 0.3.0"
18 19 s.add_dependency "eventmachine", "~> 0.12.0"
19 20 s.add_dependency "em-http-request", "~> 0.3"
20 21 s.add_dependency "em-hiredis", ">= 0.0.1"
1  lib/faye/protocol/client.rb
@@ -16,6 +16,7 @@ class Client
16 16
17 17 CONNECTION_TIMEOUT = 60.0
18 18
  19 + attr_accessor :cookies
19 20 attr_reader :endpoint, :client_id
20 21
21 22 def initialize(endpoint = nil, options = {})
13 lib/faye/transport/http.rb
... ... @@ -1,3 +1,4 @@
  1 +require 'cookiejar'
1 2 require 'em-http'
2 3 require 'em-http/version'
3 4
@@ -11,12 +12,16 @@ def self.usable?(endpoint, &callback)
11 12 def request(message, timeout)
12 13 retry_block = retry_block(message, timeout)
13 14
  15 + @client.cookies ||= CookieJar::Jar.new
  16 + cookies = @client.cookies.get_cookies(@endpoint)
  17 +
14 18 content = JSON.unparse(message)
15 19 params = {
16 20 :head => {
  21 + 'Content-Length' => content.length,
17 22 'Content-Type' => 'application/json',
18   - 'host' => URI.parse(@endpoint).host,
19   - 'Content-Length' => content.length
  23 + 'Cookie' => cookies * '; ',
  24 + 'Host' => URI.parse(@endpoint).host
20 25 },
21 26 :body => content,
22 27 :timeout => -1 # for em-http-request < 1.0
@@ -31,6 +36,10 @@ def request(message, timeout)
31 36 end
32 37 request.callback do
33 38 begin
  39 + cookies = [request.response_header['SET_COOKIE']].flatten.compact
  40 + cookies.each do |cookie|
  41 + @client.cookies.set_cookie(@endpoint, cookie)
  42 + end
34 43 receive(JSON.parse(request.response))
35 44 rescue
36 45 retry_block.call

0 comments on commit 7a08b40

Please sign in to comment.
Something went wrong with that request. Please try again.