Class: HTTP::Redirector
- Inherits:
-
Object
- Object
- HTTP::Redirector
- Defined in:
- lib/http/redirector.rb
Defined Under Namespace
Classes: EndlessRedirectError, TooManyRedirectsError
Constant Summary collapse
- REDIRECT_CODES =
HTTP status codes which indicate redirects
[300, 301, 302, 303, 307, 308].to_set.freeze
- STRICT_SENSITIVE_CODES =
Codes which which should raise StateError in strict mode if original request was any of UNSAFE_VERBS
[300, 301, 302].to_set.freeze
- UNSAFE_VERBS =
Insecure http verbs, which should trigger StateError in strict mode upon STRICT_SENSITIVE_CODES
%i[put delete post].to_set.freeze
- SEE_OTHER_ALLOWED_VERBS =
Verbs which will remain unchanged upon See Other response.
%i[get head].to_set.freeze
Instance Attribute Summary collapse
-
#max_hops ⇒ Object
readonly
Returns the value of attribute max_hops.
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Redirector
constructor
A new instance of Redirector.
-
#perform(request, response) ⇒ Object
Follows redirects until non-redirect response found.
Constructor Details
#initialize(opts = {}) ⇒ Redirector
Returns a new instance of Redirector.
42 43 44 45 46 |
# File 'lib/http/redirector.rb', line 42 def initialize(opts = {}) @strict = opts.fetch(:strict, true) @max_hops = opts.fetch(:max_hops, 5).to_i @on_redirect = opts.fetch(:on_redirect, nil) end |
Instance Attribute Details
#max_hops ⇒ Object (readonly)
Returns the value of attribute max_hops.
37 38 39 |
# File 'lib/http/redirector.rb', line 37 def max_hops @max_hops end |
#strict ⇒ Object (readonly)
Returns the value of attribute strict.
32 33 34 |
# File 'lib/http/redirector.rb', line 32 def strict @strict end |
Instance Method Details
#perform(request, response) ⇒ Object
Follows redirects until non-redirect response found
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/http/redirector.rb', line 49 def perform(request, response) @request = request @response = response @visited = [] while REDIRECT_CODES.include? @response.status.code @visited << "#{@request.verb} #{@request.uri}" raise TooManyRedirectsError if too_many_hops? raise EndlessRedirectError if endless_loop? @response.flush # XXX(ixti): using `Array#inject` to return `nil` if no Location header. @request = redirect_to(@response.headers.get(Headers::LOCATION).inject(:+)) unless .empty? @request.headers.set(Headers::COOKIE, ..map { |c| "#{c.name}=#{c.value}" }.join("; ")) end @on_redirect.call @response, @request if @on_redirect.respond_to?(:call) @response = yield @request end @response end |