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

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

E-mail: