Common Gateway Interface (CGI) is a standard, supported by almost all Web servers, that defines how information is exchanged between a Web server and an external (CGI) program. CGI programs process data that’s received from browser clients. For example, the client fills out a form and sends the information back to the server. Then the server runs the CGI program, with the following results:
- The HTTP request is received at the Web server.
- The HTTP request's Uniform Resource Identifier (URI) is parsed to identify how the request should be processed.
- Based on the URI contained in the HTTP request, action is taken to serve a file or process the request through a CGI program.
CGI applications written and compiled for the IBM i can even run a CGI application that’s written and compiled for AIX. The binary output of the compiler is executed directly from IBM i Portable Application Solutions Environment (PASE). You will need the following, based on what version of IBM i you have.
- V5R4: 5722SS1 33 Portable App Solutions Environment
- V6R1: 5761SS1 33 Portable App Solutions Environment
- V7R1: 5770SS1 33 Portable App Solutions Environment
The server supports CGI programs that are written in C++, Java, ILE C, ILE RPG and ILE COBOL. Before writing a CGI program, you need to know the concept of environment variables. An environment variable is a kind of data format that the server passes to the CGI program when receiving URL-encoded information.
Six Steps to Write an ILE C CGI Program
Let’s look at a CGI programming example written in ILE C. This example contains instructions to install and execute an ILE RPG C program that prints current date and all of the environment variables. To install the example program, follow these six steps:
Step 1: Write a sample ILE C source code and save it as samplec.txt.
int main(int args, char* argv[]) printf("Content-type: text/html; \n"); printf("\n"); printf("<br>\n"); printf(" <!--#echo var=\"DATE_LOCAL\" -->"); printf("<br><br><br>\n"); printf(" <!--#printenv -->\n"); printf("<br>\n"); return; }
Step 2: Create a directory structure
Use the following CL commands to create a directory structure:
- mkdir dir('/qsys.lib/cdemo.lib')
- mkdir dir('/cgidemo')
- mkdir dir('/cgidemo/c')
Note: You may want to ensure *RX authorities for server user profile QTMHHTTP and CGI user profile QTMHHTP1 to these new directories. Step 3: FTP to your server. Use the following FTP commands to copy the files to your server:
Step 4: Create the PGM object
Use the following CL commands to create the program (PGM) object called SAMPLEC:
- CRTCMOD MODULE(cgidemo/samplec) SRCSTMF('/cgidemo/c/samplec.c') OUTPUT(*print)
- CRTPGM PGM(CGIDEMO/SAMPLEC) MODULE(CGIDEMO/SAMPLEC) BNDSRVPGM(QHTTPSVR/QZHBCGI)
Step 5: Create an Apache server
Use the IBM Web Administration for i (http://<yourservername>:2001/HTTPAdmin) to create an Apache server named, for example, CGIDEMO.
- Add a directory for the QSYS library allowing directory access and CGI script execution, using the URL Mapping form under Server Properties.
- Add the directory for the QSYS (CGIDEMO.LIB) library, using the Container Management form under Server Properties.
- Under Tools, select Display Configuration File to ensure you have the following CGI related directives: Listen *:80 ScriptAlias /cgi-bin/ /QSYS.LIB/CGIDEMO.LIB/ <Directory /QSYS.LIB/CGIDEMO.LIB/> AllowOverride None order allow,deny allow from all SetHandler cgi-script Options +ExecCGI Options +Includes SetOutputFilter INCLUDES </Directory>
Step 6: Restart
Restart the CGIDEMO server and access http://<yourservername>:<port>/cgi-bin/samplec.pgm. Here’s output you should see if the sample ILE C CGI program is successful. Tuesday, 24-Nov-2009 12:47:24 Central Standard Time HTTPS=OFF UNIQUE_ID=X33GEAkFr4YAAGbFZPEAAAAH QIBM_USE_DESCRIPTOR_STDIO=Y HTTP_HOST=lp05ut3.rchland.ibm.com:20000 HTTP_USER_AGENT=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.04 (CK-IBM) (CK-IBM) Shiretoko/3.5.5 HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 HTTP_ACCEPT_LANGUAGE=en-us,en;q=0.5 HTTP_ACCEPT_ENCODING=gzip,deflate HTTP_ACCEPT_CHARSET=ISO-8859-1,utf-8;q=0.7,*;q=0.7 HTTP_KEEP_ALIVE=300 HTTP_CONNECTION=keep-alive HTTP_COOKIE=CoreID6=89947119652712539186271&ci=90175093,90175080,90225345,90175119,90130510,90175106,90175067; IBMPOLLCOOKIE=""; sauidp=U636780271253923646326; w3ibmProfile=200011161518280656634250576|gAME|897|ECR|en; EPSPROFILE=13E0EC4D2AFB7B2AEABB45941E98CFE8; bprememberme=rwatkin@us.ibm.com; ithc=f61f212d9627ced87c8574f7a2fe7d268d80 HTTP_CACHE_CONTROL=max-age=0 PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin SERVER_SIGNATURE= SERVER_SOFTWARE=Apache SERVER_NAME=lp05ut3.rchland.ibm.com SERVER_ADDR=9.5.175.134 SERVER_PORT=20000 REMOTE_ADDR=9.49.184.2 DOCUMENT_ROOT=/www/rwatkins/htdocs SERVER_ADMIN=[no address given] SCRIPT_FILENAME=/QSYS.LIB/RWATKINS.LIB/printenv.pgm DOCUMENT_NAME=printenv.pgm REMOTE_PORT=58960 GATEWAY_INTERFACE=CGI/1.1 SERVER_PROTOCOL=HTTP/1.1 REQUEST_METHOD=GET QUERY_STRING= REQUEST_URI=/cgi/printenv.pgm SCRIPT_NAME=/cgi/printenv.pgm DOCUMENT_URI=/cgi/printenv.pgm RULE_FILE=conf/httpd.conf CGI_MODE=EBCDIC CGI_OUTPUT_MODE=EBCDIC IBM_CCSID_VALUE=37 CGI_EBCDIC_CCSID=37 CGI_ASCII_CCSID=819 FSCP=37 NETCP=819 DATE_LOCAL=Tuesday, 24-Nov-2009 12:47:24 Central Standard Time DATE_GMT=Tuesday, 24-Nov-2009 18:47:24 GMT LAST_MODIFIED=Tuesday, 24-Nov-2009 12:47:20 Central Standard Time USER_NAME=RWATKINS SSI_ROOT=/QSYS.LIB/RWATKINS.LIB/ SSI_DIR=/ SSI_FILE=printenv.pgm
Dynamic vs. Static
Now you’re armed with the knowledge of how to write a CGI program and run it on Apache to get dynamic information. A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that doesn't change. A CGI program, on the other hand, is executed in real time, so it can output dynamic information.
Vuoi approfondire o essere richiamato? Registrati
Vuoi una dimostrazione del prodotto subito? Schedula una demo