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

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.6     ! moko        8: static const char * const IDENT_MOD_PARSER3_C="$Date: 2010-11-16 00:43:19 $";
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) {
1.5       moko       67:        return (Parser_module_config *) ap_get_module_config(r->per_dir_config, &parser3_module);
1.1       moko       68: }
                     69: 
                     70: static const char* cmd_parser_config(cmd_parms *cmd, void *mconfig, char *file_spec) {
                     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;
                     84: #endif
                     85:        // converting to parser version
                     86:        pa_request_rec pr={
                     87:                r,
                     88:                r->pool,
                     89:                r->header_only,
                     90:                &r->status,
                     91:                r->method,
                     92:                r->headers_out,
                     93:                r->subprocess_env,
                     94:                &r->content_type,
                     95:                r->uri,
                     96:                r->filename,
                     97:                r->path_info,
                     98:                r->args,
                     99: #ifdef STANDARD20_MODULE_STUFF
                    100:                r->finfo.filetype == 0
                    101: #else          
                    102:                r->finfo.st_mode == 0
                    103: #endif
1.1       moko      104:        };
1.6     ! moko      105:        return pa_parser_handler(&pr, our_dconfig(r));
1.1       moko      106: }
                    107: 
                    108: /* 
1.4       moko      109: * This function is called during process initialisation.
1.1       moko      110: */
                    111: 
1.3       moko      112: #ifdef STANDARD20_MODULE_STUFF
                    113: static void parser_module_init(apr_pool_t *p, server_rec *s) {
                    114: #else
1.2       moko      115: static void parser_module_init(server_rec *s, apr_pool_t *p) {
                    116:        ap_add_version_component(p, pa_version());
1.1       moko      117: #endif 
1.3       moko      118:        ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, p, "parser inited %d", getpid());
1.1       moko      119:        pa_setup_module_cells();
                    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: {
                    152:        {
1.5       moko      153:                "ParserConfig",                         /* directive name */
1.1       moko      154:                        (const char* (*)(void))((void *)cmd_parser_config), // config action routine
1.5       moko      155:                        (void*)0,                       /* argument to include in call */
                    156:                        (int)(OR_OPTIONS),              /* where available */
                    157:                        TAKE1,                          /* arguments */
                    158:                        "Parser config filespec"        // directive description
1.1       moko      159:        },
                    160:        {NULL}
                    161: };
                    162: 
                    163: /*--------------------------------------------------------------------------*/
                    164: /* Now the list of content handlers available from this module.             */
                    165: /*--------------------------------------------------------------------------*/
1.2       moko      166: 
                    167: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      168: static const handler_rec parser_handlers[] =
                    169: {
1.3       moko      170:        {PARSER3_HANDLER, parser_handler},
1.1       moko      171:        {NULL}
                    172: };
1.2       moko      173: #endif
1.1       moko      174: 
                    175: /*--------------------------------------------------------------------------*/
                    176: /* Finally, the list of callback routines and data structures that          */
                    177: /* provide the hooks into our module from the other parts of the server.    */
                    178: /*--------------------------------------------------------------------------*/
1.2       moko      179: 
                    180: #ifdef STANDARD20_MODULE_STUFF
                    181: 
1.1       moko      182: /* 
1.2       moko      183: * register hooks.
1.1       moko      184: */
1.2       moko      185: static void parser_register_hooks(apr_pool_t* pool)
                    186: {
1.3       moko      187: //     ap_add_version_component(pool, pa_version());
1.2       moko      188: //     ap_hook_post_config(parser_server_init, NULL, NULL, APR_HOOK_MIDDLE);
                    189:        ap_hook_handler(parser_handler, NULL, NULL, APR_HOOK_MIDDLE);
1.3       moko      190:        ap_hook_child_init(parser_module_init, NULL, NULL, APR_HOOK_MIDDLE);
1.2       moko      191: };
                    192: 
                    193: module AP_MODULE_DECLARE_DATA parser3_module =
                    194: {
                    195:        STANDARD20_MODULE_STUFF,
                    196: #else
1.1       moko      197: module MODULE_VAR_EXPORT parser3_module =
                    198: {
                    199:        STANDARD_MODULE_STUFF,
1.5       moko      200:        parser_module_init,             /* module initializer */
1.2       moko      201: #endif
1.5       moko      202:        parser_create_dir_config,       /* per-directory config creator */
1.6     ! moko      203:        0,                              /* dir config merger */
1.5       moko      204:        parser_create_server_config,    /* server config creator */
1.6     ! moko      205:        0,                              /* server config merger */
1.5       moko      206:        parser_cmds,                    /* command apr_table_t */
1.2       moko      207: #ifdef STANDARD20_MODULE_STUFF
1.5       moko      208:        parser_register_hooks           /* register hooks */
1.2       moko      209: #else
1.5       moko      210:        parser_handlers,                /* [9] list of handlers */
                    211:        0,                              /* [2] filename-to-URI translation */
                    212:        0,                              /* [5] check/validate user_id */
                    213:        0,                              /* [6] check user_id is valid *here* */
                    214:        0,                              /* [4] check access by host address */
                    215:        0,                              /* [7] MIME type checker/setter */
                    216:        0,                              /* [8] fixups */
                    217:        0,                              /* [10] logger */
                    218:        0,                              /* [3] header parser */
                    219:        0,                              /* process initializer */
                    220:        parser_module_done              /* process exit/cleanup */
1.2       moko      221: #endif // STANDARD20_MODULE_STUFF
1.1       moko      222: };
                    223: 
                    224: #if defined(_MSC_VER)
                    225: #      define APACHE_WIN32_SRC "../../../../win32/apache13/src"
                    226: #      ifdef _DEBUG
                    227: #              pragma comment(lib, APACHE_WIN32_SRC "/CoreD/ApacheCore.lib")
                    228: #      else
                    229: #              pragma comment(lib, APACHE_WIN32_SRC "/CoreR/ApacheCore.lib")
                    230: #      endif
                    231: #endif
                    232: 
                    233: 
                    234: // interface to C++
                    235: 
1.4       moko      236: #define        PA_APLOG_EMERG          0       /* system is unusable */
                    237: #define        PA_APLOG_ALERT          1       /* action must be taken immediately */
                    238: #define        PA_APLOG_CRIT           2       /* critical conditions */
                    239: #define        PA_APLOG_ERR            3       /* error conditions */
1.1       moko      240: #define        PA_APLOG_WARNING        4       /* warning conditions */
1.4       moko      241: #define        PA_APLOG_NOTICE         5       /* normal but significant condition */
                    242: #define        PA_APLOG_INFO           6       /* informational */
                    243: #define        PA_APLOG_DEBUG          7       /* debug-level messages */
1.1       moko      244: 
                    245: #define        PA_APLOG_LEVELMASK      7       /* mask off the level value */
                    246: 
1.4       moko      247: #define PA_APLOG_NOERRNO       (PA_APLOG_LEVELMASK + 1)
1.1       moko      248: 
                    249: #define PA_APLOG_MARK  __FILE__,__LINE__
                    250: 
1.2       moko      251: void pa_ap_log_rerror(const char *file, int line, int level, const pa_request_rec *s, const char *fmt, ...) {
1.1       moko      252:        const char* str;
                    253:        va_list l;
                    254:        va_start(l, fmt); 
                    255:        str=va_arg(l, const char*);
                    256:        va_end(l); 
                    257: 
                    258:        ap_log_rerror(file, line, level,
1.2       moko      259: #ifdef STANDARD20_MODULE_STUFF
                    260:                                0,
                    261: #endif
1.1       moko      262:                      (request_rec*)s->real_request_rec, "%s", str);
                    263: }
                    264: 
                    265: 
1.2       moko      266: void pa_ap_log_error(const char *file, int line, int level, const pa_server_rec *s, const char *fmt, ...) {
1.1       moko      267:        const char* str;
                    268:        va_list l;
                    269:        va_start(l, fmt); 
                    270:        str=va_arg(l, const char*);
                    271:        va_end(l); 
                    272: 
                    273:        ap_log_error(file, line, level,
1.2       moko      274: #ifdef STANDARD20_MODULE_STUFF
                    275:                                0,
                    276: #endif
1.1       moko      277:                      (server_rec*)s, "%s", str);
                    278: }
                    279: 
                    280: // ap_alloc.h
                    281: 
                    282: const char* pa_ap_table_get(const pa_table *t, const char *name) {
1.2       moko      283:        return ap_table_get((const apr_table_t*)t, name);
1.1       moko      284: }
                    285: void pa_ap_table_addn(pa_table *t, const char *name, const char *val) {
1.2       moko      286:        ap_table_addn((apr_table_t*)t, name, val);
1.1       moko      287: }
                    288: 
                    289: int pa_ap_table_size(const pa_table *t) {
1.2       moko      290:        return ap_table_elts((const apr_table_t*)t)->nelts;
1.1       moko      291: }
                    292: 
1.2       moko      293: void pa_ap_table_do(int (*comp) (void *, const char *, const char *), void *rec, const pa_table *t, ...) {
                    294:        ap_table_do(comp, rec, (apr_table_t*)t, 0);
1.1       moko      295: }
                    296: 
                    297: char * pa_ap_pstrdup(pa_pool *p, const char *s) {
1.2       moko      298:        return ap_pstrdup((apr_pool_t*)p, s);
1.1       moko      299: }
                    300: 
                    301: // http_protocol.h
                    302: 
                    303: int pa_ap_setup_client_block(pa_request_rec *r, int read_policy) {
                    304:        return ap_setup_client_block((request_rec*)r->real_request_rec,
                    305:                read_policy);
                    306: }
                    307: int pa_ap_should_client_block(pa_request_rec *r) {
                    308:        return ap_should_client_block((request_rec*)r->real_request_rec);
                    309: }
                    310: long pa_ap_get_client_block(pa_request_rec *r, char *buffer, int bufsiz) {
                    311:        return ap_get_client_block((request_rec*)r->real_request_rec,
                    312:                buffer, bufsiz);
                    313: }
                    314: void pa_ap_send_http_header(pa_request_rec *r) {
1.2       moko      315: // Apache2 send headers before body automatically
                    316: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      317:        ap_send_http_header((request_rec*)r->real_request_rec);
1.2       moko      318: #endif
1.1       moko      319: }
                    320: int pa_ap_rwrite(const void *buf, int nbyte, pa_request_rec *r) {
                    321:        return ap_rwrite(buf, nbyte, (request_rec*)r->real_request_rec);
                    322: }
                    323: 
                    324: // http_main.h
                    325: 
                    326: void pa_ap_hard_timeout(char *s, pa_request_rec *r) {
1.2       moko      327: // Apache 2 uses non-blocking I/O
                    328: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      329:        ap_hard_timeout(s, (request_rec*)r->real_request_rec);
1.2       moko      330: #endif
1.1       moko      331: }
                    332: void pa_ap_reset_timeout(pa_request_rec *r) {
1.2       moko      333: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      334:        ap_reset_timeout((request_rec*)r->real_request_rec);
1.2       moko      335: #endif
1.1       moko      336: }
                    337: void pa_ap_kill_timeout(pa_request_rec *r) {
1.2       moko      338: #ifndef STANDARD20_MODULE_STUFF
1.1       moko      339:        ap_kill_timeout((request_rec*)r->real_request_rec);
1.2       moko      340: #endif
1.1       moko      341: }
                    342: 
                    343: // util_script.h
                    344: 
                    345: void pa_ap_add_cgi_vars(pa_request_rec *r) {
                    346:        ap_add_cgi_vars((request_rec*)r->real_request_rec);
                    347: }
                    348: void pa_ap_add_common_vars(pa_request_rec *r) {
                    349:        ap_add_common_vars((request_rec*)r->real_request_rec);
                    350: }
                    351: 
1.2       moko      352: #ifndef WIN32
1.1       moko      353: // signal.h
                    354: 
                    355: void (*pa_signal (int sig, void (*disp)(int)))(int) {
                    356:        if(sig==PA_SIGPIPE && disp==PA_SIG_IGN)
                    357:                return signal(SIGPIPE, SIG_IGN);
                    358: 
                    359:        return 0;
                    360: }
1.5       moko      361: #endif

E-mail: