00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00083 int
00084 main(int ,
00085 char ** )
00086 {
00087 try {
00088 #if HAVE_GETTIMEOFDAY
00089 timeval start;
00090 gettimeofday(&start, NULL);
00091 #endif
00092
00093
00094 Cgicc cgi;
00095
00096
00097 const_form_iterator name = cgi.getElement("name");
00098 const_form_iterator value = cgi.getElement("value");
00099
00100
00101
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
00110 cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00111 cout << html().set("lang", "en").set("dir", "ltr") << endl;
00112
00113
00114
00115 cout << head() << endl;
00116
00117
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
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
00135 const CgiEnvironment& env = cgi.getEnvironment();
00136
00137
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
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
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
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
00187 cout << br() << endl;
00188 printForm(cgi);
00189 cout << hr().set("class", "half") << endl;
00190
00191
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
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
00222 cout << cgicc::div() << endl;
00223 cout << body() << html() << endl;
00224
00225
00226 return EXIT_SUCCESS;
00227 }
00228
00229
00230 catch(const std::exception& e) {
00231
00232
00233
00234
00235
00236
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
00244 cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00245 cout << html().set("lang","en").set("dir","ltr") << endl;
00246
00247
00248
00249 cout << head() << endl;
00250
00251
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
00274 cout << cgicc::div() << endl;
00275 cout << hr().set("class","half") << endl;
00276 cout << body() << html() << endl;
00277
00278 return EXIT_SUCCESS;
00279 }
00280 }