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

1.1       paf         1: #ifndef _MSC_VER
                      2: #      error compile ISAPI module with MSVC
                      3: #endif
                      4: 
                      5: #include <windows.h>
                      6: #include <process.h>
                      7: 
                      8: #include <httpext.h>
                      9: 
                     10: #include "pa_globals.h"
                     11: #include "pa_request.h"
                     12: #include "pa_version.h"
                     13: 
                     14: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
                     15: 
                     16: //@{
                     17: /// service func decl
1.3       paf        18: 
                     19: struct Service_func_context {
                     20:        LPEXTENSION_CONTROL_BLOCK lpECB;
                     21:        String *header;
                     22:        DWORD http_response_code;
                     23: };
                     24: 
1.1       paf        25: static const char *get_env(Pool& pool, const char *name) {
1.3       paf        26:        Service_func_context& ctx=*static_cast<Service_func_context *>(pool.context());
1.1       paf        27: 
                     28:        char *variable_buf=(char *)pool.malloc(MAX_STRING);
                     29:        DWORD variable_len = MAX_STRING-1;
                     30: 
1.3       paf        31:        if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name), 
1.1       paf        32:                variable_buf, &variable_len)) {
                     33:                variable_buf[variable_len]=0;
                     34:                return variable_buf;
                     35:        }
                     36: 
                     37:        variable_buf=(char *)pool.malloc(variable_len+1);
                     38:        
1.3       paf        39:        if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name), 
1.1       paf        40:                variable_buf, &variable_len)) {
                     41:                variable_buf[variable_len]=0;
                     42:                return variable_buf;
                     43:        }
                     44: 
                     45:        return 0;
                     46: }
                     47: 
                     48: static uint read_post(Pool& pool, char *buf, uint max_bytes) {
1.3       paf        49:        Service_func_context& ctx=*static_cast<Service_func_context *>(pool.context());
1.1       paf        50: 
                     51:        DWORD read_from_buf=0;
                     52:        DWORD read_from_input=0;
                     53:        DWORD total_read=0;
                     54: 
1.3       paf        55:        read_from_buf=min(ctx.lpECB->cbAvailable, max_bytes);
                     56:        memcpy(buf, ctx.lpECB->lpbData, read_from_buf);
1.1       paf        57:        total_read+=read_from_buf;
                     58: 
                     59:        if(read_from_buf<max_bytes &&
1.3       paf        60:                read_from_buf<ctx.lpECB->cbTotalBytes) {
1.1       paf        61:                DWORD cbRead=0, cbSize;
                     62: 
1.3       paf        63:                read_from_input=min(max_bytes-read_from_buf, 
                     64:                        ctx.lpECB->cbTotalBytes-read_from_buf);
1.1       paf        65:                while(cbRead < read_from_input) {
                     66:                        cbSize=read_from_input - cbRead;
1.3       paf        67:                        if(!ctx.lpECB->ReadClient(ctx.lpECB->ConnID, 
                     68:                                buf+read_from_buf+cbRead, &cbSize) || 
1.1       paf        69:                                cbSize==0) 
                     70:                                break;
                     71:                        cbRead+=cbSize;
                     72:                }
                     73:                total_read+=cbRead;
                     74:        }
                     75:        return total_read;
                     76: }
                     77: 
                     78: static void add_header_attribute(Pool& pool, const char *key, const char *value) {
1.3       paf        79:        Service_func_context& ctx=*static_cast<Service_func_context *>(pool.context());
                     80: 
                     81:        if(strcasecmp(key, "location")==0) 
                     82:                ctx.http_response_code=302;
                     83: 
                     84:        if(strcasecmp(key, "status")==0) 
                     85:                ctx.http_response_code=atoi(value);
                     86:        else {
                     87:                ctx.header->APPEND_CONST(key);
                     88:                ctx.header->APPEND_CONST(": ");
                     89:                ctx.header->APPEND_CONST(value);
                     90:                ctx.header->APPEND_CONST("\n");
                     91:        }
1.1       paf        92: }
                     93: 
1.3       paf        94: /// @todo intelligent cache-control
1.1       paf        95: static void send_header(Pool& pool) {
1.3       paf        96:        Service_func_context& ctx=*static_cast<Service_func_context *>(pool.context());
1.1       paf        97: 
1.6     ! paf        98:        ctx.header->APPEND_CONST(
        !            99:                "Connection: keep-alive\n"
        !           100:                "Cache-Control: no-cache\n"
        !           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.3       paf       112:                /*case 401:
1.1       paf       113:                        header_info.pszStatus="401 Authorization Required";
1.3       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: 
                    132: static void send_body(Pool& pool, const char *buf, size_t size) {
1.3       paf       133:        Service_func_context& ctx=*static_cast<Service_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: /// Service funcs 
                    142: Service_funcs service_funcs={
                    143:        get_env,
                    144:        read_post,
                    145:        add_header_attribute,
                    146:        send_header,
                    147:        send_body
                    148: };
                    149: 
                    150: // 
                    151: 
                    152: static void parser_init() {
                    153:        static bool globals_inited=false;
                    154:        if(globals_inited)
                    155:                return;
                    156:        globals_inited=true;
                    157: 
                    158:        static Pool pool; // global pool
                    159:        PTRY {
                    160:                // init global variables
                    161:                globals_init(pool);
                    162:                
                    163:                //...
                    164:        } PCATCH(e) { // global problem 
                    165:                const char *body=e.comment();
                    166:                // TODO: somehow report that error
                    167:        }
                    168:        PEND_CATCH
                    169: }
                    170: 
                    171: /// ISAPI //
                    172: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
                    173:        pVer->dwExtensionVersion = HSE_VERSION;
                    174:        strncpy(pVer->lpszExtensionDesc, "Parser " PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN);
                    175:        return TRUE;
                    176: }
                    177: 
1.6     ! paf       178: /** 
        !           179:        ISAPI // main workhorse
        !           180: 
        !           181:        @todo 
        !           182:                think of a better way than @c APPL_PHYSICAL_PATH
        !           183:                of obtaining the @c DOCUMENT_ROOT
        !           184:                because this only gets "the place where last IIS Application was set"
        !           185:                and if someone would redefine Application settings below the /
        !           186:                all ^table:load[/test] would open not /test but /below/test
        !           187: */
1.1       paf       188: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
                    189:        Pool pool;
                    190:        // TODO r->no_cache=1;
1.2       paf       191:        
1.1       paf       192:        bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
                    193:        PTRY { // global try
                    194:                // must be first in PTRY{}PCATCH
1.2       paf       195:                
1.3       paf       196:                Service_func_context ctx={
                    197:                        lpECB,
                    198:                        new(pool) String(pool),
                    199:                        200
                    200:                };
                    201:                pool.set_context(&ctx);
                    202:                
1.2       paf       203:                // Request info
                    204:                Request::Info request_info;
                    205:                
1.6     ! paf       206:                char document_root_buf[MAX_STRING];
        !           207:                DWORD document_root_len;
        !           208:                if (lpECB->GetServerVariable(lpECB->ConnID, "APPL_PHYSICAL_PATH", 
        !           209:                        document_root_buf, &document_root_len)) {
        !           210:                        document_root_buf[document_root_len]=0;
        !           211:                        request_info.document_root=document_root_buf;
        !           212:                } else
        !           213:                        PTHROW(0, 0,
        !           214:                                0,
        !           215:                                "no server variable APPL_PHYSICAL_PATH (error #%lu)",
        !           216:                                        GetLastError()); // never
        !           217: 
        !           218:                request_info.path_translated=lpECB->lpszPathTranslated;
1.2       paf       219:                request_info.method=lpECB->lpszMethod;
                    220:                request_info.query_string=lpECB->lpszQueryString;
                    221:                char reconstructed_uri[MAX_STRING];
                    222:                if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
                    223:                        strncpy(reconstructed_uri, lpECB->lpszPathInfo, 
                    224:                                MAX_STRING-1/*'?'*/-strlen(lpECB->lpszQueryString));
                    225:                        strcat(reconstructed_uri, "?");
                    226:                        strcat(reconstructed_uri, lpECB->lpszPathInfo);
                    227:                        request_info.uri=reconstructed_uri;
                    228:                } else
                    229:                        request_info.uri=lpECB->lpszPathInfo;
                    230:                
                    231:                request_info.content_type=lpECB->lpszContentType;
                    232:                request_info.content_length=lpECB->cbTotalBytes;
                    233:                // cookie
1.3       paf       234:                request_info.cookie=0;
1.2       paf       235:                char cookie_buf[MAX_STRING];
                    236:                {
                    237:                        DWORD cookie_len = MAX_STRING-1;
                    238:                        
                    239:                        if (lpECB->GetServerVariable(lpECB->ConnID, "HTTP_COOKIE", cookie_buf, &cookie_len)) {
                    240:                                cookie_buf[cookie_len]=0;
                    241:                                request_info.cookie=cookie_buf;
                    242:                        } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
                    243:                                char *tmp_cookie_buf=(char *)pool.malloc(cookie_len+1);
1.1       paf       244:                                
1.2       paf       245:                                if (lpECB->GetServerVariable(lpECB->ConnID, "HTTP_COOKIE", tmp_cookie_buf, &cookie_len)) {
                    246:                                        tmp_cookie_buf[cookie_len]=0;
                    247:                                        request_info.cookie=tmp_cookie_buf;
1.1       paf       248:                                }
                    249:                        }
                    250:                }
1.2       paf       251:                
                    252:                // prepare to process request
                    253:                Request request(pool,
                    254:                        request_info,
                    255:                        String::UL_HTML_TYPO
                    256:                        );
                    257:                
                    258:                // some root-controlled location
                    259:                char *root_auto_path;
                    260:                // c:\windows
                    261:                root_auto_path=(char *)pool.malloc(MAX_STRING);
                    262:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.4       paf       263: 
1.2       paf       264:                // process the request
                    265:                request.core(
                    266:                        root_auto_path, false/*may be abcent*/, // /path/to/admin/auto.p
                    267:                        0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
                    268:                        header_only);
                    269:                
                    270:                // successful finish
1.1       paf       271:        } PCATCH(e) { // global problem 
                    272:                const char *body=e.comment();
                    273:                int content_length=strlen(body);
                    274: 
                    275:                // prepare header
                    276:                (*service_funcs.add_header_attribute)(pool, "content-type", "text/plain");
                    277:                char content_length_cstr[MAX_NUMBER];
1.6     ! paf       278:                snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.1       paf       279:                (*service_funcs.add_header_attribute)(pool, "content-length", 
                    280:                        content_length_cstr);
                    281: 
                    282:                // send header
                    283:                (*service_funcs.send_header)(pool);
                    284: 
                    285:                // send body
                    286:                if(!header_only)
                    287:                        (*service_funcs.send_body)(pool, body, content_length);
                    288: 
                    289:                // unsuccessful finish
                    290:                _endthread();
                    291:        }
                    292:        PEND_CATCH
                    293:        
1.6     ! paf       294:        return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1       paf       295: }
                    296: 
                    297: 
                    298: BOOL APIENTRY DllMain(HANDLE hModule, 
                    299:                                          DWORD  ul_reason_for_call, 
                    300:                                          LPVOID lpReserved
                    301:                                          ) {
                    302:     switch (ul_reason_for_call) {
                    303:                case DLL_PROCESS_ATTACH:
                    304:                        parser_init();
                    305:                        break;
                    306:                case DLL_THREAD_ATTACH:
                    307:                        break;
                    308:                case DLL_THREAD_DETACH:
                    309:                        break;
                    310:                case DLL_PROCESS_DETACH:
                    311:                        break;
                    312:     }
                    313:     return TRUE;
                    314: }

E-mail: