Skip to content

Instantly share code, notes, and snippets.

@jtimberman
Forked from adelcambre/server.rb
Created September 27, 2013 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtimberman/6723733 to your computer and use it in GitHub Desktop.
Save jtimberman/6723733 to your computer and use it in GitHub Desktop.
require 'socket'
NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n"
socket = Socket.new(:INET, :STREAM)
sockaddr = Socket.sockaddr_in(11080, '127.0.0.1')
socket.bind(sockaddr)
socket.listen(5)
while client_socket = socket.accept[0]
path = client_socket.readline.split[1]
filename = File.expand_path("../#{path}", __FILE__)
if File.file?(filename)
contents = File.read(filename)
client_socket.write(OK)
client_socket.write("Content-Length: #{contents.size}\n\n")
client_socket.write("#{contents}")
else
client_socket.write(NOT_FOUND)
end
client_socket.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment