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

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

E-mail: