Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

cookie.cpp

Go to the documentation of this file.
00001 /*
00002  *  $Id: cookie_8cpp-source.html,v 1.1.1.3 2004/06/19 04:36:23 chrisb Exp $
00003  *
00004  *  Copyright (C) 1996 - 2003 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/HTTPHTMLHeader.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   std::ofstream gLogFile( "/change_this_path/cgicc.log", std::ios::app );
00051 #endif
00052 
00053 using namespace std;
00054 using namespace cgicc;
00055 
00056 // Main Street, USA
00057 int
00058 main(int /*argc*/, 
00059      char ** /*argv*/)
00060 {
00061   try {
00062 #if HAVE_GETTIMEOFDAY
00063     timeval start;
00064     gettimeofday(&start, NULL);
00065 #endif
00066 
00067     // Create a new Cgicc object containing all the CGI data
00068     Cgicc cgi;
00069 
00070     // Get the name and value of the cookie to set
00071     const_form_iterator name = cgi.getElement("name");
00072     const_form_iterator value = cgi.getElement("value");
00073 
00074     // Output the headers for an HTML document with the cookie only
00075     // if the cookie is not empty
00076     if(name != cgi.getElements().end() && value != cgi.getElements().end()
00077        && value->getValue().empty() == false)
00078       cout << HTTPHTMLHeader()
00079         .setCookie(HTTPCookie(name->getValue(), value->getValue()));
00080     else
00081       cout << HTTPHTMLHeader();
00082     
00083     // Output the HTML 4.0 DTD info
00084     cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00085     cout << html().set("lang", "en").set("dir", "ltr") << endl;
00086 
00087     // Set up the page's header and title.
00088     // I will put in lfs to ease reading of the produced HTML. 
00089     cout << head() << endl;
00090 
00091     // Output the style sheet portion of the header
00092     cout << style() << comment() << endl;
00093     cout << "body { color: black; background-color: white; }" << endl;
00094     cout << "hr.half { width: 60%; align: center; }" << endl;
00095     cout << "span.red, strong.red { color: red; }" << endl;
00096     cout << "div.smaller { font-size: small; }" << endl;
00097     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00098          << "background: #ddd; }" << endl;
00099     cout << "span.blue { color: blue; }" << endl;
00100     cout << "col.title { color: white; background-color: black; ";
00101     cout << "font-weight: bold; text-align: center; }" << endl;
00102     cout << "col.data { background-color: #ddd; text-align: left; }" << endl;
00103     cout << "td.data, TR.data { background-color: #ddd; text-align: left; }"
00104          << endl;
00105     cout << "td.grayspecial { background-color: #ddd; text-align: left; }"
00106          << endl;
00107     cout << "td.ltgray, tr.ltgray { background-color: #ddd; }" << endl;
00108     cout << "td.dkgray, tr.dkgray { background-color: #bbb; }" << endl;
00109     cout << "col.black, td.black, td.title, tr.title { color: white; " 
00110          << "background-color: black; font-weight: bold; text-align: center; }"
00111          << endl;
00112     cout << "col.gray, td.gray { background-color: #ddd; text-align: center; }"
00113          << endl;
00114     cout << "table.cgi { left-margin: auto; right-margin: auto; width: 90%; }"
00115          << endl;
00116 
00117     cout << comment() << style() << endl;
00118 
00119     cout << title() << "GNU cgicc v" << cgi.getVersion() 
00120          << " HTTPCookie Results" << title() << endl;
00121 
00122     cout << head() << endl;
00123     
00124     // Start the HTML body
00125     cout << body() << endl;
00126 
00127     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00128          << " v"<< cgi.getVersion() << " HTTPCookie Test Results" 
00129          << h1() << endl;
00130     
00131     // Get a pointer to the environment
00132     const CgiEnvironment& env = cgi.getEnvironment();
00133     
00134     // Generic thank you message
00135     cout << comment() << "This page generated by cgicc for "
00136          << env.getRemoteHost() << comment() << endl;
00137     cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red") 
00138          << ", " << env.getRemoteHost() 
00139          << '(' << env.getRemoteAddr() << ")!" << h4() << endl;  
00140     
00141     if(name != cgi.getElements().end() && value != cgi.getElements().end()
00142        && value->getValue().empty() == false) {
00143       cout << p() << "A cookie with the name " << em(name->getValue())
00144            << " and value " << em(value->getValue()) << " was set." << br();
00145       cout << "In order for the cookie to show up here you must "
00146            << a("refresh").set("href",env.getScriptName()) << p();
00147     }
00148 
00149     // Show the cookie info from the environment
00150     cout << h2("Cookie Information from the Environment") << endl;
00151   
00152     cout << cgicc::div().set("align","center") << endl;
00153     
00154     cout << table().set("border","0").set("rules","none").set("frame","void")
00155       .set("cellspacing","2").set("cellpadding","2")
00156       .set("class","cgi") << endl;
00157     cout << colgroup().set("span","2") << endl;
00158     cout << col().set("align","center").set("class","title").set("span","1") 
00159          << endl;
00160     cout << col().set("align","left").set("class","data").set("span","1") 
00161          << endl;
00162     cout << colgroup() << endl;
00163     
00164     cout << tr() << td("HTTPCookie").set("class","title")
00165          << td(env.getCookies()).set("class","data") << tr() << endl;
00166     
00167     cout << table() << cgicc::div() << endl;
00168 
00169 
00170     // Show the cookie info from the cookie list
00171     cout << h2("HTTP Cookies via vector") << endl;
00172   
00173     cout << cgicc::div().set("align","center") << endl;
00174   
00175     cout << table().set("border","0").set("rules","none").set("frame","void")
00176       .set("cellspacing","2").set("cellpadding","2")
00177       .set("class","cgi") << endl;
00178     cout << colgroup().set("span","2") << endl;
00179     cout << col().set("align","center").set("span","2") << endl;
00180     cout << colgroup() << endl;
00181     
00182     cout << tr().set("class","title") << td("Cookie Name") 
00183          << td("Cookie Value") << tr() << endl;
00184     
00185     // Iterate through the vector, and print out each value
00186     const_cookie_iterator iter;
00187     for(iter = env.getCookieList().begin(); 
00188         iter != env.getCookieList().end(); 
00189         ++iter) {
00190       cout << tr().set("class","data") << td(iter->getName()) 
00191            << td(iter->getValue()) << tr() << endl;
00192     }
00193     cout << table() << cgicc::div() << endl;
00194     
00195 
00196     // Now print out a footer with some fun info
00197     cout << p() << cgicc::div().set("align","center");
00198     cout << a("Back to form").set("href", cgi.getEnvironment().getReferrer()) 
00199          << endl;
00200     cout << cgicc::div() << br() << hr(set("class","half")) << endl;
00201     
00202     // Information on cgicc
00203     cout << cgicc::div().set("align","center").set("class","smaller") << endl;
00204     cout << "GNU cgi" << span("cc").set("class","red") << " v";
00205     cout << cgi.getVersion() << br() << endl;
00206     cout << "Compiled at " << cgi.getCompileTime();
00207     cout << " on " << cgi.getCompileDate() << br() << endl;
00208 
00209     cout << "Configured for " << cgi.getHost();  
00210 #if HAVE_UNAME
00211     struct utsname info;
00212     if(uname(&info) != -1) {
00213       cout << ". Running on " << info.sysname;
00214       cout << ' ' << info.release << " (";
00215       cout << info.nodename << ")." << endl;
00216     }
00217 #else
00218     cout << "." << endl;
00219 #endif
00220 
00221 #if HAVE_GETTIMEOFDAY
00222     // Information on this query
00223     timeval end;
00224     gettimeofday(&end, NULL);
00225     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00226       + (end.tv_usec - start.tv_usec);
00227 
00228     cout << br() << "Total time for request = " << us << " us";
00229     cout << " (" << (double) (us/1000000.0) << " s)";
00230 #endif
00231 
00232     // End of document
00233     cout << cgicc::div() << endl;
00234     cout << body() << html() << endl;
00235 
00236     // No chance for failure in this example
00237     return EXIT_SUCCESS;
00238   }
00239 
00240   // Did any errors occur?
00241   catch(const std::exception& e) {
00242 
00243     // This is a dummy exception handler, as it doesn't really do
00244     // anything except print out information.
00245 
00246     // Reset all the HTML elements that might have been used to 
00247     // their initial state so we get valid output
00248     html::reset();      head::reset();          body::reset();
00249     title::reset();     h1::reset();            h4::reset();
00250     comment::reset();   td::reset();            tr::reset(); 
00251     table::reset();     cgicc::div::reset();    p::reset(); 
00252     a::reset();         h2::reset();            colgroup::reset();
00253 
00254     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
00255     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00256     cout << html().set("lang","en").set("dir","ltr") << endl;
00257 
00258     // Set up the page's header and title.
00259     // I will put in lfs to ease reading of the produced HTML. 
00260     cout << head() << endl;
00261 
00262     // Output the style sheet portion of the header
00263     cout << style() << comment() << endl;
00264     cout << "body { color: black; background-color: white; }" << endl;
00265     cout << "hr.half { width: 60%; align: center; }" << endl;
00266     cout << "span.red, strong.red { color: red; }" << endl;
00267     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
00268          << "background: #ddd; }" << endl;
00269 
00270     cout << comment() << style() << endl;
00271 
00272     cout << title("GNU cgicc exception") << endl;
00273     cout << head() << endl;
00274     
00275     cout << body() << endl;
00276     
00277     cout << h1() << "GNU cgi" << span("cc", set("class","red"))
00278          << " caught an exception" << h1() << endl; 
00279   
00280     cout << cgicc::div().set("align","center").set("class","notice") << endl;
00281 
00282     cout << h2(e.what()) << endl;
00283 
00284     // End of document
00285     cout << cgicc::div() << endl;
00286     cout << hr().set("class","half") << endl;
00287     cout << body() << html() << endl;
00288     
00289     return EXIT_SUCCESS;
00290   }
00291 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 2004 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 Sat Jun 19 00:36:23 2004 for cgicc by doxygen 1.3.6