|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r53516 - in sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi: . acgi cgi common detail
From: lists.drrngrvy_at_[hidden]
Date: 2009-05-31 18:27:41
Author: drrngrvy
Date: 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
New Revision: 53516
URL: http://svn.boost.org/trac/boost/changeset/53516
Log:
Adding support for command line parsing. The command line `argv` can be passed to a basic_request<>'s constructor or to basic_request<>::load, where it will be added to the map of environment variables for that request.
Text files modified:
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi.hpp | 3 -
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi/request_impl.hpp | 5 +
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp | 90 ++++++++++++++++++++--------------------
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_impl.hpp | 1
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/request_base.hpp | 19 ++++++++
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/return.hpp | 16 +++---
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp | 3 +
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/common_headers.hpp | 11 ++--
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/save_environment.hpp | 44 ++++--------------
9 files changed, 95 insertions(+), 97 deletions(-)
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -14,9 +14,6 @@
#include "boost/cgi/detail/common_headers.hpp"
namespace cgi {
-#ifndef CGI_NO_IMPLICIT_TYPEDEFS
- //typedef acgi_request request;
-#endif
namespace acgi {
using namespace ::cgi::common; // import common namespace elements.
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi/request_impl.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi/request_impl.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/acgi/request_impl.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -23,8 +23,9 @@
: public detail::cgi_request_impl_base<common::async_stdio_connection>
{
public:
- typedef ::cgi::acgi::service protocol_service_type;
- typedef common::async_stdio_connection connection_type;
+ typedef ::cgi::acgi::service protocol_service_type;
+ typedef protocol_service_type::protocol_type protocol_type;
+ typedef common::async_stdio_connection connection_type;
typedef
::cgi::common::basic_client<
connection_type, common::tags::acgi
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -86,51 +86,41 @@
typedef typename implementation_type::client_type client_type;
typedef typename implementation_type::buffer_type buffer_type;
-
- /*
- // Throws
- basic_request(bool load_now, bool parse_post = true)
+ basic_request(const parse_options opts = parse_none
+ , char** base_env = NULL)
: detail::basic_sync_io_object<service_type>()
{
- if (load_now) load(parse_post);//this->service.load(this->implementation, true, ec);
- }
- */
-
- basic_request(const parse_options opts = parse_none)
- : detail::basic_sync_io_object<service_type>()
- {
- if (opts > parse_none) load(opts);//this->service.load(this->implementation, true, ec);
+ if (opts > parse_none) load(opts, base_env);
}
// Won't throw
basic_request(boost::system::error_code& ec
- , const bool load_now = true
- , const bool parse_post_now = true)
+ , const parse_options opts = parse_none
+ , char** base_env = NULL)
: detail::basic_sync_io_object<service_type>()
{
- const parse_options opts = parse_post_now ? parse_post : parse_env;
- if (load_now) load(opts, ec);//this->service.load(this->implementation, true, ec);
+ if (opts > parse_none) load(opts, ec);
}
- // Throws
- basic_request(protocol_service_type& s, const bool load_now = false
- , const bool parse_post_now = false)
+ // Throws
+ basic_request(protocol_service_type& s
+ , const parse_options opts = parse_none
+ , char** base_env = NULL)
: basic_io_object<service_type>(s.io_service())
{
set_protocol_service(s);
- const parse_options opts = parse_post_now ? parse_post : parse_env;
- if (load_now) load(opts);//this->service.load(this->implementation, false, ec);
+ if (opts > parse_none) load(opts, base_env);
}
- // Won't throw
+ // Won't throw
basic_request(protocol_service_type& s
, boost::system::error_code& ec
- , const bool load_now = false, const bool parse_post_now = false)
+ , const parse_options opts = parse_none
+ , char** base_env = NULL)
: basic_io_object<service_type>(s.io_service())
{
set_protocol_service(s);
- const parse_options opts = parse_post_now ? parse_post : parse_env;
- if (load_now) load(opts, ec);//this->service.load(this->implementation, false, ec);
+ if (opts > parse_none) load(opts, ec, base_env);
}
/// Make a new mutiplexed request from an existing connection.
@@ -182,34 +172,35 @@
* Note: 'loading' including reading/parsing STDIN if parse_stdin == true
*/
// Throwing semantics
- void load(parse_options parse_opts = parse_env)
+ void load(parse_options parse_opts = parse_env, char** base_env = NULL)
{
boost::system::error_code ec;
this->service.load(this->implementation, parse_opts, ec);
+ if (!ec && base_env)
+ load(base_env);
detail::throw_error(ec);
}
- // Error-code semantics (**FIXME**)
+ // Error-code semantics
boost::system::error_code
- load(parse_options parse_opts, boost::system::error_code& ec)
+ load(parse_options parse_opts, boost::system::error_code& ec
+ , char** base_environment = NULL, bool is_command_line = true)
{
- return this->service.load(this->implementation, parse_opts, ec);
- }
-/*
- // Error-code semantics (**FIXME**)
- boost::system::error_code
- load(bool parse_stdin, boost::system::error_code& ec)
- {
- return this->service.load(this->implementation, parse_stdin, ec);
+ boost::system::error_code& ec (
+ this->service.load(this->implementation, parse_opts, ec));
+ if (base_environment)
+ this->service.load_environment(this->implementation, base_environment
+ , is_command_line);
+ return ec;
+ }
+
+ void load(char** base_environment, bool is_command_line = true)
+ {
+ this->service.load_environment(this->implementation
+ , base_environment
+ , is_command_line);
}
- // Error-code semantics (**FIXME**)
- boost::system::error_code
- load(boost::system::error_code& ec, bool parse_stdin = false)
- {
- return this->service.load(this->implementation, parse_stdin, ec);
- }
-*/
/// Get the buffer containing the POST data.
/**
* **FIXME**
@@ -291,8 +282,8 @@
/// Get the client connection associated with the request
/**
- * You use the client for read/write calls. Y
- */
+ * You use the client for read/write calls.
+ */
client_type& client()
{
return this->service.client(this->implementation);
@@ -488,6 +479,10 @@
*
* request req(...);
* req[get] -> returns a -> get_map&
+ * // similarly
+ * req[post] -> returns a -> post_map&
+ * // but, once gotcha is:
+ * req[cookies] -> returns a ->cookie_map&
*
* You can use this just like a std::map<>`
*
@@ -509,6 +504,11 @@
BOOST_CGI_DETAIL_MAP_ACCESS(session)
/// Get a `common::form_map&` of either the GET or POST variables.
+ /**
+ * Note that this dynamically finds the correct data map, so is
+ * less efficient than the `get` or `post` accessors. The latter
+ * is looked up statically.
+ */
form_map& operator[](common::form_data_type const&)
{
if (request_method() == "GET")
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_impl.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_impl.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_impl.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -44,6 +44,7 @@
typedef common::basic_client<
common::stdio_connection, common::tags::cgi
> client_type;
+ typedef common::tags::cgi protocol_type;
/// Constructor
/**
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/request_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/request_base.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/request_base.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -76,7 +76,24 @@
return boost::asio::buffer(&post_buffer_[bufsz], size);
}
};
-
+
+ /// Load the base_environment into the current environment.
+ /**
+ * Parsed the base_environment and add it to the current request's
+ * environment. This overwrites any environment variables with the existing
+ * key.
+ *
+ * If `is_command_line` is true, then the first argument is skipped as this
+ * is the name of the program and ignored. Using it actually causes a crash
+ * on Windows (MSVC 9) anyway: I'm not exactly sure why.
+ */
+ template<typename ImplType>
+ void load_environment(ImplType& impl, char** base_environment, bool is_command_line)
+ {
+ if (is_command_line) ++base_environment;
+ detail::save_environment(env_vars(impl.vars_), base_environment);
+ }
+
/// Read some data into the internal buffer.
template<typename ImplType>
std::size_t
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/return.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/return.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/common/return.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -26,13 +26,13 @@
template<typename Response, typename Request>
boost::system::error_code
- return_helper(Response& resp, Request& req, int program_status)
+ return_helper(Response& response, Request& request, int program_status)
{
boost::system::error_code ec;
- resp.send(req.client(), ec);
+ response.send(request.client(), ec);
if (ec) return ec;
- req.close(resp.status(), program_status);
+ request.close(response.status(), program_status);
return ec;
}
@@ -46,16 +46,16 @@
# define BOOST_CGI_RETURN_ERROR_INCREMENT 100
#endif
-#define BOOST_CGI_RETURN(resp, req, status) \
- if ( ::cgi::common::return_helper(resp, req, status)) \
- /** error **/ \
- return status + BOOST_CGI_RETURN_ERROR_INCREMENT; \
+#define BOOST_CGI_RETURN(response, request, status) \
+ if ( ::cgi::common::return_helper(response, request, status)) \
+ /** error **/ \
+ return status + BOOST_CGI_RETURN_ERROR_INCREMENT; \
return status;
namespace cgi {
namespace common {
-#define return_(resp, req, status) BOOST_CGI_RETURN(resp, req, status)
+#define return_(response, request, status) BOOST_CGI_RETURN(response, request, status)
} // namespace common
} // namespace cgi
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -24,6 +24,7 @@
///////////////////////////////////////////////////////////
#include "boost/cgi/common/map.hpp"
#include "boost/cgi/basic_client.hpp"
+#include "boost/cgi/common/is_async.hpp"
#include "boost/cgi/common/role_type.hpp"
#include "boost/cgi/common/form_part.hpp"
#include "boost/cgi/detail/throw_error.hpp"
@@ -45,6 +46,8 @@
typedef cgi_service_impl_base<RequestImplType> self_type;
typedef common::request_base<self_type> base_type;
typedef ::cgi::common::map map_type;
+ typedef typename
+ RequestImplType::protocol_type protocol_type;
struct implementation_type
: RequestImplType
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/common_headers.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/common_headers.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/common_headers.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -15,17 +15,18 @@
#include <iostream> // **FIXME** - only for debugging info
//#include "boost/cgi/logger.hpp"
-#include "boost/cgi/common/map.hpp"
-#include "boost/cgi/import/read.hpp"
-#include "boost/cgi/import/write.hpp"
#include "boost/cgi/basic_request.hpp"
-#include "boost/cgi/import/buffer.hpp"
#include "boost/cgi/common/header.hpp"
+#include "boost/cgi/common/map.hpp"
#include "boost/cgi/common/return.hpp"
#include "boost/cgi/common/response.hpp"
-#include "boost/cgi/import/streambuf.hpp"
+#include "boost/cgi/common/source_enums.hpp"
#include "boost/cgi/http/status_code.hpp"
+#include "boost/cgi/import/buffer.hpp"
#include "boost/cgi/import/io_service.hpp"
+#include "boost/cgi/import/read.hpp"
+#include "boost/cgi/import/streambuf.hpp"
+#include "boost/cgi/import/write.hpp"
#endif // CGI_DETAIL_COMMON_HEADERS_HPP_INCLUDED__
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/save_environment.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/save_environment.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/save_environment.hpp 2009-05-31 18:27:40 EDT (Sun, 31 May 2009)
@@ -37,18 +37,21 @@
*/
template<typename MapT>
void save_environment(MapT& env_map, char** env =
- // Windows calls the environment environ_
-#if BOOST_WINDOWS
- _environ
-#else
- environ
-#endif
- )
+ // Windows calls the environment environ_
+# if BOOST_WINDOWS
+ _environ
+# else
+ environ
+# endif
+ )
{
+
+ BOOST_ASSERT(env && "Trying to save environment, but the passed in environment is empty / invalid.");
+
std::string sa;
std::string sb;
- for(; *env; ++env)
+ for(; env && *env; ++env)
{
int i=0;
int j=strlen(*env);
@@ -77,29 +80,4 @@
} // namespace detail
} // namespace cgi
-/* Alternative version which doesn't copy the 'value' of the variable
- ******************************************************************************
- void save_environment(std::map<std::string,const char*>& env_map
- , char** env = ::environ)
- {
- for(; *env; ++env)
- {
- int i=0;
- int j=strlen(*env);
- for(; i < j; ++i)
- if ((*env)[i] == '=')
- break;
-
- // Note: empty variables are not guaranteed to be set by the server, so
- // we are free to ignore them too.
- if ((*env)[i+1] != '\0')
- {
- std::string sa(*env, i);
- env_map[sa] = (*env+i+1);
- }
- }
- }
- ******************************************************************************
- */
-
#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