|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r51002 - sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2
From: lists.drrngrvy_at_[hidden]
Date: 2009-02-03 18:48:06
Author: drrngrvy
Date: 2009-02-03 18:48:06 EST (Tue, 03 Feb 2009)
New Revision: 51002
URL: http://svn.boost.org/trac/boost/changeset/51002
Log:
Adding cookie_game2 folder.
Added:
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/Jamfile.v2 (contents, props changed)
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/index.html (contents, props changed)
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/main.cpp (contents, props changed)
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/main.js (contents, props changed)
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/style.css (contents, props changed)
Added: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/Jamfile.v2 2009-02-03 18:48:06 EST (Tue, 03 Feb 2009)
@@ -0,0 +1,20 @@
+# Copyright (c) 2007 Darren Garvey
+#
+# Distributed under the Boost Software License, Version 1.0.
+# (See accompanying file LICENSE_1_0.txt or copy
+# at http://www.boost.org/LICENSE_1_0.txt)
+
+project boost/cgi/example/acgi/cookie_game ;
+
+exe acgi_cookie_game : main.cpp /boost/regex/ ;
+
+# Our install rule (builds binaries and copies them to <location>)
+install install
+ :
+ acgi_cookie_game
+ :
+ <location>$(cgi-bin)
+ ;
+
+# Only install example if you use `bjam install' or equivalent
+explicit install ;
Added: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/index.html
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/index.html 2009-02-03 18:48:06 EST (Tue, 03 Feb 2009)
@@ -0,0 +1,47 @@
+
+
+<html>
+<head>
+ <title>aCGI Cookie Game Example (using Google cTemplate)</title>
+ <link rel="stylesheet" type="text/css" href="/css/style.css" />
+ <script type="text/javascript" src="/js/main.js"></script>
+</head>
+<body>
+
+{{#HAS_NAME_IN_COOKIE_true}}
+ <h1>Hello again {{USER_NAME}}</h1>
+ <p></p>
+{{/HAS_NAME_IN_COOKIE_true}}
+{{#HAS_NAME_IN_COOKIE_false}}
+ <h1>Hello there.</h1>
+{{/HAS_NAME_IN_COOKIE_false}}
+
+<p>You can add cookies using the form below. If you add a cookie value for 'name', it will show up above.</p>
+<p>Here is list of the cookies you currently have:</p>
+
+
+{{#DATA_MAP}}
+ {{#ERROR}}
+ EMPTY
+ {{/ERROR}}
+ <ul class="nvpair">
+ {{#ROW}}
+ <li class="name">{{NAME}}</li><li class="value">{{VALUE}}</li>
+ {{/ROW}}
+ <div class="clear"></div>
+ </ul>
+{{/DATA_MAP}}
+
+ <form method="get" action="{{SCRIPT_NAME}}" id="getform">
+ <label for="name" class="name">Name:</label>
+ <input id="name" name="name" class="value" type="text" value=" {{COOKIE_NAME}}"></input>
+ <label for="value" class="name">Value:</label>
+ <input id="value" name="value" class="value" type="text" value=" {{COOKIE_VALUE}}"></input>
+ <label for="del" class="name">Delete this cookie?</label>
+ <input id="del" name="del" class="value" type="checkbox"></input>
+ <div class="clear"></div>
+ <input type="submit"></input>
+ </form>
+ <input type="submit" onclick="switch_method('getform'); switch_value(this); return false;" value="Switch to POST"></input>
+</body>
+</html>
\ No newline at end of file
Added: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/main.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/main.cpp 2009-02-03 18:48:06 EST (Tue, 03 Feb 2009)
@@ -0,0 +1,133 @@
+#include <boost/cgi/acgi.hpp>
+#include <boost/cgi/utility.hpp>
+#include <google/template.h>
+
+/**
+ * The following example has a few stages.
+ * It is best understood by compiling and testing it, and then looking at
+ * the source code.
+ */
+
+using namespace boost::acgi;
+
+// The types we use. Only here because this is an example.
+
+// Uses cTemplate, from Google. It's simple and powerful.
+typedef google::Template stencil_type;
+// You will usually load a template and then populate variables in it
+// using a TemplateDictionary.
+typedef google::TemplateDictionary dictionary_type;
+// The acgi and fcgi parts of the CGI library use a `service` class to
+// manage asynchronous dispatching (eg. async I/O). If you're not interested
+// in async I/O, you can just use the plain cgi stuff (which is the same as
+// acgi, but without the *a*sync bits).
+// If you're unsure, you can use acgi without having to really do anything with
+// the service - it's only used on two lines in this example. In one of them
+// it is constructed...
+typedef service service_type;
+// ... and on the other it's used to construct the `request`.
+typedef request request_type;
+// The `response` will make your life easier (and more efficient).
+typedef response response_type;
+
+// These are some of the functions / types / enums used in this example.
+using boost::acgi::has_key;
+using boost::acgi::cookie;
+using boost::acgi::header;
+using boost::acgi::redirect;
+using boost::acgi::parse_all;
+using boost::acgi::form;
+using boost::acgi::cookies;
+using boost::acgi::content_type;
+
+// This function just makes it easier to change the templating engine. It's
+// only here to keep the cTemplate code out of the core of this example...
+stencil_type* get_stencil(std::string const& filename)
+{
+ return google::Template::GetTemplate(filename, google::STRIP_WHITESPACE);
+}
+
+// Show the data in the passed map, updating the passed dictionary.
+template<typename MapT, typename Dict>
+void print_formatted_data(MapT& data, Dict& dict)
+{
+ Dict* subd = dict.AddSectionDictionary("DATA_MAP");
+ if (data.empty())
+ subd->ShowSection("EMPTY");
+ else
+ for(typename MapT::const_iterator iter=data.begin(), end = data.end(); iter != end; ++iter)
+ {
+ Dict* row_dict = subd->AddSectionDictionary("ROW");
+ row_dict->SetValue("NAME", iter->first.c_str());
+ row_dict->SetValue("VALUE", iter->second);
+ row_dict->ShowSection("ROW");
+ }
+}
+
+
+int main()
+{
+ try {
+ service_type srv;
+ request_type req(srv);
+
+ // Load up the request data
+ req.load(parse_all);
+
+ response_type resp;
+
+ if (has_key(req[form], "reset") && req[form]["reset"] == "true")
+ {
+ resp<< cookie("name")
+ << redirect(req, req.script_name()); // redirect them.
+ resp.send(req.client());
+ return 0;
+ }
+
+ if (has_key(req[form], "name"))
+ {
+ if (has_key(req[form], "del"))
+ resp<< cookie(req[form]["name"]);
+ else
+ resp<< cookie(req[form]["name"], req[form]["value"]);
+ resp<< redirect(req, req.script_name());
+ return_(resp, req, http::ok);
+ }
+
+ dictionary_type dict("cookie-game dict");
+
+ // First, see if they have a cookie set
+ if (has_key(req[cookies], "name"))
+ dict.SetValueAndShowSection("USER_NAME", req[cookies]["name"], "HAS_NAME_IN_COOKIE_true");
+ else
+ dict.ShowSection("HAS_NAME_IN_COOKIE_false");
+
+ print_formatted_data(req[cookies], dict);
+
+ dict.SetValue("SCRIPT_NAME", req.script_name());
+ dict.SetValue("COOKIE_NAME", get_value(req[form], "name", ""));
+ dict.SetValue("COOKIE_VALUE", req[form]["value"]);
+
+ // Load the HTML stencil now from the index.html file.
+ stencil_type* stencil = get_stencil("../templates/index.html");
+
+ // Expand the stencil with the the given dictionary into `output`.
+ std::string output;
+ stencil->Expand(&output, &dict);
+
+ // Add the template to the response.
+ resp<< content_type("text/html")
+ << output;
+
+ // Send the response to the requestor and return control.
+ return_(resp, req, http::ok);
+
+ }catch(std::exception* e){
+ std::cout<< "Exception: [" << typeid(e).name() << "] - " << e->what() << std::endl;
+ }catch(std::exception const& e){
+ std::cout<< "Exception: [" << typeid(e).name() << "] - " << e.what() << std::endl;
+ }catch(...){
+ std::cout<< "boom<blink>.</blink>";
+ }
+}
+
Added: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/main.js
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/main.js 2009-02-03 18:48:06 EST (Tue, 03 Feb 2009)
@@ -0,0 +1,30 @@
+function switch_method(ident) {
+ var f = document.getElementById(ident);
+ if (f.method == 'post') f.method = 'GET';
+ else f.method = 'POST';
+}
+function switch_value(elem) {
+ if (elem.value == 'Switch to POST') {
+ elem.value = 'Switch to GET';
+ }else{
+ elem.value = 'Switch to POST';
+ }
+}
+function switch_form(elem) {
+ var g = document.getElementById('getform');
+ var p = document.getElementById('postform');
+ if (elem.value == 'Switch to POST') {
+ p.style.visibility = 'visible';
+ p.style.display = 'block';
+ g.style.visibility = 'hidden';
+ g.style.display = 'none';
+ elem.value = 'Switch to GET';
+ } else {
+ g.style.visibility = 'visible';
+ g.style.display = 'block';
+ p.style.visibility = 'hidden';
+ p.style.display = 'none';
+ elem.value = 'Switch to POST';
+ }
+}
+
Added: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/style.css
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/acgi/cookie_game2/style.css 2009-02-03 18:48:06 EST (Tue, 03 Feb 2009)
@@ -0,0 +1,112 @@
+body { padding: 0; margin: 3%; border-color: #efe; }
+.var_map_title
+ { font-weight: bold; font-size: large; }
+.var_map
+ { border: 1px dotted; padding: 2px 3px 2px 3px; margin-bottom: 3%; }
+.pair
+ { border-top: 1px dotted; overflow: auto; padding: 0; margin: 0; }
+/*
+.name
+ { position: relative; float: left; width: 30%; font-weight: bold; }
+.value
+ { position: relative; float: left; width: 65%; left: 1%;
+ border-left: 1px solid; padding: 0 5px 0 5px;
+ overflow: auto; white-space: pre; }
+*/
+.info
+{
+ border-color: #ca3766;
+ border-width: 1px 0 1px 0;
+ border-style: solid;
+ padding: 2px 8px 2px 8px;
+ margin: 1em
+}
+#response
+{
+ text-align: center;
+ padding: 20px 30px 20px 30px;
+ margin: 1em;
+ border-width: 2px 0 2px 0;
+ border-style: solid;
+ border-color: #ca3766;
+}
+.var_map_title
+{
+ font-weight: bold;
+ font-size: large;
+}
+.var_map
+{
+ border: 1px dotted;
+ padding: 2px 3px 2px 3px;
+ margin: 15px 0 15px 0;
+}
+.var_pair
+{
+ border-top: 1px dotted;
+ overflow: auto;
+ padding: 0;
+ margin: 0;
+}
+.var_name
+{
+ position: relative;
+ float: left;
+ width: 30%;
+ font-weight: bold;
+}
+.var_value
+{
+ position: relative;
+ float: left;
+ width: 65%;
+ left: 1%;
+ border-left: 1px solid;
+ padding: 0 5px 0 5px;
+ overflow: auto;
+ white-space: pre;
+}
+.nvpair
+{
+ list-style-type: none;
+}
+form
+{
+}
+input
+{
+border-width: 0 0 1px 0;
+border-style: solid;
+border-color: #444;
+padding: 2px 3px 2px 3px;
+margin-left: 1em;
+background: #FaFbFd;
+}
+input[type=submit]
+{
+ margin-left: 35%;
+}
+.name, .value
+{
+ display: inline;
+ width: 20%;
+ float: left;
+}
+.name
+{
+ text-align: right;
+ padding-right: 1em;
+ clear: left;
+}
+.value
+{
+ text-align: left;
+ padding-left: 1em;
+}
+.clear { clear: both; }
+.cookies { margin: 10px 0 25px 0; }
+#postform
+{
+ visibility: hidden;
+ display: none;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk