0

I'm studying how to use ruby to write CGI applications on my webserver. Using the CGI library, I prepared the following code:

#!usr/bin/ruby
require 'cgi'
#print "Content-type: text/html\r\n\r\n" << Not needed with cgi.out?
cgi = CGI.new('html3')
cgi.out do
  cgi.html do
    cgi.head do
      "\n" + cgi.title {"This is a test"}
    end
    + cgi.body do "\n" +
      cgi.form do "\n" +
        cgi.hr +
        cgi.h1 { "A form: " } + "\n" +
        cgi.textarea("get_text") + "\n" +
        cgi.br +
        cgi.submit
      end
    end
  end
end

I'm using Kubuntu 13.04 using the apache2 webserver, the files are in the /usr/lib/cgi-bin directory and the chmod is as follows:

richard@Senjai:~/projects/pickaxe/mini-projects/cgi-bin$ ls -l /usr/lib/cgi-bin/
total 12
-rwxr-xr-x 1 richard richard 170 May 27 13:30 cgi_escape.rb
-rwxr-xr-x 1 richard richard 395 May 27 13:30 cgi_htmlgen.rb
-rwxr-xr-x 1 richard richard 126 May 27 13:30 cgi_test.rb

I dont think the permissions are the problem, because cgi_test.rb and cgi_escape.rb work just fine.

Does anyone have an idea of why this gives a 500 internal server error? Note that this is an offline web development environment only, so I cannot give a link to the page if anyone was interested in that.

Apache2 Error.log:

richard@Senjai:/var/log/apache2$ cat error.log 
[Mon May 27 14:06:57 2013] [notice] Apache/2.2.22 (Ubuntu) PHP/5.4.6-1ubuntu1.2 configured -- resuming normal operations
[Mon May 27 14:07:04 2013] [error] [client 127.0.0.1] (2)No such file or directory: exec of '/usr/lib/cgi-bin/cgi_htmlgen.rb' failed
[Mon May 27 14:07:04 2013] [error] [client 127.0.0.1] Premature end of script headers: cgi_htmlgen.rb
[Mon May 27 14:07:04 2013] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
4

1 回答 1

0

唯一的原因是只有糟糕的编码......除了编码错误之外,主要问题是 CGI 方法返回字符串,因此需要打印以将它们放在 STDOUT 上(这是返回 http 答案的套接字)。

试试这个:

#!/usr/bin/ruby
require 'cgi'
#print "Content-type: text/html\r\n\r\n" << Not needed with cgi.out?
cgi = CGI.new('html3')
print cgi.out {
  cgi.html {
    cgi.head {
       "\n" + cgi.title {"This is a test"}
    } + cgi.body { "\n" } +
    cgi.form { "\n" } +
    cgi.hr +
    cgi.h1 { "A form: " } + "\n" +
    cgi.textarea("get_text") + "\n" +
    cgi.br +
    cgi.submit
  }
}
于 2013-10-16T18:36:28.990 回答