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 std::ofstream gLogFile( "/change_this_path/cgicc.log", std::ios::app );
00051 #endif
00052
00053 using namespace std;
00054 using namespace cgicc;
00055
00056
00057 int
00058 main(int ,
00059 char ** )
00060 {
00061 try {
00062 #if HAVE_GETTIMEOFDAY
00063 timeval start;
00064 gettimeofday(&start, NULL);
00065 #endif
00066
00067
00068 Cgicc cgi;
00069
00070
00071 const_form_iterator name = cgi.getElement("name");
00072 const_form_iterator value = cgi.getElement("value");
00073
00074
00075
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
00084 cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00085 cout << html().set("lang", "en").set("dir", "ltr") << endl;
00086
00087
00088
00089 cout << head() << endl;
00090
00091
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
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
00132 const CgiEnvironment& env = cgi.getEnvironment();
00133
00134
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
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
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
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
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
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
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
00233 cout << cgicc::div() << endl;
00234 cout << body() << html() << endl;
00235
00236
00237 return EXIT_SUCCESS;
00238 }
00239
00240
00241 catch(const std::exception& e) {
00242
00243
00244
00245
00246
00247
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
00255 cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00256 cout << html().set("lang","en").set("dir","ltr") << endl;
00257
00258
00259
00260 cout << head() << endl;
00261
00262
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
00285 cout << cgicc::div() << endl;
00286 cout << hr().set("class","half") << endl;
00287 cout << body() << html() << endl;
00288
00289 return EXIT_SUCCESS;
00290 }
00291 }