Annotation of parser3/src/targets/apache/mod_parser3.c, revision 1.15

1.1       moko        1: /** @file
1.2       moko        2:        Parser: apache 1.3 and 2.2 module
1.1       moko        3: 
1.12      moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       moko        5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
                      8: #include "httpd.h"
                      9: #include "http_config.h"
                     10: #include "http_core.h"
                     11: #include "http_log.h"
                     12: #include "http_main.h"
                     13: #include "http_protocol.h"
                     14: #include "util_script.h"
1.2       moko       15: 
                     16: #include "pa_httpd.h"
                     17: 
1.15    ! moko       18: volatile const char * IDENT_MOD_PARSER3_C="$Id: mod_parser3.c,v 1.14 2012/04/18 18:58:33 moko Exp $" IDENT_PA_HTTPD_H;
1.12      moko       19: 
1.3       moko       20: #define PARSER3_HANDLER "parser3-handler"
                     21: 
1.2       moko       22: /*
                     23: * To Ease Compatibility
                     24: */
                     25: #ifdef STANDARD20_MODULE_STUFF
                     26: 
                     27: #include "apr_strings.h"
1.14      moko       28: #include "ap_mpm.h"
1.2       moko       29: 
                     30: #define ap_pcalloc     apr_pcalloc
                     31: #define ap_pstrdup     apr_pstrdup
                     32: 
                     33: #define ap_table_get   apr_table_get
                     34: #define ap_table_elts  apr_table_elts
                     35: #define ap_table_addn  apr_table_addn
                     36: #define ap_table_do    apr_table_do
                     37: 
                     38: #else
                     39: 
1.1       moko       40: #include "ap_alloc.h"
                     41: 
1.2       moko       42: #define apr_pool_t pool
                     43: #define apr_table_t table
                     44: 
                     45: #endif /* STANDARD20_MODULE_STUFF */
1.1       moko       46: 
                     47: /*
                     48: * Declare ourselves so the configuration routines can find and know us.
                     49: * We'll fill it in at the end of the module.
                     50: */
1.2       moko       51: 
                     52: #ifdef STANDARD20_MODULE_STUFF
                     53: module AP_MODULE_DECLARE_DATA parser3_module;
1.14      moko       54: static int is_threaded = 0;
1.2       moko       55: #else
                     56: module MODULE_VAR_EXPORT parser3_module;
                     57: #endif
1.1       moko       58: 
                     59: /*
                     60: * Locate our directory configuration record for the current request.
                     61: */
                     62: static Parser_module_config *our_dconfig(request_rec *r) {
1.5       moko       63:        return (Parser_module_config *) ap_get_module_config(r->per_dir_config, &parser3_module);
1.1       moko       64: }
                     65: 
1.9       moko       66: static const char* cmd_parser_config(cmd_parms *cmd, void *mconfig, const char *file_spec) {
1.1       moko       67:        Parser_module_config *cfg = (Parser_module_config *) mconfig;
                     68:        cfg->parser_config_filespec=file_spec;
                     69:        return NULL;
                     70: }
                     71: 
                     72: /* 
1.2       moko       73: * Now let's declare routines for each of the callback phase in order.
1.1       moko       74: */
                     75: 
1.3       moko       76: static int parser_handler(request_rec *r) {
                     77: #ifdef STANDARD20_MODULE_STUFF
                     78:        if(strcmp(r->handler, PARSER3_HANDLER))
                     79:                return DECLINED;
1.14      moko       80: 
                     81:        if(is_threaded){
                     82:                const char *message="Parser3 module requires apache2-mpm-prefork";
                     83:                r->status=HTTP_INTERNAL_SERVER_ERROR;
                     84:                r->content_type="text/plain";
                     85:                ap_rwrite(message, strlen(message), r);
                     86:                return OK;
                     87:        }
1.3       moko       88: #endif
1.13      moko       89: 
                     90:        // we setup module here to avoid GPF on init with php5-xsl installed
                     91:        pa_setup_module_cells();
                     92: 
1.3       moko       93:        // converting to parser version
                     94:        pa_request_rec pr={
                     95:                r,
                     96:                r->pool,
                     97:                r->header_only,
                     98:                &r->status,
                     99:                r->method,
                    100:                r->headers_out,
                    101:                r->subprocess_env,
                    102:                &r->content_type,
                    103:                r->uri,
                    104:                r->filename,
                    105:                r->path_info,
                    106:                r->args,
                    107: #ifdef STANDARD20_MODULE_STUFF
                    108:                r->finfo.filetype == 0
                    109: #else          
                    110:                r->finfo.st_mode == 0
                    111: #endif
1.1       moko      112:        };
1.6       moko      113:        return pa_parser_handler(&pr, our_dconfig(r));
1.1       moko      114: }
                    115: 
                    116: /* 
1.4       moko      117: * This function is called during process initialisation.
1.1       moko      118: */
                    119: 
1.3       moko      120: #ifdef STANDARD20_MODULE_STUFF
1.10      moko      121: static void parser_child_init(apr_pool_t *p, server_rec *s) {
1.3       moko      122: #else
1.2       moko      123: static void parser_module_init(server_rec *s, apr_pool_t *p) {
1.1       moko      124: #endif 
1.13      moko      125: //     ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, p, "parser inited %d", getpid());
1.1       moko      126: }
                    127: 
                    128: /* 
1.2       moko      129: * All our process-death routine does is add its trace to the log.
                    130: */
                    131: static void parser_module_done(server_rec *s, apr_pool_t *p) {
1.1       moko      132:        pa_destroy_module_cells();
                    133: }
                    134: 
                    135: /*
1.2       moko      136: * This function gets called to create a per-directory configuration record.
1.1       moko      137: */
1.2       moko      138: static void *parser_create_dir_config(apr_pool_t *p, char *dirspec) {
1.6       moko      139:        Parser_module_config *cfg= ap_pcalloc(p, sizeof(Parser_module_config));
                    140:        cfg->parser_config_filespec=NULL;
                    141:        return cfg;
1.1       moko      142: }
                    143: 
                    144: /*
1.2       moko      145: * This function gets called to create a per-server configuration record.
1.1       moko      146: */
1.2       moko      147: static void *parser_create_server_config(apr_pool_t *p, server_rec *s) {
1.6       moko      148:        Parser_module_config *cfg= ap_pcalloc(p, sizeof(Parser_module_config));
                    149:        cfg->parser_config_filespec=NULL;
                    150:        return cfg;
1.1       moko      151: }
                    152: 
                    153: /* 
                    154: * List of directives specific to our module.
                    155: */
                    156: static const command_rec parser_cmds[] =
                    157: {
1.11      moko      158:        {"ParserConfig", (const char *(*)())cmd_parser_config, 0, OR_OPTIONS, TAKE1, "Parser config filespec"},
1.10      moko      159:        {NULL}
1.1       moko      160: };
                    161: 
                    162: /*--------------------------------------------------------------------------*/
                    163: /* Now the list of content handlers available from this module.             */
                    164: /*--------------------------------------------------------------------------*/
1.2       moko      165: 
                    166: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      167: static const handler_rec parser_handlers[] =
                    168: {
1.3       moko      169:        {PARSER3_HANDLER, parser_handler},
1.1       moko      170:        {NULL}
                    171: };
1.2       moko      172: #endif
1.1       moko      173: 
                    174: /*--------------------------------------------------------------------------*/
                    175: /* Finally, the list of callback routines and data structures that          */
                    176: /* provide the hooks into our module from the other parts of the server.    */
                    177: /*--------------------------------------------------------------------------*/
1.2       moko      178: 
                    179: #ifdef STANDARD20_MODULE_STUFF
                    180: 
1.1       moko      181: /* 
1.2       moko      182: * register hooks.
1.1       moko      183: */
1.2       moko      184: static void parser_register_hooks(apr_pool_t* pool)
                    185: {
1.14      moko      186:        int mpm_query_info;
                    187:        is_threaded = ap_mpm_query(AP_MPMQ_IS_THREADED, &mpm_query_info) == APR_SUCCESS && mpm_query_info != AP_MPMQ_NOT_SUPPORTED;
1.2       moko      188:        ap_hook_handler(parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
1.10      moko      189:        ap_hook_child_init(parser_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1.2       moko      190: };
                    191: 
                    192: module AP_MODULE_DECLARE_DATA parser3_module =
                    193: {
                    194:        STANDARD20_MODULE_STUFF,
                    195: #else
1.1       moko      196: module MODULE_VAR_EXPORT parser3_module =
                    197: {
                    198:        STANDARD_MODULE_STUFF,
1.5       moko      199:        parser_module_init,             /* module initializer */
1.2       moko      200: #endif
1.5       moko      201:        parser_create_dir_config,       /* per-directory config creator */
1.6       moko      202:        0,                              /* dir config merger */
1.5       moko      203:        parser_create_server_config,    /* server config creator */
1.6       moko      204:        0,                              /* server config merger */
1.5       moko      205:        parser_cmds,                    /* command apr_table_t */
1.2       moko      206: #ifdef STANDARD20_MODULE_STUFF
1.5       moko      207:        parser_register_hooks           /* register hooks */
1.2       moko      208: #else
1.5       moko      209:        parser_handlers,                /* [9] list of handlers */
                    210:        0,                              /* [2] filename-to-URI translation */
                    211:        0,                              /* [5] check/validate user_id */
                    212:        0,                              /* [6] check user_id is valid *here* */
                    213:        0,                              /* [4] check access by host address */
                    214:        0,                              /* [7] MIME type checker/setter */
                    215:        0,                              /* [8] fixups */
                    216:        0,                              /* [10] logger */
                    217:        0,                              /* [3] header parser */
                    218:        0,                              /* process initializer */
                    219:        parser_module_done              /* process exit/cleanup */
1.2       moko      220: #endif // STANDARD20_MODULE_STUFF
1.1       moko      221: };
                    222: 
                    223: #if defined(_MSC_VER)
1.15    ! moko      224: #      define APACHE_WIN32_SRC "../../../../win32/apache22/"
1.1       moko      225: #      ifdef _DEBUG
1.15    ! moko      226: #              pragma comment(lib, APACHE_WIN32_SRC "srclib/apr/Debug/libapr-1.lib")
        !           227: #              pragma comment(lib, APACHE_WIN32_SRC "Debug/libhttpd.lib")
1.1       moko      228: #      else
1.15    ! moko      229: #              pragma comment(lib, APACHE_WIN32_SRC "srclib/apr/Release/libapr-1.lib")
        !           230: #              pragma comment(lib, APACHE_WIN32_SRC "Release/libhttpd.lib")
1.1       moko      231: #      endif
                    232: #endif
                    233: 
                    234: 
                    235: // interface to C++
                    236: 
1.2       moko      237: void pa_ap_log_rerror(const char *file, int line, int level, const pa_request_rec *s, const char *fmt, ...) {
1.1       moko      238:        const char* str;
                    239:        va_list l;
                    240:        va_start(l, fmt); 
                    241:        str=va_arg(l, const char*);
                    242:        va_end(l); 
                    243: 
                    244:        ap_log_rerror(file, line, level,
1.2       moko      245: #ifdef STANDARD20_MODULE_STUFF
1.10      moko      246:                        0,
1.2       moko      247: #endif
1.10      moko      248:                        (request_rec*)s->real_request_rec, "%s", str);
1.1       moko      249: }
                    250: 
                    251: 
1.2       moko      252: void pa_ap_log_error(const char *file, int line, int level, const pa_server_rec *s, const char *fmt, ...) {
1.1       moko      253:        const char* str;
                    254:        va_list l;
                    255:        va_start(l, fmt); 
                    256:        str=va_arg(l, const char*);
                    257:        va_end(l); 
                    258: 
                    259:        ap_log_error(file, line, level,
1.2       moko      260: #ifdef STANDARD20_MODULE_STUFF
1.10      moko      261:                        0,
1.2       moko      262: #endif
1.10      moko      263:                        (server_rec*)s, "%s", str);
1.1       moko      264: }
                    265: 
                    266: // ap_alloc.h
                    267: 
                    268: const char* pa_ap_table_get(const pa_table *t, const char *name) {
1.2       moko      269:        return ap_table_get((const apr_table_t*)t, name);
1.1       moko      270: }
                    271: void pa_ap_table_addn(pa_table *t, const char *name, const char *val) {
1.2       moko      272:        ap_table_addn((apr_table_t*)t, name, val);
1.1       moko      273: }
                    274: 
                    275: int pa_ap_table_size(const pa_table *t) {
1.2       moko      276:        return ap_table_elts((const apr_table_t*)t)->nelts;
1.1       moko      277: }
                    278: 
1.2       moko      279: void pa_ap_table_do(int (*comp) (void *, const char *, const char *), void *rec, const pa_table *t, ...) {
1.14      moko      280:        ap_table_do(comp, rec, (apr_table_t*)t, NULL);
1.1       moko      281: }
                    282: 
                    283: char * pa_ap_pstrdup(pa_pool *p, const char *s) {
1.2       moko      284:        return ap_pstrdup((apr_pool_t*)p, s);
1.1       moko      285: }
                    286: 
                    287: // http_protocol.h
                    288: 
                    289: int pa_ap_setup_client_block(pa_request_rec *r, int read_policy) {
                    290:        return ap_setup_client_block((request_rec*)r->real_request_rec,
                    291:                read_policy);
                    292: }
                    293: int pa_ap_should_client_block(pa_request_rec *r) {
                    294:        return ap_should_client_block((request_rec*)r->real_request_rec);
                    295: }
                    296: long pa_ap_get_client_block(pa_request_rec *r, char *buffer, int bufsiz) {
                    297:        return ap_get_client_block((request_rec*)r->real_request_rec,
                    298:                buffer, bufsiz);
                    299: }
                    300: void pa_ap_send_http_header(pa_request_rec *r) {
1.2       moko      301: // Apache2 send headers before body automatically
                    302: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      303:        ap_send_http_header((request_rec*)r->real_request_rec);
1.2       moko      304: #endif
1.1       moko      305: }
                    306: int pa_ap_rwrite(const void *buf, int nbyte, pa_request_rec *r) {
                    307:        return ap_rwrite(buf, nbyte, (request_rec*)r->real_request_rec);
                    308: }
                    309: 
                    310: // http_main.h
                    311: 
1.7       moko      312: void pa_ap_hard_timeout(const char *s, pa_request_rec *r) {
1.2       moko      313: // Apache 2 uses non-blocking I/O
                    314: #ifndef STANDARD20_MODULE_STUFF
1.11      moko      315:        ap_hard_timeout((char *)s, (request_rec*)r->real_request_rec);
1.2       moko      316: #endif
1.1       moko      317: }
                    318: void pa_ap_reset_timeout(pa_request_rec *r) {
1.2       moko      319: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      320:        ap_reset_timeout((request_rec*)r->real_request_rec);
1.2       moko      321: #endif
1.1       moko      322: }
                    323: void pa_ap_kill_timeout(pa_request_rec *r) {
1.2       moko      324: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      325:        ap_kill_timeout((request_rec*)r->real_request_rec);
1.2       moko      326: #endif
1.1       moko      327: }
                    328: 
                    329: // util_script.h
                    330: 
                    331: void pa_ap_add_cgi_vars(pa_request_rec *r) {
                    332:        ap_add_cgi_vars((request_rec*)r->real_request_rec);
                    333: }
                    334: void pa_ap_add_common_vars(pa_request_rec *r) {
                    335:        ap_add_common_vars((request_rec*)r->real_request_rec);
                    336: }
                    337: 
                    338: // signal.h
                    339: 
                    340: void (*pa_signal (int sig, void (*disp)(int)))(int) {
1.15    ! moko      341: #ifndef _MSC_VER
1.1       moko      342:        if(sig==PA_SIGPIPE && disp==PA_SIG_IGN)
                    343:                return signal(SIGPIPE, SIG_IGN);
1.15    ! moko      344: #endif
1.1       moko      345:        return 0;
                    346: }

E-mail: