Created
March 8, 2020 07:57
-
-
Save leastbad/00fee1a5f5ccb8fd426e80a7d84af9af to your computer and use it in GitHub Desktop.
It's a webserver
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' | |
server = TCPServer.new 5678 | |
while session = server.accept | |
request = session.gets | |
puts request | |
unless request.nil? | |
filename = request.split(" ")[1] | |
if filename == "/" | |
filename = "/index.html" | |
end | |
begin | |
data = File.open(__dir__ + filename).read | |
session.print "HTTP/1.1 200 OK\r\n" | |
session.print "Content-Type: text/html\r\n" | |
session.print "\r\n" | |
session.print data | |
rescue | |
session.print "HTTP/1.1 404 NOT FOUND\r\n" | |
session.print "Content-Type: text/html\r\n" | |
session.print "\r\n" | |
session.print "You #FAIL" | |
end | |
end | |
session.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment