4 * FastCGI example program using fcgi_stdio library
7 * Copyright (c) 1996 Open Market, Inc.
9 * See the file "LICENSE.TERMS" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * sample-store is a program designed to illustrate one technique
14 * for writing a high-performance FastCGI application that maintains
15 * permanent state. It is real enough to demonstrate a range of issues
16 * that can arise in FastCGI application programming.
18 * sample-store implements per-user shopping carts. These carts are kept
19 * in memory for speed but are backed up on disk for reliability; the
20 * program can restart at any time, affecting at most one request. Unlike
21 * CGI applications, the performance impact of sample-store's disk
22 * use is negligible: no I/O for query requests, no reads and one write
23 * for a typical update request.
25 * sample-store's on-disk representation is extremely simple. The
26 * current state of all shopping carts managed by a process is kept
27 * in two files, a snapshot and a log. Both files have the same format,
28 * a sequence of ASCII records. On restart the current state is restored
29 * by replaying the snapshot and the log. When the log grows to a certain
30 * length, sample-store writes a new snapshot and empties the log.
31 * This prevents the time needed for restart from growing without
34 * Since users "visit" Web sites, but never "leave", sample-store
35 * deletes a shopping cart after the cart has been inactive
36 * for a certain period of time. This policy prevents sample-store's
37 * memory requirements from growing without bound.
39 * sample-store operates both as a FastCGI Responder and as an
40 * Authorizer, showing how one program can play two roles.
42 * The techniques used in sample-store are not specific to shopping
43 * carts; they apply equally well to maintaining all sorts of
49 static const char rcsid[] = "$Id: sample-store.c,v 1.3 1999/07/26 05:33:00 roberts Exp $";
52 #include "fcgi_stdio.h" /* FCGI_Accept, FCGI_Finish, stdio */
53 #include <stdlib.h> /* malloc/free, getenv, strtol */
54 #include <string.h> /* strcmp, strncmp, strlen, strstr, strchr */
55 #include <tcl.h> /* Tcl_*Hash* functions */
56 #include <time.h> /* time, time_t */
57 #include <assert.h> /* assert */
58 #include <errno.h> /* errno, ENOENT */
59 #include <dirent.h> /* readdir, closedir, DIR, dirent */
60 #include <unistd.h> /* fsync */
67 * sample-store is designed to be configured as follows (for the OM server):
69 * SI_Department SampleStoreDept -EnableAnonymousTicketing 1
70 * Region /SampleStore/ * { SI_RequireSI SampleStoreDept 1 }
72 * Filemap /SampleStore $fcgi-devel-kit/examples/SampleStore
73 * AppClass SampleStoreAppClass \
74 * $fcgi-devel-kit/examples/sample-store \
75 * -initial-env STATE_DIR=$fcgi-devel-kit/examples/SampleStore.state \
76 * -initial-env CKP_THRESHOLD=100 \
77 * -initial-env CART_HOLD_MINUTES=240 \
78 * -processes 2 -affinity
79 * Responder SampleStoreAppClass /SampleStore/App
80 * AuthorizeRegion /SampleStore/Protected/ * SampleStoreAppClass
82 * sample-store looks for three initial environment variables:
85 * When sample-store is run as a single process without affinity
86 * this is the directory containing the permanent state of the
87 * process. When sample-store is run as multiple processes
88 * using session affinity, the state directory is
89 * $STATE_DIR.$FCGI_PROCESS_ID, e.g. SampleStore.state.0
90 * and SampleStore.state.1 in the config above. The process
91 * state directory must exist, but may be empty.
94 * When the log grows to contain this many records the process
95 * writes a new snapshot and truncates the log. Defaults
96 * to CKP_THRESHOLD_DEFAULT.
99 * When a cart has not been accessed for this many minutes it
100 * may be deleted. Defaults to CART_HOLD_MINUTES_DEFAULT.
102 * The program is prepared to run as multiple processes using
103 * session affinity (illustrated in config above) or as a single process.
105 * The program does not depend upon the specific URL prefix /SampleStore.
110 * This code is organized top-down, trying to put the most interesting
111 * parts first. Unfortunately, organizing the program in this way requires
112 * lots of extra declarations to take care of forward references.
114 * Utility functions for string/list processing and such
115 * are left to the very end. The program uses the Tcl hash table
116 * package because it is both adequate and readily available.
128 #define max(a,b) ((a) > (b) ? (a) : (b))
131 #define Strlen(str) (((str) == NULL) ? 0 : strlen(str))
133 void panic(char *format,
134 char *arg1, char *arg2, char *arg3, char *arg4,
135 char *arg5, char *arg6, char *arg7, char *arg8);
137 static void *Malloc(size_t size);
138 static void Free(void *ptr);
139 static char *StringNCopy(char *str, int strLen);
140 static char *StringCopy(char *str);
141 static char *StringCat(char *str1, char *str2);
142 static char *StringCat4(char *str1, char *str2, char *str3, char *str4);
143 static char *QueryLookup(char *query, char *name);
144 static char *PathTail(char *path);
146 typedef struct ListOfString {
148 struct ListOfString *tail;
150 static char *ListOfString_Head(ListOfString *list);
151 static ListOfString *ListOfString_Tail(ListOfString *list);
152 static int ListOfString_Length(ListOfString *list);
153 static int ListOfString_IsElement(ListOfString *list, char *element);
154 static ListOfString *ListOfString_AppendElement(
155 ListOfString *list, char *element);
156 static ListOfString *ListOfString_RemoveElement(
157 ListOfString *list, char *element);
159 static int IntGetEnv(char *varName, int defaultValue);
161 static void Initialize(void);
162 static void PerformRequest(void);
163 static void GarbageCollectStep(void);
164 static void ConditionalCheckpoint(void);
167 * A typical FastCGI main program: Initialize, then loop
168 * calling FCGI_Accept and performing the accepted request.
169 * Do cleanup operations incrementally between requests.
175 while (FCGI_Accept() >= 0) {
178 GarbageCollectStep();
179 ConditionalCheckpoint();
186 * All the global variables
188 typedef struct CartObj {
189 int inactive; /* This cart not accessed since mark */
190 ListOfString *items; /* Items in cart */
192 static Tcl_HashTable *cartTablePtr; /* Table of CartObj, indexed by userId */
193 static Tcl_HashTable cartTable;
194 static char *fcgiProcessId; /* Id of this process in affinity group */
195 static char *stateDir; /* Path to dir with snapshot and log */
196 char *snapshotPath, *logPath; /* Paths to current snapshot and log */
197 static int generation; /* Number embedded in paths, inc on ckp */
198 static FILE *logFile = NULL; /* Open for append to current log file */
199 static int numLogRecords; /* Number of records in current log file */
200 static int checkpointThreshold; /* Do ckp when numLogRecords exceeds this */
201 static int purge = TRUE; /* Cart collector is removing inactives */
202 static time_t timeCartsMarked; /* Time all carts marked inactive */
203 static int cartHoldSeconds; /* Begin purge when this interval elapsed */
205 #define STATE_DIR_VAR "STATE_DIR"
206 #define PID_VAR "FCGI_PROCESS_ID"
207 #define CKP_THRESHOLD_VAR "CKP_THRESHOLD"
208 #define CKP_THRESHOLD_DEFAULT 200
209 #define CART_HOLD_MINUTES_VAR "CART_HOLD_MINUTES"
210 #define CART_HOLD_MINUTES_DEFAULT 300
212 #define SNP_PREFIX "snapshot"
213 #define LOG_PREFIX "log"
214 #define TMP_SNP_NAME "tmp-snapshot"
216 #define LR_ADD_ITEM "Add"
217 #define LR_REMOVE_ITEM "Rem"
218 #define LR_EMPTY_CART "Emp"
221 static char *MakePath(char *dir, char *prefix, int gen);
222 static void AnalyzeStateDir(
223 char *dir, char *prefix, int *largestP, ListOfString **fileListP);
224 static int RecoverFile(char *pathname);
225 static void Checkpoint(void);
228 * Initialize the process by reading environment variables and files
230 static void Initialize(void)
232 ListOfString *fileList;
235 * Process miscellaneous parameters from the initial environment.
237 checkpointThreshold =
238 IntGetEnv(CKP_THRESHOLD_VAR, CKP_THRESHOLD_DEFAULT);
240 IntGetEnv(CART_HOLD_MINUTES_VAR, CART_HOLD_MINUTES_DEFAULT)*60;
242 * Create an empty in-memory shopping cart data structure.
244 cartTablePtr = &cartTable;
245 Tcl_InitHashTable(cartTablePtr, TCL_STRING_KEYS);
247 * Compute the state directory name from the initial environment
250 stateDir = getenv(STATE_DIR_VAR);
251 stateDirLen = Strlen(stateDir);
252 assert(stateDirLen > 0);
253 if(stateDir[stateDirLen - 1] == '/') {
254 stateDir[stateDirLen - 1] = '\000';
256 fcgiProcessId = getenv(PID_VAR);
257 if(fcgiProcessId != NULL) {
258 stateDir = StringCat4(stateDir, ".", fcgiProcessId, "/");
260 stateDir = StringCat(stateDir, "/");
263 * Read the state directory to determine the current
264 * generation number and a list of files that may
265 * need to be deleted (perhaps left over from an earlier
266 * system crash). Recover the current generation
267 * snapshot and log (either or both may be missing),
268 * populating the in-memory shopping cart data structure.
269 * Take a checkpoint, making the current log empty.
271 AnalyzeStateDir(stateDir, SNP_PREFIX, &generation, &fileList);
272 snapshotPath = MakePath(stateDir, SNP_PREFIX, generation);
273 RecoverFile(snapshotPath);
274 logPath = MakePath(stateDir, LOG_PREFIX, generation);
275 numLogRecords = RecoverFile(logPath);
278 * Clean up stateDir without removing the current snapshot and log.
280 while(fileList != NULL) {
281 char *cur = ListOfString_Head(fileList);
282 if(strcmp(snapshotPath, cur) && strcmp(logPath, cur)) {
285 fileList = ListOfString_RemoveElement(fileList, cur);
289 static char *MakePath(char *dir, char *prefix, int gen)
292 sprintf(nameBuffer, "%s.%d", prefix, gen);
293 return StringCat(dir, nameBuffer);
296 static void ConditionalCheckpoint(void)
298 if(numLogRecords >= checkpointThreshold) {
302 static void WriteSnapshot(char *snpPath);
304 static void Checkpoint(void)
306 char *tempSnapshotPath, *newLogPath, *newSnapshotPath;
308 * Close the current log file.
310 if(logFile != NULL) {
314 * Create a new snapshot with a temporary name.
316 tempSnapshotPath = StringCat(stateDir, TMP_SNP_NAME);
317 WriteSnapshot(tempSnapshotPath);
320 * Ensure that the new log file doesn't already exist by removing it.
322 newLogPath = MakePath(stateDir, LOG_PREFIX, generation);
325 * Commit by renaming the snapshot. The rename atomically
326 * makes the old snapshot and log obsolete.
328 newSnapshotPath = MakePath(stateDir, SNP_PREFIX, generation);
329 rename(tempSnapshotPath, newSnapshotPath);
331 * Clean up the old snapshot and log.
333 Free(tempSnapshotPath);
334 remove(snapshotPath);
336 snapshotPath = newSnapshotPath;
339 logPath = newLogPath;
341 * Open the new, empty log.
343 logFile = fopen(logPath, "a");
348 * Return *largestP = the largest int N such that the name prefix.N
349 * is in the directory dir. 0 if no such name
350 * *fileListP = list of all files in the directory dir,
351 * excluding '.' and '..'
353 static void AnalyzeStateDir(
354 char *dir, char *prefix, int *largestP, ListOfString **fileListP)
358 int prefixLen = strlen(prefix);
362 ListOfString *fileList = NULL;
365 while((dirp = readdir(dp)) != NULL) {
366 if(!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..")) {
369 curName = StringCat(dir, dirp->d_name);
370 fileList = ListOfString_AppendElement(fileList, curName);
371 if(!strncmp(dirp->d_name, prefix, prefixLen)
372 && (dirp->d_name)[prefixLen] == '.') {
373 cur = strtol(dirp->d_name + prefixLen + 1, NULL, 10);
379 assert(closedir(dp) >= 0);
381 *fileListP = fileList;
384 static int DoAddItemToCart(char *userId, char *item, int writeLog);
385 static int DoRemoveItemFromCart(char *userId, char *item, int writeLog);
386 static int DoEmptyCart(char *userId, int writeLog);
389 * Read either a snapshot or a log and perform the specified
390 * actions on the in-memory representation.
392 static int RecoverFile(char *pathname)
395 FILE *recoveryFile = fopen(pathname, "r");
396 if(recoveryFile == NULL) {
397 assert(errno == ENOENT);
400 for(numRecords = 0; ; numRecords++) {
402 char op[32], userId[32], item[64];
404 char *status = fgets(buff, sizeof(buff), recoveryFile);
406 assert(feof(recoveryFile));
407 fclose(recoveryFile);
410 count = sscanf(buff, "%31s %31s %63s", op, userId, item);
412 if(!strcmp(op, LR_ADD_ITEM)) {
413 assert(DoAddItemToCart(userId, item, FALSE) >= 0);
414 } else if(!strcmp(op, LR_REMOVE_ITEM)) {
415 assert(DoRemoveItemFromCart(userId, item, FALSE) >= 0);
416 } else if(!strcmp(op, LR_EMPTY_CART)) {
417 assert(DoEmptyCart(userId, FALSE) >= 0);
424 static void WriteLog(char *command, char *userId, char *item, int force);
427 * Read the in-memory representation and write a snapshot file
430 static void WriteSnapshot(char *snpPath)
432 Tcl_HashSearch search;
433 Tcl_HashEntry *cartEntry;
436 logFile = fopen(snpPath, "w");
437 assert(logFile != NULL);
438 cartEntry = Tcl_FirstHashEntry(cartTablePtr, &search);
439 for(cartEntry = Tcl_FirstHashEntry(cartTablePtr, &search);
440 cartEntry != NULL; cartEntry = Tcl_NextHashEntry(&search)) {
441 userId = Tcl_GetHashKey(cartTablePtr, cartEntry);
442 for(items = ((CartObj *) Tcl_GetHashValue(cartEntry))->items;
443 items != NULL; items = ListOfString_Tail(items)) {
444 WriteLog(LR_ADD_ITEM, userId, ListOfString_Head(items), FALSE);
448 fsync(fileno(logFile));
452 static void WriteLog(char *command, char *userId, char *item, int force)
454 fprintf(logFile, "%s %s %s\n", command, userId, item);
458 fsync(fileno(logFile));
462 static int RemoveOneInactiveCart(void);
463 static void MarkAllCartsInactive(void);
466 * Incremental garbage collection of inactive shopping carts:
468 * Each user access to a shopping cart clears its "inactive" bit via a
469 * call to MarkThisCartActive. When restart creates a cart it
470 * also marks the cart active.
472 * If purge == TRUE, each call to GarbageCollectStep scans for and removes
473 * the first inactive cart found. If there are no inactive carts,
474 * GarbageCollectStep marks *all* carts inactive, records the time in
475 * timeCartsMarked, and sets purge = FALSE.
477 * If purge == FALSE, each call to GarbageCollectStep checks the
478 * elapsed time since timeCartsMarked. If the elapsed time
479 * exceeds a threshold, GarbageCollectStep sets purge = TRUE.
482 static void GarbageCollectStep(void)
485 if(!RemoveOneInactiveCart()) {
486 MarkAllCartsInactive();
487 timeCartsMarked = time(NULL);
491 int diff = time(NULL)-timeCartsMarked;
492 if(diff > cartHoldSeconds) {
498 static int RemoveOneInactiveCart(void)
500 Tcl_HashSearch search;
501 Tcl_HashEntry *cartEntry;
504 cartEntry = Tcl_FirstHashEntry(cartTablePtr, &search);
505 for(cartEntry = Tcl_FirstHashEntry(cartTablePtr, &search);
506 cartEntry != NULL; cartEntry = Tcl_NextHashEntry(&search)) {
507 cart = Tcl_GetHashValue(cartEntry);
509 userId = Tcl_GetHashKey(cartTablePtr, cartEntry);
510 DoEmptyCart(userId, TRUE);
517 static Tcl_HashEntry *GetCartEntry(char *userId);
519 static void MarkAllCartsInactive(void)
521 Tcl_HashSearch search;
522 Tcl_HashEntry *cartEntry;
524 cartEntry = Tcl_FirstHashEntry(cartTablePtr, &search);
525 for(cartEntry = Tcl_FirstHashEntry(cartTablePtr, &search);
526 cartEntry != NULL; cartEntry = Tcl_NextHashEntry(&search)) {
527 cart = Tcl_GetHashValue(cartEntry);
528 cart->inactive = TRUE;
532 static void MarkThisCartActive(char *userId)
534 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
535 CartObj *cart = Tcl_GetHashValue(cartEntry);
536 cart->inactive = FALSE;
539 #define OP_DISPLAY_STORE "DisplayStore"
540 #define OP_ADD_ITEM "AddItemToCart"
541 #define OP_DISPLAY_CART "DisplayCart"
542 #define OP_REMOVE_ITEM "RemoveItemFromCart"
543 #define OP_PURCHASE "Purchase"
545 static void DisplayStore(
546 char *scriptName, char *parent, char *userId, char *processId);
547 static void AddItemToCart(
548 char *scriptName, char *parent, char *userId, char *processId,
550 static void DisplayCart(
551 char *scriptName, char *parent, char *userId, char *processId);
552 static void RemoveItemFromCart(
553 char *scriptName, char *parent, char *userId, char *processId,
555 static void Purchase(
556 char *scriptName, char *parent, char *userId, char *processId);
557 static void InvalidRequest(char *code, char *message);
558 static void Authorize(char *userId);
561 * As a Responder, this application expects to be called with the
562 * GET method and a URL of the form
564 * http://<host-port>/<script-name>?op=<op>&item=<item>
566 * The application expects the SI_UID variable to provide
567 * a user ID, either authenticated or anonymous.
569 * The application expects the directory *containing* <script-name>
570 * to contain various static HTML files related to the application.
572 * As an Authorizer, the application expects to be called with
573 * SID_UID and URL_PATH set.
576 static void PerformRequest(void)
578 char *method = getenv("REQUEST_METHOD");
579 char *role = getenv("FCGI_ROLE");
580 char *scriptName = PathTail(getenv("SCRIPT_NAME"));
582 char *op = QueryLookup(getenv("QUERY_STRING"), "op");
583 char *item = QueryLookup(getenv("QUERY_STRING"), "item");
584 char *userId = getenv("SI_UID");
586 InvalidRequest("405", "Incorrect configuration, no user id");
589 MarkThisCartActive(userId);
591 if(!strcmp(role, "RESPONDER")) {
592 if(strcmp(method, "GET")) {
593 InvalidRequest("405", "Only GET Method Allowed");
594 } else if(op == NULL || !strcmp(op, OP_DISPLAY_STORE)) {
595 DisplayStore(scriptName, parent, userId, fcgiProcessId);
596 } else if(!strcmp(op, OP_ADD_ITEM)) {
597 AddItemToCart(scriptName, parent, userId, fcgiProcessId, item);
598 } else if(!strcmp(op, OP_DISPLAY_CART)) {
599 DisplayCart(scriptName, parent, userId, fcgiProcessId);
600 } else if(!strcmp(op, OP_REMOVE_ITEM)) {
601 RemoveItemFromCart(scriptName, parent, userId, fcgiProcessId, item);
602 } else if(!strcmp(op, OP_PURCHASE)) {
603 Purchase(scriptName, parent, userId, fcgiProcessId);
605 InvalidRequest("404", "Invalid 'op' argument");
607 } else if(!strcmp(role, "AUTHORIZER")) {
610 InvalidRequest("404", "Invalid FastCGI Role");
619 * Tiny database of shop inventory. The first form is the
620 * item identifier used in a request, the second form is used
621 * for HTML display. REQUIRED_ITEM is the item required
622 * the the Authorizer. SPECIAL_ITEM is the item on the protected
623 * page (must follow unprotected items in table).
626 char *ItemNames[] = {
633 char *ItemDisplayNames[] = {
634 "<i>Brooklyn Bridge</i>",
635 "<i>RMS Titanic</i>",
636 "<i>Comet Kohoutec</i>",
637 "<i>Yellow Submarine</i>",
640 #define REQUIRED_ITEM 1
641 #define SPECIAL_ITEM 3
644 static char *ItemDisplayName(char *item)
650 for(i = 0; ItemNames[i] != NULL; i++) {
651 if(!strcmp(item, ItemNames[i])) {
652 return ItemDisplayNames[i];
658 static void DisplayNumberOfItems(int numberOfItems, char *processId);
660 static void DisplayHead(char *title, char *parent, char *gif)
662 printf("Content-type: text/html\r\n"
664 "<html>\n<head>\n<title>%s</title>\n</head>\n\n"
665 "<body bgcolor=\"ffffff\" text=\"000000\" link=\"39848c\"\n"
666 " vlink=\"808080\" alink=\"000000\">\n", title);
667 if(parent != NULL && gif != NULL) {
668 printf("<center>\n<img src=\"%s%s\" alt=\"[%s]\">\n</center>\n\n",
671 printf("<h2>%s</h2>\n<hr>\n\n", title);
675 static void DisplayFoot(void)
677 printf("<hr>\n</body>\n</html>\n");
680 static void DisplayStore(
681 char *scriptName, char *parent, char *userId, char *processId)
683 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
684 ListOfString *items = ((CartObj *) Tcl_GetHashValue(cartEntry))->items;
685 int numberOfItems = ListOfString_Length(items);
688 DisplayHead("FastCGI Shop!", parent, "Images/main-hd.gif");
689 DisplayNumberOfItems(numberOfItems, processId);
690 printf("<h3>Goods for sale:</h3>\n<ul>\n");
691 for(i = 0; i < SPECIAL_ITEM; i++) {
692 printf(" <li>Add the <a href=\"%s?op=AddItemToCart&item=%s\">%s</a>\n"
693 " to your shopping cart.\n",
694 scriptName, ItemNames[i], ItemDisplayNames[i]);
696 printf("</ul><p>\n\n");
697 printf("If the %s is in your shopping cart,\n"
698 "<a href=\"%sProtected/%s.html\">go see a special offer</a>\n"
699 "available only to %s purchasers.<p>\n\n",
700 ItemDisplayNames[REQUIRED_ITEM], parent,
701 ItemNames[REQUIRED_ITEM], ItemDisplayNames[REQUIRED_ITEM]);
702 printf("<a href=\"%sUnprotected/Purchase.html\">Purchase\n"
703 "the contents of your shopping cart.</a><p>\n\n", parent);
704 printf("<a href=\"%s?op=DisplayCart\">View the contents\n"
705 "of your shopping cart.</a><p>\n\n", scriptName);
709 static Tcl_HashEntry *GetCartEntry(char *userId)
711 Tcl_HashEntry *cartEntry = Tcl_FindHashEntry(cartTablePtr, userId);
713 if(cartEntry == NULL) {
714 CartObj *cart = Malloc(sizeof(CartObj));
715 cart->inactive = FALSE;
717 cartEntry = Tcl_CreateHashEntry(cartTablePtr, userId, &new);
719 Tcl_SetHashValue(cartEntry, cart);
724 static void AddItemToCart(
725 char *scriptName, char *parent, char *userId, char *processId,
728 if(DoAddItemToCart(userId, item, TRUE) < 0) {
729 InvalidRequest("404", "Invalid 'item' argument");
733 * DisplayStore(scriptName, parent, userId, processId);
734 * except for browser reload issue. Redirect instead.
736 printf("Location: %s?op=%s\r\n"
737 "\r\n", scriptName, OP_DISPLAY_STORE);
741 static int DoAddItemToCart(char *userId, char *item, int writeLog)
743 if(ItemDisplayName(item) == NULL) {
746 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
747 CartObj *cart = Tcl_GetHashValue(cartEntry);
748 cart->items = ListOfString_AppendElement(
749 cart->items, StringCopy(item));
751 WriteLog(LR_ADD_ITEM, userId, item, TRUE);
757 static void DisplayCart(
758 char *scriptName, char *parent, char *userId, char *processId)
760 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
761 CartObj *cart = Tcl_GetHashValue(cartEntry);
762 ListOfString *items = cart->items;
763 int numberOfItems = ListOfString_Length(items);
765 DisplayHead("Your shopping cart", parent, "Images/cart-hd.gif");
766 DisplayNumberOfItems(numberOfItems, processId);
768 for(; items != NULL; items = ListOfString_Tail(items)) {
769 char *item = ListOfString_Head(items);
770 printf(" <li>%s . . . . . \n"
771 " <a href=\"%s?op=RemoveItemFromCart&item=%s\">Click\n"
772 " to remove</a> from your shopping cart.\n",
773 ItemDisplayName(item), scriptName, item);
775 printf("</ul><p>\n\n");
776 printf("<a href=\"%sUnprotected/Purchase.html\">Purchase\n"
777 "the contents of your shopping cart.</a><p>\n\n", parent);
778 printf("<a href=\"%s?op=DisplayStore\">Return to shop.</a><p>\n\n",
783 static void RemoveItemFromCart(
784 char *scriptName, char *parent, char *userId, char *processId,
787 if(DoRemoveItemFromCart(userId, item, TRUE) < 0) {
788 InvalidRequest("404", "Invalid 'item' argument");
792 * DisplayCart(scriptName, parent, userId, processId);
793 * except for browser reload issue. Redirect instead.
795 printf("Location: %s?op=%s\r\n"
796 "\r\n", scriptName, OP_DISPLAY_CART);
800 static int DoRemoveItemFromCart(char *userId, char *item, int writeLog)
802 if(ItemDisplayName(item) == NULL) {
805 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
806 CartObj *cart = Tcl_GetHashValue(cartEntry);
807 if(ListOfString_IsElement(cart->items, item)) {
808 cart->items = ListOfString_RemoveElement(cart->items, item);
810 WriteLog(LR_REMOVE_ITEM, userId, item, TRUE);
817 static void Purchase(
818 char *scriptName, char *parent, char *userId, char *processId)
820 DoEmptyCart(userId, TRUE);
821 printf("Location: %sUnprotected/ThankYou.html\r\n"
825 static int DoEmptyCart(char *userId, int writeLog)
827 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
828 CartObj *cart = Tcl_GetHashValue(cartEntry);
829 ListOfString *items = cart->items;
831 * Write log *before* tearing down cart structure because userId
832 * is part of the structure. (Thanks, Purify.)
835 WriteLog(LR_EMPTY_CART, userId, "NullItem", TRUE);
837 while(items != NULL) {
838 items = ListOfString_RemoveElement(
839 items, ListOfString_Head(items));
842 Tcl_DeleteHashEntry(cartEntry);
846 static void NotAuthorized(void);
848 static void Authorize(char *userId)
850 Tcl_HashEntry *cartEntry = GetCartEntry(userId);
851 ListOfString *items = ((CartObj *) Tcl_GetHashValue(cartEntry))->items;
852 for( ; items != NULL; items = ListOfString_Tail(items)) {
853 if(!strcmp(ListOfString_Head(items), ItemNames[REQUIRED_ITEM])) {
854 printf("Status: 200 OK\r\n"
855 "Variable-Foo: Bar\r\n"
863 static void DisplayNumberOfItems(int numberOfItems, char *processId)
865 if(processId != NULL) {
866 printf("FastCGI process %s is serving you today.<br>\n", processId);
868 if(numberOfItems == 0) {
869 printf("Your shopping cart is empty.<p>\n\n");
870 } else if(numberOfItems == 1) {
871 printf("Your shopping cart contains 1 item.<p>\n\n");
873 printf("Your shopping cart contains %d items.<p>\n\n", numberOfItems);
877 static void InvalidRequest(char *code, char *message)
879 printf("Status: %s %s\r\n", code, message);
880 DisplayHead("Invalid request", NULL, NULL);
881 printf("%s.\n\n", message);
885 static void NotAuthorized(void)
887 printf("Status: 403 Forbidden\r\n");
888 DisplayHead("Access Denied", NULL, NULL);
889 printf("Put the %s in your cart to access this page.\n\n",
890 ItemDisplayNames[REQUIRED_ITEM]);
895 * Mundane utility functions, not specific to this application:
900 * Fail-fast version of 'malloc'
902 static void *Malloc(size_t size)
904 void *result = malloc(size);
905 assert(size == 0 || result != NULL);
910 * Protect against old, broken implementations of 'free'
912 static void Free(void *ptr)
920 * Return a new string created by calling Malloc, copying strLen
921 * characters from str to the new string, then appending a null.
923 static char *StringNCopy(char *str, int strLen)
925 char *newString = Malloc(strLen + 1);
926 memcpy(newString, str, strLen);
927 newString[strLen] = '\000';
932 * Return a new string that's a copy of str, including the null
934 static char *StringCopy(char *str)
936 return StringNCopy(str, strlen(str));
940 * Return a new string that's a copy of str1 followed by str2,
943 static char *StringCat(char *str1, char *str2)
945 return StringCat4(str1, str2, NULL, NULL);
948 static char *StringCat4(char *str1, char *str2, char *str3, char *str4)
950 int str1Len = Strlen(str1);
951 int str2Len = Strlen(str2);
952 int str3Len = Strlen(str3);
953 int str4Len = Strlen(str4);
954 char *newString = Malloc(str1Len + str2Len + str3Len + str4Len + 1);
955 memcpy(newString, str1, str1Len);
956 memcpy(newString + str1Len, str2, str2Len);
957 memcpy(newString + str1Len + str2Len, str3, str3Len);
958 memcpy(newString + str1Len + str2Len + str3Len, str4, str4Len);
959 newString[str1Len + str2Len + str3Len + str4Len] = '\000';
964 * Return a copy of the value associated with 'name' in 'query'.
965 * XXX: does not perform URL-decoding of query.
967 static char *QueryLookup(char *query, char *name)
969 int nameLen = strlen(name);
970 char *queryTail, *nameFirst, *valueFirst, *valueLast;
977 nameFirst = strstr(queryTail, name);
978 if(nameFirst == NULL) {
981 if(((nameFirst == query) || (nameFirst[-1] == '&')) &&
982 (nameFirst[nameLen] == '=')) {
983 valueFirst = nameFirst + nameLen + 1;
984 valueLast = strchr(valueFirst, '&');
985 if(valueLast == NULL) {
986 valueLast = strchr(valueFirst, '\000');
988 return StringNCopy(valueFirst, valueLast - valueFirst);
990 queryTail = nameFirst + 1;
995 * Return a copy of the characters following the final '/' character
998 static char *PathTail(char *path)
1000 char *afterSlash, *slash;
1005 while((slash = strchr(afterSlash, '/')) != NULL) {
1006 afterSlash = slash + 1;
1008 return StringCopy(afterSlash);
1012 * Return the integer value of the specified environment variable,
1013 * or a specified default value if the variable is unbound.
1015 static int IntGetEnv(char *varName, int defaultValue)
1017 char *strValue = getenv(varName);
1019 if(strValue != NULL) {
1020 value = strtol(strValue, NULL, 10);
1023 value = defaultValue;
1029 * Should the Tcl hash package detect an unrecoverable error(!), halt.
1031 void panic(char *format,
1032 char *arg1, char *arg2, char *arg3, char *arg4,
1033 char *arg5, char *arg6, char *arg7, char *arg8)
1040 * ListOfString abstraction
1043 static char *ListOfString_Head(ListOfString *list)
1048 static ListOfString *ListOfString_Tail(ListOfString *list)
1053 static int ListOfString_Length(ListOfString *list)
1056 for(; list != NULL; list = list->tail) {
1062 static int ListOfString_IsElement(ListOfString *list, char *element)
1064 for(; list != NULL; list = list->tail) {
1065 if(!strcmp(list->head, element)) {
1072 static ListOfString *ListOfString_AppendElement(
1073 ListOfString *list, char *element)
1076 ListOfString *newCell = Malloc(sizeof(ListOfString));
1077 newCell->head = element;
1078 newCell->tail = NULL;
1082 for(cur = list; cur->tail != NULL; cur = cur->tail) {
1084 cur->tail = newCell;
1089 static ListOfString *ListOfString_RemoveElement(
1090 ListOfString *list, char *element)
1093 ListOfString *prevCell = NULL;
1094 for(cur = list; cur != NULL; cur = cur->tail) {
1095 if(!strcmp(cur->head, element)) {
1096 if(prevCell == NULL) {
1099 prevCell->tail = cur->tail;