00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
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 
00048 
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 
00063 int
00064 main(int , 
00065      char ** )
00066 {
00067   try {
00068 #if HAVE_GETTIMEOFDAY
00069     timeval start;
00070     gettimeofday(&start, NULL);
00071 #endif
00072 
00073     
00074     Cgicc cgi;
00075 
00076     
00077     const_form_iterator name = cgi.getElement("name");
00078     const_form_iterator value = cgi.getElement("value");
00079 
00080     
00081     
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     
00090     cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00091     cout << html().set("lang", "en").set("dir", "ltr") << endl;
00092 
00093     
00094     
00095     cout << head() << endl;
00096 
00097     
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     
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     
00138     const CgiEnvironment& env = cgi.getEnvironment();
00139     
00140     
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     
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     
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     
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     
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     
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     
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     
00239     cout << CGICCNS div() << endl;
00240     cout << body() << html() << endl;
00241 
00242     
00243     return EXIT_SUCCESS;
00244   }
00245 
00246   
00247   catch(const STDNS exception& e) {
00248 
00249     
00250     
00251 
00252     
00253     
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     
00261     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00262     cout << html().set("lang","en").set("dir","ltr") << endl;
00263 
00264     
00265     
00266     cout << head() << endl;
00267 
00268     
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     
00291     cout << CGICCNS div() << endl;
00292     cout << hr().set("class","half") << endl;
00293     cout << body() << html() << endl;
00294     
00295     return EXIT_SUCCESS;
00296   }
00297 }