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

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

E-mail: