Boost logo

Boost-Commit :

From: lists.drrngrvy_at_[hidden]
Date: 2007-08-22 21:21:28


Author: drrngrvy
Date: 2007-08-22 21:21:26 EDT (Wed, 22 Aug 2007)
New Revision: 38859
URL: http://svn.boost.org/trac/boost/changeset/38859

Log:
Adding save_environment.hpp, to ahem, save the process enviroment variables into a map passed to it
Added:
   sandbox/SOC/2007/cgi/boost/cgi/detail/save_environment.hpp (contents, props changed)
Text files modified:
   sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_request_impl_base.hpp | 22 ++++---
   sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_service_impl_base.hpp | 101 ++++++++++++++++++++++-----------------
   sandbox/SOC/2007/cgi/boost/cgi/detail/common_headers.hpp | 2
   sandbox/SOC/2007/cgi/boost/cgi/detail/protocol_traits.hpp | 1
   4 files changed, 72 insertions(+), 54 deletions(-)

Modified: sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_request_impl_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_request_impl_base.hpp (original)
+++ sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_request_impl_base.hpp 2007-08-22 21:21:26 EDT (Wed, 22 Aug 2007)
@@ -42,32 +42,34 @@
   public:
     typedef ::cgi::map map_type;
     typedef Connection connection_type;
- typedef boost::shared_ptr<connection_type> conn_ptr;
+ typedef typename connection_type::pointer conn_ptr;
 
     /// Constructor
     cgi_request_impl_base()
       : stdin_parsed_(false)
       , http_status_(http::ok)
       , request_status_(unloaded)
- // , connection_(new )
     {
     }
 
- map_type& get_vars() { return get_vars_; }
- map_type& post_vars() { return post_vars_; }
- map_type& cookie_vars() { return cookie_vars_; }
+ map_type& env_vars() { return env_vars_; }
+ map_type& get_vars() { return get_vars_; }
+ map_type& post_vars() { return post_vars_; }
+ map_type& cookie_vars() { return cookie_vars_; }
+
+ bool& stdin_parsed() { return stdin_parsed_; }
+ http::status_code& http_status() { return http_status_; }
+ status_type& status() { return request_status_; }
 
- bool stdin_parsed() { return stdin_parsed_; }
- http::status_code& http_status() { return http_status_; }
-
- conn_ptr& connection() { return connection_; }
- std::string& null_str() { return null_str_; }
+ conn_ptr& connection() { return connection_; }
+ std::string& null_str() { return null_str_; }
 
   protected:
     //conn_ptr connection() { return connection_; }
 
     //friend class cgi_service_impl_base<RequestImpl>;
 
+ map_type env_vars_;
     map_type get_vars_;
     map_type post_vars_;
     map_type cookie_vars_;

Modified: sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_service_impl_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_service_impl_base.hpp (original)
+++ sandbox/SOC/2007/cgi/boost/cgi/detail/cgi_service_impl_base.hpp 2007-08-22 21:21:26 EDT (Wed, 22 Aug 2007)
@@ -12,7 +12,9 @@
 
 #include "boost/cgi/map.hpp"
 #include "boost/cgi/role_type.hpp"
+#include "boost/cgi/status_type.hpp"
 #include "boost/cgi/detail/extract_params.hpp"
+#include "boost/cgi/detail/save_environment.hpp"
 //#include "../connections/stdio.hpp"
 
 namespace cgi {
@@ -42,14 +44,28 @@
     {
     }
 
+ /// Return if the request is still open
+ /**
+ * For CGI, this always returns true. However, in the case that a
+ * "Location: xxx" header is sent and the header is terminated, the
+ * request can be taken to be 'closed'.
+ */
     bool is_open(implementation_type& impl)
     {
- return true;
+ return impl.status() >= aborted;
     }
 
- int close(implementation_type& impl, http::status_code&, int status)
+ /// Return the connection associated with the request
+ typename implementation_type::connection_type&
+ client(implementation_type& impl)
     {
- //impl.set_status(aborted);
+ return *impl.connection();
+ }
+
+ int close(implementation_type& impl, http::status_code& http_s, int status)
+ {
+ impl.status() = closed;
+ impl.http_status() = http_s;
       return status;
     }
 
@@ -61,14 +77,20 @@
     load(implementation_type& impl, bool parse_stdin
         , boost::system::error_code& ec)
     {
- const std::string& request_method = meta_env(impl, "REQUEST_METHOD", ec);
- if (request_method == "GET" && parse_get_vars(impl, ec))
- return ec;
+ detail::save_environment(impl.env_vars());
+ const std::string& request_method = impl.env_vars()["REQUEST_METHOD"];
+ if (request_method == "GET")
+ parse_get_vars(impl, ec);
       else
- if (request_method == "POST" && parse_stdin && parse_post_vars(impl, ec))
- return ec;
+ if (request_method == "POST" && parse_stdin)
+ parse_post_vars(impl, ec);
+
+ if (ec) return ec;
 
       parse_cookie_vars(impl, ec);
+ impl.status() = loaded;
+ BOOST_ASSERT(impl.status() >= loaded);
+
       return ec;
     }
 
@@ -89,42 +111,22 @@
       return impl.connection()->write_some(buf, ec);
     }
 
- //template<typename VarType> map_type& var(implementation_type&) const;
-
- std::string var(map_type& meta_data, const std::string& name
+ std::string& var(map_type& meta_data, const std::string& name
                    , boost::system::error_code& ec)
     {
- /* Alt:
- if ((typename map_type::iterator pos = meta_data.find(name))
- != meta_data.end())
- {
- return pos->second;
- }
- return std::string();
- *********
- for(typename map_type::iterator iter = meta_data.begin()
- ; iter != meta_data.end()
- ; ++iter)
- {
- if( iter->first == name )
- return iter->second;
- }
- return "";
- **/
-
- if( meta_data.find(name) != meta_data.end() )
- return meta_data[name];
- return "";
+ return meta_data[name];
     }
 
     std::string meta_get(implementation_type& impl, const std::string& name
                         , boost::system::error_code& ec)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       return var(impl.get_vars(), name, ec);
     }
 
     map_type& meta_get(implementation_type& impl)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       return impl.get_vars();
     }
 
@@ -144,6 +146,7 @@
                          , boost::system::error_code& ec
                          , bool greedy = true)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       const std::string& val = var(impl.post_vars(), name, ec);
       if (val.empty() && greedy && !ec)
       {
@@ -155,6 +158,7 @@
 
     map_type& meta_post(implementation_type& impl)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       return impl.post_vars();
     }
 
@@ -163,11 +167,13 @@
     std::string cookie(implementation_type& impl, const std::string& name
                       , boost::system::error_code& ec)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       return var(impl.cookie_vars(), name, ec);
     }
 
     map_type& meta_cookie(implementation_type& impl)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       return impl.cookie_vars();
     }
 
@@ -176,10 +182,17 @@
     std::string meta_env(implementation_type& impl, const std::string& name
                         , boost::system::error_code& ec)
     {
+ BOOST_ASSERT(impl.status() >= loaded);
       const char* c = ::getenv(name.c_str());
       return c ? c : impl.null_str();
     }
 
+ map_type& meta_env(implementation_type& impl)
+ {
+ BOOST_ASSERT(impl.status() >= loaded);
+ return impl.env_vars();
+ }
+
 
     role_type get_role(implementation_type& impl)
     {
@@ -197,8 +210,14 @@
     boost::system::error_code&
     parse_get_vars(RequestImpl& impl, boost::system::error_code& ec)
     {
- detail::extract_params(meta_env(impl, "QUERY_STRING", ec)
- , impl.get_vars()
+ // Make sure the request is in a pre-loaded state
+ BOOST_ASSERT (impl.status() <= unloaded);
+
+ std::string& vars = impl.env_vars()["QUERY_STRING"];
+ if (vars.empty())
+ return ec;
+
+ detail::extract_params(vars, impl.get_vars()
                             , boost::char_separator<char>
                                 ("", "=&", boost::keep_empty_tokens)
                             , ec);
@@ -211,15 +230,14 @@
     boost::system::error_code&
     parse_cookie_vars(RequestImpl& impl, boost::system::error_code& ec)
     {
- // Make sure this function hasn't already been called
- //BOOST_ASSERT( impl.cookie_vars_.empty() );
+ // Make sure the request is in a pre-loaded state
+ BOOST_ASSERT (impl.status() <= unloaded);
 
- std::string vars = meta_env(impl, "HTTP_COOKIE", ec);
+ std::string& vars(impl.env_vars()["HTTP_COOKIE"]);
       if (vars.empty())
         return ec;
 
- detail::extract_params(meta_env(impl, "HTTP_COOKIE", ec)
- , impl.cookie_vars()
+ detail::extract_params(vars, impl.cookie_vars()
                             , boost::char_separator<char>
                                 ("", "=&", boost::keep_empty_tokens)
                             , ec);
@@ -233,10 +251,8 @@
     parse_post_vars(RequestImpl& impl, boost::system::error_code& ec)
     {
       // Make sure this function hasn't already been called
- std::cerr<< "blah";
       BOOST_ASSERT (!impl.stdin_parsed());
- std::cerr<< "wom";
- std::cerr.flush();
+
       std::istream& is(std::cin);
       char ch;
       std::string name;
@@ -277,7 +293,6 @@
           default:
               str += ch;
           }
- //LOG<< "name=" << name << "; str=" << str << std::endl;
       }
       // save the last param (it won't have a trailing &)
       if( !name.empty() )

Modified: sandbox/SOC/2007/cgi/boost/cgi/detail/common_headers.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/boost/cgi/detail/common_headers.hpp (original)
+++ sandbox/SOC/2007/cgi/boost/cgi/detail/common_headers.hpp 2007-08-22 21:21:26 EDT (Wed, 22 Aug 2007)
@@ -18,7 +18,7 @@
 #include "boost/cgi/streambuf.hpp"
 #include "boost/cgi/basic_request.hpp"
 //#include "boost/cgi/basic_request_acceptor.hpp"
-#include "boost/cgi/reply.hpp"
+#include "boost/cgi/response.hpp"
 //#include "boost/cgi/logger.hpp"
 #include "boost/cgi/read.hpp"
 #include "boost/cgi/write.hpp"

Modified: sandbox/SOC/2007/cgi/boost/cgi/detail/protocol_traits.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/boost/cgi/detail/protocol_traits.hpp (original)
+++ sandbox/SOC/2007/cgi/boost/cgi/detail/protocol_traits.hpp 2007-08-22 21:21:26 EDT (Wed, 22 Aug 2007)
@@ -12,6 +12,7 @@
 #include "boost/cgi/tags.hpp"
 #include "boost/cgi/basic_request_fwd.hpp"
 #include "boost/cgi/basic_connection_fwd.hpp"
+#include "boost/cgi/basic_protocol_service_fwd.hpp"
 
 namespace cgi {
 

Added: sandbox/SOC/2007/cgi/boost/cgi/detail/save_environment.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2007/cgi/boost/cgi/detail/save_environment.hpp 2007-08-22 21:21:26 EDT (Wed, 22 Aug 2007)
@@ -0,0 +1,44 @@
+// -- detail/save_environment.hpp --
+//
+// Copyright (c) Darren Garvey 2007.
+// 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)
+//
+////////////////////////////////////////////////////////////////
+#ifndef CGI_DETAIL_SAVE_ENVIRONMENT_HPP_INCLUDED__
+#define CGI_DETAIL_SAVE_ENVIRONMENT_HPP_INCLUDED__
+
+#include <string>
+#include <cstdlib>
+
+namespace cgi {
+ namespace detail {
+
+ // The process' environment
+ extern char** environ;
+
+ /// Save all information from the process' environment variables to env_map
+ void save_environment(cgi::map& env_map)
+ {
+ for(char** env = ::environ; *env; ++env)
+ {
+ int i=0;
+ int j=strlen(*env);
+ for(; i < j; ++i)
+ if ((*env)[i] == '=')
+ break;
+
+ if ((*env)[i+1] != '\0')
+ {
+ std::string sa(*env, i);
+ std::string sb((*env+i+1), j-i-1);
+ env_map[sa] = sb;
+ }
+ }
+ }
+
+ } // namespace detail
+} // namespace cgi
+
+#endif // CGI_DETAIL_SAVE_ENVIRONMENT_HPP_INCLUDED__


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