Sha256: aec79351fd04912deafe77fce7b4a6231af6bd752abe1dd7d0f2783deda0092c

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

module WWW
  class Mechanize
    class Chain
      class HeaderResolver
        include WWW::Handler
        def initialize(keep_alive, keep_alive_time, cookie_jar, user_agent)
          @keep_alive = keep_alive
          @keep_alive_time = keep_alive_time
          @cookie_jar = cookie_jar
          @user_agent = user_agent
        end

        def handle(ctx, params)
          uri = params[:uri]
          referer = params[:referer]
          request = params[:request]

          if @keep_alive
            request['Connection'] = 'keep-alive'
            request['Keep-Alive'] = @keep_alive_time.to_s
          else
            request['Connection'] = 'close'
          end
          request['Accept-Encoding'] = 'gzip,identity'
          request['Accept-Language'] = 'en-us,en;q=0.5'
          host = "#{uri.host}#{[80, 443].include?(uri.port.to_i) ? '' : ':' + uri.port.to_s}"
          request['Host'] = host
          request['Accept-Charset'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
  
          unless @cookie_jar.empty?(uri)
            cookies = @cookie_jar.cookies(uri)
            cookie = cookies.length > 0 ? cookies.join("; ") : nil
            request.add_field('Cookie', cookie)
          end
  
          # Add Referer header to request
          if referer && referer.uri
            request['Referer'] = referer.uri.to_s
          end
  
          # Add User-Agent header to request
          request['User-Agent'] = @user_agent if @user_agent 
          super
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
mechanize-ntlm-0.9.1 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.8.5 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.9.0 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.9.1 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.9.2 lib/www/mechanize/chain/header_resolver.rb