/* Change this if the SERVER_NAME environment variable does not report the true name of your web server. */ #if 1 #define SERVER_NAME cgiServerName #endif #if 0 #define SERVER_NAME "www.boutell.dev" #endif /* You may need to change this, particularly under Windows; it is a reasonable guess as to an acceptable place to store a saved environment in order to test that feature. If that feature is not important to you, you needn't concern yourself with this. */ #ifdef WIN32 #define SAVED_ENVIRONMENT "c:\\cgicsave.env" #else #define SAVED_ENVIRONMENT "/tmp/cgicsave.env" #endif /* WIN32 */ #include #include "cgic.h" #include #include void HandleSubmit(); void ShowForm(); void CookieSet(); void Name(); void Address(); void Hungry(); void Temperature(); void Frogs(); void Color(); void Flavors(); void NonExButtons(); void RadioButtons(); void File(); void Entries(); void Cookies(); void LoadEnvironment(); void SaveEnvironment(); int cgiMain() { #ifdef DEBUG LoadEnvironment(); #endif /* DEBUG */ /* Load a previously saved CGI scenario if that button has been pressed. */ if (cgiFormSubmitClicked("loadenvironment") == cgiFormSuccess) { LoadEnvironment(); } /* Set any new cookie requested. Must be done *before* outputting the content type. */ CookieSet(); /* Send the content type, letting the browser know this is HTML */ cgiHeaderContentType("text/html"); /* Top of the page */ fprintf(cgiOut, "\n"); fprintf(cgiOut, "cgic test\n"); fprintf(cgiOut, "

cgic test

\n"); /* If a submit button has already been clicked, act on the submission of the form. */ if ((cgiFormSubmitClicked("testcgic") == cgiFormSuccess) || cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess) { HandleSubmit(); fprintf(cgiOut, "
\n"); } /* Now show the form */ ShowForm(); /* Finish up the page */ fprintf(cgiOut, "\n"); return 0; } void HandleSubmit() { Name(); Address(); Hungry(); Temperature(); Frogs(); Color(); Flavors(); NonExButtons(); RadioButtons(); File(); Entries(); Cookies(); /* The saveenvironment button, in addition to submitting the form, also saves the resulting CGI scenario to disk for later replay with the 'load saved environment' button. */ if (cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess) { SaveEnvironment(); } } void Name() { char name[81]; cgiFormStringNoNewlines("name", name, 81); fprintf(cgiOut, "Name: "); cgiHtmlEscape(name); fprintf(cgiOut, "
\n"); } void Address() { char address[241]; cgiFormString("address", address, 241); fprintf(cgiOut, "Address:
\n");
	cgiHtmlEscape(address);
	fprintf(cgiOut, "
\n"); } void Hungry() { if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) { fprintf(cgiOut, "I'm Hungry!
\n"); } else { fprintf(cgiOut, "I'm Not Hungry!
\n"); } } void Temperature() { double temperature; cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6); fprintf(cgiOut, "My temperature is %f.
\n", temperature); } void Frogs() { int frogsEaten; cgiFormInteger("frogs", &frogsEaten, 0); fprintf(cgiOut, "I have eaten %d frogs.
\n", frogsEaten); } char *colors[] = { "Red", "Green", "Blue" }; void Color() { int colorChoice; cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0); fprintf(cgiOut, "I am: %s
\n", colors[colorChoice]); } char *flavors[] = { "pistachio", "walnut", "creme" }; void Flavors() { int flavorChoices[3]; int i; int result; int invalid; result = cgiFormSelectMultiple("flavors", flavors, 3, flavorChoices, &invalid); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate ice cream.

\n"); } else { fprintf(cgiOut, "My favorite ice cream flavors are:\n"); fprintf(cgiOut, "

    \n"); for (i=0; (i < 3); i++) { if (flavorChoices[i]) { fprintf(cgiOut, "
  • %s\n", flavors[i]); } } fprintf(cgiOut, "
\n"); } } char *ages[] = { "1", "2", "3", "4" }; void RadioButtons() { int ageChoice; char ageText[10]; /* Approach #1: check for one of several valid responses. Good if there are a short list of possible button values and you wish to enumerate them. */ cgiFormRadio("age", ages, 4, &ageChoice, 0); fprintf(cgiOut, "Age of Truck: %s (method #1)
\n", ages[ageChoice]); /* Approach #2: just get the string. Good if the information is not critical or if you wish to verify it in some other way. Note that if the information is numeric, cgiFormInteger, cgiFormDouble, and related functions may be used instead of cgiFormString. */ cgiFormString("age", ageText, 10); fprintf(cgiOut, "Age of Truck: %s (method #2)
\n", ageText); } char *votes[] = { "A", "B", "C", "D" }; void NonExButtons() { int voteChoices[4]; int i; int result; int invalid; char **responses; /* Method #1: check for valid votes. This is a good idea, since votes for nonexistent candidates should probably be discounted... */ fprintf(cgiOut, "Votes (method 1):
\n"); result = cgiFormCheckboxMultiple("vote", votes, 4, voteChoices, &invalid); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate them all!

\n"); } else { fprintf(cgiOut, "My preferred candidates are:\n"); fprintf(cgiOut, "

    \n"); for (i=0; (i < 4); i++) { if (voteChoices[i]) { fprintf(cgiOut, "
  • %s\n", votes[i]); } } fprintf(cgiOut, "
\n"); } /* Method #2: get all the names voted for and trust them. This is good if the form will change more often than the code and invented responses are not a danger or can be checked in some other way. */ fprintf(cgiOut, "Votes (method 2):
\n"); result = cgiFormStringMultiple("vote", &responses); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate them all!

\n"); } else { int i = 0; fprintf(cgiOut, "My preferred candidates are:\n"); fprintf(cgiOut, "

    \n"); while (responses[i]) { fprintf(cgiOut, "
  • %s\n", responses[i]); i++; } fprintf(cgiOut, "
\n"); } /* We must be sure to free the string array or a memory leak will occur. Simply calling free() would free the array but not the individual strings. The function cgiStringArrayFree() does the job completely. */ cgiStringArrayFree(responses); } void Entries() { char **array, **arrayStep; fprintf(cgiOut, "List of All Submitted Form Field Names:

\n"); if (cgiFormEntries(&array) != cgiFormSuccess) { return; } arrayStep = array; fprintf(cgiOut, "

    \n"); while (*arrayStep) { fprintf(cgiOut, "
  • "); cgiHtmlEscape(*arrayStep); fprintf(cgiOut, "\n"); arrayStep++; } fprintf(cgiOut, "
\n"); cgiStringArrayFree(array); } void Cookies() { char **array, **arrayStep; char cname[1024], cvalue[1024]; fprintf(cgiOut, "Cookies Submitted On This Call, With Values (Many Browsers NEVER Submit Cookies):

\n"); if (cgiCookies(&array) != cgiFormSuccess) { return; } arrayStep = array; fprintf(cgiOut, "\n"); fprintf(cgiOut, "\n"); while (*arrayStep) { char value[1024]; fprintf(cgiOut, ""); fprintf(cgiOut, "
CookieValue
"); cgiHtmlEscape(*arrayStep); fprintf(cgiOut, ""); cgiCookieString(*arrayStep, value, sizeof(value)); cgiHtmlEscape(value); fprintf(cgiOut, "\n"); arrayStep++; } fprintf(cgiOut, "
\n"); cgiFormString("cname", cname, sizeof(cname)); cgiFormString("cvalue", cvalue, sizeof(cvalue)); if (strlen(cname)) { fprintf(cgiOut, "New Cookie Set On This Call:

\n"); fprintf(cgiOut, "Name: "); cgiHtmlEscape(cname); fprintf(cgiOut, "Value: "); cgiHtmlEscape(cvalue); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "If your browser accepts cookies (many do not), this new cookie should appear in the above list the next time the form is submitted.

\n"); } cgiStringArrayFree(array); } void File() { cgiFilePtr file; char name[1024]; char contentType[1024]; char buffer[1024]; int size; int got; if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) { printf("

No file was uploaded.

\n"); return; } fprintf(cgiOut, "The filename submitted was: "); cgiHtmlEscape(name); fprintf(cgiOut, "

\n"); cgiFormFileSize("file", &size); fprintf(cgiOut, "The file size was: %d bytes

\n", size); cgiFormFileContentType("file", contentType, sizeof(contentType)); fprintf(cgiOut, "The alleged content type of the file was: "); cgiHtmlEscape(contentType); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.

\n"); fprintf(cgiOut, "The file's contents are shown here:

\n"); if (cgiFormFileOpen("file", &file) != cgiFormSuccess) { fprintf(cgiOut, "Could not open the file.

\n"); return; } fprintf(cgiOut, "

\n");
	while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
		cgiFormSuccess)
	{
		cgiHtmlEscapeData(buffer, got);
	}
	fprintf(cgiOut, "
\n"); cgiFormFileClose(file); } void ShowForm() { fprintf(cgiOut, ""); fprintf(cgiOut, "
\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Text Field containing Plaintext\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Your Name\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Multiple-Line Text Field\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Checkbox\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Hungry\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Text Field containing a Numeric Value\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "Blood Temperature (80.0-120.0)\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Text Field containing an Integer Value\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "Frogs Eaten\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "Single-SELECT\n"); fprintf(cgiOut, "
\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "
\n"); fprintf(cgiOut, "Multiple-SELECT\n"); fprintf(cgiOut, "
\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "

Exclusive Radio Button Group: Age of Truck in Years\n"); fprintf(cgiOut, "1\n"); fprintf(cgiOut, "2\n"); fprintf(cgiOut, "3\n"); fprintf(cgiOut, "4\n"); fprintf(cgiOut, "

Nonexclusive Checkbox Group: Voting for Zero through Four Candidates\n"); fprintf(cgiOut, "A\n"); fprintf(cgiOut, "B\n"); fprintf(cgiOut, "C\n"); fprintf(cgiOut, "D\n"); fprintf(cgiOut, "

File Upload:\n"); fprintf(cgiOut, " (Select A Local File)\n"); fprintf(cgiOut, "

\n"); fprintf(cgiOut, "

Set a Cookie

\n"); fprintf(cgiOut, " Cookie Name\n"); fprintf(cgiOut, " Cookie Value

\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "

Save the CGI Environment

\n"); fprintf(cgiOut, "Pressing this button will submit the form, then save the CGI environment so that it can be replayed later by calling cgiReadEnvironment (in a debugger, for instance).

\n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "

\n"); } void CookieSet() { char cname[1024]; char cvalue[1024]; /* Must set cookies BEFORE calling cgiHeaderContentType */ cgiFormString("cname", cname, sizeof(cname)); cgiFormString("cvalue", cvalue, sizeof(cvalue)); if (strlen(cname)) { /* Cookie lives for one day (or until browser chooses to get rid of it, which may be immediately), and applies only to this script on this site. */ cgiHeaderCookieSetString(cname, cvalue, 86400, cgiScriptName, SERVER_NAME); } } void LoadEnvironment() { if (cgiReadEnvironment(SAVED_ENVIRONMENT) != cgiEnvironmentSuccess) { cgiHeaderContentType("text/html"); fprintf(cgiOut, "Error\n"); fprintf(cgiOut, "

Error

\n"); fprintf(cgiOut, "cgiReadEnvironment failed. Most " "likely you have not saved an environment " "yet.\n"); exit(0); } /* OK, return now and show the results of the saved environment */ } void SaveEnvironment() { if (cgiWriteEnvironment(SAVED_ENVIRONMENT) != cgiEnvironmentSuccess) { fprintf(cgiOut, "

cgiWriteEnvironment failed. Most " "likely %s is not a valid path or is not " "writable by the user that the CGI program " "is running as.

\n", SAVED_ENVIRONMENT); } else { fprintf(cgiOut, "

Environment saved. Click this button " "to restore it, playing back exactly the same " "scenario: " "

" "

\n"); } }