Created
October 12, 2018 18:54
-
-
Save kopylovvlad/2155e40993e6288da9da746877003009 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 '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