-
Notifications
You must be signed in to change notification settings - Fork 7
/
filecatcher.rb
executable file
·40 lines (35 loc) · 988 Bytes
/
filecatcher.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
#!/usr/bin/env ruby
require 'webrick'
require 'stringio'
include WEBrick
port = ENV['HTTP_PORT'] || 8081
dir = ENV['HTTP_DIR'] || File.join(Dir.pwd, "loot")
s = HTTPServer.new(:Port => port, :DocumentRoot => dir)
s.mount_proc '/upload' do |request, response|
request.query.collect do |k,v|
if k == "filename"
@fname = "#{Time.now.to_f}-#{v}".strip
@fname = File.join(dir,@fname)
end
if k == "filedata"
@fsize = File.open(@fname, "wb") {|f| f.write v}
end
end
msg = "Got #{@fname} (#{@fsize} bytes)"
response.body = "<html>#{msg}<br><a href='up'>do it again</a></html>"
end
s.mount_proc '/up' do |request, response|
response.body =<<EOF
<html>
<form action="upload" method="POST" enctype="multipart/form-data">
Filename: <input name="filename" type="text"/>
<br />
File: <input type="file" name="filedata" />
<br />
<input type="submit" value="Upload" />
</form>
</html>
EOF
end
trap("INT") { s.shutdown }
s.start