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

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.70    ! paf         7:        $Id: parser3isapi.C,v 1.69.2.1 2002/05/06 10:50:24 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.66      paf       192:        *ctx.header << "\r\n"; // ISAPI v<5 did quite well without it
1.3       paf       193:        header_info.pszHeader=ctx.header->cstr();
                    194:        header_info.cchHeader=ctx.header->size();
1.5       paf       195:        header_info.fKeepConn=true;
1.1       paf       196: 
1.3       paf       197:        ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1       paf       198: 
1.3       paf       199:        ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID, 
                    200:                HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       201: }
                    202: 
1.23      paf       203: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.53      parser    204:        SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1       paf       205: 
                    206:        DWORD num_bytes=size;
1.3       paf       207:        ctx.lpECB->WriteClient(ctx.lpECB->ConnID, 
1.23      paf       208:                const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1       paf       209: }
1.16      paf       210: 
1.1       paf       211: // 
                    212: 
1.59      paf       213: int failed_new(size_t size) {
                    214:        SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
                    215:        return 0; // not reached
                    216: }
                    217: 
1.15      paf       218: static bool parser_init() {
1.1       paf       219:        static bool globals_inited=false;
                    220:        if(globals_inited)
1.15      paf       221:                return true;
1.1       paf       222:        globals_inited=true;
                    223: 
1.59      paf       224:        _set_new_handler(failed_new);
                    225: 
1.65      paf       226:        static Pool_storage pool_storage;
                    227:        static Pool pool(&pool_storage); // global pool
1.53      parser    228:        try {
1.27      paf       229:                // init socks
                    230:                init_socks(pool);
1.32      paf       231:                // init global classes
                    232:                init_methoded_array(pool);
1.1       paf       233:                // init global variables
1.9       paf       234:                pa_globals_init(pool);
1.65      paf       235: 
1.15      paf       236:                // successful finish
                    237:                return true;
1.53      parser    238:        } catch(const Exception& e) { // global problem 
                    239:                const char *body=e.comment();
1.15      paf       240:                
                    241:                // unsuccessful finish
                    242:                return false;
1.1       paf       243:        }
                    244: }
                    245: 
                    246: /// ISAPI //
                    247: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
                    248:        pVer->dwExtensionVersion = HSE_VERSION;
1.36      parser    249:        strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
                    250:        pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15      paf       251:        return parser_init();
1.1       paf       252: }
                    253: 
1.6       paf       254: /** 
                    255:        ISAPI // main workhorse
                    256: 
1.23      paf       257:        @todo 
1.10      paf       258:                IIS: remove trailing default-document[index.html] from $request.uri.
                    259:                to do that we need to consult metabase,
1.12      paf       260:                wich is tested&works but seems slow runtime 
                    261:                and not could-be-quickly-implemented if prepared.
1.37      parser    262:        @test
                    263:                PARSER_VERSION from outside
1.6       paf       264: */
1.53      parser    265: 
                    266: void real_parser_handler(Pool& pool, LPEXTENSION_CONTROL_BLOCK lpECB, bool header_only) {
                    267:        static_cast<SAPI_func_context *>(pool.get_context())->header=new(pool) String(pool);
                    268:        
                    269:        // Request info
                    270:        Request::Info request_info;
                    271: 
                    272:        size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
                    273:        char *filespec_to_process=(char *)pool.malloc(path_translated_buf_size);
                    274:        memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
                    275: #ifdef WIN32
                    276:        back_slashes_to_slashes(filespec_to_process);
                    277: #endif
                    278: 
                    279:        if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    280:                // IIS
                    281:                size_t len=strlen(filespec_to_process)-strlen(path_info);
                    282:                char *buf=(char *)pool.malloc(len+1);
                    283:                strncpy(buf, filespec_to_process, len); buf[len]=0;
                    284:                request_info.document_root=buf;
                    285:        } else
1.67      paf       286:                throw Exception("parser.runtime",
1.53      parser    287:                        0,
                    288:                        "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
                    289: 
                    290:        request_info.path_translated=filespec_to_process;
                    291:        request_info.method=lpECB->lpszMethod;
                    292:        request_info.query_string=lpECB->lpszQueryString;
                    293:        if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
                    294:                char *reconstructed_uri=(char *)pool.malloc(
                    295:                        strlen(lpECB->lpszPathInfo)+1/*'?'*/+
                    296:                        strlen(lpECB->lpszQueryString)+1/*0*/);
                    297:                strcpy(reconstructed_uri, lpECB->lpszPathInfo);
                    298:                strcat(reconstructed_uri, "?");
                    299:                strcat(reconstructed_uri, lpECB->lpszQueryString);
                    300:                request_info.uri=reconstructed_uri;
                    301:        } else
                    302:                request_info.uri=lpECB->lpszPathInfo;
                    303:        
                    304:        request_info.content_type=lpECB->lpszContentType;
                    305:        request_info.content_length=lpECB->cbTotalBytes;
                    306:        request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    307:        request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
                    308: 
                    309:        
                    310:        // prepare to process request
                    311:        Request request(pool,
                    312:                request_info,
1.60      paf       313:                String::UL_HTML|String::UL_OPTIMIZE_BIT,
1.70    ! paf       314: #ifdef _DEBUG
        !           315:                true
        !           316: #else
        !           317:                false 
        !           318: #endif
        !           319:                        /* status_allowed */);
1.53      parser    320: 
                    321:        // some root-controlled location
                    322:        //   c:\windows
                    323:        char root_config_path[MAX_STRING];
                    324:        GetWindowsDirectory(root_config_path, MAX_STRING);
                    325:        // must be dynamic: rethrowing from request.core 
                    326:        //   may return 'source' which can be inside of 'root auto.p@exeception'
                    327:        char *root_config_filespec=(char *)pool.malloc(MAX_STRING);
                    328:        snprintf(root_config_filespec, MAX_STRING, 
                    329:                "%s/%s", 
                    330:                root_config_path, CONFIG_FILE_NAME);
                    331: 
1.65      paf       332:        // beside by binary
                    333:        static char site_config_path[MAX_STRING];
                    334:        strncpy(site_config_path, argv0, MAX_STRING-1);  site_config_path[MAX_STRING-1]=0; // filespec of my binary
                    335:        if(!(
                    336:                rsplit(site_config_path, '/') || 
                    337:                rsplit(site_config_path, '\\'))) { // strip filename
                    338:                // no path, just filename
                    339:                site_config_path[0]='.'; site_config_path[1]=0;
                    340:        }       
                    341:        char site_config_filespec[MAX_STRING];
                    342:        snprintf(site_config_filespec, MAX_STRING, 
                    343:                "%s/%s", 
                    344:                site_config_path, CONFIG_FILE_NAME);
                    345: 
1.53      parser    346:        // process the request
                    347:        request.core(
1.68      paf       348:                root_config_filespec, false /*fail_on_read_problem*/, // /path/to/root/parser3.conf
                    349:                site_config_filespec, false /*fail_on_read_problem*/, // /path/to/site/parser3.conf
1.53      parser    350:                header_only);
                    351: }
                    352: 
                    353: void call_real_parser_handler__do_SEH(Pool& pool, 
                    354:                                                                          LPEXTENSION_CONTROL_BLOCK lpECB,
                    355:                                                                          bool header_only) {
1.54      parser    356: #if _MSC_VER & !defined(_DEBUG)
1.53      parser    357:        LPEXCEPTION_POINTERS system_exception=0;
                    358:        __try {
                    359: #endif
                    360:                real_parser_handler(pool, lpECB, header_only);
                    361:                
1.54      parser    362: #if _MSC_VER & !defined(_DEBUG)
1.53      parser    363:        } __except (
                    364:                (system_exception=GetExceptionInformation()), 
                    365:                EXCEPTION_EXECUTE_HANDLER) {
                    366:                
                    367:                if(system_exception)
                    368:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.67      paf       369:                                throw Exception(0,
1.53      parser    370:                                0,
                    371:                                "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    372:                        else
1.67      paf       373:                                throw Exception(0, 0, "Exception <no exception record>");
1.53      parser    374:                        else
1.67      paf       375:                                throw Exception(0, 0, "Exception <no exception information>");
1.53      parser    376:        }
                    377: #endif
                    378: }
                    379: 
                    380: 
1.1       paf       381: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13      paf       382:        Pool_storage pool_storage;
1.15      paf       383:        Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
                    384:        SAPI_func_context ctx={
                    385:                lpECB,
1.18      paf       386:                0, // filling later: so that if there would be error pool would have ctx
1.39      parser    387:                200 // default http_response_code
1.15      paf       388:        };
                    389:        pool.set_context(&ctx);// no allocations before this line!
1.2       paf       390:        
1.1       paf       391:        bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53      parser    392:        try { // global try
                    393:                call_real_parser_handler__do_SEH(pool, lpECB, header_only);
1.2       paf       394:                // successful finish
1.53      parser    395:        } catch(const Exception& e) { // global problem
1.12      paf       396:                // don't allocate anything on pool here:
                    397:                //   possible pool' exception not catch-ed now
1.31      paf       398:                        //   and there could be out-of-memory exception
1.1       paf       399:                const char *body=e.comment();
1.16      paf       400:                // log it
                    401:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    402: 
                    403:                //
1.1       paf       404:                int content_length=strlen(body);
                    405: 
1.12      paf       406:                // prepare header // not using SAPI func wich allocates on pool
                    407:                char header_buf[MAX_STRING];
                    408:                int header_len=snprintf(header_buf, MAX_STRING,
1.30      paf       409:                        "content-type: text/plain\r\n"
                    410:                        "content-length: %lu\r\n"
1.69      paf       411: //                     "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
1.30      paf       412:                        "\r\n",
1.12      paf       413:                        content_length);
                    414: 
                    415:                HSE_SEND_HEADER_EX_INFO header_info;
                    416:                header_info.pszStatus="200 OK";
                    417:                header_info.cchStatus=strlen(header_info.pszStatus);
                    418:                header_info.pszHeader=header_buf;
                    419:                header_info.cchHeader=header_len;
                    420:                header_info.fKeepConn=true;
                    421:                
1.1       paf       422:                // send header
1.12      paf       423:                lpECB->dwHttpStatusCode=200;
                    424:                lpECB->ServerSupportFunction(lpECB->ConnID, 
                    425:                        HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       426: 
                    427:                // send body
                    428:                if(!header_only)
1.9       paf       429:                        SAPI::send_body(pool, body, content_length);
1.1       paf       430: 
                    431:                // unsuccessful finish
                    432:        }
1.65      paf       433: /*
                    434:                const char *body="test";
                    435: 
                    436:                //
                    437:                int content_length=strlen(body);
                    438: 
                    439:                // prepare header // not using SAPI func wich allocates on pool
                    440:                char header_buf[MAX_STRING];
                    441:                int header_len=snprintf(header_buf, MAX_STRING,
                    442:                        "content-type: text/plain\r\n"
                    443:                        "content-length: %lu\r\n"
                    444:                        "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                    445:                        "\r\n",
                    446:                        content_length);
                    447:                HSE_SEND_HEADER_EX_INFO header_info;
                    448:                header_info.pszStatus="200 OK";
                    449:                header_info.cchStatus=strlen(header_info.pszStatus);
                    450:                header_info.pszHeader=header_buf;
                    451:                header_info.cchHeader=header_len;
                    452:                header_info.fKeepConn=true;
                    453:                
                    454:        // send header
                    455:                lpECB->dwHttpStatusCode=200;
                    456:                lpECB->ServerSupportFunction(lpECB->ConnID, 
                    457:                        HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
                    458: 
                    459:                // send body
                    460:        DWORD num_bytes=content_length;
                    461:        lpECB->WriteClient(lpECB->ConnID, 
                    462:                (void *)body, &num_bytes, HSE_IO_SYNC);
                    463: */
1.66      paf       464: 
1.6       paf       465:        return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1       paf       466: }
1.65      paf       467: 
                    468: BOOL WINAPI DllMain(
                    469:   HINSTANCE hinstDLL,  // handle to the DLL module
                    470:   DWORD fdwReason,     // reason for calling function
                    471:   LPVOID lpvReserved   // reserved
                    472:   ) {
                    473: 
                    474:        GetModuleFileName(
                    475:          hinstDLL,    // handle to module
                    476:          argv0,  // file name of module
                    477:          sizeof(argv0)         // size of buffer
                    478:        );
                    479: 
                    480:        return TRUE;
                    481: }

E-mail: