43# include <sys/utsname.h>
57void dumpList(
const Cgicc& formData);
58void showForm(
const Cgicc& formData);
62printForm(
const Cgicc& cgi)
64 cout <<
"<form method=\"post\" action=\""
67 cout <<
"<table>" << endl;
69 cout <<
"<tr><td class=\"title\">Your name</td>"
70 <<
"<td class=\"form\">"
71 <<
"<input type=\"text\" name=\"name\" value=\"Uncle Bob\" />"
72 <<
"</td></tr>" << endl;
74 cout <<
"<tr><td class=\"title\">Your salary in millions<br />(80-120)</td>"
75 <<
"<td class=\"form\">"
76 <<
"<input type=\"text\" name=\"bucks\" value=\"93\" />"
77 <<
"</td></tr>" << endl;
79 cout <<
"<tr><td class=\"title\">Hours you've wasted on the web</td>"
80 <<
"<td class=\"form\">"
81 <<
"<input type=\"text\" name=\"time\" value=\"100\" />"
82 <<
"</td></tr>" << endl;
84 cout <<
"<tr><td class=\"title\">Your thoughts (on anything)</td>"
85 <<
"<td class=\"form\">"
86 <<
"<textarea name=\"thoughts\" rows=\"4\" cols=\"40\">"
87 <<
"I don't have any!</textarea>" <<
"</td></tr>" << endl;
89 cout <<
"<tr><td class=\"title\">Are you hungry?</td>"
90 <<
"<td class=\"form\">"
91 <<
"<input type=\"checkbox\" name=\"hungry\" checked=\"checked\" />"
92 <<
"Yes</td></tr>" << endl;
94 cout <<
"<tr><td class=\"title\">Your favorite flavors of ice cream</td>"
95 <<
"<td class=\"form\">"
96 <<
"<select name=\"flavors\" multiple=\"multiple\">"
97 <<
"<option value=\"cookie dough\">Cookie Dough</option>"
98 <<
"<option value=\"rocky road\">Rocky Road</option>"
99 <<
"<option value=\"chocolate\">Chocolate</option>"
100 <<
"<option value=\"strawberry\">Strawberry</option>"
101 <<
"<option value=\"vanilla\">Vanilla</option>"
102 <<
"</select>" <<
"</td></tr>" << endl;
104 cout <<
"<tr><td class=\"title\">Your hair color</td>"
105 <<
"<td class=\"form\">"
106 <<
"<select name=\"hair\">"
107 <<
"<option value=\"blond\">Blond</option>"
108 <<
"<option value=\"brown\">Brown</option>"
109 <<
"<option value=\"red\">Red</option>"
110 <<
"<option value=\"black\">Black</option>"
111 <<
"<option value=\"white\">White</option>"
112 <<
"<option value=\"green\">Green</option>"
113 <<
"<option value=\"multicolored\">Multicolored</option>"
114 <<
"</select>" <<
"</td></tr>" << endl;
116 cout <<
"<tr><td class=\"title\">Your web browser</td>"
117 <<
"<td class=\"form\">"
118 <<
"<input type=\"radio\" name=\"browser\" value=\"Konqueror\""
119 <<
" checked=\"checked\" />Konqeuror"
120 <<
"<input type=\"radio\" name=\"browser\" value=\"Lynx\" />Lynx"
121 <<
"<input type=\"radio\" name=\"browser\" value=\"Mozilla\" />Mozilla"
122 <<
"<input type=\"radio\" name=\"browser\" value=\"IE\" />IE"
123 <<
"<input type=\"radio\" name=\"browser\" value=\"Other\" />Other"
124 <<
"</td></tr>" << endl;
126 cout <<
"<tr><td class=\"title\">Your favorite authors</td>"
127 <<
"<td class=\"form\">"
128 <<
"<input type=\"checkbox\" name=\"authors\" value=\"O'Brian\" />"
130 <<
"<input type=\"checkbox\" name=\"authors\" value=\"Feynman\" />"
132 <<
"<input type=\"checkbox\" name=\"authors\" value=\"Camus\" />Camus"
133 <<
"<input type=\"checkbox\" name=\"authors\" value=\"Conrad\" />Conrad"
134 <<
"<input type=\"checkbox\" name=\"authors\" value=\"Vergil\" />Vergil"
135 <<
"<input type=\"checkbox\" name=\"authors\" value=\"Plato\" />Plato"
136 <<
"</td></tr>" << endl;
138 cout <<
"<tr><td class=\"title\">In the output, show</td>"
139 <<
"<td class=\"form\">"
140 <<
"<input type=\"checkbox\" name=\"showEnv\" checked=\"checked\" />"
141 <<
"Data from CgiEnvironment<br />"
142 <<
"<input type=\"checkbox\" name=\"showFE\" checked=\"checked\" />"
143 <<
"All FormEntries<br />"
144 <<
"<input type=\"checkbox\" name=\"showForm\" checked=\"checked\" />"
146 <<
"</td></tr>" << endl;
148 cout <<
"<tr><td class=\"title\">Exception Handling</td>"
149 <<
"<td class=\"form\">"
150 <<
"<input type=\"checkbox\" name=\"throw\" />"
151 <<
"Throw an exception to test error handling"
152 <<
"</td></tr>" << endl;
154 cout <<
"<tr><td class=\"title\">Save and Restore</td>"
155 <<
"<td class=\"form\">"
156 <<
"<input type=\"checkbox\" name=\"save\" />"
157 <<
" Save submission to a file<br />"
158 <<
"<input type=\"checkbox\" name=\"restore\" />"
159 <<
"Restore data from the last saved submission"
160 <<
"</td></tr>" << endl;
162 cout <<
"</table>" << endl;
164 cout <<
"<div class=\"center\"><p>"
165 <<
"<input type=\"submit\" name=\"submit\" value=\"Submit\" />"
166 <<
"<input type=\"reset\" value=\"Nevermind\" />"
167 <<
"</p></div></form>" << endl;
178 gettimeofday(&start, NULL);
186 throw std::runtime_error(
"User-requested Exception thrown in main()");
190 cout << html().set(
"lang",
"en").set(
"dir",
"ltr") << endl;
194 cout << head() << endl;
197 cout << style() <<
comment() << endl;
199 cout <<
comment() << style() << endl;
201 cout << title() <<
"GNU cgicc v" << cgi.
getVersion() <<
" Test"
204 cout << head() << endl;
207 cout << body() << endl;
209 cout << h1() <<
"GNU cgi" << span(
"cc").set(
"class",
"red")
210 <<
" v"<< cgi.
getVersion() <<
" Test" << h1() << endl;
216 cout <<
comment() <<
"This page generated by cgicc for "
218 cout << h4() <<
"Thanks for using cgi" << span(
"cc").
set(
"class",
"red")
226 cout << p(h2(
"Data Saved")) << endl;
228 cout << cgicc::div().set(
"class",
"notice") << endl;
229 cout <<
"Your data has been saved, and may be restored (by anyone) "
230 <<
"via the same form." << endl << cgicc::div() << p() << endl;
236 cout << p(h2(
"Data Restored")) << endl;
238 cout << cgicc::div().set(
"class",
"notice") << endl;
239 cout <<
"The data displayed has been restored from a file on disk."
240 << endl << cgicc::div() << p() << endl;
247 dumpEnvironment(env);
265 cout << br() << endl;
267 cout << hr().set(
"class",
"half") << endl;
270 cout << cgicc::div().set(
"align",
"center").set(
"class",
"smaller") << endl;
271 cout <<
"GNU cgi" << span(
"cc").set(
"class",
"red") <<
" v";
276 cout <<
"Configured for " << cgi.
getHost();
279 if(uname(&info) != -1) {
280 cout <<
". Running on " << info.sysname;
281 cout <<
' ' << info.release <<
" (";
282 cout << info.nodename <<
")." << endl;
291 gettimeofday(&end, NULL);
292 long us = ((end.tv_sec - start.tv_sec) * 1000000)
293 + (end.tv_usec - start.tv_usec);
295 cout << br() <<
"Total time for request = " << us <<
" us";
296 cout <<
" (" <<
static_cast<double>(us/1000000.0) <<
" s)";
300 cout << cgicc::div() << endl;
301 cout << body() << html() << endl;
308 catch(
const std::exception& e) {
315 html::reset(); head::reset(); body::reset();
316 title::reset(); h1::reset(); h4::reset();
317 comment::reset(); td::reset(); tr::reset();
318 table::reset(); cgicc::div::reset(); p::reset();
319 a::reset(); h2::reset(); colgroup::reset();
323 cout << html().set(
"lang",
"en").set(
"dir",
"ltr") << endl;
327 cout << head() << endl;
330 cout << style() <<
comment() << endl;
331 cout <<
"body { color: black; background-color: white; }" << endl;
332 cout <<
"hr.half { width: 60%; align: center; }" << endl;
333 cout <<
"span.red, STRONG.red { color: red; }" << endl;
334 cout <<
"div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
335 <<
"background: #ddd; }" << endl;
337 cout <<
comment() << style() << endl;
339 cout << title(
"GNU cgicc exception") << endl;
340 cout << head() << endl;
342 cout << body() << endl;
344 cout << h1() <<
"GNU cgi" << span(
"cc",
set(
"class",
"red"))
345 <<
" caught an exception" << h1() << endl;
347 cout << cgicc::div().
set(
"align",
"center").
set(
"class",
"notice") << endl;
349 cout << h2(e.what()) << endl;
352 cout << cgicc::div() << endl;
353 cout << hr().
set(
"class",
"half") << endl;
354 cout << body() << html() << endl;
366 cout << h2(
"Environment information from CgiEnvironment") << endl;
368 cout << cgicc::div().set(
"align",
"center") << endl;
370 cout << table() << endl;
372 cout << tr() << td(
"Request Method").set(
"class",
"title")
374 cout << tr() << td(
"Path Info").set(
"class",
"title")
375 << td(env.
getPathInfo()).set(
"class",
"data") << tr() << endl;
376 cout << tr() << td(
"Path Translated").set(
"class",
"title")
378 cout << tr() << td(
"Script Name").set(
"class",
"title")
379 << td(env.
getScriptName()).set(
"class",
"data") << tr() << endl;
380 cout << tr() << td(
"HTTP Referrer").set(
"class",
"title")
381 << td(env.
getReferrer()).set(
"class",
"data") << tr() << endl;
382 cout << tr() << td(
"HTTP Cookie").set(
"class",
"title")
383 << td(env.
getCookies()).set(
"class",
"data") << tr() << endl;
384 cout << tr() << td(
"Query String").set(
"class",
"title")
386 cout << tr() << td(
"Content Length").set(
"class",
"title")
388 << td() << tr() << endl;
389 cout << tr() << td(
"Post Data").set(
"class",
"title")
390 << td().set(
"class",
"data")
391 << pre(env.
getPostData()).set(
"class",
"data") << td()
393 cout << tr() << td(
"Remote Host").set(
"class",
"title")
394 << td(env.
getRemoteHost()).set(
"class",
"data") << tr() << endl;
395 cout << tr() << td(
"Remote Address").set(
"class",
"title")
396 << td(env.
getRemoteAddr()).set(
"class",
"data") << tr() << endl;
397 cout << tr() << td(
"Authorization Type").set(
"class",
"title")
398 << td(env.
getAuthType()).set(
"class",
"data") << tr() << endl;
399 cout << tr() << td(
"Remote User").set(
"class",
"title")
400 << td(env.
getRemoteUser()).set(
"class",
"data") << tr() << endl;
401 cout << tr() << td(
"Remote Identification").set(
"class",
"title")
403 cout << tr() << td(
"Content Type").set(
"class",
"title")
405 cout << tr() << td(
"HTTP Accept").set(
"class",
"title")
406 << td(env.
getAccept()).set(
"class",
"data") << tr() << endl;
407 cout << tr() << td(
"User Agent").set(
"class",
"title")
408 << td(env.
getUserAgent()).set(
"class",
"data") << tr() << endl;
409 cout << tr() << td(
"Server Software").set(
"class",
"title")
411 cout << tr() << td(
"Server Name").set(
"class",
"title")
412 << td(env.
getServerName()).set(
"class",
"data") << tr() << endl;
413 cout << tr() << td(
"Gateway Interface").set(
"class",
"title")
415 cout << tr() << td(
"Server Protocol").set(
"class",
"title")
417 cout << tr() << td(
"Server Port").set(
"class",
"title")
419 << td() << tr() << endl;
420 cout << tr() << td(
"HTTPS").set(
"class",
"title")
421 << td().set(
"class",
"data") << (env.
usingHTTPS() ?
"true" :
"false")
422 << td() << tr() << endl;
423 cout << tr() << td(
"Redirect Request").set(
"class",
"title")
425 cout << tr() << td(
"Redirect URL").set(
"class",
"title")
427 cout << tr() << td(
"Redirect Status").set(
"class",
"title")
430 cout << table() << cgicc::div() << endl;
435dumpList(
const Cgicc& formData)
437 cout << h2(
"Form Data via vector") << endl;
439 cout << cgicc::div().set(
"align",
"center") << endl;
441 cout << table()<< endl;
443 cout << tr().set(
"class",
"title") << td(
"Element Name")
444 << td(
"Element Value") << tr() << endl;
451 cout << tr().set(
"class",
"data") << td(iter->getName())
452 << td(iter->getValue()) << tr() << endl;
454 cout << table() << cgicc::div() << endl;
459showForm(
const Cgicc& formData)
463 cout << h2(
"Form Data via Cgicc") << endl;
465 cout << cgicc::div().set(
"class",
"notice") << endl;
469 if(name != (*formData).end() && ! name->isEmpty())
470 cout <<
"Your name is " << **name <<
'.' << br() << endl;
472 cout <<
"You don't have a name." << br() << endl;
476 if(salary != (*formData).end() && ! salary->isEmpty())
477 cout <<
"You make " << (*salary).getDoubleValue(80, 120)
478 <<
" million dollars." << br() << endl;
480 cout <<
"You don't have a salary." << br() << endl;
484 if(hours != (*formData).end() && ! (*hours).isEmpty())
485 cout <<
"You've wasted " << (*hours).getIntegerValue()
486 <<
" hours on the web." << br() << endl;
488 cout <<
"You haven't wasted any time on the web." << br() << endl;
492 if(thoughts != (*formData).end() && ! (*thoughts).isEmpty()) {
493 std::string temp = (*thoughts).getStrippedValue();
494 cout <<
"Your thoughts: " << temp << br() << endl;
497 cout <<
"You don't have any thoughts!?" << br() << endl;
501 cout <<
"You are hungry." << br() << endl;
503 cout <<
"You are not hungry." << br() << endl;
506 std::vector<FormEntry> flavors;
508 if(! flavors.empty()) {
510 for(std::string::size_type i = 0; i < flavors.size(); i++) {
511 cout << flavors[i].getValue();
512 if(i < flavors.size() - 2)
514 else if(i == flavors.size() - 2)
517 cout <<
" ice cream." << br() << endl;
520 cout <<
"You don't like ice cream!?" << br() << endl;
524 if(hair != (*formData).end())
525 cout <<
"Your hair is " << **hair <<
'.' << br() << endl;
527 cout <<
"You don't have any hair." << br() << endl;
529 cout <<
"You surf the web with " << formData(
"browser") <<
'.'
533 std::vector<FormEntry> authors;
535 if(! authors.empty()) {
536 cout <<
"You like to read books by ";
537 for(std::string::size_type i = 0; i < authors.size(); ++i) {
538 cout << authors[i].getValue();
539 if(i < authors.size() - 2)
541 else if(i == authors.size() - 2)
544 cout <<
"." << br() << endl;
547 cout <<
"You don't like to read!?" << br() << endl;
549 cout << cgicc::div() << endl;
Platform and operating system specific macro definitions.
The main header file for the GNU cgicc library.
The header file containing HTML output classes.
Class encapsulating the CGI runtime environment.
std::string getCookies() const
Get the HTTP cookies associated with this query, if any.
std::string getRemoteIdent() const
Get the remote user name retrieved from the server.
std::string getAccept() const
Get the MIME data types accepted by the client's browser.
std::string getReferrer() const
Get the URL of the page which called this CGI application.
std::string getScriptName() const
Get the full path to this CGI application.
std::string getRemoteAddr() const
Get the IP address of the remote machine making this request.
std::string getServerProtocol() const
Get the name and revision of the protocol used for this request.
bool usingHTTPS() const
Determine if this is a secure request.
std::string getRedirectRequest() const
Get the redirect request.
std::string getAuthType() const
Get the protocol-specific user authentication method used.
std::string getQueryString() const
Get the query string for this request.
std::string getPathTranslated() const
Get the translated path information (virtual to physical mapping).
std::string getPostData() const
Get the data passed to the CGI application via standard input.
std::string getServerName() const
Get the hostname, DNS name or IP address of the HTTP server.
std::string getRemoteHost() const
Get the hostname of the remote machine making this request.
std::string getServerSoftware() const
Get the name and version of the HTTP server software.
std::string getRedirectStatus() const
Get the redirect status.
unsigned long getServerPort() const
Get the port number on the server to which this request was sent.
std::string getPathInfo() const
Get the extra path information for this request, given by the client.
std::string getRemoteUser() const
Get the authenticated remote user name.
std::string getContentType() const
Get the content type of the submitted information.
std::string getUserAgent() const
Get the name of the browser used for this CGI request.
std::string getRequestMethod() const
Get the request method used for this query.
unsigned long getContentLength() const
Get the length of the data read from standard input, in chars.
std::string getRedirectURL() const
Get the redirect URL.
std::string getGatewayInterface() const
Get the name and version of the gateway interface.
The main class of the GNU cgicc library.
void restore(const std::string &filename)
Restore from a previously-saved CGI environment.
const char * getCompileDate() const
Get the date on which this library was compiled.
const CgiEnvironment & getEnvironment() const
bool queryCheckbox(const std::string &elementName) const
Query whether a checkbox is checked.
const char * getVersion() const
Get the version number of cgicc.
const char * getCompileTime() const
Get the time at which this library was compiled.
const char * getHost() const
Get the platform for which Cgicc was configured.
void save(const std::string &filename) const
Save the current CGI environment to a file.
const std::vector< FormEntry > & getElements() const
Get all the submitted form elements, excluding files.
form_iterator getElement(const std::string &name)
Find a radio button in a radio group, or a selected list item.
Specifies the DTD of the HTML 4 document.
HTMLElement & set(const std::string &name)
Set an HTMLAttribute on this HTMLElement.
The namespace containing the cgicc library.
std::vector< FormEntry >::const_iterator const_form_iterator
A vector of const FormEntry objects.
HTMLAttributeList set(const std::string &name)
Create a new HTMLAttributeList, and set an HTMLAttribute.