File: response_header_handler.rb

package info (click to toggle)
libwww-mechanize-ruby 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 883
  • sloc: ruby: 6,621; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
46
47
48
class Mechanize
  class Chain
    class ResponseHeaderHandler
      include Mechanize::Handler

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

      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('//head/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
    end
  end
end