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

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

E-mail: