Frequently Asked Questions on CGI
  1. Why doesn't my CGI program work?
  2. How to setup and run CGI in Windows computer?
  3. How to setup and run CGI in linux/unix?
  4. Why is the source code of my CGI program displayed on the Web browser, instead of being executed by the web server?
  5. Why can the file run in a terminal prompt, but cannot be executed in my CGI program?
  6. I have turned on -g to debug my Ch CGI script, why do I still get the internal error?
  7. CGI-based plotting in Ch does not work using a personal Web server in Windows?
  8. On what platforms are wide character version of Ch-CGI classes CResponse, CRequest, CServer, CCookies supported through header file <wcgi.h>?

Q: Why doesn't my CGI program work?

When you debug and run a CGI program, you will encounter some error messages. In general, it is difficult to debug a program in Common Gateway Interface. If you see an error message such as the following one from Netscape browser:


Server Error
This server has encountered an internal error which prevents it from fulfilling your request. The most likely cause is a misconfiguration. Please ask the administrator to look for messages in the server's error log.

Then, look at messages in the server's error log file. If you find
[06/Feb/1996:16:38:08] failure: for host your.host.id.num. trying to GET /your/path/your.ch, cgi-parse-output reports: the CGI program /your_absolute_cgi_path/your.ch did not produce a valid header (program terminated without a valid CGI header. Check for core dump or other abnormal termination)
This error message basically tells you that your CGI program did not produce a valid CGI header such as
Content-type: text/html
or
Content-type: text/plain
To debug your Ch CGI program, the following steps shall be taken.
  • Make sure the first output statement from the CGI program will produce a valid CGI header such as the one described in the above.
  • Make sure your program can run successfully from your terminal prompt by just typing the program name, say, your.ch . Sometimes, your program may terminate at the mid of the execution if the flow of the program depends on some environmental variables passed from the Web server. It is fine if this is the case. For example, function getnamevalue() depends on the environmental variables REQUEST_METHOD, CONTENT_LENGTH, and QUERY_STRING, and if your CGI program is not invoked by the Web server, these environmental variables are not set. The values supposedly passed from a FORM will be NULL, and the program may be terminated prematurely if run from a terminal. But, the program can be run successfully from the Common Gateway Interface.
  • Since your CGI program is executed by the web server, which uses a different user account from yours, make sure your program is readable and executable by other users in the system. In Unix, you can change the permission of your program by the following command
        chmod 755 your.ch
    
  • If your CGI program reads and writes a file, make sure the file is readable and writable by the user account of the Web server. Often times, you may need to create temporary file using the function tmpnam() in Ch if a temporary file is needed in the CGI program.
  • If you can login as the Web server, test your CGI program from the account of the Web server. You may need to get permission from your system administrator to do so.
  • If your CGI program invokes other Ch programs, you may need to add the following statement
         setbuf(stdout,NULL);
    
    before the first printing statement of your CGI program. This function will cause output to be flushed immediately instead of being buffered so that the output will be sent in a proper sequence.
  • If you use a C program as your CGI program, make sure you add the following line as the first statement of the program.
        #!/bin/ch 
    
    so that it can be used as Ch CGI code in both Unix and Windows although it is not necessary in Windows.
  • Read the tutorial on Common Gateway Interface in Ch. about how to write Ch CGI programs.

Q: How to setup and run Ch CGI in Windows computer?

First, you need to install Internet Information Services in Windows. It depends on the Windows Operating system you have. Assume you have Windows 2000 or XP or 2003.

Start Installation Procedure

  • click Control Panel
  • click Add/Remove Programs
  • click Add/Remove Windows
  • check the Internet Information Services box
You are now done with IIS Installation

Setup Web Server

  • Download Ch's CGI toolkit
For more information, read the cgi.pdf and Readme.txt file found in C:/Ch/toolkit/demos/cgi/ folder, assuming you have installed Ch to directory C:/Ch.
If you install IIS. after Ch CGI package is installed. You need to run the executable file setIISMetabase.exe* found in the folder C:/Ch/toolkit/bin/

  • copy
      C:/Ch/toolkit/demos/cgi/chhtml
    to
      C:/inetpub/wwwroot/chhtml
  • mkdir
      C:/inetpub/cgi-bin/
  • copy
      C:/Ch/toolkit/demos/cgi/chcgi
    to
      C:/inetpub/cgi-bin/chcgi

You are now done running setIISMetabase.exe

Create IIS virtual directory for CGI

  • click Control Panel again
  • click Performance and Maintenance
  • click Administrative Tools
  • click Internet Information Services
  • click the local computer directory
  • In the local computer directory, click Web sites
  • click Default website

Your new URL should look like this: http://localhost/chhtml/index.html

  • right click on the Default website
  • choose New, then Folder
  • type cgi-bin in the Alias field
  • click next
  • type c:/inetpub/cgi-bin in the Directory field
  • click next
  • check the execute box
  • click next
  • click finish
(default is index.html)
  • right click Default website
  • click properties
  • click the Documents tab
  • make sure to add default document index.html
For Help, go to http://localhost/iishelp/

Q: How to setup and run CGI in linux with selinux enforcing?

To run CGI in linux, you need to do the following steps:
in your CGI directory, run the following command:

find . -name *.ch -exec chcon -t httpd_sys_script_exec_t {} \;

in your CHHOME directory, run the following command:
find . -name '*.dl' -exec chcon -t texrel_shlib_t {} \;

In your CHHOME/extern/lib, run the following command:
find . -name *.ch -exec chcon -t httpd_sys_script_exec_t {} \;

Also, you need to run
setsebool -P allow_execmem 1 setsebool -P allow_execstack 1

Q: Why is the source code of my CGI program displayed on the Web browser, instead of being executed by the web server?

By default, only CGI programs located in the server's cgi-bin can be executed by the web server. If your CGI program is not located in cgi-bin and your Web server is not configured to recognize CGI programs with file extension .ch, your program will fail under the Common Gateway Interface. Check Ch Installation and System Administration Guide on how to setup your Web server.

Q: Why can the file run in a terminal prompt, but cannot be executed in my CGI program?

Usually, this means one of two things: either you have misconfigured your system, or your script does not output the right stuff to be a CGI script.
Before you do anything else, check this list:

  • Your Ch language environment is suggested to be installed after the web server has been installed.
  • If your web server requires directories to be marked as readable and executable, make sure the directory that contains the script is marked this way.
  • Since the web server can be configured to run as a local user, be sure that the user has access to the script file and the Ch binaries and libraries. Many web servers run as the ``Local System'' account, which generally has sufficient permissions.
  • If Ch program fails to work as expected, check your web server logs for more information.
  • If you are sure the server is running the script, but it only generates error messages in your browser, you can try the Ch cgi debug feature.

Q: I have turned on -g to debug my Ch CGI script, why do I still get the internal error?

The option -g in

 #!/bin/ch -g 
in a Ch CGI program will turn the Web browser into a text console. All output including error messages from execution of the CGI script will be displayed inside your browser. However, if the script is not executable, such an error without the detailed error information will be displayed. Make sure the account, which runs the Web server, has write and execute permission to the CGI script. In Unix, use the following command to add executable attribute to file CGI_script_file.
chmod +x CGI_script_file

Q: CGI-based plotting in Ch does not work using a personal Web server in Windows?

CGI-based plotting in Ch on a personal Web server in Windows is not supported in the current version of Ch.

Q: On what platforms are wide character version of Ch-CGI classes CResponse, CRequest, CServer, CCookies supported through header file <wcgi.h>?

They are supported in Windows, Solaris, Linux with kernel 2.2.17-14 or above. They are not supported in HP-UX at present.