Annotation of parser3/src/targets/apache/mod_parser3.c, revision 1.4
1.1 moko 1: /** @file
1.2 moko 2: Parser: apache 1.3 and 2.2 module
1.1 moko 3:
1.2 moko 4: Copyright (c) 2001-2010 ArtLebedev Group (http://www.artlebedev.com)
1.1 moko 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.4 ! moko 8: static const char * const IDENT_MOD_PARSER3_C="$Date: 2010-11-14 22:44:23 $";
1.1 moko 9:
10: #ifdef WIN32
11: #include <winsock2.h>
12: #endif
13:
14: #include "httpd.h"
15: #include "http_config.h"
16: #include "http_core.h"
17: #include "http_log.h"
18: #include "http_main.h"
19: #include "http_protocol.h"
20: #include "util_script.h"
1.2 moko 21:
22: #include "pa_httpd.h"
23:
1.3 moko 24: #define PARSER3_HANDLER "parser3-handler"
25:
1.2 moko 26: /*
27: * To Ease Compatibility
28: */
29: #ifdef STANDARD20_MODULE_STUFF
30:
31: #include "apr_strings.h"
32:
33: #define ap_pcalloc apr_pcalloc
34: #define ap_pstrdup apr_pstrdup
35:
36: #define ap_table_get apr_table_get
37: #define ap_table_elts apr_table_elts
38: #define ap_table_addn apr_table_addn
39: #define ap_table_do apr_table_do
40:
41: #else
42:
1.1 moko 43: #include "ap_alloc.h"
44:
1.2 moko 45: #define apr_pool_t pool
46: #define apr_table_t table
47:
48: #define ap_add_version_component(p, v) ap_add_version_component(v)
49:
50: #endif /* STANDARD20_MODULE_STUFF */
1.1 moko 51:
52: /*
53: * Declare ourselves so the configuration routines can find and know us.
54: * We'll fill it in at the end of the module.
55: */
1.2 moko 56:
57: #ifdef STANDARD20_MODULE_STUFF
58: module AP_MODULE_DECLARE_DATA parser3_module;
59: #else
60: module MODULE_VAR_EXPORT parser3_module;
61: #endif
1.1 moko 62:
63: /*
64: * Locate our directory configuration record for the current request.
65: */
66: static Parser_module_config *our_dconfig(request_rec *r) {
67: return (Parser_module_config *)
68: ap_get_module_config(r->per_dir_config, &parser3_module);
69: }
70:
71: static const char* cmd_parser_config(cmd_parms *cmd, void *mconfig, char *file_spec) {
72: Parser_module_config *cfg = (Parser_module_config *) mconfig;
73:
74: // remember assigned filespec into cfg
75: cfg->parser_config_filespec=file_spec;
76:
77: return NULL;
78: }
1.2 moko 79:
1.1 moko 80: static const char* cmd_parser_status_allowed(cmd_parms *cmd, void *mconfig, char *file_spec) {
81: Parser_module_config *cfg = (Parser_module_config *) mconfig;
82:
83: cfg->parser_status_allowed=1;
84:
85: return NULL;
86: }
87:
88: /*
1.2 moko 89: * Now let's declare routines for each of the callback phase in order.
1.1 moko 90: */
91:
1.3 moko 92: static int parser_handler(request_rec *r) {
93:
94: // ap_log_rerror(APLOG_MARK, APLOG_EMERG, 0, r, "handler, r->handler=%s", r->handler);
95:
96: #ifdef STANDARD20_MODULE_STUFF
97: if(strcmp(r->handler, PARSER3_HANDLER))
98: return DECLINED;
99: #endif
100: // converting to parser version
101: pa_request_rec pr={
102: r,
103: r->pool,
104: r->header_only,
105: &r->status,
106: r->method,
107: r->headers_out,
108: r->subprocess_env,
109: &r->content_type,
110: r->uri,
111: r->filename,
112: r->path_info,
113: r->args,
114: #ifdef STANDARD20_MODULE_STUFF
115: r->finfo.filetype == 0
116: #else
117: r->finfo.st_mode == 0
118: #endif
1.1 moko 119: };
120:
121: // config
1.3 moko 122: Parser_module_config *dcfg=our_dconfig(r);
123:
124: return pa_parser_handler(&pr, dcfg);
1.1 moko 125: }
126:
127: /*
1.4 ! moko 128: * This function is called during process initialisation.
1.1 moko 129: */
130:
1.3 moko 131: #ifdef STANDARD20_MODULE_STUFF
132: static void parser_module_init(apr_pool_t *p, server_rec *s) {
133: #else
1.2 moko 134: static void parser_module_init(server_rec *s, apr_pool_t *p) {
135: ap_add_version_component(p, pa_version());
1.1 moko 136: #endif
1.3 moko 137: ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, p, "parser inited %d", getpid());
1.1 moko 138: pa_setup_module_cells();
139: }
140:
141: /*
1.2 moko 142: * All our process-death routine does is add its trace to the log.
143: */
144: static void parser_module_done(server_rec *s, apr_pool_t *p) {
1.1 moko 145: pa_destroy_module_cells();
146: }
147:
148: /*
1.2 moko 149: * This function gets called to create a per-directory configuration record.
1.1 moko 150: */
1.2 moko 151: static void *parser_create_dir_config(apr_pool_t *p, char *dirspec) {
1.1 moko 152: /*
1.2 moko 153: * Allocate the space for our record from the apr_pool_t supplied.
1.1 moko 154: */
155: Parser_module_config *cfg=
156: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
157: return (void *) cfg;
158: }
159:
160: /*
1.2 moko 161: * This function gets called to merge two per-directory configuration records.
1.1 moko 162: *
1.2 moko 163: * 20011126 paf: noticed, that this is called even on virtual root merge with something "parent",
164: * while thought that that is part of merge_server...
1.1 moko 165: *
1.2 moko 166: */
167: static void *parser_merge_dir_config(apr_pool_t *p, void *parent_conf, void *newloc_conf) {
168: Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.1 moko 169: Parser_module_config *pconf = (Parser_module_config *) parent_conf;
170: Parser_module_config *nconf = (Parser_module_config *) newloc_conf;
171:
172: merged_config->parser_config_filespec = ap_pstrdup(p, nconf->parser_config_filespec?
173: nconf->parser_config_filespec:pconf->parser_config_filespec);
174: merged_config->parser_status_allowed=
175: pconf->parser_status_allowed ||
176: nconf->parser_status_allowed;
177:
178: return (void *) merged_config;
179: }
180:
181: /*
1.2 moko 182: * This function gets called to create a per-server configuration record.
1.1 moko 183: */
1.2 moko 184: static void *parser_create_server_config(apr_pool_t *p, server_rec *s) {
1.1 moko 185: /*
186: * As with the parser_create_dir_config() routine, we allocate and fill
187: * in an empty record.
188: */
189: Parser_module_config *cfg=
190: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
191:
192: return (void *) cfg;
193: }
194:
195: /*
1.2 moko 196: * This function gets called to merge two per-server configuration records.
1.1 moko 197: */
1.2 moko 198: static void *parser_merge_server_config(apr_pool_t *p, void *server1_conf, void *server2_conf)
1.1 moko 199: {
1.2 moko 200: Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
1.1 moko 201: Parser_module_config *s1conf = (Parser_module_config *) server1_conf;
202: Parser_module_config *s2conf = (Parser_module_config *) server2_conf;
203:
204: /*
205: * Our inheritance rules are our own, and part of our module's semantics.
206: * Basically, just note whence we came.
207: */
208: merged_config->parser_config_filespec = ap_pstrdup(p, s2conf->parser_config_filespec?
209: s2conf->parser_config_filespec:s1conf->parser_config_filespec);
210: merged_config->parser_status_allowed=
211: s1conf->parser_status_allowed ||
212: s2conf->parser_status_allowed;
213:
214: return (void *) merged_config;
215: }
216:
217: /*
218: * This routine gives our module an opportunity to translate the URI into an
1.2 moko 219: * actual filename.
1.1 moko 220: */
221: static int parser_translate_handler(request_rec *r) {
222: Parser_module_config *cfg=our_dconfig(r);
223: return DECLINED;
224: }
225:
226: /*
227: * This routine is called to check the authentication information sent with
1.2 moko 228: * the request
1.1 moko 229: */
230: static int parser_check_user_id(request_rec *r) {
231: Parser_module_config *cfg=our_dconfig(r);
232: return DECLINED;
233: }
234:
235: /*
236: * This routine is called to check to see if the resource being requested
237: * requires authorisation.
238: */
239: static int parser_auth_checker(request_rec *r) {
240: Parser_module_config *cfg=our_dconfig(r);
241: return DECLINED;
242: }
243:
244: /*
245: * This routine is called to check for any module-specific restrictions placed
246: * upon the requested resource. (See the mod_access module for an example.)
247: */
248: static int parser_access_checker(request_rec *r) {
249: Parser_module_config *cfg=our_dconfig(r);
250: return DECLINED;
251: }
252:
253: /*
254: * List of directives specific to our module.
255: */
256: static const command_rec parser_cmds[] =
257: {
258: {
259: "ParserConfig", /* directive name */
260: (const char* (*)(void))((void *)cmd_parser_config), // config action routine
261: (void*)0, /* argument to include in call */
262: (int)(OR_OPTIONS), /* where available */
263: TAKE1, /* arguments */
264: "Parser config filespec" // directive description
265: },
266: {
267: "ParserStatusAllowed", /* directive name */
268: (const char* (*)(void))((void *)cmd_parser_status_allowed), // config action routine
269: (void*)0, /* argument to include in call */
270: (int)(ACCESS_CONF), /* where available */
271: NO_ARGS, /* arguments */
272: "Parser status class can be used" // directive description
273: },
274: {NULL}
275: };
276:
277: /*--------------------------------------------------------------------------*/
278: /* Now the list of content handlers available from this module. */
279: /*--------------------------------------------------------------------------*/
1.2 moko 280:
281: #ifndef STANDARD20_MODULE_STUFF
1.1 moko 282: static const handler_rec parser_handlers[] =
283: {
1.3 moko 284: {PARSER3_HANDLER, parser_handler},
1.1 moko 285: {NULL}
286: };
1.2 moko 287: #endif
1.1 moko 288:
289: /*--------------------------------------------------------------------------*/
290: /* Finally, the list of callback routines and data structures that */
291: /* provide the hooks into our module from the other parts of the server. */
292: /*--------------------------------------------------------------------------*/
1.2 moko 293:
294: #ifdef STANDARD20_MODULE_STUFF
295:
1.1 moko 296: /*
1.2 moko 297: * register hooks.
1.1 moko 298: */
1.2 moko 299: static void parser_register_hooks(apr_pool_t* pool)
300: {
1.3 moko 301: // ap_add_version_component(pool, pa_version());
1.2 moko 302: // ap_hook_post_config(parser_server_init, NULL, NULL, APR_HOOK_MIDDLE);
303: ap_hook_handler(parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
1.3 moko 304: ap_hook_child_init(parser_module_init, NULL, NULL, APR_HOOK_MIDDLE);
1.2 moko 305: // ap_hook_translate_name(parser_translate_handler, NULL, NULL, APR_HOOK_MIDDLE);
306: // ap_hook_check_user_id(parser_check_user_id, NULL, NULL, APR_HOOK_MIDDLE);
307: // ap_hook_auth_checker(parser_auth_checker, NULL, NULL, APR_HOOK_MIDDLE);
308: // ap_hook_access_checker(parser_access_checker, NULL, NULL, APR_HOOK_MIDDLE);
309: };
310:
311: module AP_MODULE_DECLARE_DATA parser3_module =
312: {
313: STANDARD20_MODULE_STUFF,
314: #else
1.1 moko 315: module MODULE_VAR_EXPORT parser3_module =
316: {
317: STANDARD_MODULE_STUFF,
318: parser_module_init, /* module initializer */
1.2 moko 319: #endif
1.1 moko 320: parser_create_dir_config, /* per-directory config creator */
321: parser_merge_dir_config, /* dir config merger */
322: parser_create_server_config, /* server config creator */
323: parser_merge_server_config, /* server config merger */
1.2 moko 324: parser_cmds, /* command apr_table_t */
325: #ifdef STANDARD20_MODULE_STUFF
326: parser_register_hooks /* register hooks */
327: #else
1.1 moko 328: parser_handlers, /* [9] list of handlers */
329: parser_translate_handler, /* [2] filename-to-URI translation */
330: parser_check_user_id, /* [5] check/validate user_id */
331: parser_auth_checker, /* [6] check user_id is valid *here* */
332: parser_access_checker, /* [4] check access by host address */
333: 0, /* [7] MIME type checker/setter */
334: 0, /* [8] fixups */
1.4 ! moko 335: 0, /* [10] logger */
! 336: 0, /* [3] header parser */
! 337: 0, /* process initializer */
! 338: parser_module_done /* process exit/cleanup */
1.2 moko 339: #endif // STANDARD20_MODULE_STUFF
1.1 moko 340: };
341:
342: #if defined(_MSC_VER)
343: # define APACHE_WIN32_SRC "../../../../win32/apache13/src"
344: # ifdef _DEBUG
345: # pragma comment(lib, APACHE_WIN32_SRC "/CoreD/ApacheCore.lib")
346: # else
347: # pragma comment(lib, APACHE_WIN32_SRC "/CoreR/ApacheCore.lib")
348: # endif
349: #endif
350:
351:
352: // interface to C++
353:
1.4 ! moko 354: #define PA_APLOG_EMERG 0 /* system is unusable */
! 355: #define PA_APLOG_ALERT 1 /* action must be taken immediately */
! 356: #define PA_APLOG_CRIT 2 /* critical conditions */
! 357: #define PA_APLOG_ERR 3 /* error conditions */
1.1 moko 358: #define PA_APLOG_WARNING 4 /* warning conditions */
1.4 ! moko 359: #define PA_APLOG_NOTICE 5 /* normal but significant condition */
! 360: #define PA_APLOG_INFO 6 /* informational */
! 361: #define PA_APLOG_DEBUG 7 /* debug-level messages */
1.1 moko 362:
363: #define PA_APLOG_LEVELMASK 7 /* mask off the level value */
364:
1.4 ! moko 365: #define PA_APLOG_NOERRNO (PA_APLOG_LEVELMASK + 1)
1.1 moko 366:
367: #define PA_APLOG_MARK __FILE__,__LINE__
368:
1.2 moko 369: void pa_ap_log_rerror(const char *file, int line, int level, const pa_request_rec *s, const char *fmt, ...) {
1.1 moko 370: const char* str;
371: va_list l;
372: va_start(l, fmt);
373: str=va_arg(l, const char*);
374: va_end(l);
375:
376: ap_log_rerror(file, line, level,
1.2 moko 377: #ifdef STANDARD20_MODULE_STUFF
378: 0,
379: #endif
1.1 moko 380: (request_rec*)s->real_request_rec, "%s", str);
381: }
382:
383:
1.2 moko 384: void pa_ap_log_error(const char *file, int line, int level, const pa_server_rec *s, const char *fmt, ...) {
1.1 moko 385: const char* str;
386: va_list l;
387: va_start(l, fmt);
388: str=va_arg(l, const char*);
389: va_end(l);
390:
391: ap_log_error(file, line, level,
1.2 moko 392: #ifdef STANDARD20_MODULE_STUFF
393: 0,
394: #endif
1.1 moko 395: (server_rec*)s, "%s", str);
396: }
397:
398: // ap_alloc.h
399:
400: const char* pa_ap_table_get(const pa_table *t, const char *name) {
1.2 moko 401: return ap_table_get((const apr_table_t*)t, name);
1.1 moko 402: }
403: void pa_ap_table_addn(pa_table *t, const char *name, const char *val) {
1.2 moko 404: ap_table_addn((apr_table_t*)t, name, val);
1.1 moko 405: }
406:
407: int pa_ap_table_size(const pa_table *t) {
1.2 moko 408: return ap_table_elts((const apr_table_t*)t)->nelts;
1.1 moko 409: }
410:
1.2 moko 411: void pa_ap_table_do(int (*comp) (void *, const char *, const char *), void *rec, const pa_table *t, ...) {
412: ap_table_do(comp, rec, (apr_table_t*)t, 0);
1.1 moko 413: }
414:
415: char * pa_ap_pstrdup(pa_pool *p, const char *s) {
1.2 moko 416: return ap_pstrdup((apr_pool_t*)p, s);
1.1 moko 417: }
418:
419: // http_protocol.h
420:
421: int pa_ap_setup_client_block(pa_request_rec *r, int read_policy) {
422: return ap_setup_client_block((request_rec*)r->real_request_rec,
423: read_policy);
424: }
425: int pa_ap_should_client_block(pa_request_rec *r) {
426: return ap_should_client_block((request_rec*)r->real_request_rec);
427: }
428: long pa_ap_get_client_block(pa_request_rec *r, char *buffer, int bufsiz) {
429: return ap_get_client_block((request_rec*)r->real_request_rec,
430: buffer, bufsiz);
431: }
432: void pa_ap_send_http_header(pa_request_rec *r) {
1.2 moko 433: // Apache2 send headers before body automatically
434: #ifndef STANDARD20_MODULE_STUFF
1.1 moko 435: ap_send_http_header((request_rec*)r->real_request_rec);
1.2 moko 436: #endif
1.1 moko 437: }
438: int pa_ap_rwrite(const void *buf, int nbyte, pa_request_rec *r) {
439: return ap_rwrite(buf, nbyte, (request_rec*)r->real_request_rec);
440: }
441:
442: // http_main.h
443:
444: void pa_ap_hard_timeout(char *s, pa_request_rec *r) {
1.2 moko 445: // Apache 2 uses non-blocking I/O
446: #ifndef STANDARD20_MODULE_STUFF
1.1 moko 447: ap_hard_timeout(s, (request_rec*)r->real_request_rec);
1.2 moko 448: #endif
1.1 moko 449: }
450: void pa_ap_reset_timeout(pa_request_rec *r) {
1.2 moko 451: #ifndef STANDARD20_MODULE_STUFF
1.1 moko 452: ap_reset_timeout((request_rec*)r->real_request_rec);
1.2 moko 453: #endif
1.1 moko 454: }
455: void pa_ap_kill_timeout(pa_request_rec *r) {
1.2 moko 456: #ifndef STANDARD20_MODULE_STUFF
1.1 moko 457: ap_kill_timeout((request_rec*)r->real_request_rec);
1.2 moko 458: #endif
1.1 moko 459: }
460:
461: // util_script.h
462:
463: void pa_ap_add_cgi_vars(pa_request_rec *r) {
464: ap_add_cgi_vars((request_rec*)r->real_request_rec);
465: }
466: void pa_ap_add_common_vars(pa_request_rec *r) {
467: ap_add_common_vars((request_rec*)r->real_request_rec);
468: }
469:
1.2 moko 470: #ifndef WIN32
1.1 moko 471: // signal.h
472:
473: void (*pa_signal (int sig, void (*disp)(int)))(int) {
474: if(sig==PA_SIGPIPE && disp==PA_SIG_IGN)
475: return signal(SIGPIPE, SIG_IGN);
476:
477: return 0;
478: }
1.2 moko 479: #endif
E-mail: