removed misplaced quotes
[infodrom/cgilib] / cgitest.c
1 /*
2     cgitest.c - Testprogram for cgi.o
3     Copyright (c) 1998,9 by Martin Schulze <joey@infodrom.north.de>
4
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.
9
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.
14
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
17     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
18  */
19
20 /*
21  * Compile with: cc -o cgitest cgitest.c -lcgi
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <cgi.h>
27
28 s_cgi *cgi;
29
30 #define URL "http://www.infodrom.north.de/cgilib/"
31
32 void print_form()
33 {
34     printf ("<h1>Test-Form</h1>\n");
35     printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=post>\n");
36     printf ("Input: <input name=string size=50>\n<br>");
37     printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
38     printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
39     printf ("<center><input type=submit value=Submit> ");
40     printf ("<input type=reset value=Reset></center>\n");
41     printf ("</form>\n");
42     printf ("<hr width=50%%><form action=\"/cgi-bin/cgitest/listall\" method=post>\n");
43     printf ("Input: <input name=string size=50>\n<br>");
44     printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
45     printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
46     printf ("<center><input type=submit value=Show> ");
47     printf ("<input type=reset value=Reset></center>\n");
48     printf ("</form>\n");
49     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a><p>\n");
50     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/listall\">List Everything</a><p>\n");
51     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/setcookie\">Set Cookie</a><p>\n");
52 }
53
54 void eval_cgi()
55 {
56     printf ("<h1>Results</h1>\n\n");
57     printf ("<b>string</b>: %s<p>\n", cgiGetValue(cgi, "string"));
58     printf ("<b>text</b>: %s<p>\n", cgiGetValue(cgi, "text"));
59     printf ("<b>select</b>: %s<p>\n", cgiGetValue(cgi, "select"));
60 }
61
62 void listall (char **env)
63 {
64   char **vars;
65   char *val;
66   s_cookie *cookie;
67   int i;
68
69   printf ("<h3>Environment Variables</h3>\n<pre>\n");
70   for (i=0; env[i]; i++)
71     printf ("%s\n", env[i]);
72   
73   printf ("</pre>\n<h3>CGI Variables</h3>\n<pre>\n");
74
75   vars = cgiGetVariables (cgi);
76   if (vars) {
77       for (i=0; vars[i] != NULL; i++) {
78           val = cgiGetValue (cgi, vars[i]);
79           printf ("%s=%s\n", vars[i], val?val:"");
80       }
81       for (i=0; vars[i] != NULL; i++)
82           free (vars[i]);
83   }
84
85   printf ("</pre>\n<h3>Cookies</h3>\n<pre>\n");
86
87   vars = cgiGetCookies (cgi);
88   if (vars) {
89       for (i=0; vars[i] != NULL; i++) {
90           cookie = cgiGetCookie (cgi, vars[i]);
91           printf ("%s=%s\n", vars[i], cookie?cookie->value:"");
92       }
93       for (i=0; vars[i] != NULL; i++)
94           free (vars[i]);
95   }
96   printf ("</pre>\n");
97 }
98
99 int main (int argc, char **argv, char **env)
100 {
101     char *path_info = NULL;
102
103     cgiDebug(0, 0);
104     cgi = cgiInit();
105
106     path_info = getenv("PATH_INFO");
107     if (path_info) {
108         if (!strcmp(path_info, "/redirect")) {
109             cgiRedirect("http://www.infodrom.north.de/");
110             exit (0);
111         } else if (!strcmp(path_info, "/setcookie")) {
112             cgiSetHeader ("Set-Cookie", "Version=1; Library=cgilib; Path=/");
113             cgiHeader();
114             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
115             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
116             printf ("<h3>Cookie Library set</h3>\n");
117             printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest\">Test</a><p>\n");
118             printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a><p>\n");
119             printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/listall\">List Everything</a><p>\n");
120         } else if (!strcmp(path_info, "/listall")) {
121             cgiHeader();
122             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
123             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
124             listall (env);
125         } else {
126             cgiHeader();
127             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
128             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
129             if (strlen (path_info))
130                 printf ("path_info: %s<br>\n", path_info);
131             if (!strcmp(path_info, "/insertdata")) {
132                 eval_cgi();
133             } else
134                 print_form();
135         }
136     } else {
137         cgiHeader();
138         printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
139         printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
140         print_form();
141     }
142
143     printf ("\n<hr>\n</body>\n</html>\n");
144     return 0;
145 }
146
147 /*
148  * Local variables:
149  *  c-indent-level: 4
150  *  c-basic-offset: 4
151  *  tab-width: 8
152  * End:
153  */