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

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.6     ! paf         8: static const char * const IDENT_MOD_PARSER3_MAIN_C="$Date: 2004/04/01 11:43:54 $";
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: 
1.6     ! paf       181: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
        !           182:        size_t size;
1.1       paf       183:        pa_ap_hard_timeout("Send body", SAPI_info.r);
1.6     ! paf       184:        size = (size_t)pa_ap_rwrite(buf, size, SAPI_info.r);
1.1       paf       185:        pa_ap_kill_timeout(SAPI_info.r);
1.6     ! paf       186:        return size;
1.1       paf       187: }
                    188: 
                    189: //@}
                    190: 
                    191: /**
                    192: main workhorse
                    193: 
                    194:        @todo intelligent cache-control
                    195: */
                    196: static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.4       paf       197:        // collect garbage from prev request
                    198: #ifndef PA_DEBUG_DISABLE_GC
                    199:        {
                    200:                int saved=GC_dont_gc;
                    201:                GC_dont_gc=0;
                    202:                GC_gcollect();
                    203:                GC_dont_gc=saved;
                    204:        }
                    205: #endif
                    206: 
                    207:        // populate env
1.1       paf       208:        pa_ap_add_common_vars(SAPI_info.r);
                    209:        pa_ap_add_cgi_vars(SAPI_info.r);
                    210:        
                    211:        // Request info
                    212:        Request_info request_info;  memset(&request_info, 0, sizeof(request_info));
                    213:        
                    214:        request_info.document_root=SAPI::get_env(SAPI_info, "DOCUMENT_ROOT");
                    215:        request_info.path_translated=SAPI_info.r->filename;
                    216:        request_info.method=SAPI_info.r->method;
                    217:        request_info.query_string=SAPI_info.r->args;
                    218:        request_info.uri=SAPI::get_env(SAPI_info, "REQUEST_URI");
                    219:        request_info.content_type=SAPI::get_env(SAPI_info, "CONTENT_TYPE");
                    220:        const char* content_length=SAPI::get_env(SAPI_info, "CONTENT_LENGTH");
                    221:        request_info.content_length=content_length?atoi(content_length):0;
                    222:        request_info.cookie=SAPI::get_env(SAPI_info, "HTTP_COOKIE");
                    223:        request_info.mail_received=false;
                    224:        
                    225:        //_asm int 3;
                    226:        // prepare to process request
                    227:        Request request(
                    228:                SAPI_info,
                    229:                request_info,
                    230:                String::Language(String::L_HTML|String::L_OPTIMIZE_BIT),
                    231:                dcfg->parser_status_allowed?true:false
                    232:                );
                    233:        
                    234:        // process the request
                    235:        request.core(
                    236:                dcfg->parser_config_filespec, true, // /path/to/config
                    237:                SAPI_info.r->header_only!=0);
                    238: }
                    239: 
                    240: void call_real_parser_handler__do_SEH(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
                    241: #if _MSC_VER & !defined(_DEBUG)
                    242:        LPEXCEPTION_POINTERS system_exception=0;
                    243:        __try {
                    244: #endif
                    245:                real_parser_handler(SAPI_info, dcfg);
                    246:                
                    247: #if _MSC_VER & !defined(_DEBUG)
                    248:        } __except (
                    249:                (system_exception=GetExceptionInformation()), 
                    250:                EXCEPTION_EXECUTE_HANDLER) {
                    251:                
                    252:                if(system_exception)
                    253:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    254:                                throw Exception(0,
                    255:                                0,
                    256:                                "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    257:                        else
                    258:                                throw Exception(0, 0, "Exception <no exception record>");
                    259:                        else
                    260:                                throw Exception(0, 0, "Exception <no exception information>");
                    261:        }
                    262: #endif
                    263: }
                    264: 
                    265: /// @test r->finfo.st_mode check seems to work only on win32
                    266: int pa_parser_handler(pa_request_rec *r, Parser_module_config *dcfg) {
                    267:        //_asm int 3;
                    268: 
                    269:        // SAPI info
                    270:        SAPI_Info SAPI_info; SAPI_info.r=r;
                    271:        
                    272:        //_asm int 3;
                    273:        if(r->finfo->st_mode == 0) 
                    274:                return PA_HTTP_NOT_FOUND;
                    275:        
                    276:                /* A flag which modules can set, to indicate that the data being
                    277:                * returned is volatile, and clients should be told not to cache it.
                    278:        */
                    279:        //      r->no_cache=1;
                    280:        
                    281:        try { // global try
                    282:                call_real_parser_handler__do_SEH(SAPI_info, dcfg);
                    283:                // successful finish
                    284:        } catch(const Exception& e) { // global problem 
                    285:                // don't allocate anything on pool here:
                    286:                //   possible pool' exception not catch-ed now
                    287:                //   and there could be out-of-memory exception
                    288:                const char* body=e.comment();
                    289:                // log it
                    290:                SAPI::log(SAPI_info, "exception in request exception handler: %s", body);
                    291:                
                    292:                //
                    293:                int content_length=strlen(body);
                    294:                
                    295:                // prepare header
                    296:                SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
                    297:                char content_length_cstr[MAX_NUMBER];
                    298:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    299:                SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
                    300:                
                    301:                // send header
                    302:                SAPI::send_header(SAPI_info);
                    303:                
                    304:                // send body
                    305:                if(!r->header_only)
                    306:                        SAPI::send_body(SAPI_info, body, content_length);
                    307:                
                    308:                // unsuccessful finish
                    309:        }
                    310:        
                    311:        /*
                    312:        * We did what we wanted to do, so tell the rest of the server we
                    313:        * succeeded.
                    314:        */
                    315:        return PA_OK;
                    316: }

E-mail: