cookie.cpp
Go to the documentation of this file.
1 /* -*-mode:c++; c-file-style: "gnu";-*- */
2 /*
3  * $Id: cookie.cpp,v 1.10 2007/07/02 18:48:19 sebdiaz Exp $
4  *
5  * Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
6  * 2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
7  * Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
22  */
23 
30 #include <new>
31 #include <string>
32 #include <vector>
33 #include <stdexcept>
34 #include <iostream>
35 #include <cstdlib>
36 
37 #include "cgicc/CgiDefs.h"
38 #include "cgicc/Cgicc.h"
39 #include "cgicc/HTTPHTMLHeader.h"
40 #include "cgicc/HTMLClasses.h"
41 
42 #if HAVE_SYS_UTSNAME_H
43 # include <sys/utsname.h>
44 #endif
45 
46 #if HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 #endif
49 
50 #include "styles.h"
51 
52 using namespace std;
53 using namespace cgicc;
54 
55 // Print the form for this CGI
56 void
57 printForm(const Cgicc& cgi)
58 {
59  cout << "<form method=\"post\" action=\""
60  << cgi.getEnvironment().getScriptName() << "\">" << endl;
61 
62  cout << "<table>" << endl;
63 
64  cout << "<tr><td class=\"title\">Cookie Name</td>"
65  << "<td class=\"form\">"
66  << "<input type=\"text\" name=\"name\" />"
67  << "</td></tr>" << endl;
68 
69  cout << "<tr><td class=\"title\">Cookie Value</td>"
70  << "<td class=\"form\">"
71  << "<input type=\"text\" name=\"value\" />"
72  << "</td></tr>" << endl;
73 
74  cout << "</table>" << endl;
75 
76  cout << "<div class=\"center\"><p>"
77  << "<input type=\"submit\" name=\"submit\" value=\"Set the cookie\" />"
78  << "<input type=\"reset\" value=\"Nevermind\" />"
79  << "</p></div></form>" << endl;
80 }
81 
82 // Main Street, USA
83 int
84 main(int /*argc*/,
85  char ** /*argv*/)
86 {
87  try {
88 #if HAVE_GETTIMEOFDAY
89  timeval start;
90  gettimeofday(&start, NULL);
91 #endif
92 
93  // Create a new Cgicc object containing all the CGI data
94  Cgicc cgi;
95 
96  // Get the name and value of the cookie to set
97  const_form_iterator name = cgi.getElement("name");
98  const_form_iterator value = cgi.getElement("value");
99 
100  // Output the headers for an HTML document with the cookie only
101  // if the cookie is not empty
102  if(name != cgi.getElements().end() && value != cgi.getElements().end()
103  && value->getValue().empty() == false)
104  cout << HTTPHTMLHeader()
105  .setCookie(HTTPCookie(name->getValue(), value->getValue()));
106  else
107  cout << HTTPHTMLHeader();
108 
109  // Output the HTML 4.0 DTD info
110  cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
111  cout << html().set("lang", "en").set("dir", "ltr") << endl;
112 
113  // Set up the page's header and title.
114  // I will put in lfs to ease reading of the produced HTML.
115  cout << head() << endl;
116 
117  // Output the style sheet portion of the header
118  cout << style() << comment() << endl;
119  cout << styles;
120  cout << comment() << style() << endl;
121 
122  cout << title() << "GNU cgicc v" << cgi.getVersion()
123  << " HTTPCookie" << title() << endl;
124 
125  cout << head() << endl;
126 
127  // Start the HTML body
128  cout << body() << endl;
129 
130  cout << h1() << "GNU cgi" << span("cc").set("class","red")
131  << " v"<< cgi.getVersion() << " HTTPCookie Test Results"
132  << h1() << endl;
133 
134  // Get a pointer to the environment
135  const CgiEnvironment& env = cgi.getEnvironment();
136 
137  // Generic thank you message
138  cout << comment() << "This page generated by cgicc for "
139  << env.getRemoteHost() << comment() << endl;
140  cout << h4() << "Thanks for using cgi" << span("cc").set("class", "red")
141  << ", " << env.getRemoteHost()
142  << '(' << env.getRemoteAddr() << ")!" << h4() << endl;
143 
144  if(name != cgi.getElements().end() && value != cgi.getElements().end()
145  && value->getValue().empty() == false) {
146  cout << p() << "A cookie with the name " << em(name->getValue())
147  << " and value " << em(value->getValue()) << " was set." << br();
148  cout << "In order for the cookie to show up here you must "
149  << a("refresh").set("href",env.getScriptName()) << p();
150  }
151 
152  // Show the cookie info from the environment
153  cout << h2("Cookie Information from the Environment") << endl;
154 
155  cout << cgicc::div().set("align","center") << endl;
156 
157  cout << table() << endl;
158 
159  cout << tr() << td("HTTPCookie").set("class","title")
160  << td(env.getCookies()).set("class","data") << tr() << endl;
161 
162  cout << table() << cgicc::div() << endl;
163 
164 
165  // Show the cookie info from the cookie list
166  cout << h2("HTTP Cookies via vector") << endl;
167 
168  cout << cgicc::div().set("align","center") << endl;
169 
170  cout << table() << endl;
171 
172  cout << tr().set("class","title") << td("Cookie Name")
173  << td("Cookie Value") << tr() << endl;
174 
175  // Iterate through the vector, and print out each value
177  for(iter = env.getCookieList().begin();
178  iter != env.getCookieList().end();
179  ++iter) {
180  cout << tr().set("class","data") << td(iter->getName())
181  << td(iter->getValue()) << tr() << endl;
182  }
183  cout << table() << cgicc::div() << endl;
184 
185 
186  // Print out the form to do it again
187  cout << br() << endl;
188  printForm(cgi);
189  cout << hr().set("class", "half") << endl;
190 
191  // Information on cgicc
192  cout << cgicc::div().set("align","center").set("class","smaller") << endl;
193  cout << "GNU cgi" << span("cc").set("class","red") << " v";
194  cout << cgi.getVersion() << br() << endl;
195  cout << "Compiled at " << cgi.getCompileTime();
196  cout << " on " << cgi.getCompileDate() << br() << endl;
197 
198  cout << "Configured for " << cgi.getHost();
199 #if HAVE_UNAME
200  struct utsname info;
201  if(uname(&info) != -1) {
202  cout << ". Running on " << info.sysname;
203  cout << ' ' << info.release << " (";
204  cout << info.nodename << ")." << endl;
205  }
206 #else
207  cout << "." << endl;
208 #endif
209 
210 #if HAVE_GETTIMEOFDAY
211  // Information on this query
212  timeval end;
213  gettimeofday(&end, NULL);
214  long us = ((end.tv_sec - start.tv_sec) * 1000000)
215  + (end.tv_usec - start.tv_usec);
216 
217  cout << br() << "Total time for request = " << us << " us";
218  cout << " (" << static_cast<double>(us/1000000.0) << " s)";
219 #endif
220 
221  // End of document
222  cout << cgicc::div() << endl;
223  cout << body() << html() << endl;
224 
225  // No chance for failure in this example
226  return EXIT_SUCCESS;
227  }
228 
229  // Did any errors occur?
230  catch(const std::exception& e) {
231 
232  // This is a dummy exception handler, as it doesn't really do
233  // anything except print out information.
234 
235  // Reset all the HTML elements that might have been used to
236  // their initial state so we get valid output
237  html::reset(); head::reset(); body::reset();
238  title::reset(); h1::reset(); h4::reset();
239  comment::reset(); td::reset(); tr::reset();
240  table::reset(); cgicc::div::reset(); p::reset();
241  a::reset(); h2::reset(); colgroup::reset();
242 
243  // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
244  cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
245  cout << html().set("lang","en").set("dir","ltr") << endl;
246 
247  // Set up the page's header and title.
248  // I will put in lfs to ease reading of the produced HTML.
249  cout << head() << endl;
250 
251  // Output the style sheet portion of the header
252  cout << style() << comment() << endl;
253  cout << "body { color: black; background-color: white; }" << endl;
254  cout << "hr.half { width: 60%; align: center; }" << endl;
255  cout << "span.red, strong.red { color: red; }" << endl;
256  cout << "div.notice { border: solid thin; padding: 1em; margin: 1em 0; "
257  << "background: #ddd; }" << endl;
258 
259  cout << comment() << style() << endl;
260 
261  cout << title("GNU cgicc exception") << endl;
262  cout << head() << endl;
263 
264  cout << body() << endl;
265 
266  cout << h1() << "GNU cgi" << span("cc", set("class","red"))
267  << " caught an exception" << h1() << endl;
268 
269  cout << cgicc::div().set("align","center").set("class","notice") << endl;
270 
271  cout << h2(e.what()) << endl;
272 
273  // End of document
274  cout << cgicc::div() << endl;
275  cout << hr().set("class","half") << endl;
276  cout << body() << html() << endl;
277 
278  return EXIT_SUCCESS;
279  }
280 }
std::vector< FormEntry >::const_iterator const_form_iterator
A vector of const FormEntry objects.
Definition: Cgicc.h:68
std::string getRemoteHost() const
Get the hostname of the remote machine making this request.
const std::vector< FormEntry > & getElements() const
Get all the submitted form elements, excluding files.
Definition: Cgicc.h:348
HTTPHeader & setCookie(const HTTPCookie &cookie)
Set a cookie to go out with this HTTPResponseHeader.
Definition: HTTPHeader.h:88
const char * getCompileDate() const
Get the date on which this library was compiled.
Shortcut to HTTPContentHeader for text/html.
Platform and operating system specific macro definitions.
HTMLElement & set(const std::string &name)
Set an HTMLAttribute on this HTMLElement.
std::string getCookies() const
Get the HTTP cookies associated with this query, if any.
const char * getCompileTime() const
Get the time at which this library was compiled.
int main(int argc, char **argv)
The main function.
Definition: cardgame.cpp:1483
const std::vector< HTTPCookie > & getCookieList() const
Get a vector containing the HTTP cookies associated with this query.
form_iterator getElement(const std::string &name)
Find a radio button in a radio group, or a selected list item.
The main header file for the GNU cgicc library.
An HTML comment.
Definition: HTMLClasses.h:92
std::string getScriptName() const
Get the full path to this CGI application.
The main class of the GNU cgicc library.
Definition: Cgicc.h:103
Shortcut to HTTPContentHeader for text/html.
const CgiEnvironment & getEnvironment() const
Definition: Cgicc.h:394
const char * getVersion() const
Get the version number of cgicc.
An HTTP cookie.
Definition: HTTPCookie.h:58
std::string getRemoteAddr() const
Get the IP address of the remote machine making this request.
Specifies the DTD of the HTML 4 document.
Definition: HTMLDoctype.h:56
const char * getHost() const
Get the platform for which Cgicc was configured.
The namespace containing the cgicc library.
Definition: Cgicc.h:52
The header file containing HTML output classes.
std::vector< HTTPCookie >::const_iterator const_cookie_iterator
A vector of const HTTPCookie objects.
Class encapsulating the CGI runtime environment.

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 2004 Stephen F. Booth
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front Cover Texts, and with no Back-Cover Texts.
Documentation generated Sun Oct 16 2016 16:14:42 for cgicc by doxygen 1.8.12