/* cgitest.c - Testprogram for cgi.o Copyright (c) 1998,9,2007,8 by Martin Schulze 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* * Compile with: cc -o cgitest cgitest.c -lcgi */ #include #include #include #include #define HTML_CSS "\n" #define HTML_HEAD "\ncgilib forms\n\n" #define HTML_FOOT "
\ Infodrom Oldenburg    Project \ cgilib\n
" #define HTML_FORMCONTENT "\ \n\ \n\ \n\ \n" #define HTML_SUBMIT "
Input
Select
Radio yes \ no
Text
\n" s_cgi *cgi; void print_form() { printf ("

cgilib Test-Forms

\n"); printf ("
"); printf ("GET, display selected
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, display selected
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, multipart, display selected
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("GET, display everything
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, display everything
\n"); printf ("
\n"); printf (HTML_FORMCONTENT HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("POST, multipart, display everything
\n"); printf ("
\n"); printf (HTML_FORMCONTENT); printf ("File"); printf (HTML_SUBMIT); printf ("
\n"); printf ("
"); printf ("
"); printf ("Misc\n"); printf ("
Redirect\n"); printf ("
List Everything\n"); printf ("
Set Cookie\n"); printf ("
"); } void eval_cgi() { printf ("

Results

\n\n"); printf ("
"); printf ("string: %s

\n", cgiGetValue(cgi, "string")); printf ("radio: %s

\n", cgiGetValue(cgi, "radio")); printf ("text: %s

\n", cgiGetValue(cgi, "text")); printf ("select: %s

\n", cgiGetValue(cgi, "select")); printf ("

"); } void listall (char **env) { char **vars; char *val; char *tmp; s_cookie *cookie; s_file *file; int i; printf ("

Environment Variables

\n
\n");
  for (i=0; env[i]; i++) {
      tmp = cgiEscape (env[i]);
      if (tmp) {
	  printf ("%s\n", tmp);
	  free (tmp);
      }
  }
  
  printf ("
\n

CGI Variables

\n
\n");

  vars = cgiGetVariables (cgi);
  if (vars) {
      for (i=0; vars[i] != NULL; i++) {
	  val = cgiGetValue (cgi, vars[i]);
	  if (val) {
	      tmp = cgiEscape (val);
	      if (tmp) {
		  printf ("%s=%s\n", vars[i], tmp);
		  free (tmp);
	      } else
		  printf ("%s=...\n", vars[i]);
	  } else
	      printf ("%s=\n", vars[i]);
      }
      cgiFreeList (vars);
  } else
      printf ("No variables transmitted.\n");

  printf ("
\n

Cookies

\n
\n");

  vars = cgiGetCookies (cgi);
  if (vars) {
      for (i=0; vars[i] != NULL; i++) {
	  cookie = cgiGetCookie (cgi, vars[i]);
	  if (cookie) {
	      tmp = cgiEscape (cookie->value);
	      if (tmp) {
		  printf ("%s=%s\n", vars[i], tmp);
		  free (tmp);
	      } else
		  printf ("%s=...\n", vars[i]);
	  } else
	      printf ("%s=\n", vars[i]);
      }
      cgiFreeList (vars);
  } else
      printf ("No cookies transmitted.\n");

  printf ("
\n

Files

\n
\n");

  vars = cgiGetFiles (cgi);
  if (vars) {
      for (i=0; vars[i] != NULL; i++) {
	  file = cgiGetFile (cgi, vars[i]);
	  if (file) {
	      tmp = cgiEscape (file->filename);
	      printf ("%s=%s (%s", vars[i], tmp, file->tmpfile);
	      free (tmp);
	      if (file->type) {
		  tmp = cgiEscape (file->type);
		  printf (", %s)\n", tmp);
		  free (tmp);
	      } else
		  printf (")\n");
	  }
      }
      cgiFreeList (vars);
  } else
      printf ("No files transmitted.\n");

  printf ("
\n"); } int main (int argc, char **argv, char **env) { char *path_info = NULL; cgiDebug(0, 0); cgi = cgiInit(); path_info = getenv("PATH_INFO"); if (path_info) { if (!strcmp(path_info, "/redirect")) { cgiRedirect("http://www.infodrom.org/"); exit (0); } else if (!strcmp(path_info, "/setcookie")) { cgiSetHeader ("Set-Cookie", "Version=1; Library=cgilib; Path=/"); cgiHeader(); printf (HTML_CSS HTML_HEAD); printf ("

Cookie Library set

\n"); printf ("



Test

\n"); printf ("



Redirect

\n"); printf ("



List Everything

\n"); } else if (!strcmp(path_info, "/listall")) { cgiHeader(); printf (HTML_CSS HTML_HEAD); listall (env); } else { cgiHeader(); printf (HTML_CSS HTML_HEAD); if (strlen (path_info)) printf ("path_info: %s
\n", path_info); if (!strcmp(path_info, "/insertdata")) { eval_cgi(); } else print_form(); } } else { cgiHeader(); printf (HTML_CSS HTML_HEAD); print_form(); } printf (HTML_FOOT); cgiFree (cgi); return 0; } /* * Local variables: * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 8 * End: */