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

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.15    ! 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.15    ! 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.15    ! 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.15    ! 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.15    ! 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.15    ! 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: 
1.15    ! paf       143: static bool parser_init() {
1.1       paf       144:        static bool globals_inited=false;
                    145:        if(globals_inited)
1.15    ! paf       146:                return true;
1.1       paf       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:                
1.15    ! paf       154:                // successful finish
        !           155:                return true;
1.1       paf       156:        } PCATCH(e) { // global problem 
1.15    ! paf       157:                //const char *body=e.comment();
        !           158:                
        !           159:                // unsuccessful finish
        !           160:                return false;
1.1       paf       161:        }
                    162:        PEND_CATCH
                    163: }
                    164: 
                    165: /// ISAPI //
                    166: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
                    167:        pVer->dwExtensionVersion = HSE_VERSION;
1.15    ! paf       168:        strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN);
        !           169:        return parser_init();
1.1       paf       170: }
                    171: 
1.6       paf       172: /** 
                    173:        ISAPI // main workhorse
                    174: 
                    175:        @todo 
1.10      paf       176:                IIS: remove trailing default-document[index.html] from $request.uri.
                    177:                to do that we need to consult metabase,
1.12      paf       178:                wich is tested&works but seems slow runtime 
                    179:                and not could-be-quickly-implemented if prepared.
1.6       paf       180: */
1.1       paf       181: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13      paf       182:        Pool_storage pool_storage;
1.15    ! paf       183:        Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
        !           184:        SAPI_func_context ctx={
        !           185:                lpECB,
        !           186:                        0, // filling later: so that if there would be error pool would have ctx
        !           187:                        200
        !           188:        };
        !           189:        pool.set_context(&ctx);// no allocations before this line!
1.2       paf       190:        
1.1       paf       191:        bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
                    192:        PTRY { // global try
1.12      paf       193:                ctx.header=new(pool) String(pool);
1.3       paf       194:                
1.2       paf       195:                // Request info
                    196:                Request::Info request_info;
1.14      paf       197: 
                    198:                size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
                    199:                char *filespec_to_process=(char *)malloc(path_translated_buf_size);
                    200:                memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
                    201: #ifdef WIN32
                    202:                back_slashes_to_slashes(filespec_to_process);
                    203: #endif
                    204: 
1.11      paf       205:                if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    206:                        // IIS
                    207:                        size_t len=strlen(filespec_to_process)-strlen(path_info);
                    208:                        char *buf=(char *)pool.malloc(len+1);
                    209:                        strncpy(buf, filespec_to_process, len);
                    210:                        buf[len]=0;
                    211:                        request_info.document_root=buf;
                    212:                } else
1.6       paf       213:                        PTHROW(0, 0,
                    214:                                0,
1.11      paf       215:                                "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
1.6       paf       216: 
1.11      paf       217:                request_info.path_translated=filespec_to_process;
1.2       paf       218:                request_info.method=lpECB->lpszMethod;
                    219:                request_info.query_string=lpECB->lpszQueryString;
                    220:                if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.7       paf       221:                        char *reconstructed_uri=(char *)malloc(
                    222:                                strlen(lpECB->lpszPathInfo)+1/*'?'*/+
                    223:                                strlen(lpECB->lpszQueryString)+1/*0*/);
                    224:                        strcpy(reconstructed_uri, lpECB->lpszPathInfo);
1.2       paf       225:                        strcat(reconstructed_uri, "?");
1.7       paf       226:                        strcat(reconstructed_uri, lpECB->lpszQueryString);
1.2       paf       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;
1.9       paf       233:                request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
1.2       paf       234:                
                    235:                // prepare to process request
                    236:                Request request(pool,
                    237:                        request_info,
                    238:                        String::UL_HTML_TYPO
                    239:                        );
1.15    ! paf       240: 
1.2       paf       241:                // some root-controlled location
1.7       paf       242:                //   c:\windows
                    243:                // must be dynamic: rethrowing from request.core 
                    244:                //   may return 'source' which can be inside of 'root auto.p@exeception'
                    245:                char *root_auto_path=(char *)pool.malloc(MAX_STRING);
1.2       paf       246:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.4       paf       247: 
1.2       paf       248:                // process the request
                    249:                request.core(
                    250:                        root_auto_path, false/*may be abcent*/, // /path/to/admin/auto.p
                    251:                        0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
                    252:                        header_only);
                    253:                // successful finish
1.12      paf       254:        } PCATCH(e) { // global problem
                    255:                // don't allocate anything on pool here:
                    256:                //   possible pool' exception not catch-ed now
                    257:                //   and there could be out-of-memory exception
1.1       paf       258:                const char *body=e.comment();
                    259:                int content_length=strlen(body);
                    260: 
1.12      paf       261:                // prepare header // not using SAPI func wich allocates on pool
                    262:                char header_buf[MAX_STRING];
                    263:                int header_len=snprintf(header_buf, MAX_STRING,
                    264:                        "content-type: text/plain\n"
                    265:                        "content-length: %ul\n"
                    266:                        "expires: Fri, 23 Mar 2001 09:32:23 GMT\n"
                    267:                        "\n",
                    268:                        content_length);
                    269: 
                    270:                HSE_SEND_HEADER_EX_INFO header_info;
                    271:                header_info.pszStatus="200 OK";
                    272:                header_info.cchStatus=strlen(header_info.pszStatus);
                    273:                header_info.pszHeader=header_buf;
                    274:                header_info.cchHeader=header_len;
                    275:                header_info.fKeepConn=true;
                    276:                
1.1       paf       277:                // send header
1.12      paf       278:                lpECB->dwHttpStatusCode=200;
                    279:                lpECB->ServerSupportFunction(lpECB->ConnID, 
                    280:                        HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1       paf       281: 
                    282:                // send body
                    283:                if(!header_only)
1.9       paf       284:                        SAPI::send_body(pool, body, content_length);
1.1       paf       285: 
                    286:                // unsuccessful finish
                    287:        }
                    288:        PEND_CATCH
                    289:        
1.6       paf       290:        return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1       paf       291: }
                    292: 
1.15    ! paf       293: /*
1.1       paf       294: BOOL APIENTRY DllMain(HANDLE hModule, 
                    295:                                          DWORD  ul_reason_for_call, 
                    296:                                          LPVOID lpReserved
                    297:                                          ) {
                    298:     switch (ul_reason_for_call) {
                    299:                case DLL_PROCESS_ATTACH:
                    300:                        break;
                    301:                case DLL_THREAD_ATTACH:
                    302:                        break;
                    303:                case DLL_THREAD_DETACH:
                    304:                        break;
                    305:                case DLL_PROCESS_DETACH:
                    306:                        break;
                    307:     }
                    308:     return TRUE;
                    309: }
1.15    ! paf       310: */

E-mail: