s_cgi **cgiInit ()
{
int length;
- char *line;
+ char *line = NULL;
int numargs;
- char *cp, *ip, *esp;
+ char *cp, *ip, *esp, *sptr;
s_cgi **result;
- int i;
+ int i, k;
char tmp[101];
- line = getenv("CONTENT_LENGTH");
+ cp = getenv("REQUEST_METHOD");
+ ip = getenv("CONTENT_LENGTH");
- if (line){
- length = atoi(line);
- if ((line = (char *)malloc (length+2)) == NULL)
+ if (cp && !strcmp(cp, "POST")) {
+ if (ip) {
+ length = atoi(ip);
+ if ((line = (char *)malloc (length+2)) == NULL)
+ return NULL;
+ fgets(line, length+1, stdin);
+ } else
return NULL;
- fgets(line, length+1, stdin);
- } else {
- if (!getenv("REQUEST_METHOD")) {
- length = 0;
- printf ("(offline mode: enter name=value pairs on standard input)\n");
- for (cp = fgets(tmp, 100, stdin); cp != NULL;
- cp = fgets(tmp, 100, stdin) ) {
- if (strlen(tmp)) {
- length += strlen(tmp);
- if ((ip = (char *)malloc (length * sizeof(char))) == NULL)
- return NULL;
- bzero(ip, length);
- if (line) {
- if (line[strlen(line)-1] == '\n')
- line[strlen(line)-1] = '&';
- strcpy(ip, line);
- }
- ip = strcat(ip, tmp);
- if (line)
- free (line);
- line = ip;
- }
- } /* for */
- if (line[strlen(line)-1] == '\n')
- line[strlen(line)-1] = '\0';
+ } else if (cp && !strcmp(cp, "GET")) {
+ esp = getenv("QUERY_STRING");
+ if (esp && strlen(esp)) {
+ if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
+ return NULL;
+ sprintf (line, "%s", esp);
} else
return NULL;
+ } else {
+ length = 0;
+ printf ("(offline mode: enter name=value pairs on standard input)\n");
+ for (cp = fgets(tmp, 100, stdin); cp != NULL;
+ cp = fgets(tmp, 100, stdin) ) {
+ if (strlen(tmp)) {
+ length += strlen(tmp);
+ if ((ip = (char *)malloc (length * sizeof(char))) == NULL)
+ return NULL;
+ bzero(ip, length);
+ if (line) {
+ if (line[strlen(line)-1] == '\n')
+ line[strlen(line)-1] = '&';
+ strcpy(ip, line);
+ }
+ ip = strcat(ip, tmp);
+ if (line)
+ free (line);
+ line = ip;
+ }
+ }
+ if (line[strlen(line)-1] == '\n')
+ line[strlen(line)-1] = '\0';
}
- /* line now contains all values stored like foo=bar&foobar=barfoo&foofoo= */
+
+ /*
+ * From now on all cgi variables are stored in the variable line
+ * and look like foo=bar&foobar=barfoo&foofoo=
+ */
if (cgiDebugLevel > 0)
if (cgiDebugStderr)
}
if (i<numargs) {
- if ((result[i] = (s_cgi *)malloc(sizeof(s_cgi))) == NULL)
- return NULL;
- if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
- return NULL;
- bzero (result[i]->name, esp-cp+1);
- strncpy(result[i]->name, cp, esp-cp);
- cp = ++esp;
- if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
- return NULL;
- bzero (result[i]->value, ip-esp+1);
- strncpy(result[i]->value, cp, ip-esp);
- result[i]->value = cgiDecodeString(result[i]->value);
- if (cgiDebugLevel) {
- if (cgiDebugStderr)
- fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
- else
- printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
+
+ for (k=0; k<i && (strncmp(result[k]->name,cp, esp-cp)); k++);
+ /* try to find out if there's already such a variable */
+ if (k == i) { /* No such variable yet */
+ if ((result[i] = (s_cgi *)malloc(sizeof(s_cgi))) == NULL)
+ return NULL;
+ if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
+ return NULL;
+ bzero (result[i]->name, esp-cp+1);
+ strncpy(result[i]->name, cp, esp-cp);
+ cp = ++esp;
+ if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
+ return NULL;
+ bzero (result[i]->value, ip-esp+1);
+ strncpy(result[i]->value, cp, ip-esp);
+ result[i]->value = cgiDecodeString(result[i]->value);
+ if (cgiDebugLevel) {
+ if (cgiDebugStderr)
+ fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
+ else
+ printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
+ }
+ i++;
+ } else { /* There is already such a name, suppose a mutiple field */
+ if ((sptr = (char *)malloc((strlen(result[k]->value)+(ip-esp)+2)* sizeof(char))) == NULL)
+ return NULL;
+ bzero (sptr, strlen(result[k]->value)+(ip-esp)+2);
+ sprintf (sptr, "%s\n", result[k]->value);
+ cp = ++esp;
+ strncat(sptr, cp, ip-esp);
+ free(result[k]->value);
+ result[k]->value = sptr;
}
- i++;
}
cp = ++ip;
}
if (parms)
for (i=0;parms[i]; i++)
if (!strcmp(var,parms[i]->name)) {
- if (cgiDebug)
+ if (cgiDebugLevel > 0)
if (cgiDebugStderr)
fprintf (stderr, "%s found as %s\n", var, parms[i]->value);
else
printf ("%s found as %s<br>\n", var, parms[i]->value);
return parms[i]->value;
}
- if (cgiDebug)
+ if (cgiDebugLevel)
if (cgiDebugStderr)
fprintf (stderr, "%s not found\n", var);
else
return NULL;
}
+void cgiRedirect (const char *url)
+{
+ if (url && strlen(url)) {
+ printf ("Content-type: text/html\nContent-length: %d\n", 78+(strlen(url)*2));
+ printf ("Status: 302 Temporal Relocation\n");
+ printf ("Location: %s\n\n", url);
+ printf ("<html>\n<body>\nThe page has been moved to <a href\"%s\">%s</a>\n</body>\n</html>\n", url, url);
+ }
+}
+
+
/*
* Local variables:
* c-indent-level: 4
--- /dev/null
+.\" cgiRedirect - Redirect the browser somewhere else
+.\" Copyright (c) 1998 Martin Schulze <joey@infodrom.north.de>
+.\"
+.\" This program is free software; you can redistribute it and/or modify
+.\" it under the terms of the GNU General Public License as published by
+.\" the Free Software Foundation; either version 2 of the License, or
+.\" (at your option) any later version.
+.\"
+.\" This program is distributed in the hope that it will be useful,
+.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+.\" GNU General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License
+.\" along with this program; if not, write to the Free Software
+.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+.\"
+.TH cgiRedirect 3 "17 February 1998" "Debian GNU/Linux" "Programmer's Manual"
+.SH NAME
+cgiRedirect \- Redirect the browser somewhere else
+.SH SYNOPSYS
+.nf
+.B #include <cgi.h>
+.sp
+.BI "void cgiRedirect(char *" url );
+.fi
+.SH DESCRIPTION
+The
+.B cgiRedirect
+routine redirects the browser to another
+.IR url .
+This mechanism may be useful to redirect invalid requests to some
+static pages describing the policy.
+.SH "RETURN VALUE"
+.BR cgiRedirect ()
+does not return a value.
+
+.SH "AUTHOR"
+This cgi library is written by Martin Schulze
+<joey@infodrom.north.de>. If you have additions or improvements
+please get in touch with him.
+
+.SH "SEE ALSO"
+.BR cgiInit (3),
+.BR cgiHeader (3).
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
+/*
+ * Compile with: cc -o cgitest cgitest.c -lcgi
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <cgi.h>
printf ("<h1>Test-Form</h1>\n");
printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=post>\n");
printf ("Input: <input name=string size=50>\n<br>");
+ printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
printf ("<center><input type=submit value=Submit> ");
printf ("<input type=reset value=Reset></center>\n");
printf ("<h1>Results</h1>\n\n");
printf ("<b>string</b>: %s<p>\n", cgiGetValue(cgi, "string"));
printf ("<b>text</b>: %s<p>\n", cgiGetValue(cgi, "text"));
+ printf ("<b>select</b>: %s<p>\n", cgiGetValue(cgi, "select"));
}
{
char *path_info = NULL;
- cgiDebug(1, 0);
+ cgiDebug(0, 0);
cgi = cgiInit();
- cgiHeader();
- printf ("<html>\n<head><title>cgilib</title></title>\n\n<body>\n");
- printf ("<h1>cgilib</h1>\n");
path_info = getenv("PATH_INFO");
- printf ("path_info: %s<br>\n", path_info);
if (path_info) {
- if (!strcmp(path_info, "/insertdata")) {
- eval_cgi();
- } else
- print_form();
- } else
+ if (!strcmp(path_info, "/redirect")) {
+ cgiRedirect("http://www.infodrom.north.de/");
+ exit (0);
+ } else {
+ cgiHeader();
+ printf ("<html>\n<head><title>cgilib</title></title>\n\n<body>\n");
+ printf ("<h1>cgilib</h1>\n");
+ printf ("path_info: %s<br>\n", path_info);
+ if (!strcmp(path_info, "/insertdata")) {
+ eval_cgi();
+ } else
+ print_form();
+ }
+ } else {
+ cgiHeader();
+ printf ("<html>\n<head><title>cgilib</title></title>\n\n<body>\n");
+ printf ("<h1>cgilib</h1>\n");
print_form();
+ }
printf ("\n<hr>\n</body>\n</html>\n");
}