Class: HTTPAccess2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/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.


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

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.



68
69
70
# File 'lib/http-access2.rb', line 68

def agent_name
  @agent_name
end

Returns the value of attribute cookie_manager.



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

def cookie_manager
  @cookie_manager
end

#fromObject (readonly)

Returns the value of attribute from.



69
70
71
# File 'lib/http-access2.rb', line 69

def from
  @from
end

#ssl_configObject (readonly)

Returns the value of attribute ssl_config.



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

def ssl_config
  @ssl_config
end

#test_loopback_responseObject (readonly)

Returns the value of attribute test_loopback_response.



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

def test_loopback_response
  @test_loopback_response
end

Instance Method Details

#connect_timeoutObject



133
134
135
# File 'lib/http-access2.rb', line 133

def connect_timeout
  @session_manager.connect_timeout
end

#connect_timeout=(connect_timeout) ⇒ Object



137
138
139
140
# File 'lib/http-access2.rb', line 137

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

#debug_devObject



114
115
116
# File 'lib/http-access2.rb', line 114

def debug_dev
  @debug_dev
end

#debug_dev=(dev) ⇒ Object



118
119
120
121
122
# File 'lib/http-access2.rb', line 118

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

#default_redirect_uri_callback(res) ⇒ Object



249
250
251
252
253
# File 'lib/http-access2.rb', line 249

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

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



271
272
273
# File 'lib/http-access2.rb', line 271

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

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



307
308
309
# File 'lib/http-access2.rb', line 307

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

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



259
260
261
# File 'lib/http-access2.rb', line 259

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

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



295
296
297
# File 'lib/http-access2.rb', line 295

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.


237
238
239
240
241
# File 'lib/http-access2.rb', line 237

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



255
256
257
# File 'lib/http-access2.rb', line 255

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

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

Async interface.



291
292
293
# File 'lib/http-access2.rb', line 291

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

#no_proxyObject



182
183
184
# File 'lib/http-access2.rb', line 182

def no_proxy
  @no_proxy
end

#no_proxy=(no_proxy) ⇒ Object



186
187
188
189
# File 'lib/http-access2.rb', line 186

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

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



275
276
277
# File 'lib/http-access2.rb', line 275

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

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



311
312
313
# File 'lib/http-access2.rb', line 311

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

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



263
264
265
# File 'lib/http-access2.rb', line 263

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

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



299
300
301
# File 'lib/http-access2.rb', line 299

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

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



243
244
245
246
247
# File 'lib/http-access2.rb', line 243

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

#protocol_versionObject



124
125
126
# File 'lib/http-access2.rb', line 124

def protocol_version
  @session_manager.protocol_version
end

#protocol_version=(protocol_version) ⇒ Object



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

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

#proxyObject



160
161
162
# File 'lib/http-access2.rb', line 160

def proxy
  @proxy
end

#proxy=(proxy) ⇒ Object



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

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



267
268
269
# File 'lib/http-access2.rb', line 267

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

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



303
304
305
# File 'lib/http-access2.rb', line 303

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

#receive_timeoutObject



151
152
153
# File 'lib/http-access2.rb', line 151

def receive_timeout
  @session_manager.receive_timeout
end

#receive_timeout=(receive_timeout) ⇒ Object



155
156
157
158
# File 'lib/http-access2.rb', line 155

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

#redirect_uri_callback=(redirect_uri_callback) ⇒ Object



216
217
218
# File 'lib/http-access2.rb', line 216

def redirect_uri_callback=(redirect_uri_callback)
  @redirect_uri_callback = redirect_uri_callback
end

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



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

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



319
320
321
322
323
324
325
326
# File 'lib/http-access2.rb', line 319

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.



336
337
338
# File 'lib/http-access2.rb', line 336

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

#reset_allObject



340
341
342
# File 'lib/http-access2.rb', line 340

def reset_all
  @session_manager.reset_all
end


212
213
214
# File 'lib/http-access2.rb', line 212

def save_cookie_store
  @cookie_manager.save_cookies
end

#send_timeoutObject



142
143
144
# File 'lib/http-access2.rb', line 142

def send_timeout
  @session_manager.send_timeout
end

#send_timeout=(send_timeout) ⇒ Object



146
147
148
149
# File 'lib/http-access2.rb', line 146

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

#set_basic_auth(uri, user_id, passwd) ⇒ Object



197
198
199
200
201
202
# File 'lib/http-access2.rb', line 197

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


204
205
206
207
208
209
210
# File 'lib/http-access2.rb', line 204

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.



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

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

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



279
280
281
# File 'lib/http-access2.rb', line 279

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



315
316
317
# File 'lib/http-access2.rb', line 315

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