diff -r 000000000000 -r 6f7a81934006 lib/cgi/cgi-base.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/cgi/cgi-base.cc Wed Jan 16 22:39:43 2008 +0100 @@ -0,0 +1,110 @@ +// Copyright (C) 1999,2000 Bruce Guenter +// +// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +#include +#include +#include "ac/time.h" +#include +#include "cgi-base.h" +#include "fdbuf/fdbuf.h" + +#ifndef HAVE_SRANDOM +void srandom(unsigned int seed); +#endif + +static void redirect(mystring url) +{ + fout << "Location: " << url << "\r\n\r\n"; +} + +void content_type(mystring ct) +{ + fout << "Content-Type: " << ct << "\r\n\r\n"; +} + +static void error_message(mystring msg) +{ + content_type("text/html"); + fout << "" + "\n" + "CGI Error\n" + "\n" + "

CGI Error


\n" + "

The following error occurred while trying to process your request:\r\n" + "

\r\n" << msg << "\r\n

\r\n"; + const mystring referer = getenv("HTTP_REFERER"); + if(!referer.empty()) + fout << "

Back

\n"; + fout << "\n"; +} + +static void response_message(mystring msg) +{ + content_type("text/html"); + fout << "" + "\n" + "CGI Response\n" + "\n" + "

CGI Response


\n" + "

" << msg << "

\n"; + const mystring referer = getenv("HTTP_REFERER"); + if(!referer.empty()) + fout << "

Back

\n"; + fout << "\n"; +} + +CGI::CGI(const CGIArgs& a) + : args(a), + redirect_url(a["redirect"]), + error_redirect_url(a["error_redirect"]), + vdomain(a["vdomain"]), + password(a["password"]) +{ + must_be_defined("vdomain"); + must_be_defined("password"); +} + +void CGI::must_be_defined(mystring var) +{ + if(!args.exists(var)) + error("The input field '" + var + "' must be defined"); +} + +void CGI::success(mystring msg) +{ + if(!redirect_url) response_message(msg); + else redirect(redirect_url); + exit(0); +} + +void CGI::error(mystring msg) +{ + if(!error_redirect_url) error_message(msg); + else redirect(error_redirect_url); + exit(1); +} + +int main(void) +{ + struct timeval tv; + gettimeofday(&tv, 0); + srandom(tv.tv_usec ^ tv.tv_sec); + CGIArgs cgiargs; + if(cgiargs.error()) + error_message(cgiargs.errorstr()); + else + CGI(cgiargs).main(); +}