Skip to content

Instantly share code, notes, and snippets.

@amadden80
Created January 13, 2015 18:03
Show Gist options
  • Save amadden80/18084e9f64ce30dfd4ed to your computer and use it in GitHub Desktop.
Save amadden80/18084e9f64ce30dfd4ed to your computer and use it in GitHub Desktop.
Simple Ruby Server
require 'socket'
server = TCPServer.new 2000
loop do
client = server.accept
if request = client.gets
puts "\n------ request ------"
puts request
puts "-----------------------\n"
method, path, http_version = request.split(' ')
response = "HTTP/1.1 "
case path
when '/'
response += "200 OK\n\n"
response += "Welcome!"
when '/cooks'
response += "200 OK\n\n"
response += "WAY too many..."
when '/andrew'
response += "302 Found\n"
response += "Location: http://andrewmadden.com"
else
response += "404 Nooooot found...\n"
end
end
puts "\n***** response *****"
puts response
puts "**********************\n"
client.puts response
client.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment