Class: Web::NarfHandler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/web/sapi/webrick.rb

Instance Method Summary collapse

Constructor Details

#initialize(server, name) ⇒ NarfHandler

Returns a new instance of NarfHandler.



28
29
30
31
32
# File 'lib/web/sapi/webrick.rb', line 28

def initialize(server, name)
  super
  @script_filename = name
  @tempdir = server[:TempDir]
end

Instance Method Details

#do_GET(req, res) ⇒ Object Also known as: do_POST



34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/web/sapi/webrick.rb', line 34

def do_GET(req, res)
  cgi_out = Tempfile.new("webrick.cgiout.", @tempdir)
  cgi_err = Tempfile.new("webrick.cgierr.", @tempdir)
  
  old_stderr = $stderr
  
  begin
    $stderr    = cgi_err
    
    meta = req.meta_vars
    meta["SCRIPT_FILENAME"] = @script_filename
    meta["PATH"] = @config[:CGIPathEnv]
    
    cgd = Web::CGD::Webrick.new(:out => cgi_out,
                                :in  => StringIO.new( req.body || '' ),
                                :env => meta   )
    Web::load( @script_filename, :cgd => cgd )
    
    res.status = Web::status.to_i
 
    Web::header.each do |key, val|
      unless key =~ /status/i
        res[key] = val.join(", ")
      end
    end
    
    cgi_out.rewind
    res.body = cgi_out.read
    
    cgi_err.rewind
    if errmsg = cgi_err.read
      if errmsg.size > 0
        @logger.error("NarfHandler: #{@script_filename}:\n" + errmsg)
      end
    end 

  rescue Exception => ex
    #Web::report_error( ex )
    raise HTTPStatus::InternalServerError, ex.message

  ensure
    cgi_out.close(true)
    cgi_err.close(true)
    $stderr = old_stderr
  end
end