/*
** Generate a test pages of HTML.
**
** To disable these tests change the WEBPAGE in the comment headers
** to WEB*PAGE and comment out the code.
*/
#include "config.h"
#include "test.h"

/*
** Generate an output page that contains information about the
** current environment.
*/
void test_cgi_vardump(void){
  int i;
  extern char **environ;
  char zPwd[1000];
  getcwd(zPwd, sizeof(zPwd));
  cgi_printf("<html>\n"
         "<head><title>Test Page</title></head>\n"
         "<body bgcolor=\"white\">\n"
         "<h1>Test Page</h1>\n");
  cgi_print_all();
  cgi_printf("<h2>Environment variables:</h2>\n"
         "<p>\n");
  for(i=0; environ[i]; i++){
    cgi_printf("%h <br />\n",environ[i]);
  }
  cgi_printf("</p>\n"
         "<h2>Working directory and user id</h2>\n"
         "<p>\n"
         "pwd = %s<br />\n"
         "uid = %d<br />\n"
         "euid = %d<br />\n"
         "argc = %d<br />\n",zPwd,getpid(),geteuid(),g.argc);
  for(i=0; i<g.argc; i++){
    cgi_printf("argv[%d] = %s<br />\n",i,g.argv[i]);
  }
  cgi_printf("</p>\n"
         "</body></html>\n");
}

/*
** WEBPAGE: /test
*/
void test_page(void){
  login_check_credentials();
  if( !g.okSetup ){ login_needed(); return; }
  test_cgi_vardump();
}

/*
** WEBPAGE: /formtest1
*/
void form_test_1(void){
  login_check_credentials();
  if( !g.okSetup ){ login_needed(); return; }
  cgi_printf("<html>\n"
         "<body bgcolor=\"white\">\n"
         "<h1>Parameters:</h1>\n");
  cgi_print_all();
  cgi_printf("<h1>New Form:</h1>\n"
         "<form action=\"formtest1\" method=\"POST\" enctype=\"multipart/form-data\">\n"
         "X = <input type=\"text\" name=\"x\" size=30><br>\n"
         "F1 = <input type=\"file\" name=\"f1\"><br>\n"
         "Y = <input type=\"text\" name=\"y\" size=30><br>\n"
         "F2 = <input type=\"file\" name=\"f2\"><br>\n"
         "<input type=\"submit\" value=\"Submit\">\n"
         "</form>\n");
}

/*
** WEBPAGE: /test2
*/
void test_page_2(void){
  cgi_redirect("test");
}

/*
** WEBPAGE: /endlessloop
*/
void endlessloop_page(void){
  while(1){sleep(5);}
}

/*
** WEBPAGE: /setcookie
*/
void set_cookie_page(void){
  const char *zName = PD("name","C_NAME");
  const char *zVal = PD("value","C_VALUE");
  login_check_credentials();
  if( !g.okSetup ){ login_needed(); return; }
  cgi_set_cookie(zName, zVal, 0, 0);
  cgi_redirect("");
}


syntax highlighted by Code2HTML, v. 0.9.1