Annotation of parser3/src/targets/apache13core/mod_parser3_core.C, revision 1.5

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

E-mail: