/* ----------------------------------------------------------------- Name: testcgi.c Title: CGI test program Package: Xitami web server Written: 96/10/01 Pieter Hintjens Revised: 97/09/29 Pieter Hintjens Synopsis: Generates an HTML test page containing the arguments passed to the CGI process. To build this program you must download the SFL package from www.imatix.com. Copyright: Copyright (c) 1998 iMatix License: This is free software; you can redistribute it and/or modify it under the terms of the XITAMI License Agreement as provided in the file LICENSE.TXT. This software is distributed in the hope that it will be useful, but without any warranty. -------------------------------------------------------------------*/ #include "sfl.h" /* SFL functions */ static Bool dump_symbol (SYMBOL *symbol, ...); static char *time_str (void); int main (int argc, char *argv []) { SYMTAB *symbols; int index; char *curdir; size_t content_length; printf ("Content-Type: text/html\n\n"); printf ("CGI Test Program\n"); printf ("\n"); printf ("

CGI Test Program

\n"); /* Print argument variables, if any */ if (argc > 1) { printf ("

Arguments To Testcgi

\n"); printf ("
\n");
        for (index = 1; index < argc; index++)
            printf ("Argument %d : %s\n", index, argv [index]);
        printf ("
\n"); } /* Print POST data variables coming from stdin, if present */ content_length = (size_t) env_get_number ("HTTP_CONTENT_LENGTH", 0); if (content_length) { symbols = sym_create_table (); if (symbols) { if (cgi_parse_file_vars (symbols, stdin, "", content_length) > 0) { printf ("

POST Data Variables

\n"); printf ("
\n");
                sym_exec_all (symbols, dump_symbol);
                printf ("
\n"); } sym_delete_table (symbols); } } /* Print URL query variables coming from QUERY_STRING */ symbols = sym_create_table (); if (symbols) { if (cgi_parse_query_vars (symbols, env_get_string ("QUERY_STRING", ""), "") > 0) { printf ("

QUERY_STRING Variables

\n"); printf ("
\n");
            sym_exec_all (symbols, dump_symbol);
            printf ("
\n"); } sym_delete_table (symbols); } /* Print environment variables */ symbols = env2symb (); if (symbols) { printf ("

Environment Variables

\n"); printf ("
\n");
        sym_exec_all (symbols, dump_symbol);
        sym_delete_table (symbols);
        printf ("
\n"); } curdir = get_curdir (); printf ("

Miscellaneous Information

\n"); printf ("

Working directory: %s\n", curdir); printf ("

Current date and time: %s\n", time_str ()); printf ("\n"); mem_free (curdir); mem_assert (); return (EXIT_SUCCESS); } /* ------------------------------------------------------------------------- * This function is invoked by sym_exec_all() to process each item in * the symbol table. It always returns TRUE to keep sym_exec_all() going. */ static Bool dump_symbol (SYMBOL *symbol, ...) { printf ("%-20s = %s\n", symbol-> name, symbol-> value); return (TRUE); } /* ------------------------------------------------------------------------- * time_str * * Returns the current date and time formatted as: "yy/mm/dd hh:mm:ss". * The formatted time is in a static string that each call overwrites. */ static char * time_str (void) { static char formatted_time [18]; time_t time_secs; struct tm *time_struct; time_secs = time (NULL); time_struct = localtime (&time_secs); sprintf (formatted_time, "%2d/%02d/%02d %2d:%02d:%02d", time_struct-> tm_year + 1900, time_struct-> tm_mon + 1, time_struct-> tm_mday, time_struct-> tm_hour, time_struct-> tm_min, time_struct-> tm_sec); return (formatted_time); }