-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpServer.rb
43 lines (39 loc) · 941 Bytes
/
HttpServer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class HttpServer
def initialize(session, request, path)
@session = session
@request = request
@path = path
end
def getFile()
if @request =~ /GET .* HTTP.*/
filename = @request.gsub(/GET /, '').gsub(/ HTTP.*/, '')
end
filename = @path + filename
if !File.file?(filename)
filename = @path + 'index.html'
end
return filename
end
def respond()
file = getFile()
log "Processing #{file}"
begin
if File.exist?(file)
@session.print "HTTP/1.1 200/OK\r\nServer: Benserv\r\nContent-type: text/html\r\n\r\n"
src = File.open(file)
while (not src.eof?)
buffer = src.read(256)
@session.write(buffer)
end
src.close
else
@session.print "HTTP/1.1 404/Object Not Found\r\nServer: Benserv\r\n\r\n"
end
ensure
@session.close
end
end
end
def log(message)
puts message
end