1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/mongrel/camping.rb
zedshaw ace16f9688 Patch 5718: camping fixes from zimbatm
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@410 19e92222-5c0b-0410-8929-a290d50e31e9
2006-11-25 02:00:36 +00:00

96 lines
2.9 KiB
Ruby

# Copyright (c) 2005 Zed A. Shaw
# You can redistribute it and/or modify it under the same terms as Ruby.
#
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
# for more information.
require 'mongrel'
module Mongrel
# Support for the Camping micro framework at http://camping.rubyforge.org
# This implements the unusually long Postamble that Camping usually
# needs and shrinks it down to just a single line or two.
#
# Your Postamble would now be:
#
# Mongrel::Camping::start("0.0.0.0",3001,"/tepee",Tepee).join
#
# If you wish to get fancier than this then you can use the
# Camping::CampingHandler directly instead and do your own
# wiring:
#
# h = Mongrel::HttpServer.new(server, port)
# h.register(uri, CampingHandler.new(Tepee))
# h.register("/favicon.ico", Mongrel::Error404Handler.new(""))
#
# I add the /favicon.ico since camping apps typically don't
# have them and it's just annoying anyway.
module Camping
# This is a specialized handler for Camping applications
# that has them process the request and then translates
# the results into something the Mongrel::HttpResponse
# needs.
class CampingHandler < Mongrel::HttpHandler
attr_reader :files
attr_reader :guard
@@file_only_methods = ["GET","HEAD"]
def initialize(klass)
@files = Mongrel::DirHandler.new("/",false)
@guard = Sync.new
@klass = klass
end
def process(request, response)
if response.socket.closed?
return
end
controller = nil
@guard.synchronize(:EX) {
controller = @klass.run(request.body, request.params)
}
sendfile, clength = nil
response.status = controller.status
controller.headers.each do |k, v|
if k =~ /^X-SENDFILE$/i
sendfile = v
elsif k =~ /^CONTENT-LENGTH$/i
clength = v.to_i
else
[*v].each do |vi|
response.header[k] = vi
end
end
end
if sendfile
request.params[Mongrel::Const::PATH_INFO] = sendfile
@files.process(request, response)
elsif controller.body.respond_to? :read
response.send_status(clength)
response.send_header
while chunk = controller.body.read(16384)
response.write(chunk)
end
if controller.body.respond_to? :close
controller.body.close
end
else
body = controller.body.to_s
response.send_status(body.length)
response.send_header
response.write(body)
end
rescue Errno::EPIPE
# ignored
rescue Object => camping_error
STDERR.puts "Error calling #{@klass}.run #{camping_error.inspect}"
STDERR.puts camping_error.backtrace.join("\n")
end
end
end
end