Vendor Rack 1.0.1

Incorporate patch from Revision 496.
This commit is contained in:
Jacques Distler 2009-12-18 20:16:58 -06:00
parent a7202d54cd
commit 76f388f3e2
113 changed files with 5759 additions and 199 deletions

View file

@ -0,0 +1,61 @@
require 'rack/content_length'
module Rack
module Handler
class CGI
def self.run(app, options=nil)
serve app
end
def self.serve(app)
app = ContentLength.new(app)
env = ENV.to_hash
env.delete "HTTP_CONTENT_LENGTH"
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
env.update({"rack.version" => [1,0],
"rack.input" => $stdin,
"rack.errors" => $stderr,
"rack.multithread" => false,
"rack.multiprocess" => true,
"rack.run_once" => true,
"rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
})
env["QUERY_STRING"] ||= ""
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
env["REQUEST_PATH"] ||= "/"
status, headers, body = app.call(env)
begin
send_headers status, headers
send_body body
ensure
body.close if body.respond_to? :close
end
end
def self.send_headers(status, headers)
STDOUT.print "Status: #{status}\r\n"
headers.each { |k, vs|
vs.split("\n").each { |v|
STDOUT.print "#{k}: #{v}\r\n"
}
}
STDOUT.print "\r\n"
STDOUT.flush
end
def self.send_body(body)
body.each { |part|
STDOUT.print part
STDOUT.flush
}
end
end
end
end

View file

@ -0,0 +1,8 @@
require 'swiftcore/evented_mongrel'
module Rack
module Handler
class EventedMongrel < Handler::Mongrel
end
end
end

View file

@ -0,0 +1,90 @@
require 'fcgi'
require 'socket'
require 'rack/content_length'
require 'rack/rewindable_input'
if defined? FCGI::Stream
class FCGI::Stream
alias _rack_read_without_buffer read
def read(n, buffer=nil)
buf = _rack_read_without_buffer n
buffer.replace(buf.to_s) if buffer
buf
end
end
end
module Rack
module Handler
class FastCGI
def self.run(app, options={})
file = options[:File] and STDIN.reopen(UNIXServer.new(file))
port = options[:Port] and STDIN.reopen(TCPServer.new(port))
FCGI.each { |request|
serve request, app
}
end
def self.serve(request, app)
app = Rack::ContentLength.new(app)
env = request.env
env.delete "HTTP_CONTENT_LENGTH"
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
rack_input = RewindableInput.new(request.in)
env.update({"rack.version" => [1,0],
"rack.input" => rack_input,
"rack.errors" => request.err,
"rack.multithread" => false,
"rack.multiprocess" => true,
"rack.run_once" => false,
"rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
})
env["QUERY_STRING"] ||= ""
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
env["REQUEST_PATH"] ||= "/"
env.delete "PATH_INFO" if env["PATH_INFO"] == ""
env.delete "CONTENT_TYPE" if env["CONTENT_TYPE"] == ""
env.delete "CONTENT_LENGTH" if env["CONTENT_LENGTH"] == ""
begin
status, headers, body = app.call(env)
begin
send_headers request.out, status, headers
send_body request.out, body
ensure
body.close if body.respond_to? :close
end
ensure
rack_input.close
request.finish
end
end
def self.send_headers(out, status, headers)
out.print "Status: #{status}\r\n"
headers.each { |k, vs|
vs.split("\n").each { |v|
out.print "#{k}: #{v}\r\n"
}
}
out.print "\r\n"
out.flush
end
def self.send_body(out, body)
body.each { |part|
out.print part
out.flush
}
end
end
end
end

View file

@ -0,0 +1,60 @@
require 'lsapi'
require 'rack/content_length'
module Rack
module Handler
class LSWS
def self.run(app, options=nil)
while LSAPI.accept != nil
serve app
end
end
def self.serve(app)
app = Rack::ContentLength.new(app)
env = ENV.to_hash
env.delete "HTTP_CONTENT_LENGTH"
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
rack_input = RewindableInput.new($stdin.read.to_s)
env.update(
"rack.version" => [1,0],
"rack.input" => rack_input,
"rack.errors" => $stderr,
"rack.multithread" => false,
"rack.multiprocess" => true,
"rack.run_once" => false,
"rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
)
env["QUERY_STRING"] ||= ""
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
env["REQUEST_PATH"] ||= "/"
status, headers, body = app.call(env)
begin
send_headers status, headers
send_body body
ensure
body.close if body.respond_to? :close
end
end
def self.send_headers(status, headers)
print "Status: #{status}\r\n"
headers.each { |k, vs|
vs.split("\n").each { |v|
print "#{k}: #{v}\r\n"
}
}
print "\r\n"
STDOUT.flush
end
def self.send_body(body)
body.each { |part|
print part
STDOUT.flush
}
end
end
end
end

View file

@ -0,0 +1,87 @@
require 'mongrel'
require 'stringio'
require 'rack/content_length'
require 'rack/chunked'
module Rack
module Handler
class Mongrel < ::Mongrel::HttpHandler
def self.run(app, options={})
server = ::Mongrel::HttpServer.new(options[:Host] || '0.0.0.0',
options[:Port] || 8080)
# Acts like Rack::URLMap, utilizing Mongrel's own path finding methods.
# Use is similar to #run, replacing the app argument with a hash of
# { path=>app, ... } or an instance of Rack::URLMap.
if options[:map]
if app.is_a? Hash
app.each do |path, appl|
path = '/'+path unless path[0] == ?/
server.register(path, Rack::Handler::Mongrel.new(appl))
end
elsif app.is_a? URLMap
app.instance_variable_get(:@mapping).each do |(host, path, appl)|
next if !host.nil? && !options[:Host].nil? && options[:Host] != host
path = '/'+path unless path[0] == ?/
server.register(path, Rack::Handler::Mongrel.new(appl))
end
else
raise ArgumentError, "first argument should be a Hash or URLMap"
end
else
server.register('/', Rack::Handler::Mongrel.new(app))
end
yield server if block_given?
server.run.join
end
def initialize(app)
@app = Rack::Chunked.new(Rack::ContentLength.new(app))
end
def process(request, response)
env = {}.replace(request.params)
env.delete "HTTP_CONTENT_TYPE"
env.delete "HTTP_CONTENT_LENGTH"
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
rack_input = request.body || StringIO.new('')
rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
env.update({"rack.version" => [1,0],
"rack.input" => rack_input,
"rack.errors" => $stderr,
"rack.multithread" => true,
"rack.multiprocess" => false, # ???
"rack.run_once" => false,
"rack.url_scheme" => "http",
})
env["QUERY_STRING"] ||= ""
env.delete "PATH_INFO" if env["PATH_INFO"] == ""
status, headers, body = @app.call(env)
begin
response.status = status.to_i
response.send_status(nil)
headers.each { |k, vs|
vs.split("\n").each { |v|
response.header[k] = v
}
}
response.send_header
body.each { |part|
response.write part
response.socket.flush
}
ensure
body.close if body.respond_to? :close
end
end
end
end
end

View file

@ -0,0 +1,62 @@
require 'scgi'
require 'stringio'
require 'rack/content_length'
require 'rack/chunked'
module Rack
module Handler
class SCGI < ::SCGI::Processor
attr_accessor :app
def self.run(app, options=nil)
new(options.merge(:app=>app,
:host=>options[:Host],
:port=>options[:Port],
:socket=>options[:Socket])).listen
end
def initialize(settings = {})
@app = Rack::Chunked.new(Rack::ContentLength.new(settings[:app]))
@log = Object.new
def @log.info(*args); end
def @log.error(*args); end
super(settings)
end
def process_request(request, input_body, socket)
env = {}.replace(request)
env.delete "HTTP_CONTENT_TYPE"
env.delete "HTTP_CONTENT_LENGTH"
env["REQUEST_PATH"], env["QUERY_STRING"] = env["REQUEST_URI"].split('?', 2)
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
env["PATH_INFO"] = env["REQUEST_PATH"]
env["QUERY_STRING"] ||= ""
env["SCRIPT_NAME"] = ""
rack_input = StringIO.new(input_body)
rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
env.update({"rack.version" => [1,0],
"rack.input" => rack_input,
"rack.errors" => $stderr,
"rack.multithread" => true,
"rack.multiprocess" => true,
"rack.run_once" => false,
"rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
})
status, headers, body = app.call(env)
begin
socket.write("Status: #{status}\r\n")
headers.each do |k, vs|
vs.split("\n").each { |v| socket.write("#{k}: #{v}\r\n")}
end
socket.write("\r\n")
body.each {|s| socket.write(s)}
ensure
body.close if body.respond_to? :close
end
end
end
end
end

View file

@ -0,0 +1,8 @@
require 'swiftcore/swiftiplied_mongrel'
module Rack
module Handler
class SwiftipliedMongrel < Handler::Mongrel
end
end
end

View file

@ -0,0 +1,18 @@
require "thin"
require "rack/content_length"
require "rack/chunked"
module Rack
module Handler
class Thin
def self.run(app, options={})
app = Rack::Chunked.new(Rack::ContentLength.new(app))
server = ::Thin::Server.new(options[:Host] || '0.0.0.0',
options[:Port] || 8080,
app)
yield server if block_given?
server.start
end
end
end
end

View file

@ -0,0 +1,72 @@
require 'webrick'
require 'stringio'
require 'rack/content_length'
module Rack
module Handler
class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet
def self.run(app, options={})
options[:BindAddress] = options.delete(:Host) if options[:Host]
server = ::WEBrick::HTTPServer.new(options)
server.mount "/", Rack::Handler::WEBrick, app
trap(:INT) { server.shutdown }
trap(:TERM) { server.shutdown }
yield server if block_given?
server.start
end
def initialize(server, app)
super server
@app = Rack::ContentLength.new(app)
end
def service(req, res)
env = req.meta_vars
env.delete_if { |k, v| v.nil? }
rack_input = StringIO.new(req.body.to_s)
rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
env.update({"rack.version" => [1,0],
"rack.input" => rack_input,
"rack.errors" => $stderr,
"rack.multithread" => true,
"rack.multiprocess" => false,
"rack.run_once" => false,
"rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
})
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
env["QUERY_STRING"] ||= ""
env["REQUEST_PATH"] ||= "/"
if env["PATH_INFO"] == ""
env.delete "PATH_INFO"
else
path, n = req.request_uri.path, env["SCRIPT_NAME"].length
env["PATH_INFO"] = path[n, path.length-n]
end
status, headers, body = @app.call(env)
begin
res.status = status.to_i
headers.each { |k, vs|
if k.downcase == "set-cookie"
res.cookies.concat vs.split("\n")
else
vs.split("\n").each { |v|
res[k] = v
}
end
}
body.each { |part|
res.body << part
}
ensure
body.close if body.respond_to? :close
end
end
end
end
end