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

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

E-mail: