Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

test.cpp

Go to the documentation of this file.
00001 /*
00002  *  $Id: test.cpp,v 1.16 2001/09/03 16:14:26 sbooth Exp $
00003  *
00004  *  Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Stephen F. Booth
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00027 #include <new>
00028 #include <string>
00029 #include <vector>
00030 #include <stdexcept>
00031 #include <iostream>
00032 #include <cstdlib>
00033 
00034 #include "cgicc/CgiDefs.h"
00035 #include "cgicc/Cgicc.h"
00036 #include "cgicc/HTTPHeaders.h"
00037 #include "cgicc/HTMLClasses.h"
00038 
00039 #if HAVE_UNAME
00040 #  include <sys/utsname.h>
00041 #endif
00042 
00043 #if HAVE_SYS_TIME_H
00044 #  include <sys/time.h>
00045 #endif
00046 
00047 // To use logging, the variable gLogFile MUST be defined, and it _must_
00048 // be an ofstream
00049 #if DEBUG
00050   STDNS ofstream gLogFile( "/change_this_path/cgicc.log", STDNS ios::app );
00051 #endif
00052 
00053 #if CGICC_USE_NAMESPACES
00054   using namespace std;
00055   using namespace cgicc;
00056 #else
00057 #  define div div_
00058 #  define link link_
00059 #  define select select_
00060 #endif
00061 
00062 // Function prototypes
00063 void dumpEnvironment(const CgiEnvironment& env);
00064 void dumpList(const Cgicc& formData);
00065 void showForm(const Cgicc& formData);
00066 
00067 // Main Street, USA
00068 int
00069 main(int /*argc*/, 
00070      char ** /*argv*/)
00071 {
00072   try {
00073 #if HAVE_GETTIMEOFDAY
00074     timeval start;
00075     gettimeofday(&start, NULL);
00076 #endif
00077 
00078     // Create a new Cgicc object containing all the CGI data
00079     Cgicc cgi;
00080     
00081     // If the user wants to throw an exception, go ahead and do it
00082     if(cgi.queryCheckbox("throw") && ! cgi.queryCheckbox("restore"))
00083       throw STDNS runtime_error("User-requested Exception thrown in main()");
00084     
00085     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00086     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00087     cout << html().set("lang", "en").set("dir", "ltr") << endl;
00088 
00089     // Set up the page's header and title.
00090     // I will put in lfs to ease reading of the produced HTML. 
00091     cout << head() << endl;
00092 
00093     // Output the style sheet portion of the header
00094     cout << style() << comment() << endl;
00095     cout << "body { color: black; background-color: white; }" << endl;
00096     cout << "hr.half { width: 60%; align: center; }" << endl;
00097     cout << "span.red, strong.red { color: red; }" << endl;
00098     cout << "div.smaller { font-size: small; }" << endl;
00099     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00100          << "background: #ddd; }" << endl;
00101     cout << "span.blue { color: blue; }" << endl;
00102     cout << "col.title { color: white; background-color: black; ";
00103     cout << "font-weight: bold; text-align: center; }" << endl;
00104     cout << "col.data { background-color: #DDD; text-align: left; }" << endl;
00105     cout << "td.data, tr.data { background-color: #ddd; text-align: left; }"
00106          << endl;
00107     cout << "td.grayspecial { background-color: #ddd; text-align: left; }"
00108          << endl;
00109     cout << "td.ltgray, tr.ltgray { background-color: #ddd; }" << endl;
00110     cout << "td.dkgray, tr.dkgray { background-color: #bbb; }" << endl;
00111     cout << "col.black, td.black, td.title, tr.title { color: white; " 
00112          << "background-color: black; font-weight: bold; text-align: center; }"
00113          << endl;
00114     cout << "col.gray, td.gray { background-color: #ddd; text-align: center; }"
00115          << endl;
00116     cout << "table.cgi { left-margin: auto; right-margin: auto; width: 90%; }"
00117          << endl;
00118 
00119     cout << comment() << style() << endl;
00120 
00121     cout << title() << "GNU cgicc v" << cgi.getVersion() << " Test Results" 
00122          << title() << endl;
00123     cout << meta().set("name", "author").set("content", "Stephen F. Booth") 
00124          << endl;
00125 
00126     cout << head() << endl;
00127     
00128     // Start the HTML body
00129     cout << body() << endl;
00130 
00131     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00132          << " v"<< cgi.getVersion() << " Test Results" << h1() << endl;
00133     
00134     // Get a pointer to the environment
00135     const CgiEnvironment& env = cgi.getEnvironment();
00136     
00137     // Generic thank you message
00138     cout << comment() << "This page generated by cgicc for "
00139          << env.getRemoteHost() << comment() << endl;
00140     cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red") 
00141          << ", " << env.getRemoteHost() 
00142          << '(' << env.getRemoteAddr() << ")!" << h4() << endl;  
00143     
00144     // If the user wants to save the submission, do it
00145     if(cgi.queryCheckbox("save")) {
00146       cgi.save("save");
00147       cout << p(h2("Data Saved")) << endl;
00148       
00149       cout << CGICCNS div().set("class", "notice") << endl;
00150       cout << "Your data has been saved, and may be restored (by anyone) "
00151            << "via the same form." << endl << CGICCNS div() << p() << endl;
00152     }
00153 
00154     // If the user wants to restore from the last submission, do it
00155     if(cgi.queryCheckbox("restore")) {
00156       cgi.restore("save");
00157       cout << p(h2("Data Restored")) << endl;
00158       
00159       cout << CGICCNS div().set("class", "notice") << endl;
00160       cout << "The data displayed has been restored from a file on disk."
00161            << endl << CGICCNS div() << p() << endl;
00162     }
00163     
00164     // If the user requested a dump of the environment,
00165     // create a simple table showing the values of the 
00166     // environment variables
00167     if(cgi.queryCheckbox("showEnv"))
00168       dumpEnvironment(env);
00169     
00170     // If the user requested, print out the raw form data from 
00171     // the vector of FormEntries.  This will contain every 
00172     // element in the list.
00173     // This is one of two ways to get at form data, the other
00174     // being the use of Cgicc's getElement() methods.  
00175     if(cgi.queryCheckbox("showFE"))
00176       dumpList(cgi);
00177     
00178     // If the user requested data via Cgicc's getElement() methods, do it.
00179     // This is different than the use of the list of FormEntries 
00180     // because it requires prior knowledge of the name of form elements.
00181     // Usually they will be known, but you never know.
00182     if(cgi.queryCheckbox("showForm"))
00183       showForm(cgi);
00184 
00185     // Now print out a footer with some fun info
00186     cout << p() << CGICCNS div().set("align","center");
00187     cout << a("Back to form").set("href", cgi.getEnvironment().getReferrer()) 
00188          << endl;
00189     cout << CGICCNS div() << br() << hr(set("class","half")) << endl;
00190     
00191     // Information on cgicc
00192     cout << CGICCNS div().set("align","center").set("class","smaller") << endl;
00193     cout << "GNU cgi" << span("cc").set("class","red") << " v";
00194     cout << cgi.getVersion();
00195     cout << " by " << a("Stephen F. Booth")
00196       .set("href", "mailto:sbooth@gnu.org") << br() << endl;
00197     cout << "Compiled at " << cgi.getCompileTime();
00198     cout << " on " << cgi.getCompileDate() << br() << endl;
00199 
00200     cout << "Configured for " << cgi.getHost();  
00201 #if HAVE_UNAME
00202     struct utsname info;
00203     if(uname(&info) != -1) {
00204       cout << ". Running on " << info.sysname;
00205       cout << ' ' << info.release << " (";
00206       cout << info.nodename << ")." << endl;
00207     }
00208 #else
00209     cout << "." << endl;
00210 #endif
00211 
00212 #if HAVE_GETTIMEOFDAY
00213     // Information on this query
00214     timeval end;
00215     gettimeofday(&end, NULL);
00216     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00217       + (end.tv_usec - start.tv_usec);
00218 
00219     cout << br() << "Total time for request = " << us << " us";
00220     cout << " (" << (double) (us/1000000.0) << " s)";
00221 #endif
00222 
00223     // End of document
00224     cout << CGICCNS div() << endl;
00225     cout << body() << html() << endl;
00226 
00227     // No chance for failure in this example
00228     return EXIT_SUCCESS;
00229   }
00230 
00231   // Did any errors occur?
00232   catch(const STDNS exception& e) {
00233 
00234     // This is a dummy exception handler, as it doesn't really do
00235     // anything except print out information.
00236 
00237     // Reset all the HTML elements that might have been used to 
00238     // their initial state so we get valid output
00239     html::reset();      head::reset();          body::reset();
00240     title::reset();     h1::reset();            h4::reset();
00241     comment::reset();   td::reset();            tr::reset(); 
00242     table::reset();     CGICCNS div::reset();   p::reset(); 
00243     a::reset();         h2::reset();            colgroup::reset();
00244 
00245     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00246     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00247     cout << html().set("lang","en").set("dir","ltr") << endl;
00248 
00249     // Set up the page's header and title.
00250     // I will put in lfs to ease reading of the produced HTML. 
00251     cout << head() << endl;
00252 
00253     // Output the style sheet portion of the header
00254     cout << style() << comment() << endl;
00255     cout << "body { color: black; background-color: white; }" << endl;
00256     cout << "hr.half { width: 60%; align: center; }" << endl;
00257     cout << "span.red, STRONG.red { color: red; }" << endl;
00258     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00259          << "background: #ddd; }" << endl;
00260 
00261     cout << comment() << style() << endl;
00262 
00263     cout << title("GNU cgicc exception") << endl;
00264     cout << meta().set("name", "author")
00265                   .set("content", "Stephen F. Booth") << endl;
00266     cout << head() << endl;
00267     
00268     cout << body() << endl;
00269     
00270     cout << h1() << "GNU cgi" << span("cc", set("class","red"))
00271          << " caught an exception" << h1() << endl; 
00272   
00273     cout << CGICCNS div().set("align","center").set("class","notice") << endl;
00274 
00275     cout << h2(e.what()) << endl;
00276 
00277     // End of document
00278     cout << CGICCNS div() << endl;
00279     cout << hr().set("class","half") << endl;
00280     cout << body() << html() << endl;
00281     
00282     return EXIT_SUCCESS;
00283   }
00284 }
00285 
00286 // Print out a table of the CgiEnvironment
00287 void
00288 dumpEnvironment(const CgiEnvironment& env) 
00289 {
00290   // This is just a brain-dead dump of information.
00291   // Almost all of this code is for HTML formatting
00292   cout << h2("Environment information from CgiEnvironment") << endl;
00293   
00294   cout << CGICCNS div().set("align","center") << endl;
00295   
00296   cout << table().set("border","0").set("rules","none").set("frame","void")
00297                  .set("cellspacing","2").set("cellpadding","2")
00298                  .set("class","cgi") << endl;
00299   cout << colgroup().set("span","2") << endl;
00300   cout << col().set("align","center").set("class","title").set("span","1") 
00301        << endl;
00302   cout << col().set("align","left").set("class","data").set("span","1") 
00303        << endl;
00304   cout << colgroup() << endl;
00305   
00306   cout << tr() << td("Request Method").set("class","title") 
00307        << td(env.getRequestMethod()).set("class","data") << tr() << endl;
00308   cout << tr() << td("Path Info").set("class","title") 
00309        << td(env.getPathInfo()).set("class","data") << tr() << endl;
00310   cout << tr() << td("Path Translated").set("class","title") 
00311        << td(env.getPathTranslated()).set("class","data") << tr() << endl;
00312   cout << tr() << td("Script Name").set("class","title") 
00313        << td(env.getScriptName()).set("class","data") << tr() << endl;
00314   cout << tr() << td("HTTP Referrer").set("class","title") 
00315        << td(env.getReferrer()).set("class","data") << tr() << endl;
00316   cout << tr() << td("HTTP Cookie").set("class","title") 
00317        << td(env.getCookies()).set("class","data") << tr() << endl;
00318   cout << tr() << td("Query String").set("class","title") 
00319        << td(env.getQueryString()).set("class","data") << tr() << endl;
00320   cout << tr() << td("Content Length").set("class","title") 
00321        << td().set("class","data") << env.getContentLength() 
00322        << td() << tr() << endl;
00323   cout << tr() << td("Post Data").set("class","title")
00324        << td().set("class","data")
00325        << pre(env.getPostData()).set("class","data") << td() 
00326        << tr() << endl;
00327   cout << tr() << td("Remote Host").set("class","title") 
00328        << td(env.getRemoteHost()).set("class","data") << tr() << endl;
00329   cout << tr() << td("Remote Address").set("class","title") 
00330        << td(env.getRemoteAddr()).set("class","data") << tr() << endl;
00331   cout << tr() << td("Authorization Type").set("class","title") 
00332        << td(env.getAuthType()).set("class","data") << tr() << endl;
00333   cout << tr() << td("Remote User").set("class","title") 
00334        << td(env.getRemoteUser()).set("class","data") << tr() << endl;
00335   cout << tr() << td("Remote Identification").set("class","title") 
00336        << td(env.getRemoteIdent()).set("class","data") << tr() << endl;
00337   cout << tr() << td("Content Type").set("class","title") 
00338        << td(env.getContentType()).set("class","data") << tr() << endl;
00339   cout << tr() << td("HTTP Accept").set("class","title") 
00340        << td(env.getAccept()).set("class","data") << tr() << endl;
00341   cout << tr() << td("User Agent").set("class","title") 
00342        << td(env.getUserAgent()).set("class","data") << tr() << endl;
00343   cout << tr() << td("Server Software").set("class","title") 
00344        << td(env.getServerSoftware()).set("class","data") << tr() << endl;
00345   cout << tr() << td("Server Name").set("class","title") 
00346        << td(env.getServerName()).set("class","data") << tr() << endl;
00347   cout << tr() << td("Gateway Interface").set("class","title") 
00348        << td(env.getGatewayInterface()).set("class","data") << tr() << endl;
00349   cout << tr() << td("Server Protocol").set("class","title") 
00350        << td(env.getServerProtocol()).set("class","data") << tr() << endl;
00351   cout << tr() << td("Server Port").set("class","title") 
00352        << td().set("class","data") << env.getServerPort() 
00353        << td() << tr() << endl;
00354   cout << tr() << td("HTTPS").set("class","title")
00355        << td().set("class","data") << (env.usingHTTPS() ? "true" : "false")
00356        << td() << tr() << endl;
00357   cout << tr() << td("Redirect Request").set("class","title") 
00358        << td(env.getRedirectRequest()).set("class","data") << tr() << endl;
00359   cout << tr() << td("Redirect URL").set("class","title") 
00360        << td(env.getRedirectURL()).set("class","data") << tr() << endl;
00361   cout << tr() << td("Redirect Status").set("class","title") 
00362        << td(env.getRedirectStatus()).set("class","data") << tr() << endl;
00363   
00364   cout << table() << CGICCNS div() << endl;
00365 }
00366 
00367 // Print out the value of every form element
00368 void
00369 dumpList(const Cgicc& formData) 
00370 {
00371   cout << h2("Form Data via vector") << endl;
00372   
00373   cout << CGICCNS div().set("align","center") << endl;
00374   
00375   cout << table().set("border","0").set("rules","none").set("frame","void")
00376                  .set("cellspacing","2").set("cellpadding","2")
00377                  .set("class","cgi") << endl;
00378   cout << colgroup().set("span","2") << endl;
00379   cout << col().set("align","center").set("span","2") << endl;
00380   cout << colgroup() << endl;
00381   
00382   cout << tr().set("class","title") << td("Element Name") 
00383        << td("Element Value") << tr() << endl;
00384   
00385   // Iterate through the vector, and print out each value
00386   const_form_iterator iter;
00387   for(iter = formData.getElements().begin(); 
00388       iter != formData.getElements().end(); 
00389       ++iter) {
00390     cout << tr().set("class","data") << td(iter->getName()) 
00391          << td(iter->getValue()) << tr() << endl;
00392   }
00393   cout << table() << CGICCNS div() << endl;
00394 }
00395 
00396 // Print out information customized for each element
00397 void
00398 showForm(const Cgicc& formData) 
00399 {
00400 
00401   // I am using an if statement to check if each element is found
00402   cout << h2("Form Data via Cgicc") << endl;
00403   
00404   cout << CGICCNS div().set("class","notice") << endl;
00405 
00406   //getElement
00407   const_form_iterator name = formData.getElement("name");
00408   if(name != (*formData).end() && ! name->isEmpty())
00409     cout << "Your name is " << **name << '.' << br() << endl;
00410   else
00411     cout << "You don't have a name." << br() << endl;
00412 
00413   // getElement and getDoubleValue
00414   const_form_iterator salary = formData.getElement("bucks");
00415   if(salary != (*formData).end() && ! salary->isEmpty())
00416     cout << "You make " << (*salary).getDoubleValue(80, 120) 
00417          << " million dollars." << br() << endl;
00418   else
00419     cout << "You don't have a salary." << br() << endl;
00420 
00421   // getElement and getIntegerValue
00422   const_form_iterator hours = formData.getElement("time");
00423   if(hours != (*formData).end() && ! (*hours).isEmpty())
00424     cout << "You've wasted " << (*hours).getIntegerValue() 
00425          << " hours on the web." << br() << endl;
00426   else
00427     cout << "You haven't wasted any time on the web." << br() << endl;
00428 
00429   // getElement and getStrippedValue
00430   const_form_iterator thoughts = formData.getElement("thoughts");
00431   if(thoughts != (*formData).end() && ! (*thoughts).isEmpty()) {
00432     STDNS string temp = (*thoughts).getStrippedValue();
00433     cout << "Your thoughts : " << temp << br() << endl;
00434   }
00435   else
00436     cout << "You don't have any thoughts!?" << br() << endl;
00437   
00438   // queryCheckbox
00439   if(formData.queryCheckbox("hungry"))
00440     cout << "You are hungry." << br() << endl;
00441   else
00442     cout << "You are not hungry." << br() << endl;
00443 
00444   // getElement
00445   STDNS vector<FormEntry> flavors;
00446   formData.getElement("flavors", flavors);
00447   if(! flavors.empty()) {
00448     cout << "You like ";
00449     for(STDNS string::size_type i = 0; i < flavors.size(); i++) {
00450       cout << flavors[i].getValue();
00451       if(i < flavors.size() - 2)
00452         cout << ", ";
00453       else if(i == flavors.size() - 2)
00454         cout << " and ";
00455     }
00456     cout << " ice cream." << br() << endl;
00457   }
00458   else
00459     cout << "You don't like ice cream!?" << br() << endl;
00460   
00461   // getElement
00462   const_form_iterator hair = formData.getElement("hair");
00463   if(hair != (*formData).end())
00464     cout << "Your hair is " << **hair << '.' << br() << endl;
00465   else
00466     cout << "You don't have any hair." << br() << endl;
00467   
00468   const_form_iterator vote = formData.getElement("vote");
00469   if(vote != (*formData).end())
00470     cout << "You voted for " << **vote << '.' << br() << endl;
00471   else
00472     cout << "This should never happen. ERROR!" << br() << endl;
00473   
00474   // getElement
00475   STDNS vector<FormEntry> friends;
00476   formData.getElement("friends", friends);
00477   if(! friends.empty()) {
00478     cout << "You like ";
00479     for(STDNS string::size_type i = 0; i < friends.size(); i++) {
00480       cout << friends[i].getValue();
00481       if(i < friends.size() - 2)
00482         cout << ", ";
00483       else if(i == friends.size() - 2)
00484         cout << " and ";
00485     }
00486     cout << " on Friends." << br() << endl;
00487   }
00488   else
00489     cout << "You don't watch Friends!?" << br() << endl;
00490   
00491   cout << CGICCNS div() << endl;
00492 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996, 1997, 1998, 1999, 2000, 2001, 2002 Stephen F. Booth
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front Cover Texts, and with no Back-Cover Texts.
Documentation generated Wed Jan 9 12:31:26 2002 for cgicc by doxygen 1.2.13.1