aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2016-03-06 13:52:48 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2016-03-07 00:05:29 +0300
commitb72424fca7a6af6ff7921101c450850fef875152 (patch)
treeac295d1e228379b1b31c6af3a84e7057f2ea96ae /web
parent0f9c65e489a7b59f76ccbf2ca6e76ab0a1012932 (diff)
Support multiple instances of brep in a single Apache instance
Diffstat (limited to 'web')
-rw-r--r--web/apache/service189
-rw-r--r--web/apache/service.cxx167
-rw-r--r--web/apache/service.txx156
-rw-r--r--web/module12
4 files changed, 435 insertions, 89 deletions
diff --git a/web/apache/service b/web/apache/service
index 75c096c..4c0d395 100644
--- a/web/apache/service
+++ b/web/apache/service
@@ -8,6 +8,8 @@
#include <httpd.h>
#include <http_config.h> // module, ap_hook_*()
+#include <map>
+#include <memory> // unique_ptr
#include <string>
#include <cassert>
@@ -19,6 +21,22 @@ namespace web
{
namespace apache
{
+ // Apache has 3 configuration scopes: main server, virtual server, and
+ // directory (location). It provides configuration scope-aware modules
+ // with the ability to build a hierarchy of configuration contexts. Later,
+ // when processing a request, Apache passes the appropriate directory
+ // configuration context to the request handler.
+ //
+ // This Apache service implementation first makes a copy of the provided
+ // (in the constructor below) module exemplar for each directory context.
+ // It then initializes each of these "context exemplars" with the (merged)
+ // set of configuration options. Finally, when handling a request, it
+ // copies the corresponding "context exemplar" to create the "handling
+ // instance". Note that the "context exemplars" are create before the
+ // provided exemplar is initialized. As a result, it is possible to detect
+ // if the module's copy constructor is used to create a "context exemplar"
+ // or a "handling instance".
+ //
class service: ::module
{
public:
@@ -41,6 +59,41 @@ namespace web
{
init_directives ();
+ // Set configuration context management hooks.
+ //
+ // The overall process of building the configuration hierarchy for a
+ // module is as follows:
+ //
+ // 1. Apache creates directory and server configuration contexts for
+ // scopes containing module-defined directives by calling the
+ // create_{server,dir}_context() callback functions. For directives
+ // at the server scope the special directory context is created as
+ // well.
+ //
+ // 2. Apache calls parse_option() function for each module-defined
+ // directive. The function parses the directives and places the
+ // resulting options into the corresponding configuration context.
+ // It also establishes the directory-server contexts relations.
+ //
+ // 3. Apache calls merge_server_context() function for each virtual
+ // server. The function complements virtual server context options
+ // with the ones from the main server.
+ //
+ // 4. Apache calls config_finalizer() which complements the directory
+ // contexts options with the ones from the enclosing servers.
+ //
+ // 5. Apache calls worker_initializer() which creates module exemplar
+ // for each directory configuration context.
+ //
+ // References:
+ // http://www.apachetutor.org/dev/config
+ // http://httpd.apache.org/docs/2.4/developer/modguide.html
+ // http://wiki.apache.org/httpd/ModuleLife
+ //
+ create_server_config = &create_server_context;
+ create_dir_config = &create_dir_context;
+ merge_server_config = &merge_server_context<M>;
+
// instance<M> () is invented to delegate processing from apache
// request handler C function to the service non static member
// function. This appoach resticts number of service objects per
@@ -56,6 +109,7 @@ namespace web
delete [] cmds;
}
+ private:
template <typename M>
static service*&
instance () noexcept
@@ -88,16 +142,7 @@ namespace web
config_finalizer (apr_pool_t*, apr_pool_t*, apr_pool_t*, server_rec* s)
noexcept
{
- auto srv (instance<M> ());
- bool& parsed (srv->options_parsed_);
-
- if (!parsed)
- {
- log l (s, srv);
- srv->exemplar_.version (l);
- parsed = true;
- }
-
+ instance<M> ()->finalize_config (s);
return OK;
}
@@ -107,44 +152,140 @@ namespace web
{
auto srv (instance<M> ());
log l (s, srv);
- srv->init_worker (l);
+ srv->template init_worker<M> (l);
}
template <typename M>
static int
- request_handler (request_rec* r) noexcept
+ request_handler (request_rec* r) noexcept;
+
+ private:
+ // Our representation of the Apache configuration context.
+ //
+ // The lifetime of this object is under the control of the Apache API,
+ // which treats it as a raw sequence of bytes. In order not to tinker
+ // with the C-style structures and APR memory pools, we will keep it a
+ // (C++11) POD type with just the members required to maintain the
+ // context hierarchy.
+ //
+ // We will then use the pointers to these context objects as keys in
+ // maps to (1) the corresponding application-level option lists during
+ // the configuration cycle and to (2) the corresponding module exemplar
+ // during the HTTP request handling phase. We will also use the same
+ // type for both directory and server configuration contexts.
+ //
+ struct context
{
- auto srv (instance<M> ());
- if (!r->handler || srv->name_ != r->handler) return DECLINED;
+ // Outer (server) configuration context for the directory
+ // configuration context, NULL otherwise.
+ //
+ context* server;
- request req (r);
- log l (r->server, srv);
- return srv->template handle<M> (req, l);
- }
+ // If module directives appear directly in the server configuration
+ // scope, Apache creates a special directory context for them. This
+ // context appears at the same hierarchy level as the user-defined
+ // directory contexts of the same server scope.
+ //
+ bool special;
+
+ // Create the server configuration context.
+ //
+ context (): server (nullptr), special (false) {}
+
+ // Create the directory configuration context. Due to the Apache API
+ // implementation details it is not possible to detect the enclosing
+ // server configuration context at the time of directory context
+ // creation. As a result, the server member is set by the module's
+ // parse_option() function.
+ //
+ context (bool s): server (nullptr), special (s) {}
+
+ // Ensure the object is only destroyed by Apache.
+ //
+ ~context () = delete;
+ };
+
+ // Type of the key for configuration options and module exemplar maps.
+ //
+ using context_id = const context*;
+
+ static bool
+ is_null (context_id id) noexcept {return id == nullptr;}
+
+ static context_id
+ make_context_id (const context* c) noexcept {return c;}
+
+ // Convert Apache-provided configuration pointer to the context id.
+ //
+ static context_id
+ make_context_id (void* config) noexcept
+ {return make_context_id (static_cast<const context*> (config));}
private:
void
init_directives ();
- void
- init_worker (log& l) noexcept;
+ // Create the server configuration context. Called by the Apache API
+ // whenever a new object of that type is required.
+ //
+ static void*
+ create_server_context (apr_pool_t*, server_rec*) noexcept;
+
+ // Create the server directory configuration context. Called by the
+ // Apache API whenever a new object of that type is required.
+ //
+ static void*
+ create_dir_context (apr_pool_t*, char* dir) noexcept;
+
+ template <typename M>
+ static void*
+ merge_server_context (apr_pool_t*, void* enclosing, void* enclosed)
+ noexcept
+ {
+ // Complement the enclosed context with options of the enclosing one.
+ //
+ instance<M> ()->complement (
+ make_context_id (enclosed), make_context_id (enclosing));
+
+ return enclosed;
+ }
static const char*
- parse_option (cmd_parms* parms, void* mconfig, const char* args) noexcept;
+ parse_option (cmd_parms* parms, void* conf, const char* args) noexcept;
const char*
- add_option (const char* name, optional<std::string> value);
+ add_option (context_id id, const char* name, optional<std::string> value);
+
+ void
+ finalize_config (server_rec*);
+
+ void
+ clear_config ();
+
+ void
+ complement (context_id enclosed, context_id enclosing);
+
+ template <typename M>
+ void
+ init_worker (log&);
template <typename M>
int
- handle (request& r, log& l) noexcept;
+ handle (request&, context_id, log&) const;
private:
std::string name_;
module& exemplar_;
option_descriptions option_descriptions_;
- name_values options_;
+
+ using options = std::map<context_id, name_values>;
+ options options_;
+
+ using exemplars = std::map<context_id, std::unique_ptr<module>>;
+ exemplars exemplars_;
+
bool options_parsed_ = false;
+ bool version_logged_ = false;
};
}
}
diff --git a/web/apache/service.cxx b/web/apache/service.cxx
index 96ff855..2ca8cf0 100644
--- a/web/apache/service.cxx
+++ b/web/apache/service.cxx
@@ -4,8 +4,7 @@
#include <web/apache/service>
-#include <unistd.h> // getppid()
-#include <signal.h> // kill()
+#include <apr_pools.h>
#include <httpd.h>
#include <http_config.h>
@@ -51,11 +50,17 @@ namespace web
i.first->first.c_str (),
reinterpret_cast<cmd_func> (parse_option),
this,
- RSRC_CONF,
+
+ // Allow directives in both server and directory configuration
+ // scopes.
+ //
+ RSRC_CONF | ACCESS_CONF,
+
// Move away from TAKE1 to be able to handle empty string and
// no-value.
//
RAW_ARGS,
+
nullptr
};
}
@@ -64,21 +69,47 @@ namespace web
cmds = directives.release ();
}
- const char* service::
- parse_option (cmd_parms* parms, void*, const char* args) noexcept
+ void* service::
+ create_server_context (apr_pool_t* pool, server_rec*) noexcept
{
- // @@ Current implementation does not consider configuration context
- // (server config, virtual host, directory) for directive parsing, nor
- // for request handling.
+ // Create the object using the configuration memory pool provided by the
+ // Apache API. The lifetime of the object is equal to the lifetime of the
+ // pool.
+ //
+ void* p (apr_palloc (pool, sizeof (context)));
+ assert (p != nullptr);
+ return new (p) context ();
+ }
+
+ void* service::
+ create_dir_context (apr_pool_t* pool, char* dir) noexcept
+ {
+ // Create the object using the configuration memory pool provided by the
+ // Apache API. The lifetime of the object is equal to the lifetime of
+ // the pool.
+ //
+ void* p (apr_palloc (pool, sizeof (context)));
+ assert (p != nullptr);
+
+ // For the user-defined directory configuration context dir is the path
+ // of the corresponding directive. For the special server directory
+ // invented by Apache for server scope directives, dir is NULL.
//
+ return new (p) context (dir == nullptr);
+ }
+
+ const char* service::
+ parse_option (cmd_parms* parms, void* conf, const char* args) noexcept
+ {
service& srv (*reinterpret_cast<service*> (parms->cmd->cmd_data));
if (srv.options_parsed_)
- // Apache is inside the second pass of its messy initialization cycle
- // (more details at http://wiki.apache.org/httpd/ModuleLife). Just
- // ignore it.
+ // Apache have started the second pass of its messy initialization cycle
+ // (more details at http://wiki.apache.org/httpd/ModuleLife). This time
+ // we are parsing for real. Cleanup the existing config, and start
+ // building the new one.
//
- return 0;
+ srv.clear_config ();
// 'args' is an optionally double-quoted string. It uses double quotes
// to distinguish empty string from no-value case.
@@ -91,11 +122,52 @@ namespace web
? string (args + 1, l - 2)
: args;
- return srv.add_option (parms->cmd->name, move (value));
+ // Determine the directory and server configuration contexts for the
+ // option.
+ //
+ context* dir_context (static_cast<context*> (conf));
+ assert (dir_context != nullptr);
+
+ server_rec* server (parms->server);
+ assert (server != nullptr);
+ assert (server->module_config != nullptr);
+
+ context* srv_context (
+ static_cast<context*> (
+ ap_get_module_config (server->module_config, &srv)));
+
+ assert (srv_context != nullptr);
+
+ // Associate the directory configuration context with the enclosing
+ // server configuration context.
+ //
+ context*& s (dir_context->server);
+ if (s == nullptr)
+ s = srv_context;
+ else
+ assert (s == srv_context);
+
+ // If the option appears in the special directory configuration context,
+ // add it to the enclosing server context instead. This way it will be
+ // possible to complement all server-enclosed contexts (including this
+ // special one) with the server scope options.
+ //
+ context* c (dir_context->special ? srv_context : dir_context);
+
+ if (dir_context->special)
+ //
+ // Make sure the special directory context is also in the option lists
+ // map. Later the context will be populated with an enclosing server
+ // context options.
+ //
+ srv.options_.emplace (make_context_id (dir_context), name_values ());
+
+ return srv.add_option (
+ make_context_id (c), parms->cmd->name, move (value));
}
const char* service::
- add_option (const char* name, optional<string> value)
+ add_option (context_id id, const char* name, optional<string> value)
{
auto i (option_descriptions_.find (name));
assert (i != option_descriptions_.end ());
@@ -105,47 +177,54 @@ namespace web
if (i->second != static_cast<bool> (value))
return value ? "unexpected value" : "value expected";
- options_.emplace_back (name + name_.length () + 1, move (value));
+ options_[id].emplace_back (name + name_.length () + 1, move (value));
return 0;
}
void service::
- init_worker (log& l) noexcept
+ complement (context_id enclosed, context_id enclosing)
{
- const string func_name (
- "web::apache::service<" + name_ + ">::init_worker");
+ auto i (options_.find (enclosing));
- try
+ // The enclosing context may have no options. It can be the context of a
+ // server having no configuration directives in it's immediate scope,
+ // but having ones in it's enclosed scope (directory or virtual server).
+ //
+ if (i != options_.end ())
{
- exemplar_.init (options_, l);
+ const name_values& src (i->second);
+ name_values& dest (options_[enclosed]);
+ dest.insert (dest.begin (), src.begin (), src.end ());
}
- catch (const exception& e)
+ }
+
+ void service::
+ finalize_config (server_rec* s)
+ {
+ if (!version_logged_)
{
- l.write (nullptr, 0, func_name.c_str (), APLOG_EMERG, e.what ());
-
- // Terminate the root apache process. Indeed we can only try to
- // terminate the process, and most likely will fail in a production
- // environment where the apache root process usually runs under root,
- // and worker processes run under some other user. This is why the
- // implementation should consider the possibility of not being
- // initialized at the time of HTTP request processing. In such a case
- // it should respond with an internal server error (500 HTTP status),
- // reporting misconfiguration.
- //
- ::kill (::getppid (), SIGTERM);
+ log l (s, this);
+ exemplar_.version (l);
+ version_logged_ = true;
}
- catch (...)
- {
- l.write (nullptr,
- 0,
- func_name.c_str (),
- APLOG_EMERG,
- "unknown error");
- // Terminate the root apache process.
- //
- ::kill (::getppid (), SIGTERM);
- }
+ // Complement directory configuration contexts with options of the
+ // enclosing server configuration context. By this time virtual server
+ // contexts are already complemented with the main server configuration
+ // context options as a result of the merge_server_context() calls.
+ //
+ for (const auto& o: options_)
+ if (o.first->server != nullptr) // Is a directory configuration context.
+ complement (o.first, o.first->server);
+
+ options_parsed_ = true;
+ }
+
+ void service::
+ clear_config ()
+ {
+ options_.clear ();
+ options_parsed_ = false;
}
}
}
diff --git a/web/apache/service.txx b/web/apache/service.txx
index 300a2a8..b57befc 100644
--- a/web/apache/service.txx
+++ b/web/apache/service.txx
@@ -2,8 +2,12 @@
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
+#include <unistd.h> // getppid()
+#include <signal.h> // kill()
+
#include <http_log.h>
+#include <utility> // move()
#include <exception>
namespace web
@@ -11,37 +15,147 @@ namespace web
namespace apache
{
template <typename M>
+ void service::
+ init_worker (log& l)
+ {
+ const std::string func_name (
+ "web::apache::service<" + name_ + ">::init_worker");
+
+ try
+ {
+ const M* exemplar (dynamic_cast<const M*> (&exemplar_));
+ assert (exemplar != nullptr);
+
+ // For each directory configuration context create the module exemplar
+ // as a deep copy of the exemplar_ member and initialize it with the
+ // context-specific option list. Note that there can be contexts
+ // having no module options specified for them and no options
+ // inherited from enclosing contexts. Such contexts will not appear in
+ // the options_ map. Meanwhile 'SetHandler <modname>' directive can be
+ // in effect for such contexts, and we should be ready to handle
+ // requests for them (by using the "root exemplar").
+ //
+ for (const auto& o: options_)
+ {
+ const context* c (o.first);
+
+ if (c->server != nullptr) // Is a directory configuration context.
+ {
+ auto r (
+ exemplars_.emplace (
+ make_context_id (c),
+ std::unique_ptr<module> (new M (*exemplar))));
+
+ r.first->second->init (o.second, l);
+ }
+ }
+
+ // Options are not needed anymore. Free up the space.
+ //
+ options_.clear ();
+
+ // Initialize the "root exemplar" by default (with no options). It
+ // will be used to handle requests for configuration contexts having
+ // no options specified, and no options inherited from enclosing
+ // contexts.
+ //
+ exemplar_.init (name_values (), l);
+ }
+ catch (const std::exception& e)
+ {
+ l.write (nullptr, 0, func_name.c_str (), APLOG_EMERG, e.what ());
+
+ // Terminate the root apache process. Indeed we can only try to
+ // terminate the process, and most likely will fail in a production
+ // environment, where the apache root process usually runs under root,
+ // and worker processes run under some other user. This is why the
+ // implementation should consider the possibility of not being
+ // initialized at the time of HTTP request processing. In such a case
+ // it should respond with an internal server error (500 HTTP status),
+ // reporting misconfiguration.
+ //
+ kill (getppid (), SIGTERM);
+ }
+ catch (...)
+ {
+ l.write (nullptr,
+ 0,
+ func_name.c_str (),
+ APLOG_EMERG,
+ "unknown error");
+
+ // Terminate the root apache process.
+ //
+ kill (getppid (), SIGTERM);
+ }
+ }
+
+ template <typename M>
int service::
- handle (request& r, log& l) noexcept
+ request_handler (request_rec* r) noexcept
+ {
+ auto srv (instance<M> ());
+ if (!r->handler || srv->name_ != r->handler) return DECLINED;
+
+ assert (r->per_dir_config != nullptr);
+
+ // Obtain the request-associated configuration context id.
+ //
+ context_id id (
+ make_context_id (ap_get_module_config (r->per_dir_config, srv)));
+
+ assert (!is_null (id));
+
+ request rq (r);
+ log lg (r->server, srv);
+ return srv->template handle<M> (rq, id, lg);
+ }
+
+ template <typename M>
+ int service::
+ handle (request& rq, context_id id, log& lg) const
{
static const std::string func_name (
"web::apache::service<" + name_ + ">::handle");
try
{
- M m (static_cast<const M&> (exemplar_));
+ auto i (exemplars_.find (id));
+
+ // Use the context-specific exemplar if found, otherwise use the
+ // default one.
+ //
+ const module* exemplar (i != exemplars_.end ()
+ ? i->second.get ()
+ : &exemplar_);
+
+ const M* e (dynamic_cast<const M*> (exemplar));
+ assert (e != nullptr);
+
+ M m (*e);
- if (static_cast<module&> (m).handle (r, r, l))
- return r.flush ();
+ if (static_cast<module&> (m).handle (rq, rq, lg))
+ return rq.flush ();
- if (!r.get_write_state ())
+ if (!rq.get_write_state ())
return DECLINED;
- l.write (nullptr, 0, func_name.c_str (), APLOG_ERR,
- "handling declined while unbuffered content has been written");
+ lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR,
+ "handling declined while unbuffered content "
+ "has been written");
}
catch (const invalid_request& e)
{
- if (!e.content.empty () && !r.get_write_state ())
+ if (!e.content.empty () && !rq.get_write_state ())
{
try
{
- r.content (e.status, e.type) << e.content;
- return r.flush ();
+ rq.content (e.status, e.type) << e.content;
+ return rq.flush ();
}
catch (const std::exception& e)
{
- l.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
+ lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
}
}
@@ -49,39 +163,39 @@ namespace web
}
catch (const std::exception& e)
{
- l.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
+ lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
- if (*e.what () && !r.get_write_state ())
+ if (*e.what () && !rq.get_write_state ())
{
try
{
- r.content (HTTP_INTERNAL_SERVER_ERROR, "text/plain;charset=utf-8")
+ rq.content (HTTP_INTERNAL_SERVER_ERROR, "text/plain;charset=utf-8")
<< e.what ();
- return r.flush ();
+ return rq.flush ();
}
catch (const std::exception& e)
{
- l.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
+ lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
}
}
}
catch (...)
{
- l.write (nullptr, 0, func_name.c_str (), APLOG_ERR, "unknown error");
+ lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, "unknown error");
- if (!r.get_write_state ())
+ if (!rq.get_write_state ())
{
try
{
- r.content (HTTP_INTERNAL_SERVER_ERROR, "text/plain;charset=utf-8")
+ rq.content (HTTP_INTERNAL_SERVER_ERROR, "text/plain;charset=utf-8")
<< "unknown error";
- return r.flush ();
+ return rq.flush ();
}
catch (const std::exception& e)
{
- l.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
+ lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
}
}
}
diff --git a/web/module b/web/module
index 824551f..85896c3 100644
--- a/web/module
+++ b/web/module
@@ -86,6 +86,9 @@ namespace web
public:
using path_type = web::path;
+ virtual
+ ~request () = default;
+
// Corresponds to abs_path portion of HTTP URL as described in
// "3.2.2 HTTP URL" of http://tools.ietf.org/html/rfc2616.
// Returns '/' if no abs_path is present in URL.
@@ -120,6 +123,9 @@ namespace web
class response
{
public:
+ virtual
+ ~response () = default;
+
// Set status code, content type, and get the stream to write
// the content to. If the buffer argument is true (default),
// then buffer the entire content before sending it as a
@@ -170,6 +176,9 @@ namespace web
class log
{
public:
+ virtual
+ ~log () = default;
+
virtual void
write (const char* msg) = 0;
};
@@ -188,6 +197,9 @@ namespace web
class module
{
public:
+ virtual
+ ~module () = default;
+
// Description of configuration options supported by this module. Note:
// should be callable during static initialization.
//