Permalink
Browse files

Merge branch 'fix_cookie_redirect' of git://github.com/divoxx/em-http…

…-request
  • Loading branch information...
igrigorik committed Jun 17, 2013
2 parents 8a478d8 + ca893b8 commit 28d0adbf7edc1d8b6197cb5208ce4d000a878f83
Showing with 39 additions and 2 deletions.
  1. +12 −2 lib/em-http/client.rb
  2. +27 −0 spec/redirect_spec.rb
View
@@ -99,8 +99,18 @@ def unbind(reason = nil)
@req.followed += 1
@cookies.clear
- @cookies = @cookiejar.get(@response_header.location).map(&:to_s) if @req.pass_cookies
- @req.set_uri(@response_header.location)
+
+ url = @response_header.location
+
+ begin
+ url = URI.parse(@response_header.location)
+ url.path = "/" if url.path.empty?
+ url = url.to_s
+ rescue
+ end
+
+ @cookies = @cookiejar.get(url).map(&:to_s) if @req.pass_cookies
+ @req.set_uri(url)
@conn.redirect(self)
else
succeed(self)
View
@@ -104,6 +104,33 @@ def response(r)
end
end
+
+ it "should forward valid cookies across domains with http redirect even if Location redirects to slightly different valid URL of same domain" do
+
+ expires = (Date.today + 2).strftime('%a, %d %b %Y %T GMT')
+ response =<<-HTTP.gsub(/^ +/, '')
+ HTTP/1.1 301 MOVED PERMANENTLY
+ Location: http://127.0.0.1:8081?omg=ponies
+ Set-Cookie: foo=bar; expires=#{expires}; path=/; HttpOnly
+
+ HTTP
+
+ EventMachine.run do
+ @stub = StubServer.new(:host => '127.0.0.1', :port => 8080, :response => response)
+ @echo = StubServer.new(:host => '127.0.0.1', :port => 8081, :echo => true)
+
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :redirects => 1
+
+ http.errback { failed(http) }
+ http.callback do
+ http.response.should match(/Cookie/)
+ @stub.stop
+ @echo.stop
+ EM.stop
+ end
+ end
+ end
+
it "should redirect with missing content-length" do
EventMachine.run {
response = "HTTP/1.0 301 MOVED PERMANENTLY\r\nlocation: http://127.0.0.1:8090/redirect\r\n\r\n"

0 comments on commit 28d0adb

Please sign in to comment.