Class: EventMachine::Protocols::HttpClient2

Inherits:
Connection
  • Object
show all
Includes:
LineText2
Defined in:
lib/protocols/httpcli2.rb

Overview

Example

EM.run{
  include EM::Protocols
  conn = HttpClient2.connect 'google.com', 80

  req = conn.get('/')
  req.callback{
    p(req.content)
  }

Defined Under Namespace

Classes: Request

Constant Summary

Constants included from LineText2

LineText2::MaxBinaryLength, LineText2::MaxLineLength

Instance Attribute Summary

Attributes inherited from Connection

#signature

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LineText2

#receive_data, #receive_end_of_binary_data, #set_binary_mode, #set_delimiter, #set_line_mode, #set_text_mode

Methods inherited from Connection

#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #detach, #error?, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_sockname, #get_status, #initialize, new, #receive_data, #reconnect, #send_data, #send_datagram, #send_file_data, #set_comm_inactivity_timeout, #ssl_handshake_completed, #start_tls, #stream_file_data

Constructor Details

This class inherits a constructor from EventMachine::Connection

Class Method Details

.connect(*args) ⇒ Object

Make a connection to a remote HTTP server. Can take either a pair of arguments (which will be interpreted as a hostname/ip-address and a port), or a hash. If the arguments are a hash, then supported values include:

:host => a hostname or ip-address;
:port => a port number

– TODO, support optional encryption arguments like :ssl



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/protocols/httpcli2.rb', line 237

def self.connect *args
	if args.length == 2
		args = {:host=>args[0], :port=>args[1]}
	else
		args = args.first
	end

	h,prt,ssl = args[:host], Integer(args[:port]), (args[:tls] || args[:ssl])
	conn = EM.connect( h, prt, self )
    conn.start_tls if ssl
	conn.set_default_host_header( h, prt, ssl )
	conn
end

Instance Method Details

#connection_completedObject



270
271
272
273
# File 'lib/protocols/httpcli2.rb', line 270

def connection_completed
	super
	@connected.succeed
end

#get(args) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/protocols/httpcli2.rb', line 294

def get args
	if args.is_a?(String)
		args = {:uri=>args}
	end
	args[:verb] = "GET"
	request args
end

#pop_requestObject

– Called by a Request object when it completes.



339
340
341
# File 'lib/protocols/httpcli2.rb', line 339

def pop_request
	@requests.pop
end

#post(args) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/protocols/httpcli2.rb', line 302

def post args
	if args.is_a?(String)
		args = {:uri=>args}
	end
	args[:verb] = "POST"
	request args
end

#post_initObject



265
266
267
268
# File 'lib/protocols/httpcli2.rb', line 265

def post_init
	super
	@connected = EM::DefaultDeferrable.new
end

#receive_binary_data(text) ⇒ Object



332
333
334
# File 'lib/protocols/httpcli2.rb', line 332

def receive_binary_data text
	@requests.last.receive_text text
end

#receive_line(ln) ⇒ Object



323
324
325
326
327
328
329
330
331
# File 'lib/protocols/httpcli2.rb', line 323

def receive_line ln
	if req = @requests.last
		req.receive_line ln
	else
		p "??????????"
		p ln
	end

end

#request(args) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/protocols/httpcli2.rb', line 310

def request args
	args[:host_header] = @host_header unless args.has_key?(:host_header)
	args[:authorization] = @authorization unless args.has_key?(:authorization)
	r = Request.new self, args
	if @closed
		r.fail
	else
		(@requests ||= []).unshift r
		@connected.callback {r.send_request}
	end
	r
end

#set_default_host_header(host, port, ssl) ⇒ Object

– Compute and remember a string to be used as the host header in HTTP requests unless the user overrides it with an argument to #request.



256
257
258
259
260
261
262
# File 'lib/protocols/httpcli2.rb', line 256

def set_default_host_header host, port, ssl
	if (ssl and port != 443) or (!ssl and port != 80)
		@host_header = "#{host}:#{port}"
	else
		@host_header = host
	end
end

#unbindObject

– All pending requests, if any, must fail. We might come here without ever passing through connection_completed in case we can’t connect to the server. We’ll also get here when the connection closes (either because the server closes it, or we close it due to detecting an internal error or security violation). In either case, run down all pending requests, if any, and signal failure on them.

Set and remember a flag (@closed) so we can immediately fail any subsequent requests.



287
288
289
290
291
# File 'lib/protocols/httpcli2.rb', line 287

def unbind
	super
	@closed = true
	(@requests || []).each {|r| r.fail}
end