aboutsummaryrefslogtreecommitdiff
path: root/web/apache/service
blob: 9641a6bc0e7221583a90a62a89077ffdd36ee873 (plain)
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
// file      : web/apache/service -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef WEB_APACHE_SERVICE
#define WEB_APACHE_SERVICE

#include <httpd.h>

#include <string>
#include <cassert>

#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, M& exemplar)
          : ::module
            {
              STANDARD20_MODULE_STUFF,
              nullptr,
              nullptr,
              nullptr,
              nullptr,
              nullptr,
              &register_hooks<M>
            },
            name_ (name),
            exemplar_ (exemplar)
      {
        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 config_finalizer() function is called at the end of Apache
        // server configuration parsing.
        //
        ap_hook_post_config (&config_finalizer<M>, NULL, NULL, APR_HOOK_LAST);

        // The worker_initializer() 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 request_handler () function is called for each client request.
        //
        ap_hook_handler (&request_handler<M>, NULL, NULL, APR_HOOK_LAST);
      }

      template <typename M>
      static int
      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->exemplar_.version (l);
          parsed = true;
        }

        return OK;
      }

      template <typename M>
      static void
      worker_initializer (apr_pool_t*, server_rec* s) noexcept
      {
        log l (s);
        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->template handle<M> (req, l);
      }

    private:
      void
      init_directives ();

      void
      init_worker (log& l) noexcept;

      static const char*
      parse_option (cmd_parms* parms, void* mconfig, const char* args) noexcept;

      const char*
      add_option (const char* name, optional<std::string> value);

      template <typename M>
      int
      handle (request& r, log& l) noexcept;

    private:
      std::string name_;
      module& exemplar_;
      option_descriptions option_descriptions_;
      name_values options_;
      bool options_parsed_ = false;
    };
  }
}

#include <web/apache/service.txx>

#endif // WEB_APACHE_SERVICE