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

1.29      paf         1: /** @file
                      2:        Parser: IIS extension.
                      3: 
                      4:        Copyright (c) 2000,2001 ArtLebedev Group (http://www.artlebedev.com)
1.44      parser      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.29      paf         6: 
1.52    ! parser      7:        $Id: parser3isapi.C,v 1.51 2001/10/12 14:28:48 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, ...) {
                     96:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
                     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);
                    109: }
                    110: 
1.9       paf       111: const char *SAPI::get_env(Pool& pool, const char *name) {
1.15      paf       112:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1       paf       113: 
                    114:        char *variable_buf=(char *)pool.malloc(MAX_STRING);
                    115:        DWORD variable_len = MAX_STRING-1;
                    116: 
1.3       paf       117:        if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name), 
1.1       paf       118:                variable_buf, &variable_len)) {
                    119:                variable_buf[variable_len]=0;
                    120:                return variable_buf;
1.7       paf       121:        } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
                    122:                variable_buf=(char *)pool.malloc(variable_len+1);
                    123:                
                    124:                if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name), 
                    125:                        variable_buf, &variable_len)) {
                    126:                        variable_buf[variable_len]=0;
                    127:                        return variable_buf;
                    128:                }
1.1       paf       129:        }
1.7       paf       130:                
1.1       paf       131:        return 0;
                    132: }
                    133: 
1.26      paf       134: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
1.15      paf       135:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1       paf       136: 
                    137:        DWORD read_from_buf=0;
                    138:        DWORD read_from_input=0;
                    139:        DWORD total_read=0;
                    140: 
1.3       paf       141:        read_from_buf=min(ctx.lpECB->cbAvailable, max_bytes);
                    142:        memcpy(buf, ctx.lpECB->lpbData, read_from_buf);
1.1       paf       143:        total_read+=read_from_buf;
                    144: 
                    145:        if(read_from_buf<max_bytes &&
1.3       paf       146:                read_from_buf<ctx.lpECB->cbTotalBytes) {
1.1       paf       147:                DWORD cbRead=0, cbSize;
                    148: 
1.3       paf       149:                read_from_input=min(max_bytes-read_from_buf, 
                    150:                        ctx.lpECB->cbTotalBytes-read_from_buf);
1.1       paf       151:                while(cbRead < read_from_input) {
                    152:                        cbSize=read_from_input - cbRead;
1.3       paf       153:                        if(!ctx.lpECB->ReadClient(ctx.lpECB->ConnID, 
                    154:                                buf+read_from_buf+cbRead, &cbSize) || 
1.1       paf       155:                                cbSize==0) 
                    156:                                break;
                    157:                        cbRead+=cbSize;
                    158:                }
                    159:                total_read+=cbRead;
                    160:        }
                    161:        return total_read;
                    162: }
                    163: 
1.9       paf       164: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.15      paf       165:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.3       paf       166: 
                    167:        if(strcasecmp(key, "location")==0) 
                    168:                ctx.http_response_code=302;
                    169: 
                    170:        if(strcasecmp(key, "status")==0) 
                    171:                ctx.http_response_code=atoi(value);
                    172:        else {
                    173:                ctx.header->APPEND_CONST(key);
                    174:                ctx.header->APPEND_CONST(": ");
                    175:                ctx.header->APPEND_CONST(value);
1.30      paf       176:                ctx.header->APPEND_CONST("\r\n");
1.3       paf       177:        }
1.1       paf       178: }
                    179: 
1.23      paf       180: /// @todo intelligent cache-control
1.9       paf       181: void SAPI::send_header(Pool& pool) {
1.15      paf       182:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1       paf       183: 
1.6       paf       184:        ctx.header->APPEND_CONST(
1.30      paf       185:                "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                    186:                "\r\n");
1.1       paf       187:        HSE_SEND_HEADER_EX_INFO header_info;
                    188: 
                    189:        char status_buf[MAX_STATUS_LENGTH];
1.3       paf       190:        switch(ctx.http_response_code) {
1.1       paf       191:                case 200:
                    192:                        header_info.pszStatus="200 OK";
                    193:                        break;
                    194:                case 302:
                    195:                        header_info.pszStatus="302 Moved Temporarily";
                    196:                        break;
1.8       paf       197:                case 401:// useless untill parser auth mech
1.1       paf       198:                        header_info.pszStatus="401 Authorization Required";
1.8       paf       199:                        break;
1.1       paf       200:                default:
1.3       paf       201:                        snprintf(status_buf, MAX_STATUS_LENGTH, 
                    202:                                "%d Undescribed", ctx.http_response_code);
1.1       paf       203:                        header_info.pszStatus=status_buf;
                    204:                        break;
                    205:        }
                    206:        header_info.cchStatus=strlen(header_info.pszStatus);
1.3       paf       207:        header_info.pszHeader=ctx.header->cstr();
                    208:        header_info.cchHeader=ctx.header->size();
1.5       paf       209:        header_info.fKeepConn=true;
1.1       paf       210: 
1.3       paf       211:        ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1       paf       212: 
1.3       paf       213:        ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID, 
                    214:                HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       215: }
                    216: 
1.23      paf       217: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.15      paf       218:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1       paf       219: 
                    220:        DWORD num_bytes=size;
1.3       paf       221:        ctx.lpECB->WriteClient(ctx.lpECB->ConnID, 
1.23      paf       222:                const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1       paf       223: }
1.16      paf       224: 
1.1       paf       225: // 
                    226: 
1.15      paf       227: static bool parser_init() {
1.1       paf       228:        static bool globals_inited=false;
                    229:        if(globals_inited)
1.15      paf       230:                return true;
1.1       paf       231:        globals_inited=true;
                    232: 
1.13      paf       233:        static Pool pool(0); // global pool
1.1       paf       234:        PTRY {
1.27      paf       235:                // init socks
                    236:                init_socks(pool);
                    237: 
1.50      parser    238: #ifdef XML
                    239:                /**
                    240:                * Initialize Xerces and Xalan.
                    241:                *
                    242:                * Should be called only once per process before making
                    243:                * any other API calls.
                    244:                */
                    245:                //_asm int 3;
                    246:                XalanInitialize();
                    247:                pool.register_cleanup(callXalanTerminate, 0);
                    248: #endif
                    249: 
1.32      paf       250:                // init global classes
                    251:                init_methoded_array(pool);
1.1       paf       252:                // init global variables
1.9       paf       253:                pa_globals_init(pool);
1.1       paf       254:                
1.15      paf       255:                // successful finish
                    256:                return true;
1.1       paf       257:        } PCATCH(e) { // global problem 
1.15      paf       258:                //const char *body=e.comment();
                    259:                
                    260:                // unsuccessful finish
                    261:                return false;
1.1       paf       262:        }
                    263:        PEND_CATCH
                    264: }
                    265: 
                    266: /// ISAPI //
                    267: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
                    268:        pVer->dwExtensionVersion = HSE_VERSION;
1.36      parser    269:        strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
                    270:        pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15      paf       271:        return parser_init();
1.1       paf       272: }
                    273: 
1.6       paf       274: /** 
                    275:        ISAPI // main workhorse
                    276: 
1.23      paf       277:        @todo 
1.10      paf       278:                IIS: remove trailing default-document[index.html] from $request.uri.
                    279:                to do that we need to consult metabase,
1.12      paf       280:                wich is tested&works but seems slow runtime 
                    281:                and not could-be-quickly-implemented if prepared.
1.37      parser    282:        @test
                    283:                PARSER_VERSION from outside
1.6       paf       284: */
1.1       paf       285: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13      paf       286:        Pool_storage pool_storage;
1.15      paf       287:        Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
                    288:        SAPI_func_context ctx={
                    289:                lpECB,
1.18      paf       290:                0, // filling later: so that if there would be error pool would have ctx
1.39      parser    291:                200 // default http_response_code
1.15      paf       292:        };
1.41      parser    293:        _asm nop; // int 3;
1.15      paf       294:        pool.set_context(&ctx);// no allocations before this line!
1.2       paf       295:        
1.50      parser    296: #ifdef XML
                    297:        /**
                    298:         * Initialize Xerces and Xalan.
                    299:         *
                    300:         * Should be called only once per process before making
                    301:         * any other API calls.
                    302:         */
                    303:        //_asm int 3;
                    304:        XalanInitialize();
                    305:        pool.register_cleanup(callXalanTerminate, 0);
                    306: #endif
                    307: 
                    308: 
1.1       paf       309:        bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
                    310:        PTRY { // global try
1.12      paf       311:                ctx.header=new(pool) String(pool);
1.3       paf       312:                
1.2       paf       313:                // Request info
                    314:                Request::Info request_info;
1.14      paf       315: 
                    316:                size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
1.52    ! parser    317:                char *filespec_to_process=(char *)pool.malloc(path_translated_buf_size);
1.14      paf       318:                memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
                    319: #ifdef WIN32
                    320:                back_slashes_to_slashes(filespec_to_process);
                    321: #endif
                    322: 
1.11      paf       323:                if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    324:                        // IIS
                    325:                        size_t len=strlen(filespec_to_process)-strlen(path_info);
                    326:                        char *buf=(char *)pool.malloc(len+1);
1.36      parser    327:                        strncpy(buf, filespec_to_process, len); buf[len]=0;
1.11      paf       328:                        request_info.document_root=buf;
                    329:                } else
1.6       paf       330:                        PTHROW(0, 0,
                    331:                                0,
1.11      paf       332:                                "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
1.6       paf       333: 
1.11      paf       334:                request_info.path_translated=filespec_to_process;
1.2       paf       335:                request_info.method=lpECB->lpszMethod;
                    336:                request_info.query_string=lpECB->lpszQueryString;
                    337:                if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.52    ! parser    338:                        char *reconstructed_uri=(char *)pool.malloc(
1.7       paf       339:                                strlen(lpECB->lpszPathInfo)+1/*'?'*/+
                    340:                                strlen(lpECB->lpszQueryString)+1/*0*/);
                    341:                        strcpy(reconstructed_uri, lpECB->lpszPathInfo);
1.2       paf       342:                        strcat(reconstructed_uri, "?");
1.7       paf       343:                        strcat(reconstructed_uri, lpECB->lpszQueryString);
1.2       paf       344:                        request_info.uri=reconstructed_uri;
                    345:                } else
                    346:                        request_info.uri=lpECB->lpszPathInfo;
                    347:                
                    348:                request_info.content_type=lpECB->lpszContentType;
                    349:                request_info.content_length=lpECB->cbTotalBytes;
1.9       paf       350:                request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
1.20      paf       351:                request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
                    352: 
1.2       paf       353:                
                    354:                // prepare to process request
                    355:                Request request(pool,
                    356:                        request_info,
1.33      paf       357:                        String::UL_USER_HTML
1.2       paf       358:                        );
1.15      paf       359: 
1.2       paf       360:                // some root-controlled location
1.7       paf       361:                //   c:\windows
1.42      parser    362:                char root_config_path[MAX_STRING];
                    363:                GetWindowsDirectory(root_config_path, MAX_STRING);
1.7       paf       364:                // must be dynamic: rethrowing from request.core 
                    365:                //   may return 'source' which can be inside of 'root auto.p@exeception'
1.42      parser    366:                char *root_config_filespec=(char *)pool.malloc(MAX_STRING);
                    367:                snprintf(root_config_filespec, MAX_STRING, 
                    368:                        "%s/%s", 
                    369:                        root_config_path, CONFIG_FILE_NAME);
1.4       paf       370: 
1.2       paf       371:                // process the request
                    372:                request.core(
1.42      parser    373:                        root_config_filespec, false/*may be abcent*/, // /path/to/admin/auto.p
1.2       paf       374:                        0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
                    375:                        header_only);
                    376:                // successful finish
1.12      paf       377:        } PCATCH(e) { // global problem
                    378:                // don't allocate anything on pool here:
                    379:                //   possible pool' exception not catch-ed now
1.31      paf       380:                        //   and there could be out-of-memory exception
1.1       paf       381:                const char *body=e.comment();
1.16      paf       382:                // log it
                    383:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    384: 
                    385:                //
1.1       paf       386:                int content_length=strlen(body);
                    387: 
1.12      paf       388:                // prepare header // not using SAPI func wich allocates on pool
                    389:                char header_buf[MAX_STRING];
                    390:                int header_len=snprintf(header_buf, MAX_STRING,
1.30      paf       391:                        "content-type: text/plain\r\n"
                    392:                        "content-length: %lu\r\n"
                    393:                        "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                    394:                        "\r\n",
1.12      paf       395:                        content_length);
                    396: 
                    397:                HSE_SEND_HEADER_EX_INFO header_info;
                    398:                header_info.pszStatus="200 OK";
                    399:                header_info.cchStatus=strlen(header_info.pszStatus);
                    400:                header_info.pszHeader=header_buf;
                    401:                header_info.cchHeader=header_len;
                    402:                header_info.fKeepConn=true;
                    403:                
1.1       paf       404:                // send header
1.12      paf       405:                lpECB->dwHttpStatusCode=200;
                    406:                lpECB->ServerSupportFunction(lpECB->ConnID, 
                    407:                        HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       408: 
                    409:                // send body
                    410:                if(!header_only)
1.9       paf       411:                        SAPI::send_body(pool, body, content_length);
1.1       paf       412: 
                    413:                // unsuccessful finish
                    414:        }
                    415:        PEND_CATCH
                    416:        
1.6       paf       417:        return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1       paf       418: }

E-mail: