Class: WEBrick::CGI

Inherits:
Object
  • Object
show all
Defined in:
lib/webrick/cgi.rb

Defined Under Namespace

Classes: Socket

Constant Summary collapse

CGIError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CGI

Returns a new instance of CGI.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/webrick/cgi.rb', line 21

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

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/webrick/cgi.rb', line 19

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/webrick/cgi.rb', line 19

def logger
  @logger
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
# File 'lib/webrick/cgi.rb', line 44

def [](key)
  @config[key]
end

#service(req, res) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/webrick/cgi.rb', line 111

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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/webrick/cgi.rb', line 48

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.expand_path($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.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
  end
end