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
|
# File 'lib/filetransfer/httpserver.rb', line 3
def self.handle_client(initdata, client)
path = initdata[1].split("/")
decline = (path[2] == "decline")
path = ?/ + path[1]
if decline
$active_transfers.delete(path)
client.puts "HTTP/1.0 200\r\nContent-Type: text/plain\r\n\r\nTransfer declined"
client.close
return
end
file = $active_transfers[path]
read = true
while read
x= client.gets
read = false if x == "\r\n" || !x
end
if !file
client.puts "HTTP/1.0 404\r\nContent-Type: text/plain\r\n\r\nFile not found."
client.close
return
end
client.puts "HTTP/1.0 200\r\nContent-Type: application/octet-stream\r\nContent-Disposition: attachment; filename=\"#{file[:name]}\"\r\n\r\n#{File.read(file[:path])}"
$active_transfers.delete(path)
client.close
end
|