Class: HTTPAccess2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/reap/vendor/http-access2.rb

Overview

DESCRIPTION

HTTPAccess2::Client -- Client to retrieve web resources via HTTP.

How to create your client.

1. Create simple client.
  clnt = HTTPAccess2::Client.new

2. Accessing resources through HTTP proxy.
  clnt = HTTPAccess2::Client.new("http://myproxy:8080")

3. Set User-Agent and From in HTTP request header.(nil means "No proxy")
  clnt = HTTPAccess2::Client.new(nil, "MyAgent", "[email protected]")

How to retrieve web resources.

1. Get content of specified URL.
  puts clnt.get_content("http://www.ruby-lang.org/en/")

2. Do HEAD request.
  res = clnt.head(uri)

3. Do GET request with query.
  res = clnt.get(uri)

4. Do POST request.
  res = clnt.post(uri)
  res = clnt.get|post|head(uri, proxy)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy = nil, agent_name = nil, from = nil) ⇒ Client

SYNOPSIS

Client.new(proxy = nil, agent_name = nil, from = nil)

ARGS

proxy		A String of HTTP proxy URL. ex. "http://proxy:8080".
agent_name	A String for "User-Agent" HTTP request header.
from		A String for "From" HTTP request header.

DESCRIPTION

Create an instance.
SSLConfig cannot be re-initialized.  Create new client.


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/reap/vendor/http-access2.rb', line 98

def initialize(proxy = nil, agent_name = nil, from = nil)
  @proxy = nil	# assigned later.
  @no_proxy = nil
  @agent_name = agent_name
  @from = from
  @basic_auth = BasicAuth.new(self)
  @debug_dev = nil
  @ssl_config = SSLConfig.new(self)
  @redirect_uri_callback = method(:default_redirect_uri_callback)
  @test_loopback_response = []
  @session_manager = SessionManager.new
  @session_manager.agent_name = @agent_name
  @session_manager.from = @from
  @session_manager.ssl_config = @ssl_config
  @cookie_manager = WebAgent::CookieManager.new
  self.proxy = proxy
end

Instance Attribute Details

#agent_nameObject (readonly)

Returns the value of attribute agent_name.



70
71
72
# File 'lib/reap/vendor/http-access2.rb', line 70

def agent_name
  @agent_name
end

Returns the value of attribute cookie_manager.



73
74
75
# File 'lib/reap/vendor/http-access2.rb', line 73

def cookie_manager
  @cookie_manager
end

#fromObject (readonly)

Returns the value of attribute from.



71
72
73
# File 'lib/reap/vendor/http-access2.rb', line 71

def from
  @from
end

#ssl_configObject (readonly)

Returns the value of attribute ssl_config.



72
73
74
# File 'lib/reap/vendor/http-access2.rb', line 72

def ssl_config
  @ssl_config
end

#test_loopback_responseObject (readonly)

Returns the value of attribute test_loopback_response.



74
75
76
# File 'lib/reap/vendor/http-access2.rb', line 74

def test_loopback_response
  @test_loopback_response
end

Instance Method Details

#connect_timeoutObject



135
136
137
# File 'lib/reap/vendor/http-access2.rb', line 135

def connect_timeout
  @session_manager.connect_timeout
end

#connect_timeout=(connect_timeout) ⇒ Object



139
140
141
142
# File 'lib/reap/vendor/http-access2.rb', line 139

def connect_timeout=(connect_timeout)
  reset_all
  @session_manager.connect_timeout = connect_timeout
end

#debug_devObject



116
117
118
# File 'lib/reap/vendor/http-access2.rb', line 116

def debug_dev
  @debug_dev
end

#debug_dev=(dev) ⇒ Object



120
121
122
123
124
# File 'lib/reap/vendor/http-access2.rb', line 120

def debug_dev=(dev)
  @debug_dev = dev
  reset_all
  @session_manager.debug_dev = dev
end

#default_redirect_uri_callback(res) ⇒ Object



251
252
253
254
255
# File 'lib/reap/vendor/http-access2.rb', line 251

def default_redirect_uri_callback(res)
  uri = res.header['location'][0]
  puts "Redirect to: #{uri}" if $DEBUG
  uri
end

#delete(uri, extheader = {}, &block) ⇒ Object



273
274
275
# File 'lib/reap/vendor/http-access2.rb', line 273

def delete(uri, extheader = {}, &block)
  request('DELETE', uri, nil, nil, extheader, &block)
end

#delete_async(uri, extheader = {}) ⇒ Object



309
310
311
# File 'lib/reap/vendor/http-access2.rb', line 309

def delete_async(uri, extheader = {})
  request_async('DELETE', uri, nil, nil, extheader)
end

#get(uri, query = nil, extheader = {}, &block) ⇒ Object



261
262
263
# File 'lib/reap/vendor/http-access2.rb', line 261

def get(uri, query = nil, extheader = {}, &block)
  request('GET', uri, query, nil, extheader, &block)
end

#get_async(uri, query = nil, extheader = {}) ⇒ Object



297
298
299
# File 'lib/reap/vendor/http-access2.rb', line 297

def get_async(uri, query = nil, extheader = {})
  request_async('GET', uri, query, nil, extheader)
end

#get_content(uri, query = nil, extheader = {}, &block) ⇒ Object

SYNOPSIS

Client#get_content(uri, query = nil, extheader = {}, &block = nil)

ARGS

uri	an_URI or a_string of uri to connect.
query	a_hash or an_array of query part.  e.g. { "a" => "b" }.
		Give an array to pass multiple value like
		[["a" => "b"], ["a" => "c"]].
extheader
		a_hash of extra headers like { "SOAPAction" => "urn:foo" }.
&block	Give a block to get chunked message-body of response like
		get_content(uri) { |chunked_body| ... }
		Size of each chunk may not be the same.

DESCRIPTION

Get a_sring of message-body of response.


239
240
241
242
243
# File 'lib/reap/vendor/http-access2.rb', line 239

def get_content(uri, query = nil, extheader = {}, &block)
  retry_connect(uri, query) do |uri, query|
    get(uri, query, extheader, &block)
  end
end

#head(uri, query = nil, extheader = {}) ⇒ Object



257
258
259
# File 'lib/reap/vendor/http-access2.rb', line 257

def head(uri, query = nil, extheader = {})
  request('HEAD', uri, query, nil, extheader)
end

#head_async(uri, query = nil, extheader = {}) ⇒ Object

Async interface.



293
294
295
# File 'lib/reap/vendor/http-access2.rb', line 293

def head_async(uri, query = nil, extheader = {})
  request_async('HEAD', uri, query, nil, extheader)
end

#no_proxyObject



184
185
186
# File 'lib/reap/vendor/http-access2.rb', line 184

def no_proxy
  @no_proxy
end

#no_proxy=(no_proxy) ⇒ Object



188
189
190
191
# File 'lib/reap/vendor/http-access2.rb', line 188

def no_proxy=(no_proxy)
  @no_proxy = no_proxy
  reset_all
end

#options(uri, extheader = {}, &block) ⇒ Object



277
278
279
# File 'lib/reap/vendor/http-access2.rb', line 277

def options(uri, extheader = {}, &block)
  request('OPTIONS', uri, nil, nil, extheader, &block)
end

#options_async(uri, extheader = {}) ⇒ Object



313
314
315
# File 'lib/reap/vendor/http-access2.rb', line 313

def options_async(uri, extheader = {})
  request_async('OPTIONS', uri, nil, nil, extheader)
end

#post(uri, body = nil, extheader = {}, &block) ⇒ Object



265
266
267
# File 'lib/reap/vendor/http-access2.rb', line 265

def post(uri, body = nil, extheader = {}, &block)
  request('POST', uri, nil, body, extheader, &block)
end

#post_async(uri, body = nil, extheader = {}) ⇒ Object



301
302
303
# File 'lib/reap/vendor/http-access2.rb', line 301

def post_async(uri, body = nil, extheader = {})
  request_async('POST', uri, nil, body, extheader)
end

#post_content(uri, body = nil, extheader = {}, &block) ⇒ Object



245
246
247
248
249
# File 'lib/reap/vendor/http-access2.rb', line 245

def post_content(uri, body = nil, extheader = {}, &block)
  retry_connect(uri, nil) do |uri, query|
    post(uri, body, extheader, &block)
  end
end

#protocol_versionObject



126
127
128
# File 'lib/reap/vendor/http-access2.rb', line 126

def protocol_version
  @session_manager.protocol_version
end

#protocol_version=(protocol_version) ⇒ Object



130
131
132
133
# File 'lib/reap/vendor/http-access2.rb', line 130

def protocol_version=(protocol_version)
  reset_all
  @session_manager.protocol_version = protocol_version
end

#proxyObject



162
163
164
# File 'lib/reap/vendor/http-access2.rb', line 162

def proxy
  @proxy
end

#proxy=(proxy) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/reap/vendor/http-access2.rb', line 166

def proxy=(proxy)
  if proxy.nil?
    @proxy = nil
  else
    if proxy.is_a?(URI)
      @proxy = proxy
    else
      @proxy = URI.parse(proxy)
    end
    if @proxy.scheme == nil or @proxy.scheme.downcase != 'http' or
 @proxy.host == nil or @proxy.port == nil
	raise ArgumentError.new("unsupported proxy `#{proxy}'")
    end
  end
  reset_all
  @proxy
end

#put(uri, body = nil, extheader = {}, &block) ⇒ Object



269
270
271
# File 'lib/reap/vendor/http-access2.rb', line 269

def put(uri, body = nil, extheader = {}, &block)
  request('PUT', uri, nil, body, extheader, &block)
end

#put_async(uri, body = nil, extheader = {}) ⇒ Object



305
306
307
# File 'lib/reap/vendor/http-access2.rb', line 305

def put_async(uri, body = nil, extheader = {})
  request_async('PUT', uri, nil, body, extheader)
end

#receive_timeoutObject



153
154
155
# File 'lib/reap/vendor/http-access2.rb', line 153

def receive_timeout
  @session_manager.receive_timeout
end

#receive_timeout=(receive_timeout) ⇒ Object



157
158
159
160
# File 'lib/reap/vendor/http-access2.rb', line 157

def receive_timeout=(receive_timeout)
  reset_all
  @session_manager.receive_timeout = receive_timeout
end

#redirect_uri_callback=(redirect_uri_callback) ⇒ Object



218
219
220
# File 'lib/reap/vendor/http-access2.rb', line 218

def redirect_uri_callback=(redirect_uri_callback)
  @redirect_uri_callback = redirect_uri_callback
end

#request(method, uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object



285
286
287
288
289
# File 'lib/reap/vendor/http-access2.rb', line 285

def request(method, uri, query = nil, body = nil, extheader = {}, &block)
  conn = Connection.new
  conn_request(conn, method, uri, query, body, extheader, &block)
  conn.pop
end

#request_async(method, uri, query = nil, body = nil, extheader = {}) ⇒ Object



321
322
323
324
325
326
327
328
# File 'lib/reap/vendor/http-access2.rb', line 321

def request_async(method, uri, query = nil, body = nil, extheader = {})
  conn = Connection.new
  t = Thread.new(conn) { |tconn|
    conn_request(tconn, method, uri, query, body, extheader)
  }
  conn.async_thread = t
  conn
end

#reset(uri) ⇒ Object

Management interface.



338
339
340
# File 'lib/reap/vendor/http-access2.rb', line 338

def reset(uri)
  @session_manager.reset(uri)
end

#reset_allObject



342
343
344
# File 'lib/reap/vendor/http-access2.rb', line 342

def reset_all
  @session_manager.reset_all
end


214
215
216
# File 'lib/reap/vendor/http-access2.rb', line 214

def save_cookie_store
  @cookie_manager.save_cookies
end

#send_timeoutObject



144
145
146
# File 'lib/reap/vendor/http-access2.rb', line 144

def send_timeout
  @session_manager.send_timeout
end

#send_timeout=(send_timeout) ⇒ Object



148
149
150
151
# File 'lib/reap/vendor/http-access2.rb', line 148

def send_timeout=(send_timeout)
  reset_all
  @session_manager.send_timeout = send_timeout
end

#set_basic_auth(uri, user_id, passwd) ⇒ Object



199
200
201
202
203
204
# File 'lib/reap/vendor/http-access2.rb', line 199

def set_basic_auth(uri, user_id, passwd)
  unless uri.is_a?(URI)
    uri = URI.parse(uri)
  end
  @basic_auth.set(uri, user_id, passwd)
end


206
207
208
209
210
211
212
# File 'lib/reap/vendor/http-access2.rb', line 206

def set_cookie_store(filename)
  if @cookie_manager.cookies_file
    raise RuntimeError.new("overriding cookie file location")
  end
  @cookie_manager.cookies_file = filename
  @cookie_manager.load_cookies if filename
end

#socket_sync=(socket_sync) ⇒ Object

if your ruby is older than 2005-09-06, do not set socket_sync = false to avoid an SSL socket blocking bug in openssl/buffering.rb.



195
196
197
# File 'lib/reap/vendor/http-access2.rb', line 195

def socket_sync=(socket_sync)
  @session_manager.socket_sync = socket_sync
end

#trace(uri, query = nil, body = nil, extheader = {}, &block) ⇒ Object



281
282
283
# File 'lib/reap/vendor/http-access2.rb', line 281

def trace(uri, query = nil, body = nil, extheader = {}, &block)
  request('TRACE', uri, query, body, extheader, &block)
end

#trace_async(uri, query = nil, body = nil, extheader = {}) ⇒ Object



317
318
319
# File 'lib/reap/vendor/http-access2.rb', line 317

def trace_async(uri, query = nil, body = nil, extheader = {})
  request_async('TRACE', uri, query, body, extheader)
end