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

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

E-mail: