c2b8807438c66acd1af355362156dc6aad84c4c2
[infodrom/cgilib] / cgitest.c
1 /*
2     cgitest.c - Testprogram for cgi.o
3     Copyright (c) 1998,9,2007 by Martin Schulze <joey@infodrom.org>
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 <string.h>
27 #include <cgi.h>
28
29 s_cgi *cgi;
30
31 #define URL "http://www.infodrom.org/projects/cgilib/"
32
33 void print_form()
34 {
35     printf ("<h1>Test-Form</h1>\n");
36     printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=post>\n");
37     printf ("Input: <input name=string size=50>\n<br>");
38     printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
39     printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
40     printf ("<center><input type=submit value=Submit> ");
41     printf ("<input type=reset value=Reset></center>\n");
42     printf ("</form>\n");
43     printf ("<hr width=50%%><form action=\"/cgi-bin/cgitest/listall\" method=post>\n");
44     printf ("Input: <input name=string size=50>\n<br>");
45     printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
46     printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
47     printf ("<center><input type=submit value=Show> ");
48     printf ("<input type=reset value=Reset></center>\n");
49     printf ("</form>\n");
50     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a><p>\n");
51     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/listall?var=value&var2=val2;var3=val3\">List Everything</a><p>\n");
52     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/setcookie\">Set Cookie</a><p>\n");
53 }
54
55 void eval_cgi()
56 {
57     printf ("<h1>Results</h1>\n\n");
58     printf ("<b>string</b>: %s<p>\n", cgiGetValue(cgi, "string"));
59     printf ("<b>text</b>: %s<p>\n", cgiGetValue(cgi, "text"));
60     printf ("<b>select</b>: %s<p>\n", cgiGetValue(cgi, "select"));
61 }
62
63 void listall (char **env)
64 {
65   char **vars;
66   char *val;
67   char *tmp;
68   s_cookie *cookie;
69   int i;
70
71   printf ("<h3>Environment Variables</h3>\n<pre>\n");
72   for (i=0; env[i]; i++) {
73       tmp = cgiEscape (env[i]);
74       if (tmp) {
75           printf ("%s\n", tmp);
76           free (tmp);
77       }
78   }
79   
80   printf ("</pre>\n<h3>CGI Variables</h3>\n<pre>\n");
81
82   vars = cgiGetVariables (cgi);
83   if (vars) {
84       for (i=0; vars[i] != NULL; i++) {
85           val = cgiGetValue (cgi, vars[i]);
86           if (val) {
87               tmp = cgiEscape (val);
88               if (tmp) {
89                   printf ("%s=%s\n", vars[i], tmp);
90                   free (tmp);
91               } else
92                   printf ("%s=...\n", vars[i]);
93           } else
94               printf ("%s=\n", vars[i]);
95       }
96       for (i=0; vars[i] != NULL; i++)
97           free (vars[i]);
98   } else
99       printf ("No variables transmitted.\n");
100
101   printf ("</pre>\n<h3>Cookies</h3>\n<pre>\n");
102
103   vars = cgiGetCookies (cgi);
104   if (vars) {
105       for (i=0; vars[i] != NULL; i++) {
106           cookie = cgiGetCookie (cgi, vars[i]);
107           if (cookie) {
108               tmp = cgiEscape (cookie->value);
109               if (tmp) {
110                   printf ("%s=%s\n", vars[i], tmp);
111                   free (tmp);
112               } else
113                   printf ("%s=...\n", vars[i]);
114           } else
115               printf ("%s=\n", vars[i]);
116       }
117       for (i=0; vars[i] != NULL; i++)
118           free (vars[i]);
119   } else
120       printf ("No cookies transmitted.\n");
121
122   printf ("</pre>\n");
123 }
124
125 int main (int argc, char **argv, char **env)
126 {
127     char *path_info = NULL;
128
129     cgiDebug(0, 0);
130     cgi = cgiInit();
131
132     path_info = getenv("PATH_INFO");
133     if (path_info) {
134         if (!strcmp(path_info, "/redirect")) {
135             cgiRedirect("http://www.infodrom.org/");
136             exit (0);
137         } else if (!strcmp(path_info, "/setcookie")) {
138             cgiSetHeader ("Set-Cookie", "Version=1; Library=cgilib; Path=/");
139             cgiHeader();
140             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
141             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
142             printf ("<h3>Cookie Library set</h3>\n");
143             printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest\">Test</a><p>\n");
144             printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a><p>\n");
145             printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/listall\">List Everything</a><p>\n");
146         } else if (!strcmp(path_info, "/listall")) {
147             cgiHeader();
148             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
149             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
150             listall (env);
151         } else {
152             cgiHeader();
153             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
154             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
155             if (strlen (path_info))
156                 printf ("path_info: %s<br>\n", path_info);
157             if (!strcmp(path_info, "/insertdata")) {
158                 eval_cgi();
159             } else
160                 print_form();
161         }
162     } else {
163         cgiHeader();
164         printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
165         printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
166         print_form();
167     }
168
169     printf ("\n<hr>\n</body>\n</html>\n");
170     return 0;
171 }
172
173 /*
174  * Local variables:
175  *  c-indent-level: 4
176  *  c-basic-offset: 4
177  *  tab-width: 8
178  * End:
179  */