Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'socket'
require 'rack'
require 'rack/lobster'
app = Rack::Lobster.new
server = TCPServer.new 5678
while session = server.accept
request = session.gets
puts request
method, full_path = request.split(' ')
path, query = full_path.split('?')
status, headers, body = app.call({
'REQUEST_METHOD' => method,
'PATH_INFO' => path,
'QUERY_STRING' => query
})
session.print "HTTP/1.1 #{status}\r\n"
headers.each do |key, value|
session.print "#{key}: #{value}\r\n"
end
session.print "\r\n"
body.each do |part|
session.print part
end
session.close
end
require 'socket'
server = TCPSocket.new 'localhost', 5678
while line = server.gets
puts line
end
server.close
require 'socket'
server = TCPServer.new 5678
while session = server.accept
session.print "Hello world! The time is #{Time.now}"
session.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment