Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.57

1.29      paf         1: /** @file
                      2:        Parser: IIS extension.
                      3: 
                      4:        Copyright (c) 2000,2001 ArtLebedev Group (http://www.artlebedev.com)
1.57    ! paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.29      paf         6: 
1.57    ! paf         7:        $Id: parser3isapi.C,v 1.56 2001/10/24 16:52:25 parser Exp $
1.29      paf         8: */
                      9: 
1.1       paf        10: #ifndef _MSC_VER
1.10      paf        11: #      error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)]
1.1       paf        12: #endif
1.43      parser     13: 
                     14: #include "pa_config_includes.h"
1.1       paf        15: 
                     16: #include <windows.h>
                     17: #include <process.h>
                     18: 
                     19: #include <httpext.h>
                     20: 
1.9       paf        21: #include "pa_sapi.h"
1.1       paf        22: #include "pa_globals.h"
                     23: #include "pa_request.h"
                     24: #include "pa_version.h"
1.13      paf        25: #include "pool_storage.h"
1.24      paf        26: #include "pa_socks.h"
1.1       paf        27: 
1.51      parser     28: #ifdef XML
                     29: #include <XalanTransformer/XalanCAPI.h>
                     30: #endif
                     31: 
1.1       paf        32: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
1.44      parser     33: 
                     34: // consts
                     35: 
                     36: extern const char *main_RCSIds[];
1.46      parser     37: #ifdef USE_SMTP
1.44      parser     38: extern const char *smtp_RCSIds[];
1.45      parser     39: #endif
1.44      parser     40: extern const char *gd_RCSIds[];
                     41: extern const char *classes_RCSIds[];
                     42: extern const char *types_RCSIds[];
                     43: extern const char *parser3isapi_RCSIds[];
1.49      parser     44: #ifdef XML
                     45: extern const char *xalan_patched_RCSIds[];
                     46: #endif
1.44      parser     47: const char **RCSIds[]={
                     48:        main_RCSIds,
1.46      parser     49: #ifdef USE_SMTP
1.44      parser     50:        smtp_RCSIds,
1.45      parser     51: #endif
1.44      parser     52:        gd_RCSIds,
                     53:        classes_RCSIds,
                     54:        types_RCSIds,
                     55:        parser3isapi_RCSIds,
1.48      parser     56: #ifdef XML
                     57:        xalan_patched_RCSIds,
                     58: #endif
1.44      parser     59:        0
                     60: };
1.1       paf        61: 
1.18      paf        62: // SAPI
                     63: 
1.38      parser     64: #ifndef DOXYGEN
                     65: /*
1.18      paf        66:        ISAPI SAPI functions receive this context information. 
1.38      parser     67:        see Pool::set_context
1.18      paf        68: */
1.15      paf        69: struct SAPI_func_context {
1.3       paf        70:        LPEXTENSION_CONTROL_BLOCK lpECB;
                     71:        String *header;
                     72:        DWORD http_response_code;
                     73: };
1.38      parser     74: #endif
1.3       paf        75: 
1.50      parser     76: #ifdef XML
                     77: /**
                     78:  * Terminate Xalan and Xerces.
                     79:  *
                     80:  * Should be called only once per process after deleting all
                     81:  * instances of XalanTransformer.  Once a process has called
                     82:  * this function, it cannot use the API for the remaining
                     83:  * lifetime of the process.
                     84: 
                     85:        
                     86:        this requirement is fullfilled by using Pool::register_cleanup
                     87:  */
                     88: void callXalanTerminate(void *) {
                     89:        //_asm int 3;
                     90:        XalanTerminate();
                     91: }
                     92: #endif
                     93: 
1.26      paf        94: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
                     95: void SAPI::log(Pool& pool, const char *fmt, ...) {
1.53      parser     96:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.26      paf        97:        
                     98:        va_list args;
                     99:        va_start(args,fmt);
                    100:        char buf[MAX_STRING];
                    101:        const char *prefix="PARSER_ERROR:";
                    102:        strcpy(buf, prefix);
1.47      parser    103:        char *start=buf+strlen(prefix);
1.51      parser    104:        DWORD size=vsnprintf(start, MAX_STRING-strlen(prefix), fmt, args);
1.47      parser    105:        remove_crlf(start, start+size);
                    106: 
1.26      paf       107:        ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID, 
                    108:                HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
1.55      parser    109: }
                    110: 
                    111: /// @todo event log
1.56      parser    112: void SAPI::die(const char *fmt, ...) {
1.55      parser    113:        exit(1);
1.26      paf       114: }
                    115: 
1.9       paf       116: const char *SAPI::get_env(Pool& pool, const char *name) {
1.53      parser    117:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1       paf       118: 
                    119:        char *variable_buf=(char *)pool.malloc(MAX_STRING);
                    120:        DWORD variable_len = MAX_STRING-1;
                    121: 
1.3       paf       122:        if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name), 
1.1       paf       123:                variable_buf, &variable_len)) {
                    124:                variable_buf[variable_len]=0;
                    125:                return variable_buf;
1.7       paf       126:        } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
                    127:                variable_buf=(char *)pool.malloc(variable_len+1);
                    128:                
                    129:                if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name), 
                    130:                        variable_buf, &variable_len)) {
                    131:                        variable_buf[variable_len]=0;
                    132:                        return variable_buf;
                    133:                }
1.1       paf       134:        }
1.7       paf       135:                
1.1       paf       136:        return 0;
                    137: }
                    138: 
1.26      paf       139: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
1.53      parser    140:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1       paf       141: 
                    142:        DWORD read_from_buf=0;
                    143:        DWORD read_from_input=0;
                    144:        DWORD total_read=0;
                    145: 
1.3       paf       146:        read_from_buf=min(ctx.lpECB->cbAvailable, max_bytes);
                    147:        memcpy(buf, ctx.lpECB->lpbData, read_from_buf);
1.1       paf       148:        total_read+=read_from_buf;
                    149: 
                    150:        if(read_from_buf<max_bytes &&
1.3       paf       151:                read_from_buf<ctx.lpECB->cbTotalBytes) {
1.1       paf       152:                DWORD cbRead=0, cbSize;
                    153: 
1.3       paf       154:                read_from_input=min(max_bytes-read_from_buf, 
                    155:                        ctx.lpECB->cbTotalBytes-read_from_buf);
1.1       paf       156:                while(cbRead < read_from_input) {
                    157:                        cbSize=read_from_input - cbRead;
1.3       paf       158:                        if(!ctx.lpECB->ReadClient(ctx.lpECB->ConnID, 
                    159:                                buf+read_from_buf+cbRead, &cbSize) || 
1.1       paf       160:                                cbSize==0) 
                    161:                                break;
                    162:                        cbRead+=cbSize;
                    163:                }
                    164:                total_read+=cbRead;
                    165:        }
                    166:        return total_read;
                    167: }
                    168: 
1.9       paf       169: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.53      parser    170:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.3       paf       171: 
                    172:        if(strcasecmp(key, "location")==0) 
                    173:                ctx.http_response_code=302;
                    174: 
                    175:        if(strcasecmp(key, "status")==0) 
                    176:                ctx.http_response_code=atoi(value);
                    177:        else {
                    178:                ctx.header->APPEND_CONST(key);
                    179:                ctx.header->APPEND_CONST(": ");
                    180:                ctx.header->APPEND_CONST(value);
1.30      paf       181:                ctx.header->APPEND_CONST("\r\n");
1.3       paf       182:        }
1.1       paf       183: }
                    184: 
1.23      paf       185: /// @todo intelligent cache-control
1.9       paf       186: void SAPI::send_header(Pool& pool) {
1.53      parser    187:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1       paf       188: 
1.6       paf       189:        ctx.header->APPEND_CONST(
1.30      paf       190:                "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                    191:                "\r\n");
1.1       paf       192:        HSE_SEND_HEADER_EX_INFO header_info;
                    193: 
                    194:        char status_buf[MAX_STATUS_LENGTH];
1.3       paf       195:        switch(ctx.http_response_code) {
1.1       paf       196:                case 200:
                    197:                        header_info.pszStatus="200 OK";
                    198:                        break;
                    199:                case 302:
                    200:                        header_info.pszStatus="302 Moved Temporarily";
                    201:                        break;
1.8       paf       202:                case 401:// useless untill parser auth mech
1.1       paf       203:                        header_info.pszStatus="401 Authorization Required";
1.8       paf       204:                        break;
1.1       paf       205:                default:
1.3       paf       206:                        snprintf(status_buf, MAX_STATUS_LENGTH, 
                    207:                                "%d Undescribed", ctx.http_response_code);
1.1       paf       208:                        header_info.pszStatus=status_buf;
                    209:                        break;
                    210:        }
                    211:        header_info.cchStatus=strlen(header_info.pszStatus);
1.3       paf       212:        header_info.pszHeader=ctx.header->cstr();
                    213:        header_info.cchHeader=ctx.header->size();
1.5       paf       214:        header_info.fKeepConn=true;
1.1       paf       215: 
1.3       paf       216:        ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1       paf       217: 
1.3       paf       218:        ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID, 
                    219:                HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       220: }
                    221: 
1.23      paf       222: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.53      parser    223:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1       paf       224: 
                    225:        DWORD num_bytes=size;
1.3       paf       226:        ctx.lpECB->WriteClient(ctx.lpECB->ConnID, 
1.23      paf       227:                const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1       paf       228: }
1.16      paf       229: 
1.1       paf       230: // 
                    231: 
1.15      paf       232: static bool parser_init() {
1.1       paf       233:        static bool globals_inited=false;
                    234:        if(globals_inited)
1.15      paf       235:                return true;
1.1       paf       236:        globals_inited=true;
                    237: 
1.13      paf       238:        static Pool pool(0); // global pool
1.53      parser    239:        try {
1.27      paf       240:                // init socks
                    241:                init_socks(pool);
                    242: 
1.50      parser    243: #ifdef XML
                    244:                /**
                    245:                * Initialize Xerces and Xalan.
                    246:                *
                    247:                * Should be called only once per process before making
                    248:                * any other API calls.
                    249:                */
                    250:                //_asm int 3;
                    251:                XalanInitialize();
                    252:                pool.register_cleanup(callXalanTerminate, 0);
                    253: #endif
                    254: 
1.32      paf       255:                // init global classes
                    256:                init_methoded_array(pool);
1.1       paf       257:                // init global variables
1.9       paf       258:                pa_globals_init(pool);
1.1       paf       259:                
1.15      paf       260:                // successful finish
                    261:                return true;
1.53      parser    262:        } catch(const Exception& e) { // global problem 
                    263:                const char *body=e.comment();
1.15      paf       264:                
                    265:                // unsuccessful finish
                    266:                return false;
1.1       paf       267:        }
                    268: }
                    269: 
                    270: /// ISAPI //
                    271: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
                    272:        pVer->dwExtensionVersion = HSE_VERSION;
1.36      parser    273:        strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
                    274:        pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15      paf       275:        return parser_init();
1.1       paf       276: }
                    277: 
1.6       paf       278: /** 
                    279:        ISAPI // main workhorse
                    280: 
1.23      paf       281:        @todo 
1.10      paf       282:                IIS: remove trailing default-document[index.html] from $request.uri.
                    283:                to do that we need to consult metabase,
1.12      paf       284:                wich is tested&works but seems slow runtime 
                    285:                and not could-be-quickly-implemented if prepared.
1.37      parser    286:        @test
                    287:                PARSER_VERSION from outside
1.6       paf       288: */
1.53      parser    289: 
                    290: void real_parser_handler(Pool& pool, LPEXTENSION_CONTROL_BLOCK lpECB, bool header_only) {
                    291:        static_cast<SAPI_func_context *>(pool.get_context())->header=new(pool) String(pool);
                    292:        
                    293:        // Request info
                    294:        Request::Info request_info;
                    295: 
                    296:        size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
                    297:        char *filespec_to_process=(char *)pool.malloc(path_translated_buf_size);
                    298:        memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
                    299: #ifdef WIN32
                    300:        back_slashes_to_slashes(filespec_to_process);
                    301: #endif
                    302: 
                    303:        if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    304:                // IIS
                    305:                size_t len=strlen(filespec_to_process)-strlen(path_info);
                    306:                char *buf=(char *)pool.malloc(len+1);
                    307:                strncpy(buf, filespec_to_process, len); buf[len]=0;
                    308:                request_info.document_root=buf;
                    309:        } else
                    310:                throw Exception(0, 0,
                    311:                        0,
                    312:                        "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
                    313: 
                    314:        request_info.path_translated=filespec_to_process;
                    315:        request_info.method=lpECB->lpszMethod;
                    316:        request_info.query_string=lpECB->lpszQueryString;
                    317:        if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
                    318:                char *reconstructed_uri=(char *)pool.malloc(
                    319:                        strlen(lpECB->lpszPathInfo)+1/*'?'*/+
                    320:                        strlen(lpECB->lpszQueryString)+1/*0*/);
                    321:                strcpy(reconstructed_uri, lpECB->lpszPathInfo);
                    322:                strcat(reconstructed_uri, "?");
                    323:                strcat(reconstructed_uri, lpECB->lpszQueryString);
                    324:                request_info.uri=reconstructed_uri;
                    325:        } else
                    326:                request_info.uri=lpECB->lpszPathInfo;
                    327:        
                    328:        request_info.content_type=lpECB->lpszContentType;
                    329:        request_info.content_length=lpECB->cbTotalBytes;
                    330:        request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    331:        request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
                    332: 
                    333:        
                    334:        // prepare to process request
                    335:        Request request(pool,
                    336:                request_info,
                    337:                String::UL_USER_HTML
                    338:                );
                    339: 
                    340:        // some root-controlled location
                    341:        //   c:\windows
                    342:        char root_config_path[MAX_STRING];
                    343:        GetWindowsDirectory(root_config_path, MAX_STRING);
                    344:        // must be dynamic: rethrowing from request.core 
                    345:        //   may return 'source' which can be inside of 'root auto.p@exeception'
                    346:        char *root_config_filespec=(char *)pool.malloc(MAX_STRING);
                    347:        snprintf(root_config_filespec, MAX_STRING, 
                    348:                "%s/%s", 
                    349:                root_config_path, CONFIG_FILE_NAME);
                    350: 
                    351:        // process the request
                    352:        request.core(
                    353:                root_config_filespec, false/*may be abcent*/, // /path/to/admin/auto.p
                    354:                0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
                    355:                header_only);
                    356: }
                    357: 
                    358: void call_real_parser_handler__do_SEH(Pool& pool, 
                    359:                                                                          LPEXTENSION_CONTROL_BLOCK lpECB,
                    360:                                                                          bool header_only) {
1.54      parser    361: #if _MSC_VER & !defined(_DEBUG)
1.53      parser    362:        LPEXCEPTION_POINTERS system_exception=0;
                    363:        __try {
                    364: #endif
                    365:                real_parser_handler(pool, lpECB, header_only);
                    366:                
1.54      parser    367: #if _MSC_VER & !defined(_DEBUG)
1.53      parser    368:        } __except (
                    369:                (system_exception=GetExceptionInformation()), 
                    370:                EXCEPTION_EXECUTE_HANDLER) {
                    371:                
                    372:                if(system_exception)
                    373:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    374:                                throw Exception(0, 0,
                    375:                                0,
                    376:                                "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    377:                        else
                    378:                                throw Exception(0, 0, 0, "Exception <no exception record>");
                    379:                        else
                    380:                                throw Exception(0, 0, 0, "Exception <no exception information>");
                    381:        }
                    382: #endif
                    383: }
                    384: 
                    385: 
1.1       paf       386: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13      paf       387:        Pool_storage pool_storage;
1.15      paf       388:        Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
                    389:        SAPI_func_context ctx={
                    390:                lpECB,
1.18      paf       391:                0, // filling later: so that if there would be error pool would have ctx
1.39      parser    392:                200 // default http_response_code
1.15      paf       393:        };
                    394:        pool.set_context(&ctx);// no allocations before this line!
1.2       paf       395:        
1.50      parser    396: #ifdef XML
                    397:        /**
                    398:         * Initialize Xerces and Xalan.
                    399:         *
                    400:         * Should be called only once per process before making
                    401:         * any other API calls.
                    402:         */
                    403:        //_asm int 3;
                    404:        XalanInitialize();
                    405:        pool.register_cleanup(callXalanTerminate, 0);
                    406: #endif
                    407: 
                    408: 
1.1       paf       409:        bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53      parser    410:        try { // global try
                    411:                call_real_parser_handler__do_SEH(pool, lpECB, header_only);
1.2       paf       412:                // successful finish
1.53      parser    413:        } catch(const Exception& e) { // global problem
1.12      paf       414:                // don't allocate anything on pool here:
                    415:                //   possible pool' exception not catch-ed now
1.31      paf       416:                        //   and there could be out-of-memory exception
1.1       paf       417:                const char *body=e.comment();
1.16      paf       418:                // log it
                    419:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    420: 
                    421:                //
1.1       paf       422:                int content_length=strlen(body);
                    423: 
1.12      paf       424:                // prepare header // not using SAPI func wich allocates on pool
                    425:                char header_buf[MAX_STRING];
                    426:                int header_len=snprintf(header_buf, MAX_STRING,
1.30      paf       427:                        "content-type: text/plain\r\n"
                    428:                        "content-length: %lu\r\n"
                    429:                        "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                    430:                        "\r\n",
1.12      paf       431:                        content_length);
                    432: 
                    433:                HSE_SEND_HEADER_EX_INFO header_info;
                    434:                header_info.pszStatus="200 OK";
                    435:                header_info.cchStatus=strlen(header_info.pszStatus);
                    436:                header_info.pszHeader=header_buf;
                    437:                header_info.cchHeader=header_len;
                    438:                header_info.fKeepConn=true;
                    439:                
1.1       paf       440:                // send header
1.12      paf       441:                lpECB->dwHttpStatusCode=200;
                    442:                lpECB->ServerSupportFunction(lpECB->ConnID, 
                    443:                        HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       444: 
                    445:                // send body
                    446:                if(!header_only)
1.9       paf       447:                        SAPI::send_body(pool, body, content_length);
1.1       paf       448: 
                    449:                // unsuccessful finish
                    450:        }
                    451:        
1.6       paf       452:        return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1       paf       453: }

E-mail: