2003-12-19 21:01:26 +03:00
|
|
|
#
|
|
|
|
# cgi.rb -- Yet another CGI library
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2003 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
|
|
|
|
require "webrick/httprequest"
|
|
|
|
require "webrick/httpresponse"
|
|
|
|
require "webrick/config"
|
|
|
|
require "stringio"
|
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
class CGI
|
2003-12-23 00:13:06 +03:00
|
|
|
CGIError = Class.new(StandardError)
|
|
|
|
|
2005-04-30 02:10:56 +04:00
|
|
|
attr_reader :config, :logger
|
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def initialize(*args)
|
2003-12-23 00:13:06 +03:00
|
|
|
if defined?(MOD_RUBY)
|
|
|
|
unless ENV.has_key?("GATEWAY_INTERFACE")
|
|
|
|
Apache.request.setup_cgi_env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if %r{HTTP/(\d+\.\d+)} =~ ENV["SERVER_PROTOCOL"]
|
|
|
|
httpv = $1
|
|
|
|
end
|
|
|
|
@config = WEBrick::Config::HTTP.dup.update(
|
2005-04-30 02:10:56 +04:00
|
|
|
:ServerSoftware => ENV["SERVER_SOFTWARE"] || "null",
|
2003-12-23 00:13:06 +03:00
|
|
|
:HTTPVersion => HTTPVersion.new(httpv || "1.0"),
|
|
|
|
:RunOnCGI => true, # to detect if it runs on CGI.
|
|
|
|
:NPH => false # set true to run as NPH script.
|
|
|
|
)
|
|
|
|
if config = args.shift
|
|
|
|
@config.update(config)
|
|
|
|
end
|
2004-01-07 22:37:36 +03:00
|
|
|
@config[:Logger] ||= WEBrick::BasicLog.new($stderr)
|
2003-12-23 00:13:06 +03:00
|
|
|
@logger = @config[:Logger]
|
2003-12-19 21:01:26 +03:00
|
|
|
@options = args
|
|
|
|
end
|
|
|
|
|
2005-04-30 02:10:56 +04:00
|
|
|
def [](key)
|
|
|
|
@config[key]
|
|
|
|
end
|
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def start(env=ENV, stdin=$stdin, stdout=$stdout)
|
|
|
|
sock = WEBrick::CGI::Socket.new(@config, env, stdin, stdout)
|
|
|
|
req = HTTPRequest.new(@config)
|
|
|
|
res = HTTPResponse.new(@config)
|
2003-12-23 00:13:06 +03:00
|
|
|
unless @config[:NPH] or defined?(MOD_RUBY)
|
2003-12-20 16:01:33 +03:00
|
|
|
def res.setup_header
|
2005-02-13 11:00:35 +03:00
|
|
|
unless @header["status"]
|
|
|
|
phrase = HTTPStatus::reason_phrase(@status)
|
|
|
|
@header["status"] = "#{@status} #{phrase}"
|
|
|
|
end
|
2003-12-20 16:01:33 +03:00
|
|
|
super
|
|
|
|
end
|
2003-12-19 21:01:26 +03:00
|
|
|
def res.status_line
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
req.parse(sock)
|
2005-04-30 02:10:56 +04:00
|
|
|
req.script_name = (env["SCRIPT_NAME"] || File.expand_path($0)).dup
|
|
|
|
req.path_info = (env["PATH_INFO"] || "").dup
|
2005-09-28 10:16:59 +04:00
|
|
|
req.query_string = env["QUERY_STRING"]
|
2004-09-16 13:14:27 +04:00
|
|
|
req.user = env["REMOTE_USER"]
|
2003-12-19 21:01:26 +03:00
|
|
|
res.request_method = req.request_method
|
|
|
|
res.request_uri = req.request_uri
|
|
|
|
res.request_http_version = req.http_version
|
|
|
|
res.keep_alive = req.keep_alive?
|
|
|
|
self.service(req, res)
|
|
|
|
rescue HTTPStatus::Error => ex
|
|
|
|
res.set_error(ex)
|
|
|
|
rescue HTTPStatus::Status => ex
|
|
|
|
res.status = ex.code
|
2009-03-06 06:56:38 +03:00
|
|
|
rescue Exception => ex
|
2003-12-19 21:01:26 +03:00
|
|
|
@logger.error(ex)
|
|
|
|
res.set_error(ex, true)
|
|
|
|
ensure
|
|
|
|
req.fixup
|
2003-12-23 00:13:06 +03:00
|
|
|
if defined?(MOD_RUBY)
|
|
|
|
res.setup_header
|
|
|
|
Apache.request.status_line = "#{res.status} #{res.reason_phrase}"
|
|
|
|
Apache.request.status = res.status
|
|
|
|
table = Apache.request.headers_out
|
|
|
|
res.header.each{|key, val|
|
|
|
|
case key
|
|
|
|
when /^content-encoding$/i
|
|
|
|
Apache::request.content_encoding = val
|
|
|
|
when /^content-type$/i
|
|
|
|
Apache::request.content_type = val
|
|
|
|
else
|
|
|
|
table[key] = val.to_s
|
|
|
|
end
|
|
|
|
}
|
|
|
|
res.cookies.each{|cookie|
|
|
|
|
table.add("Set-Cookie", cookie.to_s)
|
|
|
|
}
|
|
|
|
Apache.request.send_http_header
|
|
|
|
res.send_body(sock)
|
|
|
|
else
|
|
|
|
res.send_response(sock)
|
|
|
|
end
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def service(req, res)
|
|
|
|
method_name = "do_" + req.request_method.gsub(/-/, "_")
|
|
|
|
if respond_to?(method_name)
|
|
|
|
__send__(method_name, req, res)
|
|
|
|
else
|
|
|
|
raise HTTPStatus::MethodNotAllowed,
|
|
|
|
"unsupported method `#{req.request_method}'."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Socket
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
private
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def initialize(config, env, stdin, stdout)
|
2003-12-23 00:13:06 +03:00
|
|
|
@config = config
|
2003-12-19 21:01:26 +03:00
|
|
|
@env = env
|
|
|
|
@header_part = StringIO.new
|
|
|
|
@body_part = stdin
|
|
|
|
@out_port = stdout
|
2005-09-15 19:07:05 +04:00
|
|
|
@out_port.binmode
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
@server_addr = @env["SERVER_ADDR"] || "0.0.0.0"
|
|
|
|
@server_name = @env["SERVER_NAME"]
|
|
|
|
@server_port = @env["SERVER_PORT"]
|
|
|
|
@remote_addr = @env["REMOTE_ADDR"]
|
|
|
|
@remote_host = @env["REMOTE_HOST"] || @remote_addr
|
|
|
|
@remote_port = @env["REMOTE_PORT"] || 0
|
|
|
|
|
|
|
|
begin
|
2003-12-23 00:13:06 +03:00
|
|
|
@header_part << request_line << CRLF
|
2003-12-19 21:01:26 +03:00
|
|
|
setup_header
|
2003-12-23 00:13:06 +03:00
|
|
|
@header_part << CRLF
|
|
|
|
@header_part.rewind
|
2003-12-19 21:01:26 +03:00
|
|
|
rescue Exception => ex
|
2003-12-23 00:13:06 +03:00
|
|
|
raise CGIError, "invalid CGI environment"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_line
|
2005-04-30 02:10:56 +04:00
|
|
|
meth = @env["REQUEST_METHOD"] || "GET"
|
2005-05-27 21:16:06 +04:00
|
|
|
unless url = @env["REQUEST_URI"]
|
2005-07-10 21:54:58 +04:00
|
|
|
url = (@env["SCRIPT_NAME"] || File.expand_path($0)).dup
|
|
|
|
url << @env["PATH_INFO"].to_s
|
2005-05-27 21:16:06 +04:00
|
|
|
url = WEBrick::HTTPUtils.escape_path(url)
|
|
|
|
if query_string = @env["QUERY_STRING"]
|
|
|
|
unless query_string.empty?
|
|
|
|
url << "?" << query_string
|
|
|
|
end
|
2003-12-23 00:13:06 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# we cannot get real HTTP version of client ;)
|
|
|
|
httpv = @config[:HTTPVersion]
|
2005-05-27 21:16:06 +04:00
|
|
|
return "#{meth} #{url} HTTP/#{httpv}"
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def setup_header
|
2004-12-09 10:22:45 +03:00
|
|
|
@env.each{|key, value|
|
|
|
|
case key
|
|
|
|
when "CONTENT_TYPE", "CONTENT_LENGTH"
|
|
|
|
add_header(key.gsub(/_/, "-"), value)
|
|
|
|
when /^HTTP_(.*)/
|
|
|
|
add_header($1.gsub(/_/, "-"), value)
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
2003-12-23 00:13:06 +03:00
|
|
|
}
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2004-12-09 10:22:45 +03:00
|
|
|
def add_header(hdrname, value)
|
|
|
|
unless value.empty?
|
|
|
|
@header_part << hdrname << ": " << value << CRLF
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def input
|
|
|
|
@header_part.eof? ? @body_part : @header_part
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
public
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def peeraddr
|
|
|
|
[nil, @remote_port, @remote_host, @remote_addr]
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def addr
|
|
|
|
[nil, @server_port, @server_name, @server_addr]
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2007-12-17 10:03:57 +03:00
|
|
|
def gets(eol=LF, size=nil)
|
|
|
|
input.gets(eol, size)
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def read(size=nil)
|
|
|
|
input.read(size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
input.each{|line| yield(line) }
|
|
|
|
end
|
2008-08-17 21:33:13 +04:00
|
|
|
|
|
|
|
def eof?
|
|
|
|
input.eof?
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-12-19 21:01:26 +03:00
|
|
|
def <<(data)
|
|
|
|
@out_port << data
|
|
|
|
end
|
|
|
|
|
|
|
|
def cert
|
2003-12-23 00:13:06 +03:00
|
|
|
return nil unless defined?(OpenSSL)
|
2003-12-19 21:01:26 +03:00
|
|
|
if pem = @env["SSL_SERVER_CERT"]
|
2003-12-20 16:01:33 +03:00
|
|
|
OpenSSL::X509::Certificate.new(pem) unless pem.empty?
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def peer_cert
|
2003-12-23 00:13:06 +03:00
|
|
|
return nil unless defined?(OpenSSL)
|
2003-12-19 21:01:26 +03:00
|
|
|
if pem = @env["SSL_CLIENT_CERT"]
|
2003-12-20 16:01:33 +03:00
|
|
|
OpenSSL::X509::Certificate.new(pem) unless pem.empty?
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def peer_cert_chain
|
2003-12-23 00:13:06 +03:00
|
|
|
return nil unless defined?(OpenSSL)
|
2003-12-19 21:01:26 +03:00
|
|
|
if @env["SSL_CLIENT_CERT_CHAIN_0"]
|
|
|
|
keys = @env.keys
|
|
|
|
certs = keys.sort.collect{|k|
|
|
|
|
if /^SSL_CLIENT_CERT_CHAIN_\d+$/ =~ k
|
|
|
|
if pem = @env[k]
|
2003-12-20 16:01:33 +03:00
|
|
|
OpenSSL::X509::Certificate.new(pem) unless pem.empty?
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
certs.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cipher
|
2003-12-23 00:13:06 +03:00
|
|
|
return nil unless defined?(OpenSSL)
|
2003-12-19 21:01:26 +03:00
|
|
|
if cipher = @env["SSL_CIPHER"]
|
2003-12-23 00:13:06 +03:00
|
|
|
ret = [ cipher ]
|
|
|
|
ret << @env["SSL_PROTOCOL"]
|
|
|
|
ret << @env["SSL_CIPHER_USEKEYSIZE"]
|
|
|
|
ret << @env["SSL_CIPHER_ALGKEYSIZE"]
|
|
|
|
ret
|
2003-12-19 21:01:26 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
end
|
|
|
|
end
|