Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60161 - in sandbox/SOC/2007/cgi/trunk: boost/cgi boost/cgi/common boost/cgi/utility libs/cgi/example/cgi/sessions
From: lists.drrngrvy_at_[hidden]
Date: 2010-03-04 20:26:51


Author: drrngrvy
Date: 2010-03-04 20:26:50 EST (Thu, 04 Mar 2010)
New Revision: 60161
URL: http://svn.boost.org/trac/boost/changeset/60161

Log:
Cleaning up session support.
Text files modified:
   sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request.hpp | 40 ++++++++++++++++++++--------------------
   sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request_acceptor.hpp | 6 +++---
   sandbox/SOC/2007/cgi/trunk/boost/cgi/common/basic_protocol_service.hpp | 10 ----------
   sandbox/SOC/2007/cgi/trunk/boost/cgi/common/commit.hpp | 2 +-
   sandbox/SOC/2007/cgi/trunk/boost/cgi/config.hpp | 2 +-
   sandbox/SOC/2007/cgi/trunk/boost/cgi/utility/sessions.hpp | 7 +++++++
   sandbox/SOC/2007/cgi/trunk/libs/cgi/example/cgi/sessions/main.cpp | 34 +++++++++++++++-------------------
   7 files changed, 47 insertions(+), 54 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-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -112,7 +112,6 @@
       , char** base_env = NULL)
         : detail::basic_io_object<service_type>()
     {
- set_protocol_service();
       if ((parse_options)opts > parse_none) load((parse_options)opts, base_env);
     }
 
@@ -139,7 +138,7 @@
     basic_request(protocol_service_type& s
                  , const parse_options opts = traits::parse_opts
                  , char** base_env = NULL)
- : detail::basic_io_object<service_type>(s.io_service())
+ : detail::basic_io_object<service_type>(s.get_io_service())
     {
       set_protocol_service(s);
       if (opts > parse_none) load(opts, base_env);
@@ -150,7 +149,7 @@
                  , boost::system::error_code& ec
                  , const parse_options opts = traits::parse_opts
                  , char** base_env = NULL)
- : detail::basic_io_object<service_type>(s.io_service())
+ : detail::basic_io_object<service_type>(s.get_io_service())
     {
       set_protocol_service(s);
       if (opts > parse_none) load(opts, ec, base_env);
@@ -159,7 +158,7 @@
     /// Make a new mutiplexed request from an existing connection.
     // Throws.
     basic_request(implementation_type& impl)
- : detail::basic_io_object<service_type>(impl.service_->io_service())
+ : detail::basic_io_object<service_type>(impl.service_->get_io_service())
     {
       set_protocol_service(*impl.service_);
       boost::system::error_code ec;
@@ -171,7 +170,7 @@
     /// Make a new mutiplexed request from an existing connection.
     // Won't throw.
     basic_request(implementation_type& impl, boost::system::error_code& ec)
- : detail::basic_io_object<service_type>(impl.service_->io_service())
+ : detail::basic_io_object<service_type>(impl.service_->get_io_service())
     {
       set_protocol_service(*impl.service_);
       this->service.begin_request_helper(this->implementation
@@ -193,14 +192,24 @@
     }
     
 #ifdef BOOST_CGI_ENABLE_SESSIONS
+ /// Start a session, or load the session from a cookie.
     void start_session()
     {
- std::cerr<< "Starting session" << std::endl;
- if (session.id().empty())
- session.id(this->service.make_session_id());
- std::cerr<< "Started session" << std::endl;
+ if (!session.loaded() && session.id().empty())
+ {
+ // Assume cookies have been loaded. This will throw at runtime (in
+ // a debug build) if `request.load(parse_cookie)` hasn't been called
+ // by now.
+ string_type ssid (cookies.pick(BOOST_CGI_SESSION_COOKIE_NAME, ""));
+ if (!ssid.empty()) {
+ session.id(ssid);
+ this->service.session_manager().load(session);
+ } else
+ session.id(this->service.make_session_id());
+ }
     }
 #endif // BOOST_CGI_ENABLE_SESSIONS
+
     protocol_service_type& get_protocol_service()
     {
       return *(this->implementation.service_);
@@ -304,22 +313,13 @@
 #ifdef BOOST_CGI_ENABLE_SESSIONS
         if (parse_opts & parse_session_only)
         {
- if (!!cookies && cookies.count("$ssid"))
- {
- std::cerr<< "Loading pre-existing session." << std::endl;
- session.id(cookies["$ssid"]);
- }
+ if (!!cookies && cookies.count(BOOST_CGI_SESSION_COOKIE_NAME))
+ session.id(cookies[BOOST_CGI_SESSION_COOKIE_NAME]);
           else
           if (traits::auto_start_session)
- {
- std::cerr<< "Starting new session." << std::endl;
             session.id(this->service.make_session_id());
- }
           if (!session.id().empty())
- {
             this->service.session_manager().load(session);
- std::cerr<< "Started session" << std::endl;
- }
         }
 #endif // BOOST_CGI_ENABLE_SESSIONS
       }

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request_acceptor.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request_acceptor.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/basic_request_acceptor.hpp 2010-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -49,7 +49,7 @@
     explicit basic_request_acceptor(
           common::basic_protocol_service<protocol_type, IoServiceProvider>& ps,
           port_number_type port_num = 0)
- : boost::asio::basic_io_object<RequestAcceptorService>(ps.io_service())
+ : boost::asio::basic_io_object<RequestAcceptorService>(ps.get_io_service())
     {
       this->service.set_protocol_service(this->implementation, ps);
       this->implementation.port_num_ = port_num;
@@ -84,7 +84,7 @@
           common::basic_protocol_service<protocol_type, IoServiceProvider>& ps,
           const boost::asio::ip::basic_endpoint<InternetProtocol>& endpoint,
           bool reuse_addr = true)
- : boost::asio::basic_io_object<RequestAcceptorService>(ps.io_service())
+ : boost::asio::basic_io_object<RequestAcceptorService>(ps.get_io_service())
     {
       this->service.set_protocol_service(this->implementation, ps);
 
@@ -104,7 +104,7 @@
           common::basic_protocol_service<protocol_type, IoServiceProvider>& ps,
           const InternetProtocol& ip,
           const native_type& native_acceptor)
- : boost::asio::basic_io_object<RequestAcceptorService>(ps.io_service())
+ : boost::asio::basic_io_object<RequestAcceptorService>(ps.get_io_service())
     {
       this->service.set_protocol_service(this->implementation, ps);
       boost::system::error_code ec;

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/common/basic_protocol_service.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/common/basic_protocol_service.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/common/basic_protocol_service.hpp 2010-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -44,20 +44,11 @@
     typedef std::set<request_ptr> set_type;
     typedef std::queue<request_ptr> queue_type;
 
- basic_protocol_service()
- : ios_provider_()
- , request_set_()
- , request_queue_()
- {
- std::cerr<< "Constructing" << std::endl;
- }
-
     basic_protocol_service(int pool_size_hint = 1)
       : ios_provider_(pool_size_hint)
       , request_set_()
       , request_queue_()
     {
- std::cerr<< "2.. Constructing" << std::endl;
     }
 
     basic_protocol_service(boost::asio::io_service& ios)
@@ -65,7 +56,6 @@
       , request_set_()
       , request_queue_()
     {
- std::cerr<< "3... Constructing" << std::endl;
     }
 
     ~basic_protocol_service()

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/common/commit.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/common/commit.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/common/commit.hpp 2010-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -30,7 +30,7 @@
     {
 #ifdef BOOST_CGI_ENABLE_SESSIONS
       if (!req.session.id().empty()) {
- resp<< cookie("$ssid", req.session.id());
+ resp<< cookie(BOOST_CGI_SESSION_COOKIE_NAME, req.session.id());
       }
 #endif // BOOST_CGI_ENABLE_SESSIONS
       resp.send(req.client(), ec);

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-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -50,7 +50,7 @@
 /// Library assertions, only in debug mode.
 #ifndef BOOST_CGI_DEBUG_ASSERT
 # ifdef NDEBUG
-# define BOOST_CGI_DEBUG_ASSERT(x) void
+# define BOOST_CGI_DEBUG_ASSERT(x) /** void **/
 # else
 # define BOOST_CGI_DEBUG_ASSERT(x) assert(x)
 # endif // NDEBUG

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/utility/sessions.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/utility/sessions.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/utility/sessions.hpp 2010-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -42,6 +42,10 @@
 # define BOOST_CGI_SESSIONS_DIRECTORY "../sessions/"
 #endif // BOOST_CGI_SESSIONS_DIRECTORY
 
+#ifndef BOOST_CGI_SESSION_COOKIE_NAME
+# define BOOST_CGI_SESSION_COOKIE_NAME ".ssid"
+#endif // BOOST_CGI_SESSION_COOKIE_NAME
+
 BOOST_CGI_NAMESPACE_BEGIN
  namespace common {
 
@@ -58,16 +62,19 @@
 
   basic_session(string const& id = "")
     : id_(id)
+ , loaded_(false)
   {}
   
   basic_session(T& t, string const& id = "")
     : T(t)
     , id_(id)
+ , loaded_(false)
   {}
   
   basic_session(T const& t, string const& id = "")
     : T(t)
     , id_(id)
+ , loaded_(false)
   {}
 
   string const& id () const { return id_; }

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-03-04 20:26:50 EST (Thu, 04 Mar 2010)
@@ -42,8 +42,15 @@
 struct protocol_traits<session_enabled_cgi>
   : protocol_traits<boost::cgi::tags::cgi>
 {
- typedef basic_session<context> session_type;
- static const bool auto_start_session = false;
+ typedef basic_session<context> session_type; /*<
+The session_type for the request holds all session-related data, which in this
+example is everything stored in the `context` class defined above.
+>*/
+ static const bool auto_start_session = false; /*<
+`auto_start_session` is `true` by default. Here we override it to be `false`,
+which means we must explicitly call `basic_request<>::start_session()` for
+any sessions to be created / loaded.
+>*/
 };
 
 } } } // namespace boost::cgi::common
@@ -56,19 +63,14 @@
 
 int main(int, char**)
 {
- cerr<< "Started: " << time(NULL) << endl;
   try
   {
- boost::uuids::basic_random_generator<boost::rand48> generator;
- cerr<< "Here" << endl;
- boost::uuids::uuid u = generator();
- cerr<< "generated" << endl;
-
-
     my_request req;
     response resp;
 
- //req.load(parse_session);
+ // Start the session. It is safe to call `start_session()` if a session
+ // is already open, but you shouldn't have to do that...
+ req.start_session();
 
     resp<< "one = " << req.session.data["one"]
         << ", two = " << req.session.data["two"]
@@ -79,18 +81,12 @@
     req.session.data["two"] = 2;
     req.session.data["ten"] = 10;
 
- // Set the session id, so the data is saved.
- //req.session_id_ = "1";
-
- resp<< content_type("text/plain") << "\nBlah\n";
-
- commit(req, resp);
-
- return 0;
+ // The session is saved by `commit()`.
+ return commit(req, resp);
   
   } catch (std::exception& e) {
     cerr<< "Error: " << e.what() << endl;
   }
 
- cout<< "Content-type: text/html\r\n\r\nBoom";
+ cout<< "Content-type: text/html\r\n\r\nAn error occurred.";
 }


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