. Improved test suite
. Fixed problem with name being a prefix of another
. Also decode HTTP strings for multiple fields
+ . Added cgiSetType() and cgiSetHeader() with documentation
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
.\"
-.TH cgi 5 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgi 5 "14 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
cgi \- Common Gateway Interface
.SH "SEE ALSO"
.BR cgiDebug (3),
.BR cgiHeader (3),
+.BR cgiSetType (3),
+.BR cgiSetHeader (3),
.BR cgiGetValue (3),
.BR cgiGetVariables (3).
int cgiDebugLevel = 0;
int cgiDebugStderr = 1;
+char *cgiHeaderString = NULL;
+char *cgiType = NULL;
+
+int cgiSetHeader (char *name, char *value)
+{
+ char *cp, *vp, *pivot;
+ int len;
+
+ if (!name || !strlen (name) || !value || !strlen (value))
+ return 0;
+
+ for (cp=name;*cp && *cp!=' ' && *cp!='\n' && *cp!=':';cp++);
+ for (vp=value;*vp && *vp!='\n';vp++);
+
+ if (cgiHeaderString) {
+ len = (strlen (cgiHeaderString) + cp-name + vp-value + 4) * sizeof (char);
+ if ((cgiHeaderString = (char *)realloc (cgiHeaderString,len)) == NULL)
+ return 0;
+ pivot = cgiHeaderString;
+ pivot += strlen (cgiHeaderString);
+ strncpy (pivot, name, cp-name);
+ pivot[cp-name] = ':';
+ pivot[cp-name+1] = ' ';
+ pivot[cp-name+2] = '\0';
+ strncat (pivot, value, vp-value);
+ pivot[cp-name+2+vp-value] = '\n';
+ } else {
+ len = (cp-name + vp-value + 4) * sizeof (char);
+ if ((cgiHeaderString = (char *)malloc (len)) == NULL)
+ return 0;
+ strncpy (cgiHeaderString, name, cp-name);
+ cgiHeaderString[cp-name] = ':';
+ cgiHeaderString[cp-name+1] = ' ';
+ cgiHeaderString[cp-name+2] = '\0';
+ strncat (cgiHeaderString, value, vp-value);
+ cgiHeaderString[cp-name+2+vp-value] = '\n';
+ }
+ return 1;
+}
+
+int cgiSetType (char *type)
+{
+ int len;
+ char *cp;
+
+ if (!type || !strlen (type))
+ return 0;
+ if (cgiType)
+ free (cgiType);
+
+ for (cp=type;*cp && *cp!='\n';cp++);
+
+ len = (cp-type+1) * sizeof (char);
+ if ((cgiType = (char *)malloc (len+20)) == NULL)
+ return 0;
+ memset (cgiType, 0, len);
+ strncpy (cgiType, type, cp-type);
+
+ return 1;
+}
void cgiHeader ()
{
- printf ("Content-type: text/html\n\n");
+ if (cgiType)
+ printf ("Content-type: %s\n", cgiType);
+ else
+ printf ("Content-type: text/html\n");
+ if (cgiHeaderString)
+ printf ("%s", cgiHeaderString);
+ printf ("\n");
}
void cgiDebug (int level, int where)
*value;
} s_cgi;
+/* cgiSetHeader
+ *
+ * Sets additional HTTP header lines to be printed with cgiHeader
+ */
+int cgiSetHeader (char *name, char *value);
+
+/* cgiSetType
+ *
+ * Sets result type for HTTP
+ */
+int cgiSetType (char *type);
+
/* cgiHeader
*
- * returns a valid CGI Header (Content-type...)
+ * Prints a valid CGI Header (Content-type...) etc.
*/
void cgiHeader ();
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
.\"
-.TH cgiDebug 3 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgiDebug 3 "14 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
-cgiDebug \- Set the debug level for cgi programming
+cgiDebug \- Set the debug level for CGI programming
.SH SYNOPSYS
.nf
.B #include <cgi.h>
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
.\"
-.TH cgiGetValue 3 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgiGetValue 3 "14 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
cgiGetValue \- Return the value of a CGI variable
.SH SYNOPSYS
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
.\"
-.TH cgiInit 3 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgiGetVariables 3 "14 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
cgiGetVariables \- Returns a list of CGI variables
.SH SYNOPSYS
-.\" cgiHeader - Write the cgi-bin header
+.\" cgiHeader - Print the HTTP header
.\" Copyright (c) 1998,9 by Martin Schulze <joey@infodrom.north.de>
.\"
.\" This program is free software; you can redistribute it and/or modify
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
.\"
-.TH cgiInit 3 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgiHeader 3 "15 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
-cgiHeader \- Write the cgi-bin header
+cgiHeader \- Print the HTTP header
.SH SYNOPSYS
.nf
.B #include <cgi.h>
.fi
.SH DESCRIPTION
This routine just prints out the Contents-type: line to make the web
-server happy.
+server happy. By default the CGI library assumes that you return
+regular HTML code and uses "text/html" as type. If your CGI program
+returns something else, you may set the type with
+.BR cgiSetType (3).
+You may also add additional header lines by using
+.BR cgiSetHeader (3).
.SH "RETURN VALUE"
This routine does not return a value.
.SH "SEE ALSO"
.BR cgiDebug (3),
+.BR cgiSetHeader (3),
+.BR cgiSetType (3),
.BR cgiInit (3),
.BR cgiGetValue (3).
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
.\"
-.TH cgiInit 3 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgiInit 3 "14 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
cgiInit \- Initializes CGI library
.SH SYNOPSYS
.\" 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 "14 August 1999" "Debian GNU/Linux" "Programmer's Manual"
+.TH cgiRedirect 3 "14 August 1999" "CGI Library" "Programmer's Manual"
.SH NAME
cgiRedirect \- Redirect the browser somewhere else
.SH SYNOPSYS
--- /dev/null
+.\" cgiSetHeader - Specify an additional HTTP header
+.\" Copyright (c) 1999 by 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 cgiSetHeader 3 "15 August 1999" "CGI Library" "Programmer's Manual"
+.SH NAME
+cgiSetHeader \- Specify an additional HTTP header
+.SH SYNOPSYS
+.nf
+.B #include <cgi.h>
+.sp
+.BI "int cgiSetHeader (char *" name ", char *" value );
+.fi
+.SH DESCRIPTION
+With the
+.B cgiSetHeader
+routine you may specify additional HTTP header lines such as Expires:
+or Pragma:. The order of header lines withing the HTTP header has no
+significance. You may call this routine multiple times to set
+multiple headers. They will be printed in the same order as
+specified, however, after the Content-type: header.
+
+This routine will only add the header to the internal stack, not print
+it. They will be printed by
+.BR cgiHeader (3).
+
+This routine does some syntax checking before accepting a new header.
+The
+.I name
+of a header must not contain any newline, space or colon, otherwise it
+will be truncated. The
+.I value
+must not contain any newline, otherwise it will be truncated as well.
+
+.SH "RETURN VALUE"
+On success 1 is returned, otherwise 0.
+
+.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 cgiHeader (3),
+.BR cgiInit (3).
--- /dev/null
+.\" cgiSetType - Specify the result type
+.\" Copyright (c) 1998,9 by 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 cgiSetType 3 "15 August 1999" "CGI Library" "Programmer's Manual"
+.SH NAME
+cgiSetType \- Specify the result type
+.SH SYNOPSYS
+.nf
+.B #include <cgi.h>
+.sp
+.BI "int cgiSetType (char *" type );
+.fi
+.SH DESCRIPTION
+This routine sets the result type to
+.IR type .
+This routine may be used optional. Default is "text/html". It will
+be used by
+.BR cgiHeader (3).
+
+.SH "RETURN VALUE"
+On success 1 is returned, otherwise 0.
+
+.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 cgiHeader (3),
+.BR cgiSetHeader (3),
+.BR cgiInit (3).
if (!strcmp(path_info, "/redirect")) {
cgiRedirect("http://www.infodrom.north.de/");
exit (0);
+ } else if (!strcmp(path_info, "/test")) {
+ cgiSetType ("text/tex");
+ cgiSetHeader ("Expires", "tomorrow");
+ cgiHeader();
+ exit (0);
} else if (!strcmp(path_info, "/listall")) {
cgiHeader();
printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");