1 /* Change this if the SERVER_NAME environment variable does not report
2 the true name of your web server. */
4 #define SERVER_NAME cgiServerName
7 #define SERVER_NAME "www.boutell.com"
10 /* You may need to change this, particularly under Windows;
11 it is a reasonable guess as to an acceptable place to
12 store a saved environment in order to test that feature.
13 If that feature is not important to you, you needn't
14 concern yourself with this. */
17 #define SAVED_ENVIRONMENT "c:\\cgicsave.env"
19 #define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
42 void LoadEnvironment();
43 void SaveEnvironment();
49 /* Load a previously saved CGI scenario if that button
51 if (cgiFormSubmitClicked("loadenvironment") == cgiFormSuccess) {
54 /* Set any new cookie requested. Must be done *before*
55 outputting the content type. */
57 /* Send the content type, letting the browser know this is HTML */
58 cgiHeaderContentType("text/html");
60 fprintf(cgiOut, "<HTML><HEAD>\n");
61 fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");
62 fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");
63 /* If a submit button has already been clicked, act on the
64 submission of the form. */
65 if ((cgiFormSubmitClicked("testcgic") == cgiFormSuccess) ||
66 cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess)
69 fprintf(cgiOut, "<hr>\n");
71 /* Now show the form */
73 /* Finish up the page */
74 fprintf(cgiOut, "</BODY></HTML>\n");
92 /* The saveenvironment button, in addition to submitting the form,
93 also saves the resulting CGI scenario to disk for later
94 replay with the 'load saved environment' button. */
95 if (cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess) {
102 cgiFormStringNoNewlines("name", name, 81);
103 fprintf(cgiOut, "Name: ");
105 fprintf(cgiOut, "<BR>\n");
110 cgiFormString("address", address, 241);
111 fprintf(cgiOut, "Address: <PRE>\n");
112 cgiHtmlEscape(address);
113 fprintf(cgiOut, "</PRE>\n");
117 if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) {
118 fprintf(cgiOut, "I'm Hungry!<BR>\n");
120 fprintf(cgiOut, "I'm Not Hungry!<BR>\n");
126 cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6);
127 fprintf(cgiOut, "My temperature is %f.<BR>\n", temperature);
132 cgiFormInteger("frogs", &frogsEaten, 0);
133 fprintf(cgiOut, "I have eaten %d frogs.<BR>\n", frogsEaten);
144 cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0);
145 fprintf(cgiOut, "I am: %s<BR>\n", colors[colorChoice]);
155 int flavorChoices[3];
159 result = cgiFormSelectMultiple("flavors", flavors, 3,
160 flavorChoices, &invalid);
161 if (result == cgiFormNotFound) {
162 fprintf(cgiOut, "I hate ice cream.<p>\n");
164 fprintf(cgiOut, "My favorite ice cream flavors are:\n");
165 fprintf(cgiOut, "<ul>\n");
166 for (i=0; (i < 3); i++) {
167 if (flavorChoices[i]) {
168 fprintf(cgiOut, "<li>%s\n", flavors[i]);
171 fprintf(cgiOut, "</ul>\n");
182 void RadioButtons() {
185 /* Approach #1: check for one of several valid responses.
186 Good if there are a short list of possible button values and
187 you wish to enumerate them. */
188 cgiFormRadio("age", ages, 4, &ageChoice, 0);
190 fprintf(cgiOut, "Age of Truck: %s (method #1)<BR>\n",
193 /* Approach #2: just get the string. Good
194 if the information is not critical or if you wish
195 to verify it in some other way. Note that if
196 the information is numeric, cgiFormInteger,
197 cgiFormDouble, and related functions may be
198 used instead of cgiFormString. */
199 cgiFormString("age", ageText, 10);
201 fprintf(cgiOut, "Age of Truck: %s (method #2)<BR>\n", ageText);
211 void NonExButtons() {
219 /* Method #1: check for valid votes. This is a good idea,
220 since votes for nonexistent candidates should probably
222 fprintf(cgiOut, "Votes (method 1):<BR>\n");
223 result = cgiFormCheckboxMultiple("vote", votes, 4,
224 voteChoices, &invalid);
225 if (result == cgiFormNotFound) {
226 fprintf(cgiOut, "I hate them all!<p>\n");
228 fprintf(cgiOut, "My preferred candidates are:\n");
229 fprintf(cgiOut, "<ul>\n");
230 for (i=0; (i < 4); i++) {
231 if (voteChoices[i]) {
232 fprintf(cgiOut, "<li>%s\n", votes[i]);
235 fprintf(cgiOut, "</ul>\n");
238 /* Method #2: get all the names voted for and trust them.
239 This is good if the form will change more often
240 than the code and invented responses are not a danger
241 or can be checked in some other way. */
242 fprintf(cgiOut, "Votes (method 2):<BR>\n");
243 result = cgiFormStringMultiple("vote", &responses);
244 if (result == cgiFormNotFound) {
245 fprintf(cgiOut, "I hate them all!<p>\n");
248 fprintf(cgiOut, "My preferred candidates are:\n");
249 fprintf(cgiOut, "<ul>\n");
250 while (responses[i]) {
251 fprintf(cgiOut, "<li>%s\n", responses[i]);
254 fprintf(cgiOut, "</ul>\n");
256 /* We must be sure to free the string array or a memory
257 leak will occur. Simply calling free() would free
258 the array but not the individual strings. The
259 function cgiStringArrayFree() does the job completely. */
260 cgiStringArrayFree(responses);
265 char **array, **arrayStep;
266 fprintf(cgiOut, "List of All Submitted Form Field Names:<p>\n");
267 if (cgiFormEntries(&array) != cgiFormSuccess) {
271 fprintf(cgiOut, "<ul>\n");
273 fprintf(cgiOut, "<li>");
274 cgiHtmlEscape(*arrayStep);
275 fprintf(cgiOut, "\n");
278 fprintf(cgiOut, "</ul>\n");
279 cgiStringArrayFree(array);
284 char **array, **arrayStep;
285 char cname[1024], cvalue[1024];
286 fprintf(cgiOut, "Cookies Submitted On This Call, With Values (Many Browsers NEVER Submit Cookies):<p>\n");
287 if (cgiCookies(&array) != cgiFormSuccess) {
291 fprintf(cgiOut, "<table border=1>\n");
292 fprintf(cgiOut, "<tr><th>Cookie<th>Value</tr>\n");
295 fprintf(cgiOut, "<tr>");
296 fprintf(cgiOut, "<td>");
297 cgiHtmlEscape(*arrayStep);
298 fprintf(cgiOut, "<td>");
299 cgiCookieString(*arrayStep, value, sizeof(value));
300 cgiHtmlEscape(value);
301 fprintf(cgiOut, "\n");
304 fprintf(cgiOut, "</table>\n");
305 cgiFormString("cname", cname, sizeof(cname));
306 cgiFormString("cvalue", cvalue, sizeof(cvalue));
308 fprintf(cgiOut, "New Cookie Set On This Call:<p>\n");
309 fprintf(cgiOut, "Name: ");
310 cgiHtmlEscape(cname);
311 fprintf(cgiOut, "Value: ");
312 cgiHtmlEscape(cvalue);
313 fprintf(cgiOut, "<p>\n");
314 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.<p>\n");
316 cgiStringArrayFree(array);
323 char contentType[1024];
327 if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {
328 printf("<p>No file was uploaded.<p>\n");
331 fprintf(cgiOut, "The filename submitted was: ");
333 fprintf(cgiOut, "<p>\n");
334 cgiFormFileSize("file", &size);
335 fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
336 cgiFormFileContentType("file", contentType, sizeof(contentType));
337 fprintf(cgiOut, "The alleged content type of the file was: ");
338 cgiHtmlEscape(contentType);
339 fprintf(cgiOut, "<p>\n");
340 fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.<p>\n");
341 fprintf(cgiOut, "The file's contents are shown here:<p>\n");
342 if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
343 fprintf(cgiOut, "Could not open the file.<p>\n");
346 fprintf(cgiOut, "<pre>\n");
347 while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
350 cgiHtmlEscapeData(buffer, got);
352 fprintf(cgiOut, "</pre>\n");
353 cgiFormFileClose(file);
358 fprintf(cgiOut, "<!-- 2.0: multipart/form-data is required for file uploads. -->");
359 fprintf(cgiOut, "<form method=\"POST\" enctype=\"multipart/form-data\" ");
360 fprintf(cgiOut, " action=\"");
361 cgiValueEscape(cgiScriptName);
362 fprintf(cgiOut, "\">\n");
363 fprintf(cgiOut, "<p>\n");
364 fprintf(cgiOut, "Text Field containing Plaintext\n");
365 fprintf(cgiOut, "<p>\n");
366 fprintf(cgiOut, "<input type=\"text\" name=\"name\">Your Name\n");
367 fprintf(cgiOut, "<p>\n");
368 fprintf(cgiOut, "Multiple-Line Text Field\n");
369 fprintf(cgiOut, "<p>\n");
370 fprintf(cgiOut, "<textarea NAME=\"address\" ROWS=4 COLS=40>\n");
371 fprintf(cgiOut, "Default contents go here. \n");
372 fprintf(cgiOut, "</textarea>\n");
373 fprintf(cgiOut, "<p>\n");
374 fprintf(cgiOut, "Checkbox\n");
375 fprintf(cgiOut, "<p>\n");
376 fprintf(cgiOut, "<input type=\"checkbox\" name=\"hungry\" checked>Hungry\n");
377 fprintf(cgiOut, "<p>\n");
378 fprintf(cgiOut, "Text Field containing a Numeric Value\n");
379 fprintf(cgiOut, "<p>\n");
380 fprintf(cgiOut, "<input type=\"text\" name=\"temperature\" value=\"98.6\">\n");
381 fprintf(cgiOut, "Blood Temperature (80.0-120.0)\n");
382 fprintf(cgiOut, "<p>\n");
383 fprintf(cgiOut, "Text Field containing an Integer Value\n");
384 fprintf(cgiOut, "<p>\n");
385 fprintf(cgiOut, "<input type=\"text\" name=\"frogs\" value=\"1\">\n");
386 fprintf(cgiOut, "Frogs Eaten\n");
387 fprintf(cgiOut, "<p>\n");
388 fprintf(cgiOut, "Single-SELECT\n");
389 fprintf(cgiOut, "<br>\n");
390 fprintf(cgiOut, "<select name=\"colors\">\n");
391 fprintf(cgiOut, "<option value=\"Red\">Red\n");
392 fprintf(cgiOut, "<option value=\"Green\">Green\n");
393 fprintf(cgiOut, "<option value=\"Blue\">Blue\n");
394 fprintf(cgiOut, "</select>\n");
395 fprintf(cgiOut, "<br>\n");
396 fprintf(cgiOut, "Multiple-SELECT\n");
397 fprintf(cgiOut, "<br>\n");
398 fprintf(cgiOut, "<select name=\"flavors\" multiple>\n");
399 fprintf(cgiOut, "<option value=\"pistachio\">Pistachio\n");
400 fprintf(cgiOut, "<option value=\"walnut\">Walnut\n");
401 fprintf(cgiOut, "<option value=\"creme\">Creme\n");
402 fprintf(cgiOut, "</select>\n");
403 fprintf(cgiOut, "<p>Exclusive Radio Button Group: Age of Truck in Years\n");
404 fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"1\">1\n");
405 fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"2\">2\n");
406 fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"3\" checked>3\n");
407 fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"4\">4\n");
408 fprintf(cgiOut, "<p>Nonexclusive Checkbox Group: Voting for Zero through Four Candidates\n");
409 fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"A\">A\n");
410 fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"B\">B\n");
411 fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"C\">C\n");
412 fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"D\">D\n");
413 fprintf(cgiOut, "<p>File Upload:\n");
414 fprintf(cgiOut, "<input type=\"file\" name=\"file\" value=\"\"> (Select A Local File)\n");
415 fprintf(cgiOut, "<p>\n");
416 fprintf(cgiOut, "<p>Set a Cookie<p>\n");
417 fprintf(cgiOut, "<input name=\"cname\" value=\"\"> Cookie Name\n");
418 fprintf(cgiOut, "<input name=\"cvalue\" value=\"\"> Cookie Value<p>\n");
419 fprintf(cgiOut, "<input type=\"submit\" name=\"testcgic\" value=\"Submit Request\">\n");
420 fprintf(cgiOut, "<input type=\"reset\" value=\"Reset Request\">\n");
421 fprintf(cgiOut, "<p>Save the CGI Environment<p>\n");
422 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).<p>\n");
423 fprintf(cgiOut, "<input type=\"submit\" name=\"saveenvironment\" value=\"Save Environment\">\n");
424 fprintf(cgiOut, "</form>\n");
431 /* Must set cookies BEFORE calling cgiHeaderContentType */
432 cgiFormString("cname", cname, sizeof(cname));
433 cgiFormString("cvalue", cvalue, sizeof(cvalue));
435 /* Cookie lives for one day (or until browser chooses
436 to get rid of it, which may be immediately),
437 and applies only to this script on this site. */
438 cgiHeaderCookieSetString(cname, cvalue,
439 86400, cgiScriptName, SERVER_NAME);
443 void LoadEnvironment()
445 if (cgiReadEnvironment(SAVED_ENVIRONMENT) !=
446 cgiEnvironmentSuccess)
448 cgiHeaderContentType("text/html");
449 fprintf(cgiOut, "<head>Error</head>\n");
450 fprintf(cgiOut, "<body><h1>Error</h1>\n");
451 fprintf(cgiOut, "cgiReadEnvironment failed. Most "
452 "likely you have not saved an environment "
456 /* OK, return now and show the results of the saved environment */
459 void SaveEnvironment()
461 if (cgiWriteEnvironment(SAVED_ENVIRONMENT) !=
462 cgiEnvironmentSuccess)
464 fprintf(cgiOut, "<p>cgiWriteEnvironment failed. Most "
465 "likely %s is not a valid path or is not "
466 "writable by the user that the CGI program "
467 "is running as.<p>\n", SAVED_ENVIRONMENT);
469 fprintf(cgiOut, "<p>Environment saved. Click this button "
470 "to restore it, playing back exactly the same "
472 "<form method=POST action=\"");
473 cgiValueEscape(cgiScriptName);
474 fprintf(cgiOut, "\">"
475 "<input type=\"submit\" "
476 "value=\"Load Environment\" "
477 "name=\"loadenvironment\"></form><p>\n");