[Eventmachine-talk] EventMachine and leveraging net/http

Aaron Patterson aaron at tenderlovemaking.com
Tue Mar 4 15:06:20 EST 2008


Hi all,

I've finally found time to try "eventing" WWW::Mechanize.  As far as I
can tell, Event Machine doesn't let me set many custom headers.  I was
wondering if anyone had considered allowing request objects like
Net::HTTP::Get or Net::HTTP::Post to be passed in?  Then you could set
arbitrary headers, and have code like this:

  EventMachine.run {
    request = Net::HTTP::Get.new('/')
    request['accept-encoding']  = 'gzip,identity'
    request['accept-language']  = 'en-us,en,q=0.5'
    request['user-agent']       = 'test agent'
  
    c = EventMachine::Protocols::HttpClient.send  :request,
                                                  :host         => 'google.com',
                                                  :port         => 80,
                                                  :request_obj  => request
    c.callback { |result|
      p result
      EventMachine.stop
    }
    c.errback { EventMachine.stop }
  }

I was able to patch Event Machine pretty easily to accept a Net::HTTP
request object.  I even made the interface backwards compatible so all
tests pass.  I've posted the diff to make my example work here:

  http://pastie.caboo.se/161410

Also, it would be neat of the result was a Net::HTTPResponse type
object.  I haven't really looked in to that yet though.

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Eventmachine-talk mailing list