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

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

E-mail: