Sha256: 62b6cc7904ca9be27135294af65307f7c660c7abf7865718258883f2c614859c

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 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'
          request['Host'] = uri.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

4 entries across 4 versions & 1 rubygems

Version Path
mechanize-0.8.0 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.8.2 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.8.3 lib/www/mechanize/chain/header_resolver.rb
mechanize-0.8.1 lib/www/mechanize/chain/header_resolver.rb