Class: Mayfly::Servlet
- Inherits:
-
WEBrick::HTTPServlet::AbstractServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- Mayfly::Servlet
- Defined in:
- lib/mayfly/servlet.rb
Constant Summary collapse
- @@instance =
nil
- @@instance_creation_mutex =
Mutex.new
Class Method Summary collapse
Instance Method Summary collapse
- #do_GET(req, resp) ⇒ Object
-
#initialize(config, file, max, passwd = nil) ⇒ Servlet
constructor
A new instance of Servlet.
Constructor Details
#initialize(config, file, max, passwd = nil) ⇒ Servlet
Returns a new instance of Servlet.
20 21 22 23 24 25 26 27 |
# File 'lib/mayfly/servlet.rb', line 20 def initialize(config, file, max, passwd = nil) super @file = file @max = max @count = 1 @count_mutex = Mutex.new @passwd = passwd end |
Class Method Details
.get_instance(config, *options) ⇒ Object
14 15 16 17 18 |
# File 'lib/mayfly/servlet.rb', line 14 def self.get_instance(config, *) @@instance_creation_mutex.synchronize { @@instance = @@instance || self.new(config, *) } end |
Instance Method Details
#do_GET(req, resp) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mayfly/servlet.rb', line 29 def do_GET(req, resp) if (@count > @max) resp.status = 401 end if (@passwd) WEBrick::HTTPAuth.basic_auth(req, resp, 'mayfly') do |user, pass| user == 'mayfly' && pass == @passwd end end st = File::stat(@file) resp['content-type'] = WEBrick::HTTPUtils::mime_type(@file, WEBrick::HTTPUtils::DefaultMimeTypes) resp['content-length'] = st.size resp['content-disposition'] = "attachment; filename=\"#{File.basename(@file)}\"" resp['last-modified'] = st.mtime.httpdate resp['Cache-Control'] = 'no-cache, must-revalidate' close_callback = nil @count_mutex.synchronize { @count += 1 close_callback = Proc.new do growl("mayfly served #{@file} to #{req.peeraddr[2]}") if (@count > @max) @server.shutdown if (@count >= @max) growl("mayfly has died, it served #{@file} #{@max} times") end end } if (@count <= @max + 1) resp.body = File.new(@file, close_callback) end end |