home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 May / APC452.ISO / netkit / xitami / xitami.exe / TESTCGI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-19  |  4.8 KB  |  151 lines

  1. /*  ----------------------------------------------------------------<Prolog>-
  2.     Name:       testcgi.c
  3.     Title:      CGI test program
  4.     Package:    Xitami web server
  5.  
  6.     Written:    96/10/01  Pieter Hintjens <ph@imatix.com>
  7.     Revised:    97/09/29  Pieter Hintjens <ph@imatix.com>
  8.  
  9.     Synopsis:   Generates an HTML test page containing the arguments passed
  10.                 to the CGI process.
  11.  
  12.                 To build this program you must download the SFL package from
  13.                 www.imatix.com.
  14.  
  15.     Copyright:  Copyright (c) 1998 iMatix
  16.     License:    This is free software; you can redistribute it and/or modify
  17.                 it under the terms of the XITAMI License Agreement as provided
  18.                 in the file LICENSE.TXT.  This software is distributed in
  19.                 the hope that it will be useful, but without any warranty.
  20.  ------------------------------------------------------------------</Prolog>-*/
  21.  
  22. #include "sfl.h"                        /*  SFL functions                    */
  23.  
  24. static Bool dump_symbol (SYMBOL *symbol, ...);
  25. static char *time_str   (void);
  26.  
  27. int
  28. main (int argc, char *argv [])
  29. {
  30.     SYMTAB
  31.         *symbols;
  32.     int
  33.         index;
  34.     char
  35.         *curdir;
  36.     size_t
  37.         content_length;
  38.  
  39.     printf ("Content-Type: text/html\n\n");
  40.     printf ("<HTML><HEAD><TITLE>CGI Test Program</TITLE></HEAD>\n");
  41.     printf ("<BODY>\n");
  42.     printf ("<H1>CGI Test Program</H1>\n");
  43.  
  44.     /*  Print argument variables, if any                                     */
  45.     if (argc > 1)
  46.       {
  47.         printf ("<H2>Arguments To Testcgi</H2>\n");
  48.         printf ("<PRE>\n");
  49.         for (index = 1; index < argc; index++)
  50.             printf ("<B>Argument %d </B>: %s\n", index, argv [index]);
  51.         printf ("</PRE>\n");
  52.       }
  53.  
  54.     /*  Print POST data variables coming from stdin, if present              */
  55.     content_length = (size_t) env_get_number ("HTTP_CONTENT_LENGTH", 0);
  56.     if (content_length)
  57.       {
  58.         symbols = sym_create_table ();
  59.         if (symbols)
  60.           {
  61.             if (cgi_parse_file_vars (symbols, stdin, "", content_length) > 0)
  62.               {
  63.                 printf ("<H2>POST Data Variables</H2>\n");
  64.                 printf ("<PRE>\n");
  65.                 sym_exec_all (symbols, dump_symbol);
  66.                 printf ("</PRE>\n");
  67.               }
  68.             sym_delete_table (symbols);
  69.           }
  70.       }
  71.  
  72.     /*  Print URL query variables coming from QUERY_STRING                   */
  73.     symbols = sym_create_table ();
  74.     if (symbols)
  75.       {
  76.         if (cgi_parse_query_vars (symbols,
  77.             env_get_string ("QUERY_STRING", ""), "") > 0)
  78.           {
  79.             printf ("<H2>QUERY_STRING Variables</H2>\n");
  80.             printf ("<PRE>\n");
  81.             sym_exec_all (symbols, dump_symbol);
  82.             printf ("</PRE>\n");
  83.           }
  84.         sym_delete_table (symbols);
  85.       }
  86.  
  87.     /*  Print environment variables                                          */
  88.     symbols = env2symb ();
  89.     if (symbols)
  90.       {
  91.         printf ("<H2>Environment Variables</H2>\n");
  92.         printf ("<PRE>\n");
  93.         sym_exec_all (symbols, dump_symbol);
  94.         sym_delete_table (symbols);
  95.         printf ("</PRE>\n");
  96.       }
  97.  
  98.     curdir = get_curdir ();
  99.     printf ("<H2>Miscellaneous Information</H2>\n");
  100.     printf ("<P>Working directory: %s\n", curdir);
  101.     printf ("<P>Current date and time: %s\n", time_str ());
  102.     printf ("</BODY></HTML>\n");
  103.  
  104.     mem_free (curdir);
  105.     mem_assert ();
  106.     return (EXIT_SUCCESS);
  107. }
  108.  
  109. /*  -------------------------------------------------------------------------
  110.  *  This function is invoked by sym_exec_all() to process each item in
  111.  *  the symbol table.  It always returns TRUE to keep sym_exec_all() going.
  112.  */
  113.  
  114. static Bool
  115. dump_symbol (SYMBOL *symbol, ...)
  116. {
  117.     printf ("<B>%-20s</B> = %s\n", symbol-> name, symbol-> value);
  118.     return (TRUE);
  119. }
  120.  
  121.  
  122. /*  -------------------------------------------------------------------------
  123.  *  time_str
  124.  *
  125.  *  Returns the current date and time formatted as: "yy/mm/dd hh:mm:ss".
  126.  *  The formatted time is in a static string that each call overwrites.
  127.  */
  128.  
  129. static char *
  130. time_str (void)
  131. {
  132.     static char
  133.         formatted_time [18];
  134.     time_t
  135.         time_secs;
  136.     struct tm
  137.         *time_struct;
  138.  
  139.     time_secs   = time (NULL);
  140.     time_struct = localtime (&time_secs);
  141.  
  142.     sprintf (formatted_time, "%2d/%02d/%02d %2d:%02d:%02d",
  143.                               time_struct-> tm_year,
  144.                               time_struct-> tm_mon + 1,
  145.                               time_struct-> tm_mday,
  146.                               time_struct-> tm_hour,
  147.                               time_struct-> tm_min,
  148.                               time_struct-> tm_sec);
  149.     return (formatted_time);
  150. }
  151.