Class: Mechanize::Chain::ResponseHeaderHandler

Inherits:
Object
  • Object
show all
Includes:
Handler
Defined in:
lib/mechanize/chain/response_header_handler.rb

Instance Attribute Summary

Attributes included from Handler

#chain

Instance Method Summary collapse

Constructor Details

#initialize(cookie_jar, connection_cache) ⇒ ResponseHeaderHandler

Returns a new instance of ResponseHeaderHandler.



6
7
8
9
# File 'lib/mechanize/chain/response_header_handler.rb', line 6

def initialize(cookie_jar, connection_cache)
  @cookie_jar = cookie_jar
  @connection_cache = connection_cache
end

Instance Method Details

#handle(ctx, params) ⇒ Object



11
12
13
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
# File 'lib/mechanize/chain/response_header_handler.rb', line 11

def handle(ctx, params)
  response = params[:response]
  uri = params[:uri]
  page = params[:page]
  cache_obj = (@connection_cache["#{uri.host}:#{uri.port}"] ||= {
                 :connection         => nil,
                 :keep_alive_options => {},
               })

  # If the server sends back keep alive options, save them
  if keep_alive_info = response['keep-alive']
    keep_alive_info.split(/,\s*/).each do |option|
      k, v = option.split(/\=/)
      cache_obj[:keep_alive_options] ||= {}
      cache_obj[:keep_alive_options][k.intern] = v
    end
  end

  if page.is_a?(Page) && page.body =~ /Set-Cookie/n
    page.search('//meta[@http-equiv="Set-Cookie"]').each do |meta|
      Cookie::parse(uri, meta['content']) { |c|
        Mechanize.log.debug("saved cookie: #{c}") if Mechanize.log
        @cookie_jar.add(uri, c)
      }
    end
  end

  (response.get_fields('Set-Cookie')||[]).each do |cookie|
    Cookie::parse(uri, cookie) { |c|
      Mechanize.log.debug("saved cookie: #{c}") if Mechanize.log
      @cookie_jar.add(uri, c)
    }
  end
  super
end