Module: FileTransfer::HTTPserver

Defined in:
lib/filetransfer/httpserver.rb

Class Method Summary collapse

Class Method Details

.handle_client(initdata, client) ⇒ Object



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
# File 'lib/filetransfer/httpserver.rb', line 3

def self.handle_client(initdata, client)
  path = initdata[1].split("/")
  decline = (path[2] == "decline")
  path = ?/ + path[1]
  sock_domain, remote_port, remote_hostname, remote_ip = client.peeraddr

  if decline
    `zenity --error --title="Transfer Declined" --text="Your transfer to #{remote_hostname} has been declined." --no-wrap`
    $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

  `zenity --info --title="Transfer Accepted" --text="Your transfer to #{remote_hostname} has been accepted." --no-wrap`
  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