Skip to content

Commit

Permalink
don't throw exceptions in redirect middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed May 1, 2011
1 parent c90c670 commit be9ec71
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
24 changes: 12 additions & 12 deletions lib/em-http/client.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module EventMachine

class InvalidRedirectError < StandardError
end

class HttpClient
include Deferrable
include HttpEncoding
Expand Down Expand Up @@ -86,21 +83,24 @@ def unbind
if redirect?

begin
@state = :redirecting
@conn.middleware.each do |m|
m.response(self) if m.respond_to?(:response)
end
@state = :finished
@req.followed += 1
@req.set_uri(@response_header.location)
@conn.redirect(self)
rescue EventMachine::InvalidRedirectError
@state = :finished
succeed(self)

# one of the injected middlewares could have changed
# our redirect settings, check if we still want to
# follow the location header
if redirect?
@req.followed += 1
@req.set_uri(@response_header.location)
@conn.redirect(self)
else
succeed(self)
end

rescue Exception => e
on_error(e.message)
end

else
succeed(self)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/em-http/http_client_options.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class HttpClientOptions
attr_reader :uri, :method, :host, :port, :proxy
attr_reader :headers, :file, :body, :query, :path
attr_reader :keepalive, :redirects, :pass_cookies
attr_reader :keepalive, :pass_cookies

attr_accessor :followed
attr_accessor :followed, :redirects

def initialize(uri, options, method)
@keepalive = options[:keepalive] || false # default to single request per connection
Expand Down
27 changes: 12 additions & 15 deletions spec/redirect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
require 'helper'

class RedirectMiddleware
class << self
attr_accessor :call_count
end

RedirectMiddleware.call_count = 0
attr_reader :call_count

def request(c, h, r)
RedirectMiddleware.call_count += 1
h['EM-Middleware'] = RedirectMiddleware.call_count.to_s
[h, r]
def initialize
@call_count = 0
end

def response(r)
RedirectMiddleware.call_count = (r.response_header['EM_MIDDLEWARE'].to_i + 1)
def request(c, h, r)
@call_count += 1
[h.merge({'EM-Middleware' => @call_count.to_s}), r]
end
end

class PickyRedirectMiddleware < RedirectMiddleware
def response(r)
raise EventMachine::InvalidRedirectError if r.state == :redirecting && r.response_header['LOCATION'][-1] == '3'
super
if r.redirect? && r.response_header['LOCATION'][-1] == '3'
# set redirects to 0 to avoid further processing
r.req.redirects = 0
end
end
end

Expand Down Expand Up @@ -180,7 +177,7 @@ def response(r)
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
RedirectMiddleware.call_count.should == 6
http.response_header['EM_MIDDLEWARE'].to_i.should == 3
EM.stop
}
}
Expand All @@ -196,7 +193,7 @@ def response(r)
http.response_header.status.should == 301
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/redirect/middleware_redirects_2'
EM.stop
}
}
}
end

Expand Down

0 comments on commit be9ec71

Please sign in to comment.