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 <exception>
#include <string>
#include <cassert>
#include <httpd/httpd.h>
#include <httpd/http_config.h>
#include <web/module>
#include <web/apache/log>
#include <web/apache/request>
namespace web
{
namespace apache
{
class service : ::module
{
public:
// Note that the module exemplar is stored by-reference.
//
template <typename M>
service (const std::string& name, const M& exemplar)
: ::module
{
STANDARD20_MODULE_STUFF,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
®ister_hooks<M>
},
name_ (name),
exemplar_ (exemplar)
// Doesn't look like handle_ member is required at all.
// handle_ (&handle_impl<M>)
{
// instance<M> () invented to delegate processing from apache request
// handler C function to service non static member function.
// This appoach resticts number of service objects per module
// implementation class with just one instance.
//
service*& srv = instance<M> ();
assert (srv == nullptr);
srv = this;
}
template <typename M>
static service*&
instance () noexcept
{
static service* instance;
return instance;
}
template <typename M>
static void
register_hooks (apr_pool_t *pool) noexcept
{
ap_hook_handler (&request_handler<M>, NULL, NULL, APR_HOOK_LAST);
}
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);
// As soons as M (), handle () and flush () can throw need to handle
// exceptions here.
//
try
{
M m (static_cast<const M&> (srv->exemplar_));
static_cast<module&> (m).handle (req, req, l);
return req.flush();
}
catch (const std::exception& e)
{
l.write (nullptr, 0, __PRETTY_FUNCTION__, APLOG_ERR, e.what ());
}
catch (...)
{
l.write (nullptr,
0,
__PRETTY_FUNCTION__,
APLOG_ERR,
"unknown error");
}
return HTTP_INTERNAL_SERVER_ERROR;
}
//@@ Implementation calls handle_ function pointer below:
//
// handle_ (rq, rs, l, exemplar_);
//
private:
/*
template <typename M>
static void
handle_impl (request& rq, response& rs, log& l, const module& exemplar)
{
M m (static_cast<const M&> (exemplar));
static_cast<module&> (m).handle (rq, rs, l);
}
*/
std::string name_;
const module& exemplar_;
// void (*handle_) (request&, response&, log&, const module&);
};
}
}
#endif // WEB_APACHE_SERVICE
|