Last Updated: February 25, 2016
·
1.217K
· thinkclay

A mini web server in ruby

require 'socket'

server = TCPServer.new '127.0.0.1', 8888

while session = server.accept
  session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"

  if request = session.gets
    route = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp

    session.print "<h1>You requested a file: #{route}</h1><br />"

    for route in route.split('/')
      session.print route + "<br />"
    end
  end

  session.close
end