Six Steps to write an ILE C CGI program

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 program (CGI program).

 

CGI programs process data that is 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.

1.        The HTTP request is received at the Web server.

2.        The HTTP request's Uniform Resource Identifier (URI) is parsed to identify how the request should be processed.

3.        Based on the URI contained in the HTTP request, action is taken to server 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 is written and compiled for AIX. The binary output of the compiler is executed directly from IBM i Portable Application Solutions Environment (IBM i PASE).

·        V5R4: 5722SS1    33      Portable App Solutions Environment

·        V6R1: 5761SS1    33      Portable App Solutions Environment

·        V6R1: 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 beginning writing a CGI program, you need to know the concept Environment Variables. Environment variable is a kind of data format that the server pass to the CGI program when receiving URL-encoded information.

 

Now, I will give 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 environment variables. 

 

To install the example program, do the following:

  1. Write a sample ILE C source code, save 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;                                     

}

 

  1. Use the following CL commands to create a directory structure:
    • mkdir dir('/qsys.lib/cdemo.lib')
    • mkdir dir('/cgidemo')
    • mkdir dir('/cgidemo/c')

NoteYou may want to ensure *RX authorities for server user profile QTMHHTTP and CGI user profile QTMHHTP1 to these new directories.

 

  1. FTP to your server, use the following FTP commands to copy the files to your server:
  1.  
    • put samplec.txt /cgidemo/c/samplec.c

 

  1. Use the following CL commands to create the program (PGM) object called SAMPLEC:
  1.  
    • CRTCMOD MODULE(cgidemo/samplec) SRCSTMF(‘/cgidemo/c/samplec.c’) OUTPUT(*print)
    • CRTPGM PGM(CGIDEMO/SAMPLEC) MODULE(CGIDEMO/SAMPLEC) BNDSRVPGM(QHTTPSVR/QZHBCGI)

 

  1. Use IBM Web Administration for i (http://<yourservername>:2001/HTTPAdmin) to create an Apache server named, for example, CGIDEMO.
  1.  
    • Add a directory for the QSYS library allowing access to the directory and allowing CGI script execution, using the URL Mapping form under Server Propertie
    • Add the directory for the QSYS (CGIDEMO.LIB) library, using the Container Management form under Server Propertie. .
    • 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>

  1. Restart the CGIDEMO server and access: http://<yourservername>:<port>/cgi-bin/samplec.pgm.

 

Here is 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值