Sha256: 00509e1633dc8828028d866be6fa1d42ab1b6a3157b8e14525aa5ce8762b468a

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 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}#{uri.port.to_i == 80 ? '' : ':' + 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

1 entries across 1 versions & 1 rubygems

Version Path
mechanize-0.8.4 lib/www/mechanize/chain/header_resolver.rb