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

1.1       moko        1: /** @file
                      2: Parser: apache 1.3 module, part, compiled by parser3project.
                      3: 
1.6     ! 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: 
1.6     ! moko        8: volatile const char * IDENT_MOD_PARSER3_CORE_C="$Id: 2010-11-23 00:13:47 $";
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: #include "pa_socks.h"
                     21: 
                     22: #if _MSC_VER && !defined(_DEBUG)
                     23: #      include <windows.h>
                     24: #      define PA_SUPPRESS_SYSTEM_EXCEPTION
                     25: #endif
                     26: 
                     27: // generals
                     28: 
                     29: void pa_setup_module_cells() {
1.4       moko       30:        static bool globals_inited=false;
1.1       moko       31:        if(globals_inited)
                     32:                return;
                     33:        globals_inited=true;
                     34:        
                     35:        /// no trying to __try here [yet]
                     36:        try {
                     37:                // init socks
                     38:                pa_socks_init();
                     39:                
                     40:                // init global variables
                     41:                pa_globals_init();
                     42:        } catch(const Exception& e) { // global problem 
                     43:                SAPI::abort("setup_module_cells failed: %s", e.comment());
                     44:        }
                     45: }
                     46: 
                     47: void pa_destroy_module_cells() {
                     48:        pa_globals_done();
                     49: 
                     50:        pa_socks_done();
                     51: }
                     52: 
                     53: 
                     54: //@{
                     55: /// SAPI func decl
                     56: 
                     57: class SAPI_Info {
                     58: public:
                     59:        pa_request_rec* r;
                     60: };
                     61: 
                     62: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
                     63:        va_list args;
                     64:        va_start(args,fmt);
                     65:        char buf[MAX_LOG_STRING];
                     66:        size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
                     67:        size=remove_crlf(buf, buf+size);
                     68:        pa_ap_log_rerror(0, 0, PA_APLOG_ERR | PA_APLOG_NOERRNO, SAPI_info.r, "%s", buf);
                     69:        va_end(args);
                     70: }
                     71: 
                     72: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
                     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);
                     77:        
                     78:        // exit & try to produce core dump
                     79:        if(write_core)
                     80:                abort();
                     81:        else
                     82:                exit(1);
                     83: }
                     84: 
                     85: void SAPI::die(const char* fmt, ...) {
                     86:        va_list args;
                     87:        va_start(args, fmt);
                     88:        die_or_abort(fmt, args, false/*write core?*/);
                     89:        va_end(args);
                     90: }
                     91: 
                     92: void SAPI::abort(const char* fmt, ...) {
                     93:        va_list args;
                     94:        va_start(args, fmt);
                     95:        die_or_abort(fmt, args, true/*write core?*/);
                     96:        va_end(args);
                     97: }
                     98: 
                     99: char* SAPI::get_env(SAPI_Info& SAPI_info, const char* name) {
                    100:        const char* dont_return_me=pa_ap_table_get(SAPI_info.r->subprocess_env, name);
                    101:        return dont_return_me?pa_strdup(dont_return_me):0;
                    102: }
                    103: 
                    104: #ifndef DOXYGEN
                    105: struct SAPI_environment_append_info {
                    106:        const char** cur;
                    107: };
                    108: #endif
                    109: static const char* mk_env_pair(const char* key, const char* value) {
                    110:        char *result=new(PointerFreeGC) char[strlen(key)+1/*=*/+strlen(value)+1/*0*/];
                    111:        strcpy(result, key); strcat(result, "="); strcat(result, value);
                    112:        return result;
                    113: }
                    114: static int SAPI_environment_append(void *d, const char* k, const char* val) {
                    115:        if( k && val ) {
                    116:                SAPI_environment_append_info& info=
                    117:                        *static_cast<SAPI_environment_append_info *>(d);
                    118:                *info.cur++=mk_env_pair(k, val);
                    119:        }
                    120:        return 1/*true*/;
                    121: }
                    122: const char* const* SAPI::environment(SAPI_Info& SAPI_info) {
                    123:        const pa_table *t=SAPI_info.r->subprocess_env;
                    124:        const char** result=new(UseGC) const char*[pa_ap_table_size(t)+1/*0*/];
                    125:        SAPI_environment_append_info info={result};
                    126:        pa_ap_table_do(SAPI_environment_append, &info, t, 0); *info.cur=0; // mark EOE
                    127:        return result;
                    128: }
                    129: 
                    130: size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) {
                    131: /*    pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server, 
                    132: "mod_parser3: SAPI::read_post(max=%u)", max_bytes);
                    133:        */
                    134:        int retval;
                    135:        if((retval = pa_ap_setup_client_block(SAPI_info.r, PA_REQUEST_CHUNKED_ERROR)))
                    136:                return 0;
                    137:        if(!pa_ap_should_client_block(SAPI_info.r))
                    138:                return 0;
                    139:        
                    140:        uint total_read_bytes=0;
                    141:        void (*handler)(int)=pa_signal(PA_SIGPIPE, PA_SIG_IGN);
                    142:        while (total_read_bytes<max_bytes) {
                    143:                pa_ap_hard_timeout("Read POST information", SAPI_info.r); /* start timeout timer */
                    144:                uint read_bytes=
                    145:                        pa_ap_get_client_block(SAPI_info.r, buf+total_read_bytes, max_bytes-total_read_bytes);
                    146:                pa_ap_reset_timeout(SAPI_info.r);
                    147:                if (read_bytes<=0)
                    148:                        break;
                    149:                total_read_bytes+=read_bytes;
                    150:        }
                    151:        pa_signal(PA_SIGPIPE, handler);
                    152:        return total_read_bytes;
                    153: }
                    154: 
                    155: /// @test location provide with protocol. think about internal redirects
                    156: void SAPI::add_header_attribute(SAPI_Info& SAPI_info,
                    157:                                const char* dont_store_key, const char* dont_store_value) {
                    158:        if(strcasecmp(dont_store_key, "location")==0) 
                    159:                *SAPI_info.r->status=302;
                    160:        
                    161:        if(strcasecmp(dont_store_key, HTTP_CONTENT_TYPE)==0) {
                    162:        /* r->content_type, *not* r->headers_out("Content-type").  If you don't
                    163:        * set it, it will be filled in with the server's default type (typically
                    164:        * "text/plain").  You *must* also ensure that r->content_type is lower
                    165:        * case.
                    166:                */
                    167:                *SAPI_info.r->content_type = pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value);
                    168:        } else if(strcasecmp(dont_store_key, HTTP_STATUS)==0) 
                    169:                *SAPI_info.r->status=atoi(dont_store_value);
                    170:        else
                    171:                pa_ap_table_addn(SAPI_info.r->headers_out, 
                    172:                pa_ap_pstrdup(SAPI_info.r->pool, capitalize(dont_store_key)), 
                    173:                pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value));
                    174: }
                    175: 
                    176: void SAPI::send_header(SAPI_Info& SAPI_info) {
                    177:        pa_ap_hard_timeout("Send header", SAPI_info.r);
                    178:        pa_ap_send_http_header(SAPI_info.r);
                    179:        pa_ap_kill_timeout(SAPI_info.r);
                    180: }
                    181: 
                    182: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
                    183:        pa_ap_hard_timeout("Send body", SAPI_info.r);
                    184:        size = (size_t)pa_ap_rwrite(buf, size, SAPI_info.r);
                    185:        pa_ap_kill_timeout(SAPI_info.r);
                    186:        return size;
                    187: }
                    188: 
                    189: //@}
                    190: 
                    191: #if !defined(PA_DEBUG_DISABLE_GC) && !defined(WIN32)
                    192: extern long GC_large_alloc_warn_suppressed; 
                    193: #endif
                    194: 
                    195: /**
                    196: main workhorse
                    197: 
                    198:        @todo intelligent cache-control
                    199: */
                    200: static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
                    201:        // collect garbage from prev request
                    202: #if !defined(PA_DEBUG_DISABLE_GC) && !defined(WIN32)
                    203:        {
                    204:                GC_dont_gc=0;
                    205:                GC_gcollect();
1.5       moko      206:                GC_dont_gc=1;
1.1       moko      207:                GC_large_alloc_warn_suppressed=0;
                    208:        }
                    209: #endif
                    210: 
                    211:        // populate env
                    212:        pa_ap_add_common_vars(SAPI_info.r);
                    213:        pa_ap_add_cgi_vars(SAPI_info.r);
                    214:        
                    215:        // Request info
                    216:        Request_info request_info;  memset(&request_info, 0, sizeof(request_info));
                    217:        
                    218:        request_info.document_root=SAPI::get_env(SAPI_info, "DOCUMENT_ROOT");
                    219:        request_info.path_translated=SAPI_info.r->filename;
                    220:        request_info.method=SAPI_info.r->method;
                    221:        request_info.query_string=SAPI_info.r->args;
                    222:        request_info.uri=SAPI::get_env(SAPI_info, "REQUEST_URI");
                    223:        request_info.content_type=SAPI::get_env(SAPI_info, "CONTENT_TYPE");
                    224:        const char* content_length=SAPI::get_env(SAPI_info, "CONTENT_LENGTH");
                    225:        request_info.content_length=content_length?atoi(content_length):0;
                    226:        request_info.cookie=SAPI::get_env(SAPI_info, "HTTP_COOKIE");
                    227:        request_info.mail_received=false;
                    228:        
                    229:        // prepare to process request
                    230:        Request request(
                    231:                SAPI_info,
                    232:                request_info,
1.3       moko      233:                String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)
1.1       moko      234:                );
                    235:        
                    236:        // process the request
                    237:        request.core(
                    238:                dcfg->parser_config_filespec, true, // /path/to/config
                    239:                SAPI_info.r->header_only!=0);
                    240: }
                    241: 
                    242: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
                    243: static const Exception 
                    244: call_real_parser_handler__do_PEH_return_it(
                    245:        SAPI_Info& SAPI_info, Parser_module_config *dcfg) 
                    246: {
                    247:        try {
                    248:                real_parser_handler(SAPI_info, dcfg);
                    249:        } catch(const Exception& e) {
                    250:                return e;
                    251:        }
                    252: 
                    253:        return Exception();
                    254: }
                    255: static void call_real_parser_handler__supress_system_exception(
                    256:        SAPI_Info& SAPI_info, Parser_module_config *dcfg) 
                    257: {
                    258:        Exception parser_exception;
                    259:        LPEXCEPTION_POINTERS system_exception=0;
                    260: 
                    261:        __try {
                    262:                parser_exception=call_real_parser_handler__do_PEH_return_it(
                    263:                        SAPI_info, dcfg);
                    264:        } __except (
                    265:                (system_exception=GetExceptionInformation()), 
                    266:                EXCEPTION_EXECUTE_HANDLER) 
                    267:        {
                    268: 
                    269:                if(system_exception)
                    270:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    271:                                throw Exception("system",
                    272:                                        0,
                    273:                                        "0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    274:                        else
                    275:                                throw Exception("system", 
                    276:                                        0, 
                    277:                                        "<no exception record>");
                    278:                else
                    279:                        throw Exception("system", 
                    280:                                0, 
                    281:                                "<no exception information>");
                    282:        }
                    283: 
                    284:        if(parser_exception)
                    285:                throw Exception(parser_exception);
                    286: }
                    287: #endif
                    288: 
                    289: /// @test r->finfo.st_mode check seems to work only on win32
                    290: int pa_parser_handler(pa_request_rec *r, Parser_module_config *dcfg) {
                    291:        // SAPI info
                    292:        SAPI_Info SAPI_info; SAPI_info.r=r;
                    293:        
1.2       moko      294:        if(r->file_not_found ) 
1.1       moko      295:                return PA_HTTP_NOT_FOUND;
                    296:        
                    297:        try { // global try
                    298: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
                    299:                call_real_parser_handler__supress_system_exception(
                    300: #else
                    301:                real_parser_handler(
                    302: #endif
                    303:                        SAPI_info, dcfg);
                    304: 
                    305:                // successful finish
                    306:        } catch(const Exception& e) { // global problem 
                    307:                // don't allocate anything on pool here:
                    308:                //   possible pool' exception not catch-ed now
                    309:                //   and there could be out-of-memory exception
                    310:                char buf[MAX_STRING];
                    311:                snprintf(buf, MAX_STRING, "Unhandled exception %s",
                    312:                        e.comment());
                    313:                // log it
                    314:                SAPI::log(SAPI_info, "%s", buf);
                    315:                
                    316:                //
                    317:                int content_length=strlen(buf);
                    318:                
                    319:                // prepare header
                    320:                // capitalized headers are used for preventing malloc during capitalization
                    321:                SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain");
                    322:                // don't use 'format' function because it calls malloc
                    323:                char content_length_cstr[MAX_NUMBER];
                    324:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    325:                SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_LENGTH_CAPITALIZED, content_length_cstr);
                    326:                
                    327:                // send header
                    328:                SAPI::send_header(SAPI_info);
                    329:                
                    330:                // send body
                    331:                if(!r->header_only)
                    332:                        SAPI::send_body(SAPI_info, buf, content_length);
                    333:                
                    334:                // unsuccessful finish
                    335:        }
                    336:        
                    337:        /*
                    338:        * We did what we wanted to do, so tell the rest of the server we
                    339:        * succeeded.
                    340:        */
                    341:        return PA_OK;
                    342: }

E-mail: