Sun Java Sytem Web Server 7.0 blogs Amit's Weblog

Wednesday Jul 04, 2007

CGI_as_file_type Enabling CGI as file type in Sun Java System Web Server 7.0

In my previous blog, I have listed the steps about how to configure Sun Java System Web Server to run Perl progams as CGI applications on Windows. As we have noticed that all the Perl programs with any file extension residing in the Shell CGI directory will be treated as CGI applications. But what if you want to instruct the server to treat all files with certain extensions as CGI programs, regardless of which directory they reside in. This could be easily done by enabling "CGI as file type". 
CGI as file type can be enabled per URI or for the entire virtual server. (Enabling CGI as file type  per URI is being introduced in Sun Java System Web Server Update 1)

Let us try to configure Sun Java System Web Server to run a simple perl program test.cgi residing in a document directory of an instance(e.g <install-root>/https-<config>/docs) on Windows. I am assuming that  you have created the file association for .cgi on Windows (You can refer to my previous blog about "Setting Windows File extensions"). This is how our test.cgi looks like:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>A Simple Perl CGI</title>
</head>
<body>
<h1>A Simple Perl CGI</h1>
<p>Hello World</p>
</body>
HTML
exit;

The first and foremost task is to enable "CGI as file type"  via Administration GUI/CLI.

Enabling CGI as file type via Administration GUI
Since performing any configuration task is very easy via Administration GUI, so let us first use Administration GUI to enable CGI as file type. Please note that I am using Sun Java System Web Server Update 1. For Sun Java System Web Server 7.0, please refer to the Web Server 7.0 docs
  • Login to Administration GUI
  • You will see a bunch of common tasks for the configuration and the virtual server in the Common Tasks Page. Select the configuration and virtual server for which you want to enable CGI as file type.
  • Click on the "CGI Directories" link under the Virtual Server Tasks. This will take you to CGI Settings Page.
  • Go the "CGI as File Type" section and click on the "New..." button in the "CGI as File Type Enabled URIs" table. This action will bring up a pop up window.
  • Make sure that  "Entire Virtual Server" radio button is clicked. Click on the OK button
CGI as file type snapshot
  • Deploy the configuration by clicking the "Deployment Pending" link on the top right side of the page.
Though we have enabled CGI as file type, the server won't know how to execute test.cgi until there is a Mime Type associated with .cgi. If you try to access the test.cgi at this point (http://<server-name>:port/test.cgi), you will see the following error messages in the instance server logs (<install-root>/https-<config>/logs/errors)

[04/Jul/2007:12:16:33] failure ( 3324): for host 127.0.0.1 trying to GET /test.cgi, send-cgi reports: HTTP4068: cannot execute CGI script <install-root>/https-<config>/docs/test.cgi (The specified program is not a Windows executable)
[04/Jul/2007:12:16:33] failure ( 3324): for host 127.0.0.1 trying to GET /test.cgi, func_exec reports: HTTP2302: Function send-cgi aborted the request without setting the status code

This error is becuase of the reason that by defaut, there is a MIME Type with a value - magnus-internal/cgi (which instructs the server to treat the program as Windows executable)associated with a .cgi file extension and thus the server treats test.cgi as windows executable and hence fails while executing it. We are required to create a MIME Type with the value - magnus-internal/shellcgi (which instructs the server to use the windows shell program[cmd] to execute the program) to run all the Perl programs as CGI applications on Windows.

Creating MIME Type via Administration GUI
  • Navigate to <configuration> -> General -> MIME Types page
  • Click on "New..." button under the MIME Types table. This will bring a pop up window.
  • Create a magnus-internal/shellcgi MIME type as shown in the following snapshot
MIME Type Snapshot
  • Click on OK button and you are done with creating a new MIME type.
  • Deploy the configuration by clicking the "Deployment Pending" link on the top right side of the page.

Note -
In our example, we also have to remove the file extension "cgi" for the MIME value - magnus-internal/cgi since we want to treat .cgi as shellcgi and not as cgi. To do that search for the "magnus-internal/cgi" in the MIME Types table and edit the MIME Type by clicking on the File Suffix entry link.



Now let us try to access the resource test.cgi by typing in the URL http://<server-name>:port/test.cgi in the browser.

CGI program snapshot


Enabling CGI as file type via Administration CLI
We could do same set of tasks using CLI as well. The following are the CLI commands which we can use to enable CGI as file type and creating a new MIME type.

wadm> enable-cgi-file-type --config=<config-name> --vs=<vs-name> --uri-pattern=/*

wadm> create-mime-type --config=<config-name> --extensions=cgi--category=type magnus-internal/shellcgi
Comments:

1

Posted by 65.41.7.55 on October 08, 2008 at 02:23 PM PDT #

What if you have a file like /cgi-bin/foo that your webserver is calling as an executable?

I am getting [24/Jul/2009:14:37:11] failure (21586): for host 10.156.xxx.yyy trying to POST /cgi-bin/sector_tool, cgi_start_output reports: HTTP4068: cannot execute CGI script <path>/docs/cgi-bin/sector_tool (exec() failure [Exec format error]), chroot=<none>, dir=<none>, user=<none>, group=<none>

The executable if sector_tool: PA-RISC2.0 shared executable dynamically linked - not stripped

and the webserver is running Red Hat Enterprise Linux ES release 3 (Taroon Update 9)

Do you think maybe the executable might be compiled for HP-UX?

Posted by Jeff Carroll on July 24, 2009 at 07:54 AM PDT #

Looks very interesting. Thanks for sharing,great post! ^ ^

Posted by tiffany on October 19, 2009 at 06:22 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed