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

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

E-mail: