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

cookie.cpp

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

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 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 Sun Mar 17 16:40:57 2002 for cgicc by doxygen 1.2.13.1