Class: NOMS::Command::UserAgent

Inherits:
Base
  • Object
show all
Defined in:
lib/noms/command/useragent.rb

Instance Method Summary collapse

Methods inherited from Base

#default_logger

Constructor Details

#initialize(origin, attrs = {}) ⇒ UserAgent

Returns a new instance of UserAgent.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/noms/command/useragent.rb', line 23

def initialize(origin, attrs={})
    @origin = origin
    @client = HTTPClient.new :agent_name => "noms/#{NOMS::Command::VERSION}"
    # TODO Replace with TOFU implementation
    @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @redirect_checks = [ ]
    @log = attrs[:logger] || default_logger

    @log.debug "(UserAgent) specified identities = #{attrs[:specified_identities]}"
    @auth = NOMS::Command::Auth.new(:logger => @log,
                                    :specified_identities => (attrs[:specified_identities] || []))
    # TODO: Set cookie jar to something origin-specific
    # TODO: Set user-agent to something nomsy
    # caching
    @client.redirect_uri_callback = lambda do |uri, res|
        raise NOMS::Command::Error.new "Bad redirect URL #{url}" unless check_redirect(uri)
        @client.default_redirect_uri_callback(uri, res)
    end
end

Instance Method Details

#absolute_url(url) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/noms/command/useragent.rb', line 57

def absolute_url(url)
    @log.debug "Calculating absolute url of #{url} in context of #{@origin}"
    begin
        url = URI.parse url unless url.respond_to? :scheme
        url = URI.join(@origin, url) unless url.absolute?
        url
    rescue StandardError => e
        raise NOMS::Command::Error.new "Error parsing URL #{url} in context of #{@origin} (#{e.class}): #{e.message}"
    end
end

#add_redirect_check(&block) ⇒ Object



118
119
120
121
# File 'lib/noms/command/useragent.rb', line 118

def add_redirect_check(&block)
    @log.debug "Adding #{block} to redirect checks"
    @redirect_checks << block
end

#authObject



43
44
45
# File 'lib/noms/command/useragent.rb', line 43

def auth
    @auth
end

#check_redirect(url) ⇒ Object



47
48
49
50
# File 'lib/noms/command/useragent.rb', line 47

def check_redirect(url)
    @log.debug "Running #{@redirect_checks.size} redirect checks on #{url}" unless @redirect_checks.empty?
    @redirect_checks.all? { |check| check.call(url) }
end

#clear_redirect_checksObject



123
124
125
126
# File 'lib/noms/command/useragent.rb', line 123

def clear_redirect_checks
    @log.debug "Clearing redirect checks"
    @redirect_checks = [ ]
end

#get(url, headers = {}) ⇒ Object



108
109
110
# File 'lib/noms/command/useragent.rb', line 108

def get(url, headers={})
    request('GET', url, nil, headers)
end

#origin=(new_origin) ⇒ Object



52
53
54
55
# File 'lib/noms/command/useragent.rb', line 52

def origin=(new_origin)
    @log.debug "Setting my origin to #{new_origin}"
    @origin = new_origin
end

#pop_redirect_checkObject



128
129
130
131
132
133
# File 'lib/noms/command/useragent.rb', line 128

def pop_redirect_check
    unless @redirect_checks.empty?
        @log.debug "Popping redirect check: #{@redirect_checks[-1]}"
        @redirect_checks.pop
    end
end

#request(method, url, data = nil, headers = {}, tries = 10, identity = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/noms/command/useragent.rb', line 68

def request(method, url, data=nil, headers={}, tries=10, identity=nil)
    req_url = absolute_url(url)
    @log.debug "#{method} #{req_url}" + (headers.empty? ? '' : headers.inspect)
    response = @client.request(method.to_s.upcase, req_url, '', data, headers)
    @log.debug "-> #{response.status} #{response.reason} (#{response.content.size} bytes of #{response.contenttype})"
    @log.debug JSON.pretty_generate(response.headers)
    case response.status
    when 401
        @log.debug "   handling unauthorized"
        if identity
            @log.debug "   we have an identity #{identity} but are trying again"
            if tries > 0
                @log.debug "loading authentication identity for #{url}"
                identity = @auth.load(url, response)
                @client.set_auth(identity['domain'], identity['username'], identity['password'])
                response, req_url = self.request(method, url, data, headers, tries - 1, identity)
            end
        else
            identity = @auth.load(url, response)
            @client.set_auth(identity['domain'], identity['username'], identity['password'])
            response, req_url = self.request(method, url, data, headers, 2, identity)
        end
    when 302, 301
        new_url = response.header['location'].first
        if check_redirect new_url
            @log.debug "redirect to #{new_url}"
            raise NOMS::Command::Error.new "Can't follow redirect to #{new_url}: too many redirects" if tries <= 0
            response, req_url = self.request(method, new_url, data, headers, tries - 1)
        end
    end

    if identity and response.ok?
        @log.debug "Login succeeded, saving #{identity}"
        identity.save
    end

    @log.debug "<- #{response.status} #{response.reason} <- #{req_url}"
    [response, req_url]
end

#wait(on = nil) ⇒ Object

Wait for all asynchronous requests to complete. A stub while these are simulated



114
115
116
# File 'lib/noms/command/useragent.rb', line 114

def wait(on=nil)
    []
end