Class: WEBrick::CGI
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | lib/webrick/cgi.rb |
Overview
Constant Summary
-
CGIError =
The CGI error exception class
Class.new(StandardError)
Class Method Summary
-
.new(*args) ⇒ CGI
constructor
Creates a new
CGI
interface.
Instance Attribute Summary
Instance Method Summary
-
#[](key)
Reads
key
from the configuration. -
#service(req, res)
Services the request
req
which will fill in the responseres
. -
#start(env = ENV, stdin = $stdin, stdout = $stdout)
Starts the
CGI
process with the given environmentenv
and standard input and outputstdin
andstdout
.
Constructor Details
.new(*args) ⇒ CGI
Creates a new CGI
interface.
The first argument in args
is a configuration hash which would update Config::HTTP.
Any remaining arguments are stored in the @options
instance variable for use by a subclass.
# File 'lib/webrick/cgi.rb', line 56
def initialize(*args) 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( :ServerSoftware => ENV["SERVER_SOFTWARE"] || "null", :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 @config[:Logger] ||= WEBrick::BasicLog.new($stderr) @logger = @config[:Logger] @options = args end
Instance Attribute Details
#config (readonly)
The CGI configuration. This is based on Config::HTTP
# File 'lib/webrick/cgi.rb', line 40
attr_reader :config
#logger (readonly)
The CGI logger
# File 'lib/webrick/cgi.rb', line 45
attr_reader :logger
Instance Method Details
#[](key)
Reads key
from the configuration
# File 'lib/webrick/cgi.rb', line 82
def [](key) @config[key] end
#service(req, res)
Services the request req
which will fill in the response res
. See HTTPServlet::AbstractServlet#service for details.
# File 'lib/webrick/cgi.rb', line 157
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
#start(env = ENV, stdin = $stdin, stdout = $stdout)
Starts the CGI
process with the given environment env
and standard input and output stdin
and stdout
.
# File 'lib/webrick/cgi.rb', line 90
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) unless @config[:NPH] or defined?(MOD_RUBY) def res.setup_header unless @header["status"] phrase = HTTPStatus::reason_phrase(@status) @header["status"] = "#{@status} #{phrase}" end super end def res.status_line "" end end begin req.parse(sock) req.script_name = (env["SCRIPT_NAME"] || File. ($0)).dup req.path_info = (env["PATH_INFO"] || "").dup req.query_string = env["QUERY_STRING"] req.user = env["REMOTE_USER"] 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 rescue Exception => ex @logger.error(ex) res.set_error(ex, true) ensure req.fixup 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. .each{|| table.add("Set-Cookie", .to_s) } Apache.request.send_http_header res.send_body(sock) else res.send_response(sock) end end end