Class: Steam::Browser::HtmlUnit::Connection

Inherits:
Object
  • Object
show all
Includes:
Java::Com::Gargoylesoftware::Htmlunit
Defined in:
lib/steam/browser/html_unit/connection.rb

Constant Summary collapse

NameValuePair =

HtmlUnit 2.7

Java.import(classifier, 'NameValuePair')
@@lock =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Connection

Returns a new instance of Connection.



36
37
38
39
# File 'lib/steam/browser/html_unit/connection.rb', line 36

def initialize(connection)
  @connection = connection
  @java = MockWebConnection.new
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



34
35
36
# File 'lib/steam/browser/html_unit/connection.rb', line 34

def connection
  @connection
end

#javaObject (readonly)

Returns the value of attribute java.



34
35
36
# File 'lib/steam/browser/html_unit/connection.rb', line 34

def java
  @java
end

Instance Method Details

#getResponse(request_settings) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/steam/browser/html_unit/connection.rb', line 41

def getResponse(request_settings)
  # FIXME preserve original scheme, host + port
  method = request_settings.getHttpMethod.toString.dup
  url    = request_settings.getUrl.toString.dup

  body   = request_settings.getRequestBody
  body   = body.toString.dup if body

  params = request_settings.getRequestParameters
  params = Hash[*params.toArray.map { |e| [e.name, e.value] }.flatten]

  input  = body ? body : Rack::Utils.build_nested_query(params)
  env    = Request.new(:method => method, :url => url, :input => input).env

  status, headers, response = connection.call(env)
  response.body.close if response.body.respond_to?(:close)

  set_response(request_settings.getUrl, response)
  java.getResponse(request_settings)
rescue Exception => e
  puts "#{e.class.name}: #{e.message}"
  e.backtrace.each { |line| puts line }
end

#set_response(url, response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/steam/browser/html_unit/connection.rb', line 65

def set_response(url, response)
  body    = response.body.join
  status  = response.status
  message = Rack::Utils::HTTP_STATUS_CODES[status.to_i]
  charset = Steam.config[:charset]
  headers = response.header.map { |key, value| NameValuePair.new(key, value) }
  headers = Java::Util::Arrays.asList(headers)
  content_type = response.content_type

  java.setResponse(url, body, status, message, content_type, charset, headers)
end