Class: RaptorIO::Protocol::HTTP::Request::Manipulators::RedirectFollower

Inherits:
RaptorIO::Protocol::HTTP::Request::Manipulator show all
Defined in:
lib/raptor-io/protocol/http/request/manipulators/redirect_follower.rb

Overview

Implements automatic HTTP redirect following.

Author:

  • Tasos Laskos

Instance Attribute Summary

Attributes inherited from RaptorIO::Protocol::HTTP::Request::Manipulator

#client, #options, #request

Instance Method Summary collapse

Methods inherited from RaptorIO::Protocol::HTTP::Request::Manipulator

#datastore, #delegate, inherited, #initialize, #shortname, shortname, #validate_options, validate_options, validate_options!

Constructor Details

This class inherits a constructor from RaptorIO::Protocol::HTTP::Request::Manipulator

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/raptor-io/protocol/http/request/manipulators/redirect_follower.rb', line 14

def run
  # This request has already been handled.
  return if request.root_redirect_id

  callbacks = request.callbacks.dup
  request.clear_callbacks

  request.on_complete do |response|
    root_redirect_id = request.root_redirect_id ?
        request.root_redirect_id : request.object_id

    if response.redirect?
      if redirections[root_redirect_id].size < max
        redirections[root_redirect_id] << response

        crequest = request.dup
        crequest.root_redirect_id = root_redirect_id

        # RFC says the Location URI must be a full absolute URL however not
        # all webapps respect that.
        crequest.url = crequest.parsed_url.merge( response.headers['Location'] ).to_s

        client.queue( crequest )
        next
      else
        response.redirections = redirections.delete( root_redirect_id )
      end
    end

    request.callbacks = callbacks
    request.handle_response response
  end
end