Hi I was going to use event machine to implement a simple web server, but I''ve just found the eventmachine_httpserver gem :) Is there any documentation or examples available for it? What are the callback methods I can/must override to work with it? Thanks in advance, Andre
On 10/26/07, Andre Nathan <andre at digirati.com.br> wrote:> I was going to use event machine to implement a simple web server, but > I''ve just found the eventmachine_httpserver gem :) > > Is there any documentation or examples available for it? What are the > callback methods I can/must override to work with it?At its simplest: module EventMachine module HttpServer def process_http_request send_data "HTTP/1.1 200 ...\r\nContent-type: text/plain\r\nContent-length: 9\r\n\r\nIt Works!" close_connection_after_writing end end end # add epoll setup here as desired. EM.run { EM.start_server "127.0.0.1", 8080, EventMachine::HttpServer } Define a receive_post_data() method if you want to handle POST data a chunk at a time (a good idea if you ever expect to receive POST/PUTs with very large bodies of data). It comes with an evma_httpserver/response.rb that simplifies response generation, which you can use. It''s documented pretty well, so I recommend just looking at it. require ''evma_httpserver'' That will require both the EventMachine::HttpServer and the EventMachine::HttpResponse libs at the same time. Kirk Haines
On Fri, 2007-10-26 at 07:31 -0700, Kirk Haines wrote:> At its simplest:[...] Thanks Kirk! I''ll give it a try. Andre
On Fri, 2007-10-26 at 07:31 -0700, Kirk Haines wrote:> It comes with an evma_httpserver/response.rb that simplifies response > generation, which you can use. It''s documented pretty well, so I > recommend just looking at it.I guess I''m not using it correctly... def check_authorization(auth) return if auth == ''secret'' resp = EventMachine::HttpResponse.new resp.status = 401 resp.send_response end When send_response is called, it gives me response.rb:104:in `send_headers'': undefined method `send_data'' for #<EventMachine::HttpResponse:0xb797f4d4> (NoMethodError) Any idea how to fix that? Andre
On 10/26/07, Andre Nathan <andre at digirati.com.br> wrote:> > On Fri, 2007-10-26 at 07:31 -0700, Kirk Haines wrote: > > It comes with an evma_httpserver/response.rb that simplifies response > > generation, which you can use. It''s documented pretty well, so I > > recommend just looking at it. > > I guess I''m not using it correctly... > > def check_authorization(auth) > return if auth == ''secret'' > resp = EventMachine::HttpResponse.new > resp.status = 401 > resp.send_response > end > > When send_response is called, it gives me > > response.rb:104:in `send_headers'': undefined method `send_data'' for > #<EventMachine::HttpResponse:0xb797f4d4> (NoMethodError) > > Any idea how to fix that?EM::HttpResponse is just a data wrapper. It requires a connection delegate in order to actually send data. Here''s one way: module MyServer include EM::HttpServer def process_http_request resp = EM::DelegatedHttpResponse.new( self ) resp.status = 401 resp.content = "Unauthorized" resp.send_response close_connection_after writing end end EM.run { EM.start_server "0.0.0.0", 8080, MyServer } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20071026/05de5837/attachment.html
On Fri, 2007-10-26 at 14:17 -0400, Francis Cianfrocca wrote:> EM::HttpResponse is just a data wrapper. It requires a connection > delegate in order to actually send data. Here''s one way: > > module MyServer > include EM::HttpServer > def process_http_request > resp = EM::DelegatedHttpResponse.new( self ) > resp.status = 401 > resp.content = "Unauthorized" > resp.send_response > close_connection_after writing > end > endSorry, I guess I''m being a bit dense but it''s the first time I use EM... How would I define the DelegatedHttpResponse class? Do you mean "connection delegate" as in using Ruby''s delegation mechanisms? Andre
On 10/26/07, Andre Nathan <andre at digirati.com.br> wrote:> > On Fri, 2007-10-26 at 14:17 -0400, Francis Cianfrocca wrote: > > EM::HttpResponse is just a data wrapper. It requires a connection > > delegate in order to actually send data. Here''s one way: > > > > module MyServer > > include EM::HttpServer > > def process_http_request > > resp = EM::DelegatedHttpResponse.new( self ) > > resp.status = 401 > > resp.content = "Unauthorized" > > resp.send_response > > close_connection_after writing > > end > > end > > Sorry, I guess I''m being a bit dense but it''s the first time I use EM... > How would I define the DelegatedHttpResponse class? Do you mean > "connection delegate" as in using Ruby''s delegation mechanisms?DelegatedHttpResponse is a built-in subclass of HttpResponse. HttpResponse was designed to be generic, which is the the communications stuff was factored out. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20071026/ba66075f/attachment.html
On Fri, 2007-10-26 at 17:48 -0400, Francis Cianfrocca wrote:> DelegatedHttpResponse is a built-in subclass of HttpResponse. > HttpResponse was designed to be generic, which is the the > communications stuff was factored out.Then I must be missing something obvious, cause running the example you posted gives me a.rb:8:in `process_http_request'': uninitialized constant EventMachine::DelegatedHttpResponse (NameError) (I''m using eventmachine 0.9.0 and eventmachine_httpserver 0.0.1 installed with rubygems). Andre
On 10/26/07, Andre Nathan <andre at digirati.com.br> wrote:> > On Fri, 2007-10-26 at 17:48 -0400, Francis Cianfrocca wrote: > > DelegatedHttpResponse is a built-in subclass of HttpResponse. > > HttpResponse was designed to be generic, which is the the > > communications stuff was factored out. > > Then I must be missing something obvious, cause running the > example you posted gives me > > a.rb:8:in `process_http_request'': uninitialized constant > EventMachine::DelegatedHttpResponse (NameError) > > (I''m using eventmachine 0.9.0 and eventmachine_httpserver 0.0.1 > installed with rubygems). > > AndreTry the attached gem. Maybe the release is out of date. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20071026/8012fd0b/attachment-0001.html -------------- next part -------------- A non-text attachment was scrubbed... Name: em_httpserver-0.1.0.gem Type: application/octet-stream Size: 35840 bytes Desc: not available Url : http://rubyforge.org/pipermail/eventmachine-talk/attachments/20071026/8012fd0b/attachment-0001.obj