lib/cgi/cgi-base.cc
changeset 0 6f7a81934006
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include <stdlib.h>
       
    19 #include "ac/time.h"
       
    20 #include <unistd.h>
       
    21 #include "cgi-base.h"
       
    22 #include "fdbuf/fdbuf.h"
       
    23 
       
    24 #ifndef HAVE_SRANDOM
       
    25 void srandom(unsigned int seed);
       
    26 #endif
       
    27 
       
    28 static void redirect(mystring url)
       
    29 {
       
    30   fout << "Location: " << url << "\r\n\r\n";
       
    31 }
       
    32 
       
    33 void content_type(mystring ct)
       
    34 {
       
    35   fout << "Content-Type: " << ct << "\r\n\r\n";
       
    36 }
       
    37 
       
    38 static void error_message(mystring msg)
       
    39 {
       
    40   content_type("text/html");
       
    41   fout << "<html>"
       
    42     "<head>\n"
       
    43     "<title>CGI Error</title></head>\n"
       
    44     "<body>\n"
       
    45     "<p align=center><h1>CGI Error</h1></p><hr>\n"
       
    46     "<p>The following error occurred while trying to process your request:\r\n"
       
    47     "<pre>\r\n" << msg << "\r\n</pre></p>\r\n";
       
    48   const mystring referer = getenv("HTTP_REFERER");
       
    49   if(!referer.empty())
       
    50     fout << "<p><a href=\"" << referer << "\">Back</a></p>\n";
       
    51   fout << "</body>\n";
       
    52 }
       
    53 
       
    54 static void response_message(mystring msg)
       
    55 {
       
    56   content_type("text/html");
       
    57   fout << "<html>"
       
    58     "<head>\n"
       
    59     "<title>CGI Response</title></head>\n"
       
    60     "<body>\n"
       
    61     "<p align=center><h1>CGI Response</h1></p><hr>\n"
       
    62     "<p>" << msg << "</p>\n";
       
    63   const mystring referer = getenv("HTTP_REFERER");
       
    64   if(!referer.empty())
       
    65     fout << "<p><a href=\"" << referer << "\">Back</a></p>\n";
       
    66   fout << "</body>\n";
       
    67 }
       
    68 
       
    69 CGI::CGI(const CGIArgs& a)
       
    70   : args(a),
       
    71     redirect_url(a["redirect"]),
       
    72     error_redirect_url(a["error_redirect"]),
       
    73     vdomain(a["vdomain"]),
       
    74     password(a["password"])
       
    75 {
       
    76   must_be_defined("vdomain");
       
    77   must_be_defined("password");
       
    78 }
       
    79 
       
    80 void CGI::must_be_defined(mystring var)
       
    81 {
       
    82   if(!args.exists(var))
       
    83     error("The input field '" + var + "' must be defined");
       
    84 }
       
    85 
       
    86 void CGI::success(mystring msg)
       
    87 {
       
    88   if(!redirect_url) response_message(msg);
       
    89   else redirect(redirect_url);
       
    90   exit(0);
       
    91 }
       
    92 
       
    93 void CGI::error(mystring msg)
       
    94 {
       
    95   if(!error_redirect_url) error_message(msg);
       
    96   else redirect(error_redirect_url);
       
    97   exit(1);
       
    98 }
       
    99 
       
   100 int main(void)
       
   101 {
       
   102   struct timeval tv;
       
   103   gettimeofday(&tv, 0);
       
   104   srandom(tv.tv_usec ^ tv.tv_sec);
       
   105   CGIArgs cgiargs;
       
   106   if(cgiargs.error())
       
   107     error_message(cgiargs.errorstr());
       
   108   else
       
   109     CGI(cgiargs).main();
       
   110 }