You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
997 B
Ruby
62 lines
997 B
Ruby
#the rubnet server
|
|
|
|
require 'socket'
|
|
|
|
HTTP_NIL = "NIL"
|
|
|
|
RUBNET = "#{ANSI_RED}RUBNET#{ANSI_CLEAR}"
|
|
RUBNET_COMPILE = 'COMPILE'
|
|
|
|
def rubnet_message(msg)
|
|
puts "#{RUBNET}: #{msg}"
|
|
end
|
|
|
|
class Request
|
|
attr_reader :method
|
|
attr_reader :data
|
|
attr_reader :uri
|
|
attr_reader :version
|
|
attr_reader :headers
|
|
|
|
def parse_request(str)
|
|
if @data[0].nil?
|
|
@method = HTTP_NIL
|
|
return
|
|
end
|
|
@method = @data[0].split(" ")[0]
|
|
@uri = @data[0].split(" ")[1]
|
|
@version = @data[0].split(" ")[2]
|
|
|
|
@headers = @data.map.drop(1) { |header|
|
|
{header.split(": ")[0] => header.split(": ")[1]}
|
|
}
|
|
end
|
|
|
|
def initialize(cli)
|
|
@data = [cli.gets]
|
|
parse_request(@data)
|
|
end
|
|
end
|
|
|
|
|
|
def rubnet_main
|
|
TCPServer.open("127.0.0.1", RUBNET_PORT) { |srv|
|
|
mutex = Mutex.new
|
|
loop do
|
|
cli = srv.accept
|
|
mutex.synchronize {
|
|
h = Request.new(cli)
|
|
|
|
case h.method
|
|
when RUBNET_COMPILE
|
|
rubnet_compile(h)
|
|
else
|
|
rubnet_message("unknown method: #{h.method}")
|
|
end
|
|
}
|
|
end
|
|
}
|
|
end
|
|
|
|
rubnet_main
|