Annotation of parser3/src/targets/apache/mod_parser3_core.C, revision 1.25

1.1       moko        1: /** @file
1.8       moko        2: Parser: apache 1.3/2.X module, part, compiled by parser3project.
1.1       moko        3: 
1.16      moko        4:        Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       moko        5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
1.25    ! moko        8: volatile const char * IDENT_MOD_PARSER3_CORE_C="$Id: mod_parser3_core.C,v 1.24 2020/10/12 20:57:08 moko Exp $";
1.1       moko        9: 
                     10: #include "pa_config_includes.h"
                     11: 
                     12: #include "pa_globals.h"
                     13: 
                     14: #include "pa_httpd.h"
                     15: 
                     16: #include "pa_common.h"
                     17: #include "pa_sapi.h"
                     18: #include "classes.h"
                     19: #include "pa_request.h"
                     20: 
1.17      moko       21: #if defined(_MSC_VER) && !defined(_DEBUG)
1.9       moko       22: #      include <windows.h>
1.1       moko       23: #      define PA_SUPPRESS_SYSTEM_EXCEPTION
                     24: #endif
                     25: 
                     26: // generals
                     27: 
1.7       moko       28: static bool globals_inited=false;
                     29: 
1.1       moko       30: void pa_setup_module_cells() {
                     31:        if(globals_inited)
                     32:                return;
                     33:        globals_inited=true;
                     34:        
                     35:        /// no trying to __try here [yet]
                     36:        try {
1.18      moko       37:                // init libraries
1.1       moko       38:                pa_globals_init();
                     39:        } catch(const Exception& e) { // global problem 
1.21      moko       40:                SAPI::die("setup_module_cells failed: %s", e.comment());
1.1       moko       41:        }
                     42: }
                     43: 
                     44: void pa_destroy_module_cells() {
1.7       moko       45:        if(!globals_inited)
                     46:                return;
                     47: 
1.1       moko       48:        pa_globals_done();
                     49: }
                     50: 
                     51: 
                     52: //@{
                     53: /// SAPI func decl
                     54: 
                     55: class SAPI_Info {
                     56: public:
                     57:        pa_request_rec* r;
                     58: };
                     59: 
                     60: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
                     61:        va_list args;
                     62:        va_start(args,fmt);
                     63:        char buf[MAX_LOG_STRING];
                     64:        size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
                     65:        size=remove_crlf(buf, buf+size);
                     66:        pa_ap_log_rerror(0, 0, PA_APLOG_ERR | PA_APLOG_NOERRNO, SAPI_info.r, "%s", buf);
                     67:        va_end(args);
                     68: }
                     69: 
1.21      moko       70: void SAPI::die(const char* fmt, ...) {
                     71:        va_list args;
                     72:        va_start(args, fmt);
1.1       moko       73:        char buf[MAX_LOG_STRING];
                     74:        size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
                     75:        size=remove_crlf(buf, buf+size);
                     76:        pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_EMERG, 0, "%s", buf);
1.21      moko       77:        exit(1);
                     78: //     va_end(args);
1.1       moko       79: }
                     80: 
1.11      moko       81: char* SAPI::Env::get(SAPI_Info& SAPI_info, const char* name) {
1.1       moko       82:        const char* dont_return_me=pa_ap_table_get(SAPI_info.r->subprocess_env, name);
                     83:        return dont_return_me?pa_strdup(dont_return_me):0;
                     84: }
                     85: 
                     86: #ifndef DOXYGEN
                     87: struct SAPI_environment_append_info {
                     88:        const char** cur;
                     89: };
                     90: #endif
1.22      moko       91: 
1.1       moko       92: static const char* mk_env_pair(const char* key, const char* value) {
                     93:        char *result=new(PointerFreeGC) char[strlen(key)+1/*=*/+strlen(value)+1/*0*/];
                     94:        strcpy(result, key); strcat(result, "="); strcat(result, value);
                     95:        return result;
                     96: }
1.22      moko       97: 
1.1       moko       98: static int SAPI_environment_append(void *d, const char* k, const char* val) {
                     99:        if( k && val ) {
                    100:                SAPI_environment_append_info& info=
                    101:                        *static_cast<SAPI_environment_append_info *>(d);
                    102:                *info.cur++=mk_env_pair(k, val);
                    103:        }
                    104:        return 1/*true*/;
                    105: }
1.22      moko      106: 
1.11      moko      107: const char* const* SAPI::Env::get(SAPI_Info& SAPI_info) {
1.1       moko      108:        const pa_table *t=SAPI_info.r->subprocess_env;
1.12      moko      109:        const char** result=new const char*[pa_ap_table_size(t)+1/*0*/];
1.1       moko      110:        SAPI_environment_append_info info={result};
                    111:        pa_ap_table_do(SAPI_environment_append, &info, t, 0); *info.cur=0; // mark EOE
                    112:        return result;
                    113: }
                    114: 
                    115: size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) {
1.21      moko      116: //     pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server, "mod_parser3: SAPI::read_post(max=%u)", max_bytes);
                    117: 
1.1       moko      118:        int retval;
                    119:        if((retval = pa_ap_setup_client_block(SAPI_info.r, PA_REQUEST_CHUNKED_ERROR)))
                    120:                return 0;
                    121:        if(!pa_ap_should_client_block(SAPI_info.r))
                    122:                return 0;
                    123:        
                    124:        uint total_read_bytes=0;
                    125:        void (*handler)(int)=pa_signal(PA_SIGPIPE, PA_SIG_IGN);
                    126:        while (total_read_bytes<max_bytes) {
                    127:                pa_ap_hard_timeout("Read POST information", SAPI_info.r); /* start timeout timer */
                    128:                uint read_bytes=
                    129:                        pa_ap_get_client_block(SAPI_info.r, buf+total_read_bytes, max_bytes-total_read_bytes);
                    130:                pa_ap_reset_timeout(SAPI_info.r);
                    131:                if (read_bytes<=0)
                    132:                        break;
                    133:                total_read_bytes+=read_bytes;
                    134:        }
                    135:        pa_signal(PA_SIGPIPE, handler);
                    136:        return total_read_bytes;
                    137: }
                    138: 
                    139: /// @test location provide with protocol. think about internal redirects
1.22      moko      140: void SAPI::add_header_attribute(SAPI_Info& SAPI_info, const char* dont_store_key, const char* dont_store_value) {
1.1       moko      141:        if(strcasecmp(dont_store_key, "location")==0) 
                    142:                *SAPI_info.r->status=302;
                    143:        
                    144:        if(strcasecmp(dont_store_key, HTTP_CONTENT_TYPE)==0) {
                    145:        /* r->content_type, *not* r->headers_out("Content-type").  If you don't
                    146:        * set it, it will be filled in with the server's default type (typically
                    147:        * "text/plain").  You *must* also ensure that r->content_type is lower
                    148:        * case.
                    149:                */
                    150:                *SAPI_info.r->content_type = pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value);
                    151:        } else if(strcasecmp(dont_store_key, HTTP_STATUS)==0) 
1.25    ! moko      152:                *SAPI_info.r->status=atoi(dont_store_value);
1.1       moko      153:        else
                    154:                pa_ap_table_addn(SAPI_info.r->headers_out, 
                    155:                pa_ap_pstrdup(SAPI_info.r->pool, capitalize(dont_store_key)), 
                    156:                pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value));
                    157: }
                    158: 
                    159: void SAPI::send_header(SAPI_Info& SAPI_info) {
                    160:        pa_ap_hard_timeout("Send header", SAPI_info.r);
                    161:        pa_ap_send_http_header(SAPI_info.r);
                    162:        pa_ap_kill_timeout(SAPI_info.r);
                    163: }
                    164: 
                    165: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
                    166:        pa_ap_hard_timeout("Send body", SAPI_info.r);
                    167:        size = (size_t)pa_ap_rwrite(buf, size, SAPI_info.r);
                    168:        pa_ap_kill_timeout(SAPI_info.r);
                    169:        return size;
                    170: }
                    171: 
                    172: //@}
                    173: 
                    174: /**
                    175: main workhorse
                    176:        @todo intelligent cache-control
                    177: */
                    178: static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
                    179:        // collect garbage from prev request
1.8       moko      180: #ifndef PA_DEBUG_DISABLE_GC
                    181:        GC_dont_gc=0;
                    182:        GC_gcollect();
                    183:        GC_dont_gc=1;
1.10      moko      184: #endif
1.1       moko      185: 
                    186:        // populate env
                    187:        pa_ap_add_common_vars(SAPI_info.r);
                    188:        pa_ap_add_cgi_vars(SAPI_info.r);
                    189:        
                    190:        // Request info
                    191:        Request_info request_info;  memset(&request_info, 0, sizeof(request_info));
                    192:        
1.11      moko      193:        request_info.document_root=SAPI::Env::get(SAPI_info, "DOCUMENT_ROOT");
1.1       moko      194:        request_info.path_translated=SAPI_info.r->filename;
                    195:        request_info.method=SAPI_info.r->method;
                    196:        request_info.query_string=SAPI_info.r->args;
1.15      moko      197:        request_info.uri=request_info.strip_absolute_uri(SAPI::Env::get(SAPI_info, "REQUEST_URI"));
1.11      moko      198:        request_info.content_type=SAPI::Env::get(SAPI_info, "CONTENT_TYPE");
1.23      moko      199:        request_info.content_length=pa_atoui(SAPI::Env::get(SAPI_info, "CONTENT_LENGTH"), 10);
1.11      moko      200:        request_info.cookie=SAPI::Env::get(SAPI_info, "HTTP_COOKIE");
1.1       moko      201:        request_info.mail_received=false;
                    202:        
                    203:        // prepare to process request
                    204:        Request request(
                    205:                SAPI_info,
                    206:                request_info,
1.3       moko      207:                String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)
1.1       moko      208:                );
                    209:        
                    210:        // process the request
                    211:        request.core(
                    212:                dcfg->parser_config_filespec, true, // /path/to/config
                    213:                SAPI_info.r->header_only!=0);
                    214: }
                    215: 
                    216: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.20      moko      217: static const Exception call_real_parser_handler__do_PEH_return_it(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.1       moko      218:        try {
                    219:                real_parser_handler(SAPI_info, dcfg);
                    220:        } catch(const Exception& e) {
                    221:                return e;
                    222:        }
                    223: 
                    224:        return Exception();
                    225: }
1.20      moko      226: 
                    227: static void call_real_parser_handler__supress_system_exception(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.1       moko      228:        Exception parser_exception;
                    229:        LPEXCEPTION_POINTERS system_exception=0;
                    230: 
                    231:        __try {
1.20      moko      232:                parser_exception=call_real_parser_handler__do_PEH_return_it(SAPI_info, dcfg);
                    233:        } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.1       moko      234:                if(system_exception)
                    235:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.20      moko      236:                                throw Exception("system", 0, "0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
1.1       moko      237:                        else
1.20      moko      238:                                throw Exception("system", 0, "<no exception record>");
1.1       moko      239:                else
1.20      moko      240:                        throw Exception("system", 0, "<no exception information>");
1.1       moko      241:        }
                    242: 
                    243:        if(parser_exception)
                    244:                throw Exception(parser_exception);
                    245: }
                    246: #endif
                    247: 
                    248: /// @test r->finfo.st_mode check seems to work only on win32
                    249: int pa_parser_handler(pa_request_rec *r, Parser_module_config *dcfg) {
                    250:        // SAPI info
                    251:        SAPI_Info SAPI_info; SAPI_info.r=r;
                    252:        
1.24      moko      253:        if(r->file_not_found)
1.1       moko      254:                return PA_HTTP_NOT_FOUND;
                    255:        
                    256:        try { // global try
                    257: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
                    258:                call_real_parser_handler__supress_system_exception(
                    259: #else
                    260:                real_parser_handler(
                    261: #endif
                    262:                        SAPI_info, dcfg);
                    263: 
                    264:                // successful finish
1.24      moko      265:        } catch(const Exception& e) { // exception in unhandled exception
1.1       moko      266:                // log it
1.24      moko      267:                SAPI::log(SAPI_info, "%s", e.comment());
1.1       moko      268:                
                    269:                // prepare header
                    270:                // capitalized headers are used for preventing malloc during capitalization
                    271:                SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain");
                    272:                
                    273:                // send header
                    274:                SAPI::send_header(SAPI_info);
                    275:                
                    276:                // send body
                    277:                if(!r->header_only)
1.24      moko      278:                        SAPI::send_body(SAPI_info, e.comment(), strlen(e.comment()));
1.1       moko      279:                
                    280:                // unsuccessful finish
                    281:        }
                    282:        
                    283:        /*
                    284:        * We did what we wanted to do, so tell the rest of the server we
                    285:        * succeeded.
                    286:        */
                    287:        return PA_OK;
                    288: }

E-mail: