| Class | WEBrick::CGI |
| In: |
lib/webrick/cgi.rb
|
| Parent: | Object |
| CGIError | = | Class.new(StandardError) |
| config | [R] | |
| logger | [R] |
# File lib/webrick/cgi.rb, line 21
21: def initialize(*args)
22: if defined?(MOD_RUBY)
23: unless ENV.has_key?("GATEWAY_INTERFACE")
24: Apache.request.setup_cgi_env
25: end
26: end
27: if %r{HTTP/(\d+\.\d+)} =~ ENV["SERVER_PROTOCOL"]
28: httpv = $1
29: end
30: @config = WEBrick::Config::HTTP.dup.update(
31: :ServerSoftware => ENV["SERVER_SOFTWARE"] || "null",
32: :HTTPVersion => HTTPVersion.new(httpv || "1.0"),
33: :RunOnCGI => true, # to detect if it runs on CGI.
34: :NPH => false # set true to run as NPH script.
35: )
36: if config = args.shift
37: @config.update(config)
38: end
39: @config[:Logger] ||= WEBrick::BasicLog.new($stderr)
40: @logger = @config[:Logger]
41: @options = args
42: end
# File lib/webrick/cgi.rb, line 111
111: def serviceserviceservice(req, res)
112: method_name = "do_" + req.request_method.gsub(/-/, "_")
113: if respond_to?(method_name)
114: __send__(method_name, req, res)
115: else
116: raise HTTPStatus::MethodNotAllowed,
117: "unsupported method `#{req.request_method}'."
118: end
119: end
# File lib/webrick/cgi.rb, line 48
48: def start(env=ENV, stdin=$stdin, stdout=$stdout)
49: sock = WEBrick::CGI::Socket.new(@config, env, stdin, stdout)
50: req = HTTPRequest.new(@config)
51: res = HTTPResponse.new(@config)
52: unless @config[:NPH] or defined?(MOD_RUBY)
53: def res.setup_header
54: unless @header["status"]
55: phrase = HTTPStatus::reason_phrase(@status)
56: @header["status"] = "#{@status} #{phrase}"
57: end
58: super
59: end
60: def res.status_line
61: ""
62: end
63: end
64:
65: begin
66: req.parse(sock)
67: req.script_name = (env["SCRIPT_NAME"] || File.expand_path($0)).dup
68: req.path_info = (env["PATH_INFO"] || "").dup
69: req.query_string = env["QUERY_STRING"]
70: req.user = env["REMOTE_USER"]
71: res.request_method = req.request_method
72: res.request_uri = req.request_uri
73: res.request_http_version = req.http_version
74: res.keep_alive = req.keep_alive?
75: self.service(req, res)
76: rescue HTTPStatus::Error => ex
77: res.set_error(ex)
78: rescue HTTPStatus::Status => ex
79: res.status = ex.code
80: rescue Exception => ex
81: @logger.error(ex)
82: res.set_error(ex, true)
83: ensure
84: req.fixup
85: if defined?(MOD_RUBY)
86: res.setup_header
87: Apache.request.status_line = "#{res.status} #{res.reason_phrase}"
88: Apache.request.status = res.status
89: table = Apache.request.headers_out
90: res.header.each{|key, val|
91: case key
92: when /^content-encoding$/i
93: Apache::request.content_encoding = val
94: when /^content-type$/i
95: Apache::request.content_type = val
96: else
97: table[key] = val.to_s
98: end
99: }
100: res.cookies.each{|cookie|
101: table.add("Set-Cookie", cookie.to_s)
102: }
103: Apache.request.send_http_header
104: res.send_body(sock)
105: else
106: res.send_response(sock)
107: end
108: end
109: end