Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61678 - in sandbox/SOC/2007/cgi/trunk: boost/cgi boost/cgi/common boost/cgi/impl libs/cgi/doc/src libs/cgi/doc/src/images libs/cgi/doc/src/images/callouts libs/cgi/doc/src/tutorial libs/cgi/example libs/cgi/example/cgi libs/cgi/example/cgi/cookies libs/cgi/example/cgi/custom_sessions libs/cgi/example/cgi/sessions libs/cgi/example/fcgi libs/cgi/example/fcgi/echo libs/cgi/test/run
From: lists.drrngrvy_at_[hidden]
Date: 2010-04-29 12:10:04


Author: drrngrvy
Date: 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
New Revision: 61678
URL: http://svn.boost.org/trac/boost/changeset/61678

Log:
* Moving some configuration macros out to config.hpp
* Re-adding ability to auto-remove uploaded files, based on compile time options
* A few updates to the examples and docs.
* Add basic test for sessions
Added:
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/1.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/10.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/11.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/12.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/13.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/14.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/15.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/2.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/3.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/4.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/5.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/6.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/7.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/8.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/9.png (contents, props changed)
   sandbox/SOC/2007/cgi/trunk/libs/cgi/test/run/sessions.cpp (contents, props changed)
Text files modified:
   sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp | 9 --
   sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_parser.hpp | 2
   sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_part.hpp | 7 -
   sandbox/SOC/2007/cgi/trunk/boost/cgi/config.hpp | 31 ++++++
   sandbox/SOC/2007/cgi/trunk/boost/cgi/impl/form_parser.ipp | 14 ---
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/cgi.qbk | 7
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/examples.qbk | 1
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/introduction.qbk | 25 ++---
   sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/tutorial/cgi_quickstart.cpp | 19 ++-
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/cookies/main.cpp | 3
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/custom_sessions/main.cpp | 4
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/doc.qbk | 6
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/sessions/main.cpp | 36 ++++++-
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/doc.qbk | 2
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/doc.qbk | 12 --
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp | 180 +++++++++++++++++++--------------------
   16 files changed, 186 insertions(+), 172 deletions(-)

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -40,15 +40,6 @@
 # include "boost/cgi/utility/sessions.hpp"
 #endif // BOOST_CGI_ENABLE_SESSIONS
 
-#ifndef BOOST_CGI_POST_MAX
- /// Restrict POST data to less than 7MB per request.
-# define BOOST_CGI_POST_MAX 6663322
-#endif // BOOST_CGI_POST_MAX
-
-#ifndef BOOST_CGI_DEFAULT_CHARSET
-# define BOOST_CGI_DEFAULT_CHARSET "UTF-8"
-#endif
-
 BOOST_CGI_NAMESPACE_BEGIN
  namespace common {
 

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_parser.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_parser.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_parser.hpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -40,7 +40,7 @@
  * directory containing executable files.
  */
 #if !defined(BOOST_CGI_UPLOAD_DIRECTORY)
-# define BOOST_CGI_UPLOAD_DIRECTORY "../upload/"
+# define BOOST_CGI_UPLOAD_DIRECTORY "../uploads/"
 #endif // BOOST_CGI_UPLOAD_DIRECTORY
 
 BOOST_CGI_NAMESPACE_BEGIN

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_part.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_part.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/common/form_part.hpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -35,20 +35,17 @@
      
      ~form_part()
      {
- /*
+#ifndef BOOST_CGI_KEEP_FILE_UPLOADS
        try {
            // Delete any file saved in uploading this.
            namespace fs = boost::filesystem;
            if (!path.empty() && fs::exists(path)) {
-#ifndef NDEBUG
- std::cerr<< "Removing file: " << path << std::endl;
-#endif // NDEBUG
              fs::remove(path);
            }
        } catch(...) {
            // pass
        }
- */
+#endif // BOOST_CGI_KEEP_FILE_UPLOADS
      }
 
      meta_data_map_type meta_data_;

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/config.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/config.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/config.hpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -13,10 +13,41 @@
  namespace cgi {}
 } // namespace boost
 
+/// Make the library keep file uploads on disk.
+/**
+ * File uploads are save to the hard disk in an upload directory. By default,
+ * they are deleted at the end of the request.
+ */
+//#define BOOST_CGI_KEEP_FILE_UPLOADS
+
 #ifndef NDEBUG
 # include <iostream>
 #endif // NDEBUG
 
+/// Characters that should be removed from any file uploads.
+/**
+ * The filename of any file uploads is a stripped version of
+ * the provided file's name. As such, we need to clean the uploaded
+ * file's name.
+ */
+#ifndef BOOST_CGI_UNSAFE_FILENAME_CHARS
+# define BOOST_CGI_UNSAFE_FILENAME_CHARS ":~.|/\\"
+#endif // BOOST_CGI_UNSAFE_FILENAME_CHARS
+
+/// The maximum amount of POST data that can be uploaded.
+/**
+ * Set this to -1 for no limit.
+ */
+#ifndef BOOST_CGI_POST_MAX
+ /// Restrict POST data to less than 7MB per request.
+# define BOOST_CGI_POST_MAX 6663322
+#endif // BOOST_CGI_POST_MAX
+
+/// The default charset to add to responses.
+#ifndef BOOST_CGI_DEFAULT_CHARSET
+# define BOOST_CGI_DEFAULT_CHARSET "UTF-8"
+#endif
+
 /// The namespace of the library.
 /**
  * Default value:

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/impl/form_parser.ipp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/impl/form_parser.ipp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/impl/form_parser.ipp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -19,17 +19,6 @@
 #include <fstream>
 #include <boost/lexical_cast.hpp>
 #include <boost/algorithm/string/trim.hpp>
-#include <boost/random/mersenne_twister.hpp>
-
-/// Characters that should be removed from any file uploads.
-/**
- * The filename of any file uploads is a stripped version of
- * the provided file's name. As such, we need to clean the uploaded
- * file's name.
- */
-#ifndef BOOST_CGI_UNSAFE_FILENAME_CHARS
-# define BOOST_CGI_UNSAFE_FILENAME_CHARS ":~.|"
-#endif // BOOST_CGI_UNSAFE_FILENAME_CHARS
 
 BOOST_CGI_NAMESPACE_BEGIN
 
@@ -95,7 +84,6 @@
             name.swap(result);
             break;
          case '&': // we now have the name/value pair, so save it
- // **FIXME** have to have .c_str() ?
             context_->data_map[name.c_str()] = result;
             result.clear();
             name.clear();
@@ -189,7 +177,6 @@
           // Load the data to a local file.
           string_type content (
               buffer.substr(meta.length()+4, next_pos-meta.length()-4));
- //boost::mt19937 rng(time(NULL));
           string_type randomatter (
             boost::lexical_cast<string_type>(time(NULL)));
           string_type user_ip (context_->random_string);
@@ -202,7 +189,6 @@
               internal_filename.c_str()
             , std::ios::out | std::ios::binary);
           file<< content;
- //file.flush();
           context_->uploads_map[part.name.c_str()] = part;
         }
       }

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/cgi.qbk
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/cgi.qbk (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/cgi.qbk 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -86,9 +86,9 @@
 These documents are either hosted at [@http://sourceforge.net/projects/cgi/ sourceforge], or at [@http://svn.boost.org/svn/boost/browser/sandbox/SOC/2007/cgi svn.boost.org]. For free!
 ]
 
-[/include preface.qbk]
+[include preface.qbk]
 
-[/include:intro introduction.qbk]
+[include:intro introduction.qbk]
 
 [/include quickstart.qbk]
 
@@ -110,7 +110,7 @@
 
 [/xinclude ../cgi_dox.xml]
 
-[/include:examples examples.qbk]
+[include:examples examples.qbk]
 
 [/include:reference reference.qbk]
 
@@ -127,3 +127,4 @@
 [/xinclude quickref.xml]
 
 [/include reference.qbk]
+

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/examples.qbk
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/examples.qbk (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/examples.qbk 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -1 +1,2 @@
 
+[include ../example/doc.qbk]

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/1.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/10.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/11.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/12.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/13.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/14.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/15.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/2.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/3.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/4.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/5.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/6.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/7.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/8.png
==============================================================================
Binary file. No diff available.

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/images/callouts/9.png
==============================================================================
Binary file. No diff available.

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/introduction.qbk
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/introduction.qbk (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/introduction.qbk 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -31,18 +31,13 @@
 Output (as viewed in a web browser):
 ``
 Hello there, Universe. -- Mr. Allison
-
 ``
 
-This CGI library is a reasonably high-level library for creating CGI or FastCGI programs. Its scope is intentionally limited to the ''controller'' portion of the Model-View-Controller idiom. In other words, XML/HTML templates are not addressed, even if their use is highly recommended.
-
-[tip
- The __amort_example__ example uses Google cTemplate for dealing with HTML templates. Consider having a look at it, or at the upcoming ''Karma'' part of Boost.Spirit.
-]
+This CGI library is a reasonably high-level library for creating CGI or FastCGI programs and its scope is intentionally limited to the ''controller'' portion of the Model-View-Controller idiom. If you want access to XML / HTML templates, you should consider taking a look at [@http://code.google.com/p/google-ctemplate/ cTemplate] or the Karma part of [@http://boost.org/libs/spirit/ Boost.Spirit]. A very lightweight wrapper over cTemplate is provided with the library in the form of the [classref boost::cgi::common::stencil stencil] class, which you may find useful.
 
 [h4 Concepts]
 
-The library provides abstractions which hide details of the varying specifications of CGI, FastCGI and SCGI. The main abstractions are, briefly:
+The library provides abstractions which hide details of the varying specifications of CGI and FastCGI. The main abstractions are, briefly:
 
 [table
 [
@@ -68,30 +63,28 @@
 ]
 [
   [`Client`[footnote Taken from simultaneous suggestions by Phil Endecott and Chris Kohlhoff].]
- [Each request has an associated Client, which may represent a connection to the HTTP server associated with the current request. It is used when writing replies to requests. Note: it is possible to avoid exposure to it entirely if you use the `response` class and the `return_` macro.
+ [Each request has an associated Client, which may represent a connection to the HTTP server associated with the current request. It is used when writing replies to requests. Note: it is possible to avoid exposure to it entirely if you use the [classref boost::cgi::common::response response] class and [funcref boost::cgi::common::commit commit()].
   ]
 ]
 ]
 
 [h4 Protcols]
 
-[:['See __protocol_details__ for more.]] [/ **FIXME** ]
-
 In a nutshell, CGI is the simple and 'original' way of communicating with web servers. I'll assume you know what it is: one request per process and communication using standard input/output. A nice and simple way of making ['dynamic web pages].
 
-[tip If you're new to CGI, have a look at this: [@http://hoohoo.ncsa.uiuc.edu/cgi/]]
+[tip If you're new to CGI, have a look at the original "spec": [@http://hoohoo.ncsa.uiuc.edu/cgi/] or on [@http://wikipedia.org/wiki/Common_Gateway_Interface Wikipedia].]
 
-__FastCGI__ was then developed as a means of allowing much more scalable CGI-like programs to be written. In fact, the FastCGI specification implies scalability was the main motivation for the protocol. Communication with the server works over sockets or pipes (only TCP sockets are supported for now). Each process and each connection can be used for handling multiple requests. In theory this means you could have a single monolithic process behind your HTTP server handling all incoming requests concurrently.
+FastCGI was developed as a means of allowing much more scalable CGI-like programs to be written. In fact, the FastCGI specification implies extreme efficiency and scalability were key motivations for the protocol. In FastCGI, communication with the server works over sockets or pipes using a binary-encoded protocol. Each process and each connection can be used for handling multiple requests, simultaneously if necessary. In theory this means you could have a single monolithic process behind your HTTP server handling all incoming requests concurrently.
 
 [note
- Some initial benchmarks show that simple FastCGI programs are 3-5x faster than their CGI counterparts. When using database connections and/or HTML templates, the gap should get even bigger.
+ Some initial benchmarks show that the simplest FastCGI programs are 3-5x faster than their CGI counterparts. When database connections, HTML templates or any other expensive state is required the gap will be more significant.
 ]
 
-__SCGI__ is essentially a simpler version of FastCGI - hence [*S]imple[*CGI] - but is still a significant step up from vanilla CGI, mainly because each process can handle multiple requests. Use of FastCGI is recommended over SCGI, but unfortunately support for FastCGI is unreliable and buggy with some servers and/or you may not have that option. [/ **FIXME**] SCGI support isn't included yet.
-
 [h4 Multiple Requests per Process]
 
-Having persistent processes is very handy. It removes so many of the limitations of traditional CGI programming; suddenly CGI programs aren't so different to desktop applications. CGI 'scripts' become FastCGI servers, capable of handling an arbitrary number of requests with each invocation (assuming they don't crash, or leak memory!). This gives you the freedom to keep database connections open between requests or cache ready-parsed responses, for example. Processing of a client request can even be continued in the case of the client crashing - the `response` can then be stored and given to them when they return - saving precious CPU cycles.
+Having persistent processes is very handy: this feature alone removes many of the limitations of traditional CGI programming.
+
+Instead of CGI 'scripts', you can suddenly have FastCGI daemons, which aren't so different to desktop applications! Your web applications can handle an arbitrary number of requests with each invocation (assuming they don't crash, or leak memory!). You have the freedom to keep database connections open between requests or cache ready-parsed responses, for example. It becomes realistic to store expensive state in your web applications, safely under the assumption that your program starts up and shuts down infrequently and done preemptively by your favourite web server. Processing of a client request can even be continued in the case of the client crashing - the `response` can then be stored and given to them when they return - saving precious CPU cycles.
 
 The downside is added complexity: managing multiple requests and having to keep a tight reign on memory/resource consumption.
 

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/tutorial/cgi_quickstart.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/tutorial/cgi_quickstart.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/doc/src/tutorial/cgi_quickstart.cpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -30,8 +30,12 @@
 /*<
 In our request handler, we will assume that the request has been fully-parsed
 and we can access all of the request data. The request data is available using
-`std::map<>`-like public members of a `cgi::request`.[footnote
-The data is stored internally in a single `fusion::vector<>`]
+public members of a `cgi::request`. These member variables are instances of
+the [classref boost::cgi::common::data_map_proxy data_map_proxy], which has a
+`std::map<>`-like interface along with some additional helper functions to
+facilitate common CGI tasks, such as lexical conversion to different types.
+[footnote The data is stored internally in a single `fusion::vector<>` which
+is not currently publicly accessible.]
 
 A CGI request has several types of variables available. These are listed in
 the table below, assuming that `req` is an instance of `cgi::request`:
@@ -51,7 +55,8 @@
   ]
   [
     [POST] [`req.post`] [The HTTP POST data that is sent in an HTTP request's
- body. File uploads are not stored in this map.]
+ body. For file uploads, the file's name is the value stored in the map. You
+ should use `req.uploads` for more information on file uploads.]
   ]
   [
     [Cookies] [`req.cookies`] [Cookies are sent in the HTTP_COOKIE environment
@@ -60,9 +65,11 @@
   ]
   [
     [File Uploads] [`req.uploads`] [File uploads, sent in an HTTP POST where
- the body is MIME-encoded as multipart/form-data. Uploaded files are read
- onto the server's file system. The value of an upload variable is the path
- of the file.]
+ the body is MIME-encoded as multipart/form-data. Uploaded files are written
+ onto the server's file system and meta-data related to the file is stored in
+ a [classref boost::cgi::common::form_part form_part]. The value of an upload
+ variable is the `form_part` for the upload and all `form_part`s are implicitly
+ convertible to a string, which corresponds to the original filename.]
   ]
   [
     [Form] [`req.form`] [The form variables are either the GET variables or

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/cookies/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/cookies/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/cookies/main.cpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -6,7 +6,8 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 //
 //////////////////////////////////////////////////////////////////
-//[cgi_cookie_game2
+//
+//[cgi_cookies
 //
 // Cookie Test With cTemplate
 // --------------------------

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/custom_sessions/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/custom_sessions/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/custom_sessions/main.cpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -1,4 +1,6 @@
 
+//[custom_sessions
+
 #include <boost/cgi/cgi.hpp>
 #include <iostream>
 
@@ -41,3 +43,5 @@
 
   cout<< "Content-type: text/html\r\n\r\nAn error occurred.";
 }
+
+//]

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/doc.qbk
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/doc.qbk (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/doc.qbk 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -5,11 +5,11 @@
 
 [include echo/doc.qbk]
 
-[include command_line/doc.qbk]
+[include sessions/doc.qbk]
 
-[include cookie_game/doc.qbk]
+[include cookies/doc.qbk]
 
-[include cookie_game2/doc.qbk]
+[include cookies2/doc.qbk]
 
 [include file_browser/doc.qbk]
 

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/sessions/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/sessions/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/sessions/main.cpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -1,28 +1,46 @@
+// -- sessions/main.hpp --
+//
+// Copyright (c) Darren Garvey 2009.
+// 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)
+//
+////////////////////////////////////////////////////////////////
+//
+//[cgi_sessions
+//
+// A simple example, using the default session type of a
+// std::map<string,string>. Session support is optional and
+// you should define BOOST_CGI_ENABLE_SESSIONS to use it.
+//
 
 #include <boost/cgi/cgi.hpp>
 #include <iostream>
 
 using namespace std;
-using namespace boost::cgi;
+namespace cgi = boost::cgi;
 
 int main(int, char**)
 {
   try
   {
- request req;
- response resp;
+ cgi::request req;
+ cgi::response resp;
     
+ // You can use pick() to return a default value when the item
+ // is not found in the request data.
     if (req.get.pick("clear", "") == "1") {
       req.stop_session();
       resp<< "Cleared session";
- return commit(req, resp);
+ return cgi::commit(req, resp);
     }
 
- // Start the session. It is safe to call `start_session()` if a session
- // is already open, but you shouldn't have to do that...
+ // Start the session. It's safe to call `start_session()` if a session
+ // is already open.
     req.start_session();
 
- resp<< content_type("text/plain")
+ // Output the current session data.
+ resp<< cgi::content_type("text/plain")
         << "one = " << req.session["one"]
         << ", two = " << req.session["two"]
         << ", ten = " << req.session["ten"];
@@ -33,7 +51,7 @@
     req.session["ten"] = "10";
 
     // The session is saved by `commit()`.
- return commit(req, resp);
+ return cgi::commit(req, resp);
   
   } catch (std::exception& e) {
     cerr<< "Error: " << e.what() << endl;
@@ -41,3 +59,5 @@
 
   cout<< "Content-type: text/html\r\n\r\nAn error occurred.";
 }
+
+//]

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/doc.qbk
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/doc.qbk (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/doc.qbk 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -25,7 +25,5 @@
 
 [include fcgi/doc.qbk]
 
-[include xcgi/doc.qbk]
-
 [endsect][/ examples]
 

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/doc.qbk
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/doc.qbk (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/doc.qbk 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -13,14 +13,4 @@
 
 [include upload/doc.qbk]
 
-[section Servers]
-
-[include server1/doc.qbk]
-
-[include server2/doc.qbk]
-
-[include server3/doc.qbk]
-
-[endsect]
-
-[endsect]
+[endsect]

Modified: sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp (original)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/echo/main.cpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -1,4 +1,4 @@
-// -- main.hpp --
+// -- echo/main.hpp --
 //
 // Copyright (c) Darren Garvey 2007-2009.
 // Distributed under the Boost Software License, Version 1.0.
@@ -25,9 +25,9 @@
 //using namespace std;
 using std::cerr;
 using std::endl;
-using namespace boost::fcgi;
+namespace fcgi = boost::fcgi;
 
-// The styling information for the page, just to make things look nicer.
+// The styling information for the page, just to make things look clearer.
 static const char* gCSS_text =
 "body { padding: 0; margin: 3%; border-color: #efe; }"
 "ul.data-map .title"
@@ -46,35 +46,6 @@
     "{ clear: both; }"
 ;
 
-//
-// This function writes the title and map contents to the ostream in an
-// HTML-encoded format (to make them easier on the eye).
-//
-template<typename OStream, typename Request, typename Map>
-void format_map(OStream& os, Request& req, Map& m, const std::string& title)
-{
- os<< "<ul class=\"data-map\">"
- "<div class=\"title\">"
- << title
- << "</div>";
-
- if (m.empty())
- os<< "<li>EMPTY</li>";
- else
- for (typename Map::const_iterator i = m.begin(); i != m.end(); ++i)
- {
- os<< "<li>"
- "<div class=\"name\">"
- << i->first
- << "</div>"
- "<div class=\"value\">"
- << i->second
- << "</div>"
- "</li>";
- }
- os<< "<div class=\"clear\"></div></ul>";
-}
-
 std::size_t process_id()
 {
 #if defined(BOOST_WINDOWS)
@@ -84,26 +55,78 @@
 #endif
 }
 
+template<typename OStream, typename Request, typename Map>
+void format_map(OStream& os, Request& req, Map& m, const std::string& title);
+
+int handle_request(fcgi::request& req);
 
-int handle_request(request& req)
+int main()
+{
+try {
+
+ // Make a `service` (more about this in other examples).
+ fcgi::service s;
+
+ // Make an `acceptor` for accepting requests through.
+ fcgi::acceptor a(s);
+
+ //
+ // An acceptor can take a request handler as an argument to `accept` and it
+ // will accept a request and pass the handler the request. The return value
+ // of `accept` when used like this is the result of the handler.
+ //
+ // Note that a request handler is any function or function object with the
+ // signature:
+ // boost::function<int (boost::fcgi::request&)>
+ // See the documentation for Boost.Function and Boost.Bind for more.
+ //
+ // The acceptor maintains an internal queue of requests and will reuse a
+ // dead request if one is waiting.
+ //
+ // After the initial setup, we can enter a loop to handle one request at a
+ // time until there's an error of some sort.
+ //
+ int status(0);
+ do {
+ status = a.accept(&handle_request);
+ } while (!status);
+
+ return status;
+
+}catch(boost::system::system_error const& se){
+ // This is the type of exception thrown by the library.
+ std::cerr<< "[fcgi] System error: " << se.what() << std::endl;
+ return -1;
+}catch(std::exception const& e){
+ // Catch any other exceptions
+ std::cerr<< "[fcgi] Exception: " << e.what() << std::endl;
+ return -2;
+}catch(...){
+ std::cerr<< "[fcgi] Uncaught exception!" << std::endl;
+ return -3;
+}
+}
+
+/// Handle one request.
+int handle_request(fcgi::request& req)
 {
   boost::system::error_code ec;
   
   //
   // Load in the request data so we can access it easily.
   //
- req.load(parse_all); // Read and parse STDIN (ie. POST) data.
+ req.load(fcgi::parse_all);
 
   //
   // Construct a `response` object (makes writing/sending responses easier).
   //
- response resp;
+ fcgi::response resp;
 
   //
   // Responses in CGI programs require at least a 'Content-type' header. The
   // library provides helpers for several common headers:
   //
- resp<< content_type("text/html");
+ resp<< fcgi::content_type("text/html");
   
   // You can also stream text to a response.
   // All of this just prints out the form
@@ -131,10 +154,7 @@
              "<input type=submit value=submit />"
            "</form><p />";
 
- //
- // Use the function defined above to show some of the request data.
- // (this function isn't part of the library)
- //
+ // Show the request data in a formatted table.
   format_map(resp, req, req.env, "Environment Variables");
   format_map(resp, req, req.get, "GET Variables");
   format_map(resp, req, req.post, "POST Variables");
@@ -154,62 +174,36 @@
 
   // This function finishes up. The optional third argument
   // is the program status (default: 0).
- return commit(req, resp);
+ return fcgi::commit(req, resp);
 }
 
-int main()
+//
+// This function writes the title and map contents to the ostream in an
+// HTML-encoded format (to make them easier on the eye).
+//
+template<typename OStream, typename Request, typename Map>
+void format_map(OStream& os, Request& req, Map& m, const std::string& title)
 {
-try {
-
- // Make a `service` (more about this in other examples).
- service s;
-
- // Make an `acceptor` for accepting requests through.
- acceptor a(s);
-
- //
- // After the initial setup, we can enter a loop to handle one request at a
- // time until there's an error of some sort.
- //
- int ret(0);
- for (int i(5);i;--i)
- {
- //
- // An acceptor can take a request handler as an argument to `accept` and it
- // will accept a request and pass the handler the request. The return value
- // of `accept` when used like this is the result of the handler.
- //
- // Note that a request handler is any function or function object with the
- // signature:
- // boost::function<int (boost::fcgi::request&)>
- // See the documentation for Boost.Function and Boost.Bind for more.
- //
- // The acceptor maintains an internal queue of requests and will reuse a
- // dead request if one is waiting.
- //
- ret = a.accept(&handle_request);
- if (ret)
- break;
- }
-
- std::cerr<< "Processing finished. Press enter to continue..." << std::endl;
- std::cin.get();
-
- return ret;
+ os<< "<ul class=\"data-map\">"
+ "<div class=\"title\">"
+ << title
+ << "</div>";
 
-}catch(boost::system::system_error const& se){
- // This is the type of error thrown by the library.
- std::cerr<< "[fcgi] System error: " << se.what() << std::endl;
- return -1;
-}catch(std::exception const& e){
- // Catch any other exceptions
- std::cerr<< "[fcgi] Exception: " << e.what() << std::endl;
- return -2;
-}catch(...){
- std::cerr<< "[fcgi] Uncaught exception!" << std::endl;
- return -3;
-}
- std::cerr<< "Press enter to continue." << std::endl;
- std::cin.get();
+ if (m.empty())
+ os<< "<li>EMPTY</li>";
+ else
+ for (typename Map::const_iterator i = m.begin(); i != m.end(); ++i)
+ {
+ os<< "<li>"
+ "<div class=\"name\">"
+ << i->first
+ << "</div>"
+ "<div class=\"value\">"
+ << i->second
+ << "</div>"
+ "</li>";
+ }
+ os<< "<div class=\"clear\"></div></ul>";
 }
+
 //]

Added: sandbox/SOC/2007/cgi/trunk/libs/cgi/test/run/sessions.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/trunk/libs/cgi/test/run/sessions.cpp 2010-04-29 12:10:02 EDT (Thu, 29 Apr 2010)
@@ -0,0 +1,65 @@
+
+#define BOOST_CGI_ENABLE_SESSIONS
+#include <boost/cgi/utility/sessions.hpp>
+
+#define BOOST_TEST_MODULE sessions_test_suite
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+#include <string>
+#include <map>
+
+BOOST_AUTO_TEST_CASE(session_test)
+{
+ using namespace std;
+ using namespace boost::cgi::common;
+
+ typedef basic_session_manager<tags::cgi> session_manager;
+ typedef basic_session<std::map<std::string,std::string> > session_type;
+
+ session_manager session_mgr;
+
+ {
+ session_type session;
+
+ session_mgr.start(session);
+ session["foo"] = "bar";
+ session["two"] = "2";
+
+ cout<< "session[\"two\"] = " << session["two"] << endl;
+
+ BOOST_CHECK_EQUAL(session["foo"], "bar");
+ BOOST_CHECK_EQUAL(session["two"], "2");
+ BOOST_CHECK_NE(session["two"], "4");
+
+ session.id("aaaaa");
+ session.loaded(true);
+ session_mgr.save(session);
+ }
+
+ {
+ session_type session;
+ session.id("aaaaa");
+ session_mgr.load(session);
+
+ BOOST_CHECK_EQUAL(session["foo"], "bar");
+ BOOST_CHECK_EQUAL(session["two"], "2");
+ BOOST_CHECK_NE(session["two"], "4");
+ }
+
+ {
+ session_type session;
+ session.id("not_an_existing_session");
+ session_mgr.load(session);
+
+ BOOST_CHECK_NE(session["foo"], "bar");
+ BOOST_CHECK_NE(session["two"], "2");
+ BOOST_CHECK_NE(session["two"], "4");
+ }
+
+ {
+ session_type session;
+ session.id("aaaaa");
+ session_mgr.close(session);
+ BOOST_CHECK_EQUAL(session.loaded(), false);
+ }
+}
\ No newline at end of file


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