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

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

E-mail: