2 cgitest.c - Testprogram for cgi.o
3 Copyright (c) 1998,9,2007,8 by Martin Schulze <joey@infodrom.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Compile with: cc -o cgitest cgitest.c -lcgi
29 #define HTML_CSS "<style type=\"text/css\">\n\
30 body { background-color: #afafff; }\n\
31 .formbox { border: 1px solid black; background: #dedede; padding: 2px; margin: 5px; }\n\
32 a:link, a:visited { text-decoration: none; }\n\
33 .footer { border-top: 1px solid black; font-size: 80%%; }\n\
36 #define HTML_HEAD "<html>\n<head><title>cgilib forms</title></head>\n<body>\n"
37 #define HTML_FOOT "<div class=\"footer\">\
38 <a href=\"http://www.infodrom.org/\">Infodrom Oldenburg</a> Project \
39 <a href=\"http://www.infodrom.org/projects/cgilib/\">cgilib</a>\n</div></body></html>"
41 #define HTML_FORMCONTENT "<table>\
42 <tr><td valign=\"top\">Input</td><td><input name=string size=50></td></tr>\n\
43 <tr><td valign=\"top\">Select</td><td><select name=select multiple>\n\
44 <option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select></td></tr>\n\
45 <tr><td valign=\"top\">Radio</td><td><input type=\"radio\" name=\"radio\" value=\"yes\"> yes \
46 <input type=\"radio\" name=\"radio\" value=\"no\"> no</td></tr>\n\
47 <tr><td valign=\"top\">Text</td><td><textarea name=text cols=50>\n</textarea></td></tr>\n"
49 #define HTML_SUBMIT "</table><center><input type=submit value=Submit> <input type=reset value=Reset></center>\n"
55 printf ("<h1>cgilib Test-Forms</h1>\n");
57 printf ("<div class=\"formbox\">");
58 printf ("<b>GET, display selected</b><br>\n");
59 printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=GET>\n");
60 printf (HTML_FORMCONTENT HTML_SUBMIT);
64 printf ("<div class=\"formbox\">");
65 printf ("<b>POST, display selected</b><br>\n");
66 printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=post>\n");
67 printf (HTML_FORMCONTENT HTML_SUBMIT);
71 printf ("<div class=\"formbox\">");
72 printf ("<b>POST, multipart, display selected</b><br>\n");
73 printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=POST enctype=\"multipart/form-data\">\n");
74 printf (HTML_FORMCONTENT HTML_SUBMIT);
78 printf ("<div class=\"formbox\">");
79 printf ("<b>GET, display everything</b><br>\n");
80 printf ("<form action=\"/cgi-bin/cgitest/listall\" method=GET>\n");
81 printf (HTML_FORMCONTENT HTML_SUBMIT);
85 printf ("<div class=\"formbox\">");
86 printf ("<b>POST, display everything</b><br>\n");
87 printf ("<form action=\"/cgi-bin/cgitest/listall\" method=POST>\n");
88 printf (HTML_FORMCONTENT HTML_SUBMIT);
92 printf ("<div class=\"formbox\">");
93 printf ("<b>POST, multipart, display everything</b><br>\n");
94 printf ("<form action=\"/cgi-bin/cgitest/listall\" method=post enctype=\"multipart/form-data\">\n");
95 printf (HTML_FORMCONTENT);
96 printf ("<tr><td>File</td><td><input name=file type=file></td></tr>");
101 printf ("<div class=\"formbox\">");
102 printf ("<b>Misc</b>\n");
103 printf ("<br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a>\n");
104 printf ("<br><a href=\"/cgi-bin/cgitest/listall?var=value&var2=val2;var3=val3\">List Everything</a>\n");
105 printf ("<br><a href=\"/cgi-bin/cgitest/setcookie\">Set Cookie</a>\n");
111 printf ("<h1>Results</h1>\n\n");
112 printf ("<div class=\"formbox\">");
113 printf ("<b>string</b>: %s<p>\n", cgiGetValue(cgi, "string"));
114 printf ("<b>radio</b>: %s<p>\n", cgiGetValue(cgi, "radio"));
115 printf ("<b>text</b>: %s<p>\n", cgiGetValue(cgi, "text"));
116 printf ("<b>select</b>: %s<p>\n", cgiGetValue(cgi, "select"));
120 void listall (char **env)
129 printf ("<h3>Environment Variables</h3>\n<pre>\n");
130 for (i=0; env[i]; i++) {
131 tmp = cgiEscape (env[i]);
133 printf ("%s\n", tmp);
138 printf ("</pre>\n<h3>CGI Variables</h3>\n<pre>\n");
140 vars = cgiGetVariables (cgi);
142 for (i=0; vars[i] != NULL; i++) {
143 val = cgiGetValue (cgi, vars[i]);
145 tmp = cgiEscape (val);
147 printf ("%s=%s\n", vars[i], tmp);
150 printf ("%s=...\n", vars[i]);
152 printf ("%s=\n", vars[i]);
156 printf ("No variables transmitted.\n");
158 printf ("</pre>\n<h3>Cookies</h3>\n<pre>\n");
160 vars = cgiGetCookies (cgi);
162 for (i=0; vars[i] != NULL; i++) {
163 cookie = cgiGetCookie (cgi, vars[i]);
165 tmp = cgiEscape (cookie->value);
167 printf ("%s=%s\n", vars[i], tmp);
170 printf ("%s=...\n", vars[i]);
172 printf ("%s=\n", vars[i]);
176 printf ("No cookies transmitted.\n");
178 printf ("</pre>\n<h3>Files</h3>\n<pre>\n");
180 vars = cgiGetFiles (cgi);
182 for (i=0; vars[i] != NULL; i++) {
183 file = cgiGetFile (cgi, vars[i]);
185 tmp = cgiEscape (file->filename);
186 printf ("%s=%s (%s", vars[i], tmp, file->tmpfile);
189 tmp = cgiEscape (file->type);
190 printf (", %s)\n", tmp);
198 printf ("No files transmitted.\n");
203 int main (int argc, char **argv, char **env)
205 char *path_info = NULL;
210 path_info = getenv("PATH_INFO");
212 if (!strcmp(path_info, "/redirect")) {
213 cgiRedirect("http://www.infodrom.org/");
215 } else if (!strcmp(path_info, "/setcookie")) {
216 cgiSetHeader ("Set-Cookie", "Version=1; Library=cgilib; Path=/");
218 printf (HTML_CSS HTML_HEAD);
219 printf ("<h3>Cookie Library set</h3>\n");
220 printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest\">Test</a><p>\n");
221 printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a><p>\n");
222 printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/listall\">List Everything</a><p>\n");
223 } else if (!strcmp(path_info, "/listall")) {
225 printf (HTML_CSS HTML_HEAD);
229 printf (HTML_CSS HTML_HEAD);
230 if (strlen (path_info))
231 printf ("path_info: %s<br>\n", path_info);
232 if (!strcmp(path_info, "/insertdata")) {
239 printf (HTML_CSS HTML_HEAD);