Rudiments
apachemodule.h
1// Copyright (c) 1999-2018 David Muse
2// See the COPYING file for more information
3
4#ifndef RUDIMENTS_APACHEMODULE_H
5#define RUDIMENTS_APACHEMODULE_H
6
7#ifndef HTTP_MODULE_NAME
8 #error HTTP_MODULE_NAME not defined
9#endif
10
11#define XDO_CHILD_INIT_FUNCTION(NM) DO_CHILD_INIT_FUNCTION(NM)
12#define DO_CHILD_INIT_FUNCTION(NM) do_##NM##_init
13#define XCHILD_INIT_FUNCTION(NM) CHILD_INIT_FUNCTION(NM)
14#define CHILD_INIT_FUNCTION(NM) NM##_init
15
16#define XDO_HANDLER_FUNCTION(NM) DO_HANDLER_FUNCTION(NM)
17#define DO_HANDLER_FUNCTION(NM) do_##NM##_handler
18#define XHANDLER_FUNCTION(NM) HANDLER_FUNCTION(NM)
19#define HANDLER_FUNCTION(NM) NM##_handler
20
21#define XDO_CHILD_EXIT_FUNCTION(NM) DO_CHILD_EXIT_FUNCTION(NM)
22#define DO_CHILD_EXIT_FUNCTION(NM) do_##NM##_exit
23#define XCHILD_EXIT_FUNCTION(NM) CHILD_EXIT_FUNCTION(NM)
24#define CHILD_EXIT_FUNCTION(NM) NM##_exit
25
26#define XMODULE_STRUCT(NM) MODULE_STRUCT(NM)
27#define MODULE_STRUCT(NM) NM##_module
28
29#define XXFILE_NAME(NM) XFILE_NAME(NM)
30#define XFILE_NAME(NM) FILE_NAME(mod_##NM)
31#define FILE_NAME(NM) #NM".c"
32
33#define XMOD_NAME(NM) MOD_NAME(NM)
34#define MOD_NAME(NM) #NM
35
36#include <rudiments/apachehttpserverapi.h>
37#include <rudiments/private/apacheincludes.h>
38
39#ifdef APACHE_2
40 #include <rudiments/charstring.h>
41#endif
42
44 void *requestrec;
45 void *serverrec;
46};
47
48static bool httpModuleInit(httpserverapi *sapi);
49static bool httpModuleExit(httpserverapi *sapi);
50static bool httpModuleMain(httpserverapi *sapi);
51
52extern "C" {
53
54#ifdef APACHE_2
55
56 static void XCHILD_INIT_FUNCTION(HTTP_MODULE_NAME)(
57 apr_pool_t *p,
58 server_rec *s);
59 static apr_status_t XHANDLER_FUNCTION(HTTP_MODULE_NAME)(
60 request_rec *r);
61 static apr_status_t XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME)(void *);
62
63
64 static void XDO_CHILD_INIT_FUNCTION(HTTP_MODULE_NAME)(
65 apr_pool_t *p,
66 server_rec *s) {
67 XCHILD_INIT_FUNCTION(HTTP_MODULE_NAME)(p,s);
68 apr_pool_cleanup_register(p,s,
69 XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME),
70 XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME));
71 }
72
73 static apr_status_t XDO_HANDLER_FUNCTION(HTTP_MODULE_NAME)(
74 request_rec *r) {
75 if (charstring::compare(r->handler,
76 XMOD_NAME(HTTP_MODULE_NAME))) {
77 return DECLINED;
78 }
79 return XHANDLER_FUNCTION(HTTP_MODULE_NAME)(r);
80 }
81
82
83 static void register_hooks(apr_pool_t *pool) {
84 ap_hook_child_init(XDO_CHILD_INIT_FUNCTION(HTTP_MODULE_NAME),
85 NULL,NULL,APR_HOOK_FIRST);
86 ap_hook_handler(XDO_HANDLER_FUNCTION(HTTP_MODULE_NAME),
87 NULL,NULL,APR_HOOK_FIRST);
88 }
89
90
91 module AP_MODULE_DECLARE_DATA XMODULE_STRUCT(HTTP_MODULE_NAME) = {
92 MODULE_MAGIC_NUMBER_MAJOR,
93 MODULE_MAGIC_NUMBER_MINOR,
94 -1,
95 XXFILE_NAME(HTTP_MODULE_NAME),
96 NULL,
97 NULL,
98 MODULE_MAGIC_COOKIE,
99 NULL, /* module initializer */
100 NULL, /* create per-dir config structures */
101 NULL, /* merge per-dir config structures */
102 NULL, /* create per-server config structures */
103 NULL, /* merge per-server config structures */
104 NULL, /* command handlers */
105 register_hooks /* register hooks */
106 };
107
108
109 static void XCHILD_INIT_FUNCTION(HTTP_MODULE_NAME)(apr_pool_t *p,
110 server_rec *s) {
111 apacheapistruct aas;
112 aas.requestrec=NULL;
113 aas.serverrec=s;
114
115 apachehttpserverapi sapi((void *)&aas);
116 httpModuleInit(&sapi);
117 }
118
119 static apr_status_t XHANDLER_FUNCTION(HTTP_MODULE_NAME)(
120 request_rec *r) {
121
122 apacheapistruct aas;
123 aas.requestrec=r;
124 aas.serverrec=r->server;
125
126 apachehttpserverapi sapi((void *)&aas);
127 return (httpModuleMain(&sapi))?OK:DECLINED;
128 }
129
130 static apr_status_t XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME)(void *v) {
131
132 apacheapistruct aas;
133 aas.requestrec=NULL;
134 aas.serverrec=(server_rec *)v;
135
136 apachehttpserverapi sapi((void *)&aas);
137 return (httpModuleExit(&sapi))?OK:DECLINED;
138 }
139
140#else
141
142 static void XCHILD_INIT_FUNCTION(HTTP_MODULE_NAME)(
143 server_rec *s, pool *p);
144 static int XHANDLER_FUNCTION(HTTP_MODULE_NAME)(
145 request_rec *r);
146 static void XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME)(
147 server_rec *s, pool *p);
148
149
150 static const handler_rec handler_struct[] = {
151 { XMOD_NAME(HTTP_MODULE_NAME),
152 XHANDLER_FUNCTION(HTTP_MODULE_NAME) },
153 { NULL, NULL }
154 };
155
156
157 #ifdef MODULE_MAGIC_COOKIE
158 #define GW_MODULE_MAGIC_COOKIE MODULE_MAGIC_COOKIE,
159 #else
160 #define GW_MODULE_MAGIC_COOKIE
161 #endif
162
163 module MODULE_VAR_EXPORT XMODULE_STRUCT(HTTP_MODULE_NAME) = {
164 MODULE_MAGIC_NUMBER_MAJOR,
165 MODULE_MAGIC_NUMBER_MINOR,
166 -1,
167 XXFILE_NAME(HTTP_MODULE_NAME),
168 NULL,
169 NULL,
170 GW_MODULE_MAGIC_COOKIE
171 NULL, /* module initializer */
172 NULL, /* create per-dir config structures */
173 NULL, /* merge per-dir config structures */
174 NULL, /* create per-server config structures */
175 NULL, /* merge per-server config structures */
176 NULL, /* table of config file commands */
177 handler_struct, /* [#8] MIME-typed-dispatched handlers */
178 NULL, /* [#1] URI to filename translation */
179 NULL, /* [#4] validate user id from request */
180 NULL, /* [#5] check if the user is ok _here_ */
181 NULL, /* [#3] check access by host address */
182 NULL, /* [#6] determine MIME type */
183 NULL, /* [#7] pre-run fixups */
184 NULL, /* [#9] log a transaction */
185 NULL, /* [#2] header parser */
186 XCHILD_INIT_FUNCTION(HTTP_MODULE_NAME), /* child_init */
187 XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME), /* child_exit */
188 NULL /* [#0] post read-request */
189 #ifdef EAPI
190 ,NULL, /* EAPI: add_module */
191 NULL, /* EAPI: remove_module */
192 NULL, /* EAPI: rewrite_command */
193 NULL /* EAPI: new_connection */
194 #endif
195 };
196
197
198 static void XCHILD_INIT_FUNCTION(HTTP_MODULE_NAME)(
199 server_rec *s, pool *p) {
200 apacheapistruct aas;
201 aas.requestrec=NULL;
202 aas.serverrec=s;
203
204 apachehttpserverapi sapi((void *)&aas);
205 httpModuleInit(&sapi);
206 }
207
208 static int XHANDLER_FUNCTION(HTTP_MODULE_NAME)(request_rec *r) {
209
210 apacheapistruct aas;
211 aas.requestrec=r;
212 aas.serverrec=r->server;
213
214 apachehttpserverapi sapi((void *)&aas);
215 return (httpModuleMain(&sapi))?OK:DECLINED;
216 }
217
218 static void XCHILD_EXIT_FUNCTION(HTTP_MODULE_NAME)(
219 server_rec *s, pool *p) {
220
221 apacheapistruct aas;
222 aas.requestrec=NULL;
223 aas.serverrec=s;
224
225 apachehttpserverapi sapi((void *)&aas);
226 httpModuleExit(&sapi);
227 }
228#endif
229
230}
231
232#endif
Definition apachehttpserverapi.h:20
static int32_t compare(const char *str1, const char *str2)
Definition httpserverapi.h:22
Definition apachemodule.h:43