Created
June 21, 2016 03:07
-
-
Save toretore/46d903100f25af15f421fecf64a140e9 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' | |
def log(s) | |
puts "#{Time.now.strftime('%H:%M:%S')}: #{s}" | |
end | |
html = "<a href=\"/foo\">Link</a>" | |
server = TCPServer.new('', 1337) | |
loop do | |
socket = server.accept | |
log "New connection, new socket" | |
s = socket.recv(1024) | |
log "#{s.bytesize}b: #{s.split("\r\n")[0]}" | |
if socket.eof? | |
log "Socket closed by browser" | |
else | |
log "Sending response" | |
socket.puts "HTTP/1.1 200 OK" | |
socket.puts "Content-Length: #{html.bytesize}" | |
socket.puts | |
socket.write html | |
socket.close | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment