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

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

E-mail: