cgicc: cookie.cpp Source File

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

# cookie.cpp

Go to the documentation of this file.

    00001 /* -*-mode:c++; c-file-style: "gnu";-*- */
    00002 /*
    00003  *  $Id: cookie.cpp,v 1.10 2007/07/02 18:48:19 sebdiaz Exp $
    00004  *
    00005  *  Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
    00006  *                       2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
    00007  *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
    00008  *
    00009  *  This library is free software; you can redistribute it and/or
    00010  *  modify it under the terms of the GNU Lesser General Public
    00011  *  License as published by the Free Software Foundation; either
    00012  *  version 3 of the License, or (at your option) any later version.
    00013  *
    00014  *  This library is distributed in the hope that it will be useful,
    00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00017  *  Lesser General Public License for more details.
    00018  *
    00019  *  You should have received a copy of the GNU Lesser General Public
    00020  *  License along with this library; if not, write to the Free Software
    00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
    00022  */
    00023
    00030 #include <new>
    00031 #include <string>
    00032 #include <vector>
    00033 #include <stdexcept>
    00034 #include <iostream>
    00035 #include <cstdlib>
    00036
    00037 #include "cgicc/CgiDefs.h"
    00038 #include "cgicc/Cgicc.h"
    00039 #include "cgicc/HTTPHTMLHeader.h"
    00040 #include "cgicc/HTMLClasses.h"
    00041
    00042 #if HAVE_SYS_UTSNAME_H
    00043 #  include <sys/utsname.h>
    00044 #endif
    00045
    00046 #if HAVE_SYS_TIME_H
    00047 #  include <sys/time.h>
    00048 #endif
    00049
    00050 #include "styles.h"
    00051
    00052 using namespace std;
    00053 using namespace cgicc;
    00054
    00055 // Print the form for this CGI
    00056 void
    00057 printForm(const Cgicc& cgi)
    00058 {
    00059   cout << "<form method=\"post\" action=\""
    00060        << cgi.getEnvironment().getScriptName() << "\">" << endl;
    00061
    00062   cout << "<table>" << endl;
    00063
    00064   cout << "<tr><td class=\"title\">Cookie Name</td>"
    00065        << "<td class=\"form\">"
    00066        << "<input type=\"text\" name=\"name\" />"
    00067        << "</td></tr>" << endl;
    00068
    00069   cout << "<tr><td class=\"title\">Cookie Value</td>"
    00070        << "<td class=\"form\">"
    00071        << "<input type=\"text\" name=\"value\" />"
    00072        << "</td></tr>" << endl;
    00073
    00074   cout << "</table>" << endl;
    00075
    00076   cout << "<div class=\"center\"><p>"
    00077        << "<input type=\"submit\" name=\"submit\" value=\"Set the cookie\" />"
    00078        << "<input type=\"reset\" value=\"Nevermind\" />"
    00079        << "</p></div></form>" << endl;
    00080 }
    00081
    00082 // Main Street, USA
    00083 int
    00084 main(int /*argc*/,
    00085      char ** /*argv*/)
    00086 {
    00087   try {
    00088 #if HAVE_GETTIMEOFDAY
    00089     timeval start;
    00090     gettimeofday(&start, NULL);
    00091 #endif
    00092
    00093     // Create a new Cgicc object containing all the CGI data
    00094     Cgicc cgi;
    00095
    00096     // Get the name and value of the cookie to set
    00097     const_form_iterator name = cgi.getElement("name");
    00098     const_form_iterator value = cgi.getElement("value");
    00099
    00100     // Output the headers for an HTML document with the cookie only
    00101     // if the cookie is not empty
    00102     if(name != cgi.getElements().end() && value != cgi.getElements().end()
    00103        && value->getValue().empty() == false)
    00104       cout << HTTPHTMLHeader()
    00105         .setCookie(HTTPCookie(name->getValue(), value->getValue()));
    00106     else
    00107       cout << HTTPHTMLHeader();
    00108
    00109     // Output the HTML 4.0 DTD info
    00110     cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
    00111     cout << html().set("lang", "en").set("dir", "ltr") << endl;
    00112
    00113     // Set up the page's header and title.
    00114     // I will put in lfs to ease reading of the produced HTML.
    00115     cout << head() << endl;
    00116
    00117     // Output the style sheet portion of the header
    00118     cout << style() << comment() << endl;
    00119     cout << styles;
    00120     cout << comment() << style() << endl;
    00121
    00122     cout << title() << "GNU cgicc v" << cgi.getVersion()
    00123          << " HTTPCookie" << title() << endl;
    00124
    00125     cout << head() << endl;
    00126
    00127     // Start the HTML body
    00128     cout << body() << endl;
    00129
    00130     cout << h1() << "GNU cgi" << span("cc").set("class","red")
    00131          << " v"<< cgi.getVersion() << " HTTPCookie Test Results"
    00132          << 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(name != cgi.getElements().end() && value != cgi.getElements().end()
    00145        && value->getValue().empty() == false) {
    00146       cout << p() << "A cookie with the name " << em(name->getValue())
    00147            << " and value " << em(value->getValue()) << " was set." << br();
    00148       cout << "In order for the cookie to show up here you must "
    00149            << a("refresh").set("href",env.getScriptName()) << p();
    00150     }
    00151
    00152     // Show the cookie info from the environment
    00153     cout << h2("Cookie Information from the Environment") << endl;
    00154
    00155     cout << cgicc::div().set("align","center") << endl;
    00156
    00157     cout << table() << endl;
    00158
    00159     cout << tr() << td("HTTPCookie").set("class","title")
    00160          << td(env.getCookies()).set("class","data") << tr() << endl;
    00161
    00162     cout << table() << cgicc::div() << endl;
    00163
    00164
    00165     // Show the cookie info from the cookie list
    00166     cout << h2("HTTP Cookies via vector") << endl;
    00167
    00168     cout << cgicc::div().set("align","center") << endl;
    00169
    00170     cout << table() << endl;
    00171
    00172     cout << tr().set("class","title") << td("Cookie Name")
    00173          << td("Cookie Value") << tr() << endl;
    00174
    00175     // Iterate through the vector, and print out each value
    00176     const_cookie_iterator iter;
    00177     for(iter = env.getCookieList().begin();
    00178         iter != env.getCookieList().end();
    00179         ++iter) {
    00180       cout << tr().set("class","data") << td(iter->getName())
    00181            << td(iter->getValue()) << tr() << endl;
    00182     }
    00183     cout << table() << cgicc::div() << endl;
    00184
    00185
    00186     // Print out the form to do it again
    00187     cout << br() << endl;
    00188     printForm(cgi);
    00189     cout << hr().set("class", "half") << endl;
    00190
    00191     // Information on cgicc
    00192     cout << cgicc::div().set("align","center").set("class","smaller") << endl;
    00193     cout << "GNU cgi" << span("cc").set("class","red") << " v";
    00194     cout << cgi.getVersion() << br() << endl;
    00195     cout << "Compiled at " << cgi.getCompileTime();
    00196     cout << " on " << cgi.getCompileDate() << br() << endl;
    00197
    00198     cout << "Configured for " << cgi.getHost();
    00199 #if HAVE_UNAME
    00200     struct utsname info;
    00201     if(uname(&info) != -1) {
    00202       cout << ". Running on " << info.sysname;
    00203       cout << ' ' << info.release << " (";
    00204       cout << info.nodename << ")." << endl;
    00205     }
    00206 #else
    00207     cout << "." << endl;
    00208 #endif
    00209
    00210 #if HAVE_GETTIMEOFDAY
    00211     // Information on this query
    00212     timeval end;
    00213     gettimeofday(&end, NULL);
    00214     long us = ((end.tv_sec - start.tv_sec) * 1000000)
    00215       + (end.tv_usec - start.tv_usec);
    00216
    00217     cout << br() << "Total time for request = " << us << " us";
    00218     cout << " (" << static_cast<double>(us/1000000.0) << " s)";
    00219 #endif
    00220
    00221     // End of document
    00222     cout << cgicc::div() << endl;
    00223     cout << body() << html() << endl;
    00224
    00225     // No chance for failure in this example
    00226     return EXIT_SUCCESS;
    00227   }
    00228
    00229   // Did any errors occur?
    00230   catch(const std::exception& e) {
    00231
    00232     // This is a dummy exception handler, as it doesn't really do
    00233     // anything except print out information.
    00234
    00235     // Reset all the HTML elements that might have been used to
    00236     // their initial state so we get valid output
    00237     html::reset();      head::reset();          body::reset();
    00238     title::reset();     h1::reset();            h4::reset();
    00239     comment::reset();   td::reset();            tr::reset();
    00240     table::reset();     cgicc::div::reset();    p::reset();
    00241     a::reset();         h2::reset();            colgroup::reset();
    00242
    00243     // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
    00244     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
    00245     cout << html().set("lang","en").set("dir","ltr") << endl;
    00246
    00247     // Set up the page's header and title.
    00248     // I will put in lfs to ease reading of the produced HTML.
    00249     cout << head() << endl;
    00250
    00251     // Output the style sheet portion of the header
    00252     cout << style() << comment() << endl;
    00253     cout << "body { color: black; background-color: white; }" << endl;
    00254     cout << "hr.half { width: 60%; align: center; }" << endl;
    00255     cout << "span.red, strong.red { color: red; }" << endl;
    00256     cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
    00257          << "background: #ddd; }" << endl;
    00258
    00259     cout << comment() << style() << endl;
    00260
    00261     cout << title("GNU cgicc exception") << endl;
    00262     cout << head() << endl;
    00263
    00264     cout << body() << endl;
    00265
    00266     cout << h1() << "GNU cgi" << span("cc", set("class","red"))
    00267          << " caught an exception" << h1() << endl;
    00268
    00269     cout << cgicc::div().set("align","center").set("class","notice") << endl;
    00270
    00271     cout << h2(e.what()) << endl;
    00272
    00273     // End of document
    00274     cout << cgicc::div() << endl;
    00275     cout << hr().set("class","half") << endl;
    00276     cout << body() << html() << endl;
    00277
    00278     return EXIT_SUCCESS;
    00279   }
    00280 }