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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// file : web/apache/service.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <unistd.h> // getppid()
#include <signal.h> // kill(), SIGTERM
#include <http_log.h> // APLOG_*
#include <utility> // move()
#include <exception>
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::
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
{
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);
for (M m (*e);;)
{
try
{
if (static_cast<module&> (m).handle (rq, rq, lg))
return rq.flush ();
if (rq.state () == request_state::initial)
return DECLINED;
lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR,
"handling declined being partially executed");
break;
}
catch (const module::retry&)
{
// Retry to handle the request.
//
rq.rewind ();
}
}
}
catch (const invalid_request& e)
{
if (!e.content.empty () && rq.state () < request_state::writing)
{
try
{
rq.content (e.status, e.type) << e.content;
return rq.flush ();
}
catch (const std::exception& e)
{
lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
}
}
return e.status;
}
catch (const std::exception& e)
{
lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
if (*e.what () && rq.state () < request_state::writing)
{
try
{
rq.content (
HTTP_INTERNAL_SERVER_ERROR, "text/plain;charset=utf-8")
<< e.what ();
return rq.flush ();
}
catch (const std::exception& e)
{
lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
}
}
}
catch (...)
{
lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, "unknown error");
if (rq.state () < request_state::writing)
{
try
{
rq.content (
HTTP_INTERNAL_SERVER_ERROR, "text/plain;charset=utf-8")
<< "unknown error";
return rq.flush ();
}
catch (const std::exception& e)
{
lg.write (nullptr, 0, func_name.c_str (), APLOG_ERR, e.what ());
}
}
}
return HTTP_INTERNAL_SERVER_ERROR;
}
}
}
|