Class: Pione::Util::CGIExecutor

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

Overview

CGIExecutor is a execution helper for CGI programs.

Instance Method Summary collapse

Constructor Details

#initialize(cgi_path, cgi_info, chdir, timeout) ⇒ CGIExecutor

Returns a new instance of CGIExecutor.

Parameters:

  • cgi_path (Pathname)

    path of the CGI program

  • cgi_info (CGIInfo)

    various informations for CGI program



144
145
146
147
148
149
150
151
152
153
# File 'lib/pione/util/cgi.rb', line 144

def initialize(cgi_path, cgi_info, chdir, timeout)
  @cgi_path = cgi_path
  @cgi_info = cgi_info
  @chdir = chdir
  @timeout = timeout
  @umask = 077
  @cgi_stdin = Temppath.create
  @cgi_stdout = Temppath.create
  @pid = nil
end

Instance Method Details

#execObject

Execute the CGI program.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/pione/util/cgi.rb', line 156

def exec
  unless @cgi_path.exist?
    raise CGIError.not_exist(@cgi_path)
  end

  env = @cgi_info.create_env
  options = create_options
  args = @cgi_info.create_arguments

  Timeout.timeout(@timeout) do
    @pid = Kernel.spawn(env, @cgi_path.to_s, *args, options)
    Process.waitpid(@pid)
    if @cgi_stdout.exist?
      return analyze_response(Location[@cgi_stdout].read)
    else
      raise CGIError.response_not_found
    end
  end
rescue Timeout::Error
  if @pid
    begin
      Process.kill(15, @pid)
    rescue
    ensure
      CGIError.timeouted
    end
  end
rescue Errno::EACCES => e
  CGIError.cannot_execute_cgi(@cgi_path)
end