/************************************************************************** * Persistent CGI Example * *************************************************************************** * * * Request this program with following URL: * * http://hostname:port/cgi-bin/persistc.pgm?bin=1 * * * * This is a demo to demonstrate the capabilities of Persistent CGI on * * the AS/400 platform. Persistent CGI is an extension to the CGI * * interface that allows a CGI program to remain active across multiple * * browser requests and maintain a session with that browser client. * * This allows files to be left open, the state to be maintained, and * * long running database transactions to be committed or rolled-back * * based on end-user input. The AS/400 CGI program must be written using * * named activation groups which allows the program to remain active * * after returning to the server. The CGI program notifies the server * * it wants to remain persistent using the "Accept-HTSession" CGI header * * as the first header it returns. This header defines the session ID * * associated with this instance of the CGI program and is not returned * * to the browser. Subsequent URL requests to this program must contain * * the session ID as the first parameter after the program name. The * * server uses this ID to route the request to the specific instance of * * the CGI program. The CGI program should regenerate this session ID * * for each request. It is strongly recommended that you use Secure * * Sockets Layer (SSL) for persistent and secure business transaction * * processing. * * * * To create a unique HTSession we combine the process id, which should * * be unique already, with a random number. For demonstration purposes * * this will be a sufficiently unique Session Id. * * * **************************************************************************/ #include #include #include #include /* System API used to create unique session id */ #include #include #define BUFSIZE 1024 unsigned int MAXINPUT = BUFSIZE; /* Maximum input data.*/ int static count = 1; pid_t static processId = -1; int static randnum = -1; int main() { char *pt; char carac[2] = " "; int bin = 1; char * sessionId; char localId[30]; stdin = fopen("stdin", "r"); stdout = fopen("stdout", "w"); stderr = fopen("stderr", "w"); /* get a unique session handle */ if((sessionId = getenv("UNIQUE_ID")) == NULL) { processId = getpid(); srand((unsigned int)time(NULL)); randnum = rand(); sessionId = localId; sprintf(sessionId,"%d",(processId + randnum)); } pt = getenv("QUERY_STRING"); bin = 1; /* initialize bin to 1 */ if(*pt != '\0') /* Request without QUERY_STRING uses bin=1 */ { carac[0] = pt[4]; /* get 5th char in QUERY_STRING */ bin=atoi(carac); } /*******************************************************************/ /* The following resets the static variable (count) after a timeout*/ /* occurs. This insures a user always goes back to the initial */ /* html if his previous persistent session times out */ /*******************************************************************/ if (count == 1) /* if the state is initial */ bin=1; /* go back to the first form */ /*******************************************************************/ /* Determine which html page to present to the user based on the */ /* bin value returned from the browser */ /*******************************************************************/ if(bin == 1) { printf("Accept-HTSession: %s\n", sessionId); printf("HTTimeout: 1\n"); printf("Content-type: text/html \n\n"); printf("Test persistent CGI"); printf("

The first form

"); printf("Count: %i",count); printf("
"); printf(""); printf("

Start Persistent CGI session

"); printf(""); printf("
"); printf("

While clicking 'Finish' will end the persistent cgi session "); printf("after the response has been returned.

"); printf("
"); printf(""); printf(""); printf("
"); count++; } if(bin == 2) { printf("Accept-HTSession: %s\n", sessionId); printf("Content-type: text/html \n\n"); printf(" Test persistent CGI"); printf("

The second form

"); printf("Count: %i",count); printf("
"); printf(""); printf("

Persistent CGI is working if count=2

"); printf(""); printf("
"); printf("

While clicking 'Finish' will end the persistent cgi session "); printf("after the response has been returned.

"); printf("
"); printf(""); printf(""); printf("
"); count++; } if(bin == 3) { printf("Accept-HTSession: %s\n", sessionId); printf("Content-type: text/html \n\n"); printf(" Test persistent CGI"); printf("

The third form

"); printf("Count: %i",count); printf("
"); printf("

Persistent CGI is working if count=3

"); printf(""); printf(""); printf("
"); printf("

While clicking 'Finish' will end the persistent cgi session "); printf("after the response has been returned.

"); printf("
"); printf(""); printf(""); printf("
"); count++; } if(bin == 4) { printf("Accept-HTSession: %s\n", sessionId); printf("Content-type: text/html \n\n"); printf(" Test persistent CGI"); printf("

The fourth form

"); printf("Count: %i",count); printf("

Persistent CGI is working if count=4"); printf("
Clicking 'Next' will continue to increment the counter"); printf(" in our persistent session

"); printf("
"); printf(""); printf(""); printf("
"); printf("

While clicking 'Finish' will end the persistent cgi session "); printf("after the response has been returned.

"); printf("
"); printf(""); printf(""); printf("
"); count++; } if(bin == 5) { printf("Accept-HTSession: %s\n", sessionId); printf("Content-type: text/html \n\n"); printf(" Test persistent CGI"); printf("
"); printf("

The fifth form

"); printf("Count: %i",count); printf("

Persistent CGI is working if count=5"); printf("

Clicking 'Finish' will end the persistent cgi session "); printf("after the response has been returned.

"); printf(""); printf(""); printf("
"); count++; } if(bin == 6) { printf("Content-type: text/html \n\n"); printf(" Test persistent CGI"); printf("

The Persistent CGI session is completed.

The next"); printf(" request for this program will reset the counter to 1.

"); printf("

Click 'Again' to start a new Persistent CGI session.

"); printf("
"); printf(""); printf(""); printf("
"); count = 1; /* need to reset count */ } fflush(stdout); fflush(stderr); fclose(stdout); fclose(stdin); fclose(stderr); return 0; }