0
votes

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
1
Do you have an error log?mu is too short
Added the error.log. Those three lines show up everytime I refresh the page.Senjai
If I run one of the scripts that work. like cgi_escape.rb the only message I get is the Favicon one (which really doesn't matter.) These don't use the CGI library for output though, only a series of print statements.Senjai
What happens if you include the Content-Type header?mu is too short
I uncommented it, but the same error occurs. From the description of the error message it seems like I need to close something... I created this from an example from the Pickaxe book 1.9. (Programming ruby 1.9) but even a direct copy and paste of the code from the book doesnt work.Senjai

1 Answers

0
votes

The only reason is bad coding only... Coding errors apart, the main issue is that CGI methods return strings, so a print is needed to put them on STDOUT (which is the socket returning http answer).

Try with this:

#!/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
  }
}