Skip to content

Instantly share code, notes, and snippets.

@mvrilo
Created November 16, 2010 11:56
Show Gist options
  • Save mvrilo/701732 to your computer and use it in GitHub Desktop.
Save mvrilo/701732 to your computer and use it in GitHub Desktop.
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