4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/ws.rb', line 4
def self.start_server(options)
server = TCPServer.new("127.0.0.1", options[:port])
loop do
Thread.start(server.accept) do |conn|
file = conn.gets.scan(/(?<=\/).*(?=\sHTTP)/)[0].split("/")
file_path = File.join(options[:directory], file)
file_path = File.join(file_path, options[:index]) if File.directory? file_path
mime_type = get_mime_type file_path
begin
file_handle = File.open(file_path, "r")
puts "#{Time.now.inspect}: Sending #{file_path} to client." if options[:verbose]
conn.print ("200/OK", mime_type)
conn.print file_handle.read()
file_handle.close
rescue Errno::ENOENT
puts "#{Time.now.inspect}: Not Found: #{file_path}" if options[:verbose]
conn.print ("404/NOT FOUND", "text/html")
conn.print "404: File not found."
end
conn.close
end
end
end
|