Class: WWW::Mechanize::Chain::HeaderResolver

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

Instance Attribute Summary

Attributes included from Handler

#chain

Instance Method Summary collapse

Constructor Details

#initialize(keep_alive, keep_alive_time, cookie_jar, user_agent, headers) ⇒ HeaderResolver

Returns a new instance of HeaderResolver.



6
7
8
9
10
11
12
# File 'lib/www/mechanize/chain/header_resolver.rb', line 6

def initialize(keep_alive, keep_alive_time, cookie_jar, user_agent, headers)
  @keep_alive = keep_alive
  @keep_alive_time = keep_alive_time
  @cookie_jar = cookie_jar
  @user_agent = user_agent
  @headers = headers
end

Instance Method Details

#handle(ctx, params) ⇒ Object



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
49
# File 'lib/www/mechanize/chain/header_resolver.rb', line 14

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 

  @headers.each do |k,v|
    request[k] = v
  end if request
  super
end