-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplorkserv
executable file
·44 lines (37 loc) · 1.01 KB
/
plorkserv
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
44
#!/usr/bin/env ruby
ENV['GEM_PATH'] = File.join(File.dirname(__FILE__), "gems")
require "rubygems"
require "plork-announce"
require "thin"
unless ARGV.length >= 2
$stderr.puts "usage: plorkserv name dir [port (default = 3000)]"
exit
end
name = ARGV[0]
dir = ARGV[1]
port = (ARGV[2] || 3000).to_i
PlorkAnnounce.announce(name, port, name, :tcp)
# responds to all requests with a tar of the given directory
# the tar is recreated for each request
app = proc do |env|
data = case env["REQUEST_PATH"]
when "/"
IO.popen("tar c -C #{dir} .", "r") { |f| f.read }
when "/gz"
IO.popen("tar zc -C #{dir} .", "r") { |f| f.read }
else nil
end
if data
[
200, # Status code
{ # Response headers
'Content-Type' => 'application/x-tar',
'Content-Length' => data.length.to_s,
},
[data] # Response body
]
else
[404, {'Content-Length' => "0"}, ""]
end
end
Thin::Server.new("0.0.0.0", port, app).start