Class: EventMachine::Protocols::HttpClient2 Deprecated
- Inherits:
-
Connection
- Object
- Connection
- EventMachine::Protocols::HttpClient2
- Includes:
- LineText2
- Defined in:
- lib/em/protocols/httpclient2.rb
Overview
Please use [EM-HTTP-Request](github.com/igrigorik/em-http-request) instead.
### Usage
“‘ruby EM.run{
conn = EM::Protocols::HttpClient2.connect 'google.com', 80
req = conn.get('/')
req.callback{ |response|
p(response.status)
p(response.headers)
p(response.content)
}
} “‘
Defined Under Namespace
Classes: Request
Constant Summary
Constants included from LineText2
Instance Attribute Summary
Attributes inherited from Connection
Class Method Summary collapse
-
.connect(*args) ⇒ Object
Make a connection to a remote HTTP server.
Instance Method Summary collapse
- #connection_completed ⇒ Object
-
#get(args) ⇒ Object
Get a url.
-
#initialize ⇒ HttpClient2
constructor
A new instance of HttpClient2.
-
#pop_request ⇒ Object
– Called by a Request object when it completes.
-
#post(args) ⇒ Object
Post to a url.
- #post_init ⇒ Object
- #receive_binary_data(text) ⇒ Object
- #receive_line(ln) ⇒ Object
- #request(args) ⇒ Object
-
#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.
-
#unbind ⇒ Object
– All pending requests, if any, must fail.
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_cipher_bits, #get_cipher_name, #get_cipher_protocol, #get_idle_time, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sni_hostname, #get_sock_opt, #get_sockname, #get_status, new, #notify_readable=, #notify_readable?, #notify_writable=, #notify_writable?, #pause, #paused?, #pending_connect_timeout, #pending_connect_timeout=, #proxy_completed, #proxy_incoming_to, #proxy_target_unbound, #receive_data, #reconnect, #resume, #send_data, #send_datagram, #send_file_data, #set_sock_opt, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data, #watch_only?
Constructor Details
#initialize ⇒ HttpClient2
Returns a new instance of HttpClient2.
49 50 51 52 53 54 55 |
# File 'lib/em/protocols/httpclient2.rb', line 49 def initialize warn "HttpClient2 is deprecated and will be removed. EM-Http-Request should be used instead." @authorization = nil @closed = nil @requests = nil end |
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
:ssl => true to enable ssl
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/em/protocols/httpclient2.rb', line 248 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_completed ⇒ Object
311 312 313 314 |
# File 'lib/em/protocols/httpclient2.rb', line 311 def connection_completed super @connected.succeed end |
#get(args) ⇒ Object
Get a url
req = conn.get(:uri => '/')
req.callback{|response| puts response.content }
267 268 269 270 271 272 273 |
# File 'lib/em/protocols/httpclient2.rb', line 267 def get args if args.is_a?(String) args = {:uri=>args} end args[:verb] = "GET" request args end |
#pop_request ⇒ Object
– Called by a Request object when it completes.
368 369 370 |
# File 'lib/em/protocols/httpclient2.rb', line 368 def pop_request @requests.pop end |
#post(args) ⇒ Object
Post to a url
req = conn.post('/data')
req.callback{|response| puts response.content }
– XXX there’s no way to supply a POST body.. wtf?
281 282 283 284 285 286 287 |
# File 'lib/em/protocols/httpclient2.rb', line 281 def post args if args.is_a?(String) args = {:uri=>args} end args[:verb] = "POST" request args end |
#post_init ⇒ Object
305 306 307 308 |
# File 'lib/em/protocols/httpclient2.rb', line 305 def post_init super @connected = EM::DefaultDeferrable.new end |
#receive_binary_data(text) ⇒ Object
360 361 362 |
# File 'lib/em/protocols/httpclient2.rb', line 360 def receive_binary_data text @requests.last.receive_text text end |
#receive_line(ln) ⇒ Object
350 351 352 353 354 355 356 357 |
# File 'lib/em/protocols/httpclient2.rb', line 350 def receive_line ln if req = @requests.last req.receive_line ln else p "??????????" p ln end end |
#request(args) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/em/protocols/httpclient2.rb', line 336 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.
295 296 297 298 299 300 301 |
# File 'lib/em/protocols/httpclient2.rb', line 295 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 |
#unbind ⇒ Object
– 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.
329 330 331 332 333 |
# File 'lib/em/protocols/httpclient2.rb', line 329 def unbind super @closed = true (@requests || []).each {|r| r.fail} end |