Created
November 16, 2010 11:56
-
-
Save mvrilo/701732 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
class Example < EventMachine::Connection | |
def receive_data data | |
send_data "EventMachine minimal server\n\n-----\n#{data}-----\n#{request(data).inspect}" | |
puts request(data).inspect | |
close_connection_after_writing | |
end | |
private | |
# headers_2_hash from EM::P::HeaderAndContentProtocol | |
def request headers | |
hash = {} | |
headers.each { |h| | |
if /\A([^\s:]+)\s*:\s*/ =~ h | |
tail = $'.dup | |
hash[ $1.downcase.gsub(/-/,"_").intern ] = tail.gsub(/\r\n/, "") | |
else | |
ar = h.gsub(/\r\n/,"").split("\s") | |
hash[:method] ||= ar[0] | |
hash[:uri] ||= ar[1] | |
hash[:protocol] ||= ar[2] | |
end | |
} | |
hash | |
end | |
end | |
EventMachine::run { | |
puts "\nServer started at localhost:8888\n\n" | |
EventMachine.epoll | |
EventMachine::start_server "0.0.0.0", 8888, Example | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment