--- parser3/src/targets/apache13/Attic/mod_parser3.C 2001/10/12 12:15:32 1.2 +++ parser3/src/targets/apache13/Attic/mod_parser3.C 2002/02/08 07:27:50 1.16 @@ -1,10 +1,10 @@ /** @file Parser: apache 1.3 module. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://paf.design.ru) - $Id: mod_parser3.C,v 1.2 2001/10/12 12:15:32 parser Exp $ + $Id: mod_parser3.C,v 1.16 2002/02/08 07:27:50 paf Exp $ */ #include "httpd.h" @@ -15,6 +15,12 @@ #include "http_protocol.h" #include "util_script.h" +#include "pa_config_includes.h" + +#if _MSC_VER +# include +#endif + #include "pa_sapi.h" #include "classes.h" #include "pa_common.h" @@ -27,14 +33,6 @@ #include #endif -#ifdef _DEBUG -# define DEBUG_PREFIX "debug_" -# define PARSER3_MODULE debug_parser3_module -#else -# define DEBUG_PREFIX -# define PARSER3_MODULE parser3_module -#endif - // consts extern const char *main_RCSIds[]; @@ -68,6 +66,7 @@ const char **RCSIds[]={ struct Parser_module_config { const char* parser_root_config_filespec; ///< filespec of admin's config file const char* parser_site_config_filespec; ///< filespec of site's config file + bool parser_status_allowed; }; #ifdef XML @@ -92,14 +91,14 @@ void callXalanTerminate(void *) { * Declare ourselves so the configuration routines can find and know us. * We'll fill it in at the end of the module. */ -extern "C" module MODULE_VAR_EXPORT PARSER3_MODULE; +extern "C" module MODULE_VAR_EXPORT parser3_module; /* * Locate our directory configuration record for the current request. */ static Parser_module_config *our_dconfig(request_rec *r) { return (Parser_module_config *) - ap_get_module_config(r->per_dir_config, &PARSER3_MODULE); + ap_get_module_config(r->per_dir_config, &parser3_module); } static const char *cmd_parser_config(cmd_parms *cmd, void *mconfig, char *file_spec) { @@ -110,6 +109,14 @@ static const char *cmd_parser_config(cmd return NULL; } +static const char *cmd_parser_status_allowed(cmd_parms *cmd, void *mconfig, char *file_spec) { + //_asm int 3; + Parser_module_config *cfg = (Parser_module_config *) mconfig; + + cfg->parser_status_allowed=true; + + return NULL; +} /*--------------------------------------------------------------------------*/ @@ -141,7 +148,7 @@ static const char *cmd_parser_config(cmd //@{ /// SAPI func decl void SAPI::log(Pool& pool, const char *fmt, ...) { - request_rec *r=static_cast(pool.context()); + request_rec *r=static_cast(pool.get_context()); va_list args; va_start(args,fmt); @@ -152,13 +159,25 @@ void SAPI::log(Pool& pool, const char *f va_end(args); } +void SAPI::die(const char *fmt, ...) { + va_list args; + va_start(args,fmt); + char buf[MAX_STRING]; + size_t size=vsnprintf(buf, MAX_STRING, fmt, args); + remove_crlf(buf, buf+size); + ap_log_error(APLOG_MARK, APLOG_EMERG, 0, "%s", buf); + va_end(args); + + exit(1); +} + const char *SAPI::get_env(Pool& pool, const char *name) { - request_rec *r=static_cast(pool.context()); + request_rec *r=static_cast(pool.get_context()); return (const char *)ap_table_get(r->subprocess_env, name); } size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) { - request_rec *r=static_cast(pool.context()); + request_rec *r=static_cast(pool.get_context()); /* ap_log_error(APLOG_MARK, APLOG_DEBUG, r->server, "mod_parser3: SAPI::read_post(max=%u)", max_bytes); @@ -184,8 +203,9 @@ size_t SAPI::read_post(Pool& pool, char return total_read_bytes; } +/// @test location provide with protocol. think about internal redirects void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) { - request_rec *r=static_cast(pool.context()); + request_rec *r=static_cast(pool.get_context()); if(strcasecmp(key, "location")==0) r->status=302; @@ -204,7 +224,7 @@ void SAPI::add_header_attribute(Pool& po } void SAPI::send_header(Pool& pool) { - request_rec *r=static_cast(pool.context()); + request_rec *r=static_cast(pool.get_context()); ap_hard_timeout("Send header", r); ap_send_http_header(r); @@ -212,7 +232,7 @@ void SAPI::send_header(Pool& pool) { } void SAPI::send_body(Pool& pool, const void *buf, size_t size) { - request_rec *r=static_cast(pool.context()); + request_rec *r=static_cast(pool.get_context()); ap_hard_timeout("Send body", r); ap_rwrite(buf, size, r); @@ -226,8 +246,69 @@ void SAPI::send_body(Pool& pool, const v @todo intelligent cache-control */ +static void real_parser_handler(Pool& pool, request_rec *r) { + ap_add_common_vars(r); + ap_add_cgi_vars(r); + + // Request info + Request::Info request_info; + request_info.document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"); + request_info.path_translated=r->filename; + request_info.method=r->method; + request_info.query_string=r->args; + request_info.uri=SAPI::get_env(pool, "REQUEST_URI"); + request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE"); + const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH"); + request_info.content_length=content_length?atoi(content_length):0; + request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE"); + request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT"); + + // config + Parser_module_config *dcfg=our_dconfig(r); + + //_asm int 3; + // prepare to process request + Request request(pool, + request_info, + String::UL_HTML|String::UL_OPTIMIZE_BIT, + dcfg->parser_status_allowed + ); + + // process the request + request.core( + dcfg->parser_root_config_filespec, true, // /path/to/admin/config + dcfg->parser_site_config_filespec, true, // /path/to/site/config + r->header_only!=0); +} + +void call_real_parser_handler__do_SEH(Pool& pool, request_rec *r) { +#if _MSC_VER & !defined(_DEBUG) + LPEXCEPTION_POINTERS system_exception=0; + __try { +#endif + real_parser_handler(pool, r); + +#if _MSC_VER & !defined(_DEBUG) + } __except ( + (system_exception=GetExceptionInformation()), + EXCEPTION_EXECUTE_HANDLER) { + + if(system_exception) + if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord) + throw Exception(0, 0, + 0, + "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress); + else + throw Exception(0, 0, 0, "Exception "); + else + throw Exception(0, 0, 0, "Exception "); + } +#endif +} + +/// @test r->finfo.st_mode check seems to work only on win32 static int parser_handler(request_rec *r) { -// _asm int 3; + //_asm int 3; if(r->finfo.st_mode == 0) return NOT_FOUND; @@ -246,49 +327,15 @@ static int parser_handler(request_rec *r pool.register_cleanup(callXalanTerminate, 0); #endif - Parser_module_config *dcfg=our_dconfig(r); - - /* A flag which modules can set, to indicate that the data being * returned is volatile, and clients should be told not to cache it. */ - r->no_cache=1; - - PTRY { // global try - ap_add_common_vars(r); - ap_add_cgi_vars(r); - - // Request info - Request::Info request_info; - request_info.document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"); - request_info.path_translated=r->filename; - request_info.method=r->method; - request_info.query_string=r->args; - request_info.uri=SAPI::get_env(pool, "REQUEST_URI"); - request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE"); - const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH"); - request_info.content_length=content_length?atoi(content_length):0; - request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE"); - request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT"); - - //_asm int 3; - // prepare to process request - Request request(pool, - request_info, - String::UL_USER_HTML - ); - - // process the request - request.core( - dcfg->parser_root_config_filespec, true, // /path/to/admin/config - dcfg->parser_site_config_filespec, true, // /path/to/site/config - r->header_only!=0); - // no actions with request' data past this point - // request.exception not not handled here, but all - // request' data are associated with it's pool=exception +// r->no_cache=1; + try { // global try + call_real_parser_handler__do_SEH(pool, r); // successful finish - } PCATCH(e) { // global problem + } catch(const Exception& e) { // global problem // don't allocate anything on pool here: // possible pool' exception not catch-ed now // and there could be out-of-memory exception @@ -314,7 +361,6 @@ static int parser_handler(request_rec *r // unsuccessful finish } - PEND_CATCH /* * We did what we wanted to do, so tell the rest of the server we @@ -323,6 +369,19 @@ static int parser_handler(request_rec *r return OK; } +#if _MSC_VER +int failed_new(size_t size) { + SAPI::die("out of memory in 'new', failed to allocated %u bytes", size); + return 0; // not reached +} +#endif + +#ifdef HAVE_SET_NEW_HANDLER +void failed_new() { + SAPI::die("out of memory in 'new'"); +} +#endif + /*--------------------------------------------------------------------------*/ /* */ /* Now let's declare routines for each of the callback phase in order. */ @@ -366,11 +425,20 @@ static void setup_module_cells() { return; globals_inited=true; +#ifdef WIN32 + _set_new_handler(failed_new); +#endif + +#ifdef HAVE_SET_NEW_HANDLER + std::set_new_handler(failed_new); +#endif + /* * allocate our module-private pool. */ static Pool pool(ap_make_sub_pool(NULL)); // global pool - PTRY { + /// no trying to __try here [yet] + try { // init socks init_socks(pool); @@ -389,12 +457,9 @@ static void setup_module_cells() { init_methoded_array(pool); // init global variables pa_globals_init(pool); - } PCATCH(e) { // global problem - ap_log_error(APLOG_MARK, APLOG_EMERG, 0, - "setup_module_cells failed: ", e.comment()); - exit(1); + } catch(const Exception& e) { // global problem + SAPI::die("setup_module_cells failed: %s", e.comment()); } - PEND_CATCH } static void parser_server_init(server_rec *s, pool *p) { @@ -421,6 +486,7 @@ static void parser_server_init(server_re * structure. */ static void *parser_create_dir_config(pool *p, char *dirspec) { + //_asm int 3; /* * Allocate the space for our record from the pool supplied. */ @@ -430,8 +496,7 @@ static void *parser_create_dir_config(po * Now fill in the defaults. If there are any `parent' configuration * records, they'll get merged as part of a separate callback. */ - cfg->parser_root_config_filespec = 0; - cfg->parser_site_config_filespec = 0; + return (void *) cfg; } @@ -449,22 +514,31 @@ static void *parser_create_dir_config(po * * The return value is a pointer to the created module-specific structure * containing the merged values. + + 20011126 paf: noticed, that this is called even on virtual root merge with something "parent", + while thought that that is part of merge_server... + */ static void *parser_merge_dir_config(pool *p, void *parent_conf, void *newloc_conf) { + //_asm int 3; Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config)); Parser_module_config *pconf = (Parser_module_config *) parent_conf; Parser_module_config *nconf = (Parser_module_config *) newloc_conf; - // always from parent - merged_config->parser_root_config_filespec = ap_pstrdup(p, pconf->parser_root_config_filespec); - /* + merged_config->parser_root_config_filespec = ap_pstrdup(p, nconf->parser_root_config_filespec? + nconf->parser_root_config_filespec:pconf->parser_root_config_filespec); + merged_config->parser_site_config_filespec = ap_pstrdup(p, nconf->parser_site_config_filespec? + nconf->parser_site_config_filespec:pconf->parser_site_config_filespec); + merged_config->parser_status_allowed= + pconf->parser_status_allowed || + nconf->parser_status_allowed; + + /* * Some things get copied directly from the more-specific record, rather * than getting merged. */ - merged_config->parser_site_config_filespec = ap_pstrdup(p, nconf->parser_site_config_filespec? - nconf->parser_site_config_filespec:pconf->parser_site_config_filespec); return (void *) merged_config; } @@ -477,16 +551,14 @@ static void *parser_merge_dir_config(poo * structure. */ static void *parser_create_server_config(pool *p, server_rec *s) { + //_asm int 3; /* - * As with the parser_create_dir_config() reoutine, we allocate and fill + * As with the parser_create_dir_config() routine, we allocate and fill * in an empty record. */ Parser_module_config *cfg= (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config)); - cfg->parser_root_config_filespec = 0; - cfg->parser_site_config_filespec = 0; - return (void *) cfg; } @@ -506,6 +578,7 @@ static void *parser_create_server_config static void *parser_merge_server_config(pool *p, void *server1_conf, void *server2_conf) { + //_asm int 3; Parser_module_config *merged_config = (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config)); @@ -520,6 +593,9 @@ static void *parser_merge_server_config( s2conf->parser_root_config_filespec:s1conf->parser_root_config_filespec); merged_config->parser_site_config_filespec = ap_pstrdup(p, s2conf->parser_site_config_filespec? s2conf->parser_site_config_filespec:s1conf->parser_site_config_filespec); + merged_config->parser_status_allowed= + s1conf->parser_status_allowed || + s2conf->parser_status_allowed; return (void *) merged_config; } @@ -596,7 +672,7 @@ static int parser_access_checker(request static const command_rec parser_cmds[] = { { - DEBUG_PREFIX"ParserRootConfig", /* directive name */ + "ParserRootConfig", /* directive name */ (const char *(*)(void))((void *)cmd_parser_config), // config action routine (void*)true, /* argument to include in call */ (int)(ACCESS_CONF|RSRC_CONF), /* where available */ @@ -604,13 +680,21 @@ static const command_rec parser_cmds[] = "Parser root config filespec (Admin)" // directive description }, { - DEBUG_PREFIX"ParserSiteConfig", /* directive name */ + "ParserSiteConfig", /* directive name */ (const char *(*)(void))((void *)cmd_parser_config), // config action routine (void*)false, /* argument to include in call */ (int)(OR_OPTIONS), /* where available */ TAKE1, /* arguments */ "Parser site config filespec" // directive description }, + { + "ParserStatusAllowed", /* directive name */ + (const char *(*)(void))((void *)cmd_parser_status_allowed), // config action routine + (void*)0, /* argument to include in call */ + (int)(ACCESS_CONF), /* where available */ + NO_ARGS, /* arguments */ + "Parser status class can be used" // directive description + }, {NULL} }; @@ -633,7 +717,7 @@ static const command_rec parser_cmds[] = */ static const handler_rec parser_handlers[] = { - {DEBUG_PREFIX"parser3-handler", parser_handler}, + {"parser3-handler", parser_handler}, {NULL} }; @@ -651,7 +735,7 @@ static const handler_rec parser_handlers * during request processing. Note that not all routines are necessarily * called (such as if a resource doesn't have access restrictions). */ -module MODULE_VAR_EXPORT PARSER3_MODULE = +module MODULE_VAR_EXPORT parser3_module = { STANDARD_MODULE_STUFF, parser_server_init, /* module initializer */