Skip to content

Instantly share code, notes, and snippets.

@kopylovvlad
Created October 12, 2018 18:54
Show Gist options
  • Save kopylovvlad/2155e40993e6288da9da746877003009 to your computer and use it in GitHub Desktop.
Save kopylovvlad/2155e40993e6288da9da746877003009 to your computer and use it in GitHub Desktop.
require 'socket'
# run on localhost:3000
server = TCPServer.new('localhost', 3000)
loop do
Thread.start(server.accept) do |socket|
request = socket.gets
method, path = request.split
sleep_time = rand(5)
# fake calculation is here
sleep(sleep_time)
response = "Hello World!\n" +
"your request: #{request}" +
"method: #{method.inspect}\n" +
"path: #{path.inspect}\n" +
"sleep time: #{sleep_time}\n"
socket.print "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Connection: close\r\n" +
"Content-Length: #{response.bytesize}\r\n"
socket.print "\r\n"
socket.print response
socket.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment