Class: HTTPAccess2::Client

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rss-client/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)

Defined Under Namespace

Classes: RetryableResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

hash_find_value, parse_challenge_param, uri_dirname, uri_part_of, #urify

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.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rss-client/http-access2.rb', line 159

def initialize(proxy = nil, agent_name = nil, from = nil)
  @proxy = nil        # assigned later.
  @no_proxy = nil
  @agent_name = agent_name
  @from = from
  @www_auth = WWWAuth.new
  @proxy_auth = ProxyAuth.new
  @request_filter = [@proxy_auth, @www_auth]
  @debug_dev = nil
  @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 = SSLConfig.new(self)
  @cookie_manager = WebAgent::CookieManager.new
  self.proxy = proxy
end

Instance Attribute Details

#agent_nameObject (readonly)

Returns the value of attribute agent_name.



125
126
127
# File 'lib/rss-client/http-access2.rb', line 125

def agent_name
  @agent_name
end

Returns the value of attribute cookie_manager.



128
129
130
# File 'lib/rss-client/http-access2.rb', line 128

def cookie_manager
  @cookie_manager
end

#fromObject (readonly)

Returns the value of attribute from.



126
127
128
# File 'lib/rss-client/http-access2.rb', line 126

def from
  @from
end

#proxy_authObject (readonly)

Returns the value of attribute proxy_auth.



131
132
133
# File 'lib/rss-client/http-access2.rb', line 131

def proxy_auth
  @proxy_auth
end

#request_filterObject (readonly)

Returns the value of attribute request_filter.



130
131
132
# File 'lib/rss-client/http-access2.rb', line 130

def request_filter
  @request_filter
end

#ssl_configObject (readonly)

Returns the value of attribute ssl_config.



127
128
129
# File 'lib/rss-client/http-access2.rb', line 127

def ssl_config
  @ssl_config
end

#test_loopback_responseObject (readonly)

Returns the value of attribute test_loopback_response.



129
130
131
# File 'lib/rss-client/http-access2.rb', line 129

def test_loopback_response
  @test_loopback_response
end

#www_authObject (readonly)

Returns the value of attribute www_auth.



132
133
134
# File 'lib/rss-client/http-access2.rb', line 132

def www_auth
  @www_auth
end

Instance Method Details

#connect_timeoutObject



197
198
199
# File 'lib/rss-client/http-access2.rb', line 197

def connect_timeout
  @session_manager.connect_timeout
end

#connect_timeout=(connect_timeout) ⇒ Object



201
202
203
204
# File 'lib/rss-client/http-access2.rb', line 201

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

#debug_devObject



178
179
180
# File 'lib/rss-client/http-access2.rb', line 178

def debug_dev
  @debug_dev
end

#debug_dev=(dev) ⇒ Object



182
183
184
185
186
# File 'lib/rss-client/http-access2.rb', line 182

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

#default_redirect_uri_callback(uri, res) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rss-client/http-access2.rb', line 331

def default_redirect_uri_callback(uri, res)
  newuri = URI.parse(res.header['location'][0])
  unless newuri.is_a?(URI::HTTP)
    newuri = uri + newuri
    STDERR.puts(
      "could be a relative URI in location header which is not recommended")
    STDERR.puts(
      "'The field value consists of a single absolute URI' in HTTP spec")
  end
  puts "Redirect to: #{newuri}" if $DEBUG
  newuri
end

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



360
361
362
# File 'lib/rss-client/http-access2.rb', line 360

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

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



410
411
412
# File 'lib/rss-client/http-access2.rb', line 410

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

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



348
349
350
# File 'lib/rss-client/http-access2.rb', line 348

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

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



398
399
400
# File 'lib/rss-client/http-access2.rb', line 398

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.


313
314
315
316
317
# File 'lib/rss-client/http-access2.rb', line 313

def get_content(uri, query = nil, extheader = {}, &block)
  follow_redirect(uri, query) { |uri, query|
    get(uri, query, extheader, &block)
  }.content
end

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



344
345
346
# File 'lib/rss-client/http-access2.rb', line 344

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

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

Async interface.



394
395
396
# File 'lib/rss-client/http-access2.rb', line 394

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

#no_proxyObject



247
248
249
# File 'lib/rss-client/http-access2.rb', line 247

def no_proxy
  @no_proxy
end

#no_proxy=(no_proxy) ⇒ Object



251
252
253
254
# File 'lib/rss-client/http-access2.rb', line 251

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

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



364
365
366
# File 'lib/rss-client/http-access2.rb', line 364

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

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



414
415
416
# File 'lib/rss-client/http-access2.rb', line 414

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

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



352
353
354
# File 'lib/rss-client/http-access2.rb', line 352

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

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



402
403
404
# File 'lib/rss-client/http-access2.rb', line 402

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

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



319
320
321
322
323
# File 'lib/rss-client/http-access2.rb', line 319

def post_content(uri, body = nil, extheader = {}, &block)
  follow_redirect(uri, nil) { |uri, query|
    post(uri, body, extheader, &block)
  }.content
end

#protocol_versionObject



188
189
190
# File 'lib/rss-client/http-access2.rb', line 188

def protocol_version
  @session_manager.protocol_version
end

#protocol_version=(protocol_version) ⇒ Object



192
193
194
195
# File 'lib/rss-client/http-access2.rb', line 192

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

#proxyObject



224
225
226
# File 'lib/rss-client/http-access2.rb', line 224

def proxy
  @proxy
end

#proxy=(proxy) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rss-client/http-access2.rb', line 228

def proxy=(proxy)
  if proxy.nil?
    @proxy = nil
    @proxy_auth.reset_challenge
  else
    @proxy = urify(proxy)
    if @proxy.scheme == nil or @proxy.scheme.downcase != 'http' or
        @proxy.host == nil or @proxy.port == nil
      raise ArgumentError.new("unsupported proxy `#{proxy}'")
    end
    @proxy_auth.reset_challenge
    if @proxy.user || @proxy.password
      @proxy_auth.set_auth(@proxy.user, @proxy.password)
    end
  end
  reset_all
  @proxy
end

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



356
357
358
# File 'lib/rss-client/http-access2.rb', line 356

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

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



406
407
408
# File 'lib/rss-client/http-access2.rb', line 406

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

#receive_timeoutObject



215
216
217
# File 'lib/rss-client/http-access2.rb', line 215

def receive_timeout
  @session_manager.receive_timeout
end

#receive_timeout=(receive_timeout) ⇒ Object



219
220
221
222
# File 'lib/rss-client/http-access2.rb', line 219

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

#redirect_uri_callback=(redirect_uri_callback) ⇒ Object



293
294
295
# File 'lib/rss-client/http-access2.rb', line 293

def redirect_uri_callback=(redirect_uri_callback)
  @redirect_uri_callback = redirect_uri_callback
end

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



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rss-client/http-access2.rb', line 372

def request(method, uri, query = nil, body = nil, extheader = {}, &block)
  uri = urify(uri)
  conn = Connection.new
  res = nil
  retry_count = 5
  while retry_count > 0
    begin
      prepare_request(method, uri, query, body, extheader) do |req, proxy|
        do_get_block(req, proxy, conn, &block)
      end
      res = conn.pop
      break
    rescue Client::RetryableResponse
      res = conn.pop
      retry_count -= 1
    end
  end
  res
end

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



422
423
424
425
426
427
428
429
430
431
432
# File 'lib/rss-client/http-access2.rb', line 422

def request_async(method, uri, query = nil, body = nil, extheader = {})
  uri = urify(uri)
  conn = Connection.new
  t = Thread.new(conn) { |tconn|
    prepare_request(method, uri, query, body, extheader) do |req, proxy|
      do_get_stream(req, proxy, tconn)
    end
  }
  conn.async_thread = t
  conn
end

#reset(uri) ⇒ Object

Management interface.



442
443
444
445
# File 'lib/rss-client/http-access2.rb', line 442

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

#reset_allObject



447
448
449
# File 'lib/rss-client/http-access2.rb', line 447

def reset_all
  @session_manager.reset_all
end


289
290
291
# File 'lib/rss-client/http-access2.rb', line 289

def save_cookie_store
  @cookie_manager.save_cookies
end

#send_timeoutObject



206
207
208
# File 'lib/rss-client/http-access2.rb', line 206

def send_timeout
  @session_manager.send_timeout
end

#send_timeout=(send_timeout) ⇒ Object



210
211
212
213
# File 'lib/rss-client/http-access2.rb', line 210

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

#set_auth(uri, user, passwd) ⇒ Object



262
263
264
265
266
# File 'lib/rss-client/http-access2.rb', line 262

def set_auth(uri, user, passwd)
  uri = urify(uri)
  @www_auth.set_auth(uri, user, passwd)
  reset_all
end

#set_basic_auth(uri, user, passwd) ⇒ Object

for backward compatibility



269
270
271
272
273
# File 'lib/rss-client/http-access2.rb', line 269

def set_basic_auth(uri, user, passwd)
  uri = urify(uri)
  @www_auth.basic_auth.set(uri, user, passwd)
  reset_all
end


281
282
283
284
285
286
287
# File 'lib/rss-client/http-access2.rb', line 281

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

#set_proxy_auth(user, passwd) ⇒ Object



275
276
277
278
279
# File 'lib/rss-client/http-access2.rb', line 275

def set_proxy_auth(user, passwd)
  uri = urify(uri)
  @proxy_auth.set_auth(user, passwd)
  reset_all
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.



258
259
260
# File 'lib/rss-client/http-access2.rb', line 258

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

#strict_redirect_uri_callback(uri, res) ⇒ Object



325
326
327
328
329
# File 'lib/rss-client/http-access2.rb', line 325

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

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



368
369
370
# File 'lib/rss-client/http-access2.rb', line 368

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



418
419
420
# File 'lib/rss-client/http-access2.rb', line 418

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