C CGI Programming Example - IBM

You are in:IBM i Technology Updates > Web Integration on i > IBM HTTP Server for i > Introduction > CGI programming example > C CGI programming example

This sample code is provided by IBM for illustrative purposes only. It has not been fully tested. It is provided as-is without any warranties of any kind, including but not limited to the implied warranties of merchantability and fitness for a particular purpose.

This program is a simple C program that demonstrates reading standard input, reading environment variables, and writing standard output. The input data comes from information typed in HTML fields. This program will read this input information and write exactly what is read to standard output.

To install the example program, do the following:

  1. Download the following files:
    • samplec.txt (TXT,15KB) - contains the C source code.
    • samplec.zip (ZIP,1KB) - contains the html code that makes the request for the CGI.
  2. Use the following CL commands to create a directory structure:
    1. mkdir dir('/qsys.lib/cdemo.lib')
    2. mkdir dir('/qsys.lib/cdemo.lib/qcsrc.file')
    3. mkdir dir('/cdemo')
    4. mkdir dir('/cdemo/html') Be aware the mkdir command will use hierarchical file authorities, unless you specify otherwise, for the *PUBLIC users. You may want to specifically add read and execute authorities for server user profiles QTMHHTTP and QTMHHTP1 to these new directories.
  3. Use FTP to connect to your server.
  4. Use the following FTP commands to copy the files to your server:
    1. put samplec.html /cdemo/html/samplec.html
    2. put samplec.txt /qsys.lib/cdemo.lib/qcsrc.file/samplec.mbr
  5. Use the following CL commands to create the program (PGM) object called SAMPLEC:
    1. CRTCMOD MODULE(cdemo/samplec) SRCFILE(cdemo/qcsrc) SRCMBR(samplec) OUTPUT(*print)
    2. CRTPGM PGM(CDEMO/SAMPLEC) MODULE(CDEMO/SAMPLEC) BNDSRVPGM(QHTTPSVR/QZHBCGI)
  6. Use your browser to access the HTTP Server Configuration and Administration forms to create an Apache server named, for example, CCONFIG.
    • Set the document root to the /cdemo/html directory.
    • Add a directory for the QSYS library allowing access to the directory and allowing CGI script execution, using the Alias and Redirection form.
    • Add the directory for the QSYS (CDEMO.LIB) library, using the Context Managementform.
    • Under Tools, select Display Configuration File to make sure you have the following CGI related directives:

      DocumentRoot /cdemo/html/ Alias /html/ /cdemo/html/ ScriptAlias /cgi-bin/ /QSYS.LIB/CDEMO.LIB/ <Directory /cdemo/html/> AllowOverride None Options None order allow,deny allow from all </Directory> <Directory /QSYS.LIB/CDEMO.LIB/> AllowOverride None order allow,deny allow from all SetHandler cgi-script Options +ExecCGI </Directory>

  7. Start the CCONFIG instance from the GUI. From your browser type: http://yourservername:port/html/samplec.html .
  8. The input for this program comes from CGI standard input or the environment variable QUERY_STRING. For an HTTP request method of POST, the input is read from standard input and for an HTTP request method of GET, the input is read from QUERY_STRING. Standard C APIs are used in the example. HTTP server CGI application APIs like QtmhGetEnv would also work.​

Từ khóa » C Cgi