--- parser3/src/targets/apache/mod_parser3_core.C 2020/08/13 10:52:34 1.20 +++ parser3/src/targets/apache/mod_parser3_core.C 2024/12/06 00:40:12 1.42 @@ -1,11 +1,11 @@ /** @file Parser: apache 1.3/2.X module, part, compiled by parser3project. - Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_MOD_PARSER3_CORE_C="$Id: mod_parser3_core.C,v 1.20 2020/08/13 10:52:34 moko Exp $"; +volatile const char * IDENT_MOD_PARSER3_CORE_C="$Id: mod_parser3_core.C,v 1.42 2024/12/06 00:40:12 moko Exp $"; #include "pa_config_includes.h" @@ -26,6 +26,7 @@ volatile const char * IDENT_MOD_PARSER3_ // generals static bool globals_inited=false; +const char* parser3_mode="apache"; // $status:mode void pa_setup_module_cells() { if(globals_inited) @@ -37,7 +38,7 @@ void pa_setup_module_cells() { // init libraries pa_globals_init(); } catch(const Exception& e) { // global problem - SAPI::abort("setup_module_cells failed: %s", e.comment()); + SAPI::die("setup_module_cells failed: %s", e.comment()); } } @@ -67,31 +68,23 @@ void SAPI::log(SAPI_Info& SAPI_info, con va_end(args); } -static void die_or_abort(const char* fmt, va_list args, bool write_core) { +void SAPI::die(const char* fmt, ...) { + va_list args; + va_start(args, fmt); char buf[MAX_LOG_STRING]; size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args); size=remove_crlf(buf, buf+size); pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_EMERG, 0, "%s", buf); - - // exit & try to produce core dump - if(write_core) - abort(); - else - exit(1); -} - -void SAPI::die(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - die_or_abort(fmt, args, false/*write core?*/); - va_end(args); + exit(1); +// va_end(args); } -void SAPI::abort(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - die_or_abort(fmt, args, true/*write core?*/); - va_end(args); +void SAPI::send_error(SAPI_Info& SAPI_info, const char *exception_cstr, const char *status){ + // capitalized headers passed for preventing malloc during capitalization + add_header_attribute(SAPI_info, HTTP_STATUS_CAPITALIZED, status); + add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain"); + send_headers(SAPI_info); + send_body(SAPI_info, exception_cstr, strlen(exception_cstr)); } char* SAPI::Env::get(SAPI_Info& SAPI_info, const char* name) { @@ -99,36 +92,35 @@ char* SAPI::Env::get(SAPI_Info& SAPI_inf return dont_return_me?pa_strdup(dont_return_me):0; } +bool SAPI::Env::set(SAPI_Info&, const char*, const char*) { + return false; +} + #ifndef DOXYGEN struct SAPI_environment_append_info { const char** cur; }; #endif -static const char* mk_env_pair(const char* key, const char* value) { - char *result=new(PointerFreeGC) char[strlen(key)+1/*=*/+strlen(value)+1/*0*/]; - strcpy(result, key); strcat(result, "="); strcat(result, value); - return result; -} + static int SAPI_environment_append(void *d, const char* k, const char* val) { - if( k && val ) { - SAPI_environment_append_info& info= - *static_cast(d); - *info.cur++=mk_env_pair(k, val); + if(k && val) { + SAPI_environment_append_info& info=*static_cast(d); + *info.cur++=pa_strcat(k, "=", val); } return 1/*true*/; } + const char* const* SAPI::Env::get(SAPI_Info& SAPI_info) { const pa_table *t=SAPI_info.r->subprocess_env; - const char** result=new const char*[pa_ap_table_size(t)+1/*0*/]; + const char** result=new(PointerGC) const char*[pa_ap_table_size(t)+1/*0*/]; SAPI_environment_append_info info={result}; pa_ap_table_do(SAPI_environment_append, &info, t, 0); *info.cur=0; // mark EOE return result; } size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) { -/* pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server, -"mod_parser3: SAPI::read_post(max=%u)", max_bytes); - */ +// pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server, "mod_parser3: SAPI::read_post(max=%u)", max_bytes); + int retval; if((retval = pa_ap_setup_client_block(SAPI_info.r, PA_REQUEST_CHUNKED_ERROR))) return 0; @@ -151,8 +143,7 @@ size_t SAPI::read_post(SAPI_Info& SAPI_i } /// @test location provide with protocol. think about internal redirects -void SAPI::add_header_attribute(SAPI_Info& SAPI_info, - const char* dont_store_key, const char* dont_store_value) { +void SAPI::add_header_attribute(SAPI_Info& SAPI_info, const char* dont_store_key, const char* dont_store_value) { if(strcasecmp(dont_store_key, "location")==0) *SAPI_info.r->status=302; @@ -171,12 +162,15 @@ void SAPI::add_header_attribute(SAPI_Inf pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value)); } -void SAPI::send_header(SAPI_Info& SAPI_info) { +void SAPI::send_headers(SAPI_Info& SAPI_info) { pa_ap_hard_timeout("Send header", SAPI_info.r); pa_ap_send_http_header(SAPI_info.r); pa_ap_kill_timeout(SAPI_info.r); } +void SAPI::clear_headers(SAPI_Info&) { +} + size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) { pa_ap_hard_timeout("Send body", SAPI_info.r); size = (size_t)pa_ap_rwrite(buf, size, SAPI_info.r); @@ -186,24 +180,13 @@ size_t SAPI::send_body(SAPI_Info& SAPI_i //@} -#ifndef PA_DEBUG_DISABLE_GC -#ifndef _MSC_VER -extern long GC_large_alloc_warn_suppressed; -#endif -#endif - /** main workhorse - @todo intelligent cache-control */ static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) { // collect garbage from prev request -#ifndef PA_DEBUG_DISABLE_GC - GC_dont_gc=0; - GC_gcollect(); - GC_dont_gc=1; -#endif + pa_gc_collect(); // populate env pa_ap_add_common_vars(SAPI_info.r); @@ -218,22 +201,21 @@ static void real_parser_handler(SAPI_Inf request_info.query_string=SAPI_info.r->args; request_info.uri=request_info.strip_absolute_uri(SAPI::Env::get(SAPI_info, "REQUEST_URI")); request_info.content_type=SAPI::Env::get(SAPI_info, "CONTENT_TYPE"); - const char* content_length=SAPI::Env::get(SAPI_info, "CONTENT_LENGTH"); - request_info.content_length=content_length?atoi(content_length):0; + request_info.content_length=pa_atoul(SAPI::Env::get(SAPI_info, "CONTENT_LENGTH")); request_info.cookie=SAPI::Env::get(SAPI_info, "HTTP_COOKIE"); request_info.mail_received=false; // prepare to process request - Request request( - SAPI_info, - request_info, - String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) - ); + Request request(SAPI_info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); // process the request - request.core( - dcfg->parser_config_filespec, true, // /path/to/config - SAPI_info.r->header_only!=0); + const char *config=dcfg->parser_config_filespec; +#ifdef SYSTEM_CONFIG_FILE + if(!config && entry_exists(SYSTEM_CONFIG_FILE)){ + config=SYSTEM_CONFIG_FILE; + } +#endif + request.core(config, SAPI_info.r->header_only!=0); } #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION @@ -273,7 +255,7 @@ int pa_parser_handler(pa_request_rec *r, // SAPI info SAPI_Info SAPI_info; SAPI_info.r=r; - if(r->file_not_found ) + if(r->file_not_found) return PA_HTTP_NOT_FOUND; try { // global try @@ -285,35 +267,10 @@ int pa_parser_handler(pa_request_rec *r, SAPI_info, dcfg); // successful finish - } 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 - char buf[MAX_STRING]; - snprintf(buf, MAX_STRING, "Unhandled exception %s", - e.comment()); + } catch(const Exception& e) { // just in case // log it - SAPI::log(SAPI_info, "%s", buf); - - // - int content_length=strlen(buf); - - // prepare header - // capitalized headers are used for preventing malloc during capitalization - SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain"); - // don't use 'format' function because it calls malloc - char content_length_cstr[MAX_NUMBER]; - snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length); - SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_LENGTH_CAPITALIZED, content_length_cstr); - - // send header - SAPI::send_header(SAPI_info); - - // send body - if(!r->header_only) - SAPI::send_body(SAPI_info, buf, content_length); - - // unsuccessful finish + SAPI::log(SAPI_info, "%s", e.comment()); + SAPI::send_error(SAPI_info, e.comment()); } /*