1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// file : web/apache/service -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license : MIT; see accompanying LICENSE file
#ifndef WEB_APACHE_SERVICE
#define WEB_APACHE_SERVICE
#include <httpd/httpd.h>
#include <string>
#include <vector>
#include <cassert>
#include <algorithm> // move()
#include <web/module>
#include <web/apache/log>
#include <web/apache/request>
namespace web
{
namespace apache
{
class service: ::module
{
public:
using option_names = std::vector<std::string>;
// Note that the module exemplar is stored by-reference.
//
template <typename M>
service (const std::string& name,
M& exemplar,
option_names opts = option_names ())
: ::module
{
STANDARD20_MODULE_STUFF,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
®ister_hooks<M>
},
name_ (name),
exemplar_ (exemplar),
option_names_ (std::move (opts))
{
init_directives();
// 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
// specific module implementation class with just one instance.
//
service*& srv (instance<M> ());
assert (srv == nullptr);
srv = this;
}
~service ()
{
delete [] cmds;
}
template <typename M>
static service*&
instance () noexcept
{
static service* instance;
return instance;
}
template <typename M>
static void
register_hooks (apr_pool_t*) noexcept
{
// The registered function is called right after apache worker
// process is started. Called for every new process spawned.
//
ap_hook_child_init (&worker_initializer<M>, NULL, NULL, APR_HOOK_LAST);
// The registered function is called for each client request.
//
ap_hook_handler (&request_handler<M>, NULL, NULL, APR_HOOK_LAST);
}
template <typename M>
static void
worker_initializer (apr_pool_t*, server_rec* server) noexcept
{
log l (server);
instance<M> ()->init_worker(l);
}
template <typename M>
static int
request_handler (request_rec* r) noexcept
{
auto srv (instance<M> ());
if (!r->handler || srv->name_ != r->handler) return DECLINED;
request req (r);
log l (r->server);
return srv->handle<M>(req, l);
}
private:
void
init_directives();
void
init_worker(log& l) noexcept;
static const char*
add_option (cmd_parms *parms, void *mconfig, const char *value) noexcept;
template <typename M>
int handle(request& r, log& l) noexcept;
private:
std::string name_;
module& exemplar_;
option_names option_names_;
name_values options_;
};
}
}
#include <web/apache/service.txx>
#endif // WEB_APACHE_SERVICE
|