97db51a8eb217ad6c0c99fb68b74ecefc5b869cd
[infodrom/cgilib] / cgitest.c
1 /*
2     cgitest.c - Testprogram for cgi.o
3     Copyright (c) 1998,9,2007,8 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 #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\
34 </style>\n"
35
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>&nbsp;&nbsp;&nbsp;&nbsp;Project \
39 <a href=\"http://www.infodrom.org/projects/cgilib/\">cgilib</a>\n</div></body></html>"
40
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"
48
49 #define HTML_SUBMIT "</table><center><input type=submit value=Submit> <input type=reset value=Reset></center>\n"
50
51 s_cgi *cgi;
52
53 void print_form()
54 {
55     printf ("<h1>cgilib Test-Forms</h1>\n");
56
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);
61     printf ("</form>\n");
62     printf ("</div>");
63
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);
68     printf ("</form>\n");
69     printf ("</div>");
70
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);
75     printf ("</form>\n");
76     printf ("</div>");
77
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);
82     printf ("</form>\n");
83     printf ("</div>");
84
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);
89     printf ("</form>\n");
90     printf ("</div>");
91
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>");
97     printf (HTML_SUBMIT);
98     printf ("</form>\n");
99     printf ("</div>");
100
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");
106     printf ("</div>");
107 }
108
109 void eval_cgi()
110 {
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"));
117     printf ("</div>");
118 }
119
120 void listall (char **env)
121 {
122   char **vars;
123   char *val;
124   char *tmp;
125   s_cookie *cookie;
126   s_file *file;
127   int i;
128
129   printf ("<h3>Environment Variables</h3>\n<pre>\n");
130   for (i=0; env[i]; i++) {
131       tmp = cgiEscape (env[i]);
132       if (tmp) {
133           printf ("%s\n", tmp);
134           free (tmp);
135       }
136   }
137   
138   printf ("</pre>\n<h3>CGI Variables</h3>\n<pre>\n");
139
140   vars = cgiGetVariables (cgi);
141   if (vars) {
142       for (i=0; vars[i] != NULL; i++) {
143           val = cgiGetValue (cgi, vars[i]);
144           if (val) {
145               tmp = cgiEscape (val);
146               if (tmp) {
147                   printf ("%s=%s\n", vars[i], tmp);
148                   free (tmp);
149               } else
150                   printf ("%s=...\n", vars[i]);
151           } else
152               printf ("%s=\n", vars[i]);
153       }
154       cgiFreeList (vars);
155   } else
156       printf ("No variables transmitted.\n");
157
158   printf ("</pre>\n<h3>Cookies</h3>\n<pre>\n");
159
160   vars = cgiGetCookies (cgi);
161   if (vars) {
162       for (i=0; vars[i] != NULL; i++) {
163           cookie = cgiGetCookie (cgi, vars[i]);
164           if (cookie) {
165               tmp = cgiEscape (cookie->value);
166               if (tmp) {
167                   printf ("%s=%s\n", vars[i], tmp);
168                   free (tmp);
169               } else
170                   printf ("%s=...\n", vars[i]);
171           } else
172               printf ("%s=\n", vars[i]);
173       }
174       cgiFreeList (vars);
175   } else
176       printf ("No cookies transmitted.\n");
177
178   printf ("</pre>\n<h3>Files</h3>\n<pre>\n");
179
180   vars = cgiGetFiles (cgi);
181   if (vars) {
182       for (i=0; vars[i] != NULL; i++) {
183           file = cgiGetFile (cgi, vars[i]);
184           if (file) {
185               tmp = cgiEscape (file->filename);
186               printf ("%s=%s (%s", vars[i], tmp, file->tmpfile);
187               free (tmp);
188               if (file->type) {
189                   tmp = cgiEscape (file->type);
190                   printf (", %s)\n", tmp);
191                   free (tmp);
192               } else
193                   printf (")\n");
194           }
195       }
196       cgiFreeList (vars);
197   } else
198       printf ("No files transmitted.\n");
199
200   printf ("</pre>\n");
201 }
202
203 int main (int argc, char **argv, char **env)
204 {
205     char *path_info = NULL;
206
207     cgiDebug(0, 0);
208     cgi = cgiInit();
209
210     path_info = getenv("PATH_INFO");
211     if (path_info) {
212         if (!strcmp(path_info, "/redirect")) {
213             cgiRedirect("http://www.infodrom.org/");
214             exit (0);
215         } else if (!strcmp(path_info, "/setcookie")) {
216             cgiSetHeader ("Set-Cookie", "Version=1; Library=cgilib; Path=/");
217             cgiHeader();
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")) {
224             cgiHeader();
225             printf (HTML_CSS HTML_HEAD);
226             listall (env);
227         } else {
228             cgiHeader();
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")) {
233                 eval_cgi();
234             } else
235                 print_form();
236         }
237     } else {
238         cgiHeader();
239         printf (HTML_CSS HTML_HEAD);
240         print_form();
241     }
242
243     printf (HTML_FOOT);
244     cgiFree (cgi);
245     return 0;
246 }
247
248 /*
249  * Local variables:
250  *  c-indent-level: 4
251  *  c-basic-offset: 4
252  *  tab-width: 8
253  * End:
254  */