Created
October 12, 2018 18:54
-
-
Save kopylovvlad/259cda121db785cd1f02f2280f6d6cc3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
socket = server.accept | |
request = socket.gets | |
method, path = request.split | |
sleep_time = rand(5) | |
# some 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment