Diff for /parser3/src/targets/isapi/parser3isapi.C between versions 1.14 and 1.35

version 1.14, 2001/03/24 09:24:46 version 1.35, 2001/06/28 07:44:17
Line 1 Line 1
   /** @file
           Parser: IIS extension.
   
           Copyright (c) 2000,2001 ArtLebedev Group (http://www.artlebedev.com)
   
           Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
   */
   static const char *RCSId="$Id$"; 
   
 #ifndef _MSC_VER  #ifndef _MSC_VER
 #       error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)]  #       error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)]
 #endif  #endif
Line 12 Line 21
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_version.h"  #include "pa_version.h"
 #include "pool_storage.h"  #include "pool_storage.h"
   #include "pa_socks.h"
   
 #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")  #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
   
 //@{  // SAPI
 /// SAPI funcs decl  
 struct sapi_func_context {  /** 
           ISAPI SAPI functions receive this context information. 
   
           @see Pool::set_context
   */
   struct SAPI_func_context {
         LPEXTENSION_CONTROL_BLOCK lpECB;          LPEXTENSION_CONTROL_BLOCK lpECB;
         String *header;          String *header;
         DWORD http_response_code;          DWORD http_response_code;
 };  };
   
   // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
   void SAPI::log(Pool& pool, const char *fmt, ...) {
           SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
           
           va_list args;
           va_start(args,fmt);
           char buf[MAX_STRING];
           const char *prefix="PARSER_ERROR:";
           strcpy(buf, prefix);
           DWORD size=vsnprintf(buf+strlen(prefix), MAX_STRING-strlen(prefix), fmt, args);
           
           ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID, 
                   HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
   }
   
 const char *SAPI::get_env(Pool& pool, const char *name) {  const char *SAPI::get_env(Pool& pool, const char *name) {
         sapi_func_context& ctx=*static_cast<sapi_func_context *>(pool.context());          SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
   
         char *variable_buf=(char *)pool.malloc(MAX_STRING);          char *variable_buf=(char *)pool.malloc(MAX_STRING);
         DWORD variable_len = MAX_STRING-1;          DWORD variable_len = MAX_STRING-1;
Line 46  const char *SAPI::get_env(Pool& pool, co Line 76  const char *SAPI::get_env(Pool& pool, co
         return 0;          return 0;
 }  }
   
 uint SAPI::read_post(Pool& pool, char *buf, uint max_bytes) {  size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
         sapi_func_context& ctx=*static_cast<sapi_func_context *>(pool.context());          SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
   
         DWORD read_from_buf=0;          DWORD read_from_buf=0;
         DWORD read_from_input=0;          DWORD read_from_input=0;
Line 77  uint SAPI::read_post(Pool& pool, char *b Line 107  uint SAPI::read_post(Pool& pool, char *b
 }  }
   
 void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {  void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
         sapi_func_context& ctx=*static_cast<sapi_func_context *>(pool.context());          SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
   
         if(strcasecmp(key, "location")==0)           if(strcasecmp(key, "location")==0) 
                 ctx.http_response_code=302;                  ctx.http_response_code=302;
Line 88  void SAPI::add_header_attribute(Pool& po Line 118  void SAPI::add_header_attribute(Pool& po
                 ctx.header->APPEND_CONST(key);                  ctx.header->APPEND_CONST(key);
                 ctx.header->APPEND_CONST(": ");                  ctx.header->APPEND_CONST(": ");
                 ctx.header->APPEND_CONST(value);                  ctx.header->APPEND_CONST(value);
                 ctx.header->APPEND_CONST("\n");                  ctx.header->APPEND_CONST("\r\n");
         }          }
 }  }
   
 /// @todo intelligent cache-control  /// @todo intelligent cache-control
 void SAPI::send_header(Pool& pool) {  void SAPI::send_header(Pool& pool) {
         sapi_func_context& ctx=*static_cast<sapi_func_context *>(pool.context());          SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
   
         ctx.header->APPEND_CONST(          ctx.header->APPEND_CONST(
                 "expires: Fri, 23 Mar 2001 09:32:23 GMT\n"                  "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                 "\n");                  "\r\n");
         HSE_SEND_HEADER_EX_INFO header_info;          HSE_SEND_HEADER_EX_INFO header_info;
   
         char status_buf[MAX_STATUS_LENGTH];          char status_buf[MAX_STATUS_LENGTH];
Line 129  void SAPI::send_header(Pool& pool) { Line 159  void SAPI::send_header(Pool& pool) {
                 HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);                  HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
 }  }
   
 void SAPI::send_body(Pool& pool, const char *buf, size_t size) {  void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
         sapi_func_context& ctx=*static_cast<sapi_func_context *>(pool.context());          SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
   
         DWORD num_bytes=size;          DWORD num_bytes=size;
         ctx.lpECB->WriteClient(ctx.lpECB->ConnID,           ctx.lpECB->WriteClient(ctx.lpECB->ConnID, 
                 const_cast<char *>(buf), &num_bytes, HSE_IO_SYNC);                  const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
 }  }
 //@}  
   
 //   // 
   
 static void parser_init() {  static bool parser_init() {
         static bool globals_inited=false;          static bool globals_inited=false;
         if(globals_inited)          if(globals_inited)
                 return;                  return true;
         globals_inited=true;          globals_inited=true;
   
         static Pool pool(0); // global pool          static Pool pool(0); // global pool
         PTRY {          PTRY {
                   // init socks
                   init_socks(pool);
   
                   // init global classes
                   init_methoded_array(pool);
                 // init global variables                  // init global variables
                 pa_globals_init(pool);                  pa_globals_init(pool);
                                   
                 //...                  // successful finish
                   return true;
         } PCATCH(e) { // global problem           } PCATCH(e) { // global problem 
                 const char *body=e.comment();                  //const char *body=e.comment();
                 // TODO: somehow report that error                  
                   // unsuccessful finish
                   return false;
         }          }
         PEND_CATCH          PEND_CATCH
 }  }
Line 162  static void parser_init() { Line 199  static void parser_init() {
 /// ISAPI //  /// ISAPI //
 BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {  BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
         pVer->dwExtensionVersion = HSE_VERSION;          pVer->dwExtensionVersion = HSE_VERSION;
         strncpy(pVer->lpszExtensionDesc, "Parser " PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN);          strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN);
         return TRUE;          return parser_init();
 }  }
   
 /**   /** 
Line 177  BOOL WINAPI GetExtensionVersion(HSE_VERS Line 214  BOOL WINAPI GetExtensionVersion(HSE_VERS
 */  */
 DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {  DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
         Pool_storage pool_storage;          Pool_storage pool_storage;
         Pool pool(&pool_storage);          Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
           SAPI_func_context ctx={
                   lpECB,
                   0, // filling later: so that if there would be error pool would have ctx
                   200
           };
           pool.set_context(&ctx);// no allocations before this line!
                   
         bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;          bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
         PTRY { // global try          PTRY { // global try
                 sapi_func_context ctx={  
                         lpECB,  
                         0, // filling later: so that if there would be error pool would have ctx  
                         200  
                 };  
                 pool.set_context(&ctx);  
                 ctx.header=new(pool) String(pool);                  ctx.header=new(pool) String(pool);
                                   
                 // Request info                  // Request info
Line 228  DWORD WINAPI HttpExtensionProc(LPEXTENSI Line 265  DWORD WINAPI HttpExtensionProc(LPEXTENSI
                 request_info.content_type=lpECB->lpszContentType;                  request_info.content_type=lpECB->lpszContentType;
                 request_info.content_length=lpECB->cbTotalBytes;                  request_info.content_length=lpECB->cbTotalBytes;
                 request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");                  request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                   request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
   
                                   
                 // prepare to process request                  // prepare to process request
                 Request request(pool,                  Request request(pool,
                         request_info,                          request_info,
                         String::UL_HTML_TYPO                          String::UL_USER_HTML
                         );                          );
                   
                 // some root-controlled location                  // some root-controlled location
                 //   c:\windows                  //   c:\windows
                 // must be dynamic: rethrowing from request.core                   // must be dynamic: rethrowing from request.core 
Line 251  DWORD WINAPI HttpExtensionProc(LPEXTENSI Line 290  DWORD WINAPI HttpExtensionProc(LPEXTENSI
         } PCATCH(e) { // global problem          } PCATCH(e) { // global problem
                 // don't allocate anything on pool here:                  // don't allocate anything on pool here:
                 //   possible pool' exception not catch-ed now                  //   possible pool' exception not catch-ed now
                 //   and there could be out-of-memory exception                          //   and there could be out-of-memory exception
                 const char *body=e.comment();                  const char *body=e.comment();
                   // log it
                   SAPI::log(pool, "exception in request exception handler: %s", body);
   
                   //
                 int content_length=strlen(body);                  int content_length=strlen(body);
   
                 // prepare header // not using SAPI func wich allocates on pool                  // prepare header // not using SAPI func wich allocates on pool
                 char header_buf[MAX_STRING];                  char header_buf[MAX_STRING];
                 int header_len=snprintf(header_buf, MAX_STRING,                  int header_len=snprintf(header_buf, MAX_STRING,
                         "content-type: text/plain\n"                          "content-type: text/plain\r\n"
                         "content-length: %ul\n"                          "content-length: %lu\r\n"
                         "expires: Fri, 23 Mar 2001 09:32:23 GMT\n"                          "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
                         "\n",                          "\r\n",
                         content_length);                          content_length);
   
                 HSE_SEND_HEADER_EX_INFO header_info;                  HSE_SEND_HEADER_EX_INFO header_info;
Line 286  DWORD WINAPI HttpExtensionProc(LPEXTENSI Line 329  DWORD WINAPI HttpExtensionProc(LPEXTENSI
                   
         return HSE_STATUS_SUCCESS_AND_KEEP_CONN;          return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
 }  }
   
   
 BOOL APIENTRY DllMain(HANDLE hModule,   
                                           DWORD  ul_reason_for_call,   
                                           LPVOID lpReserved  
                                           ) {  
     switch (ul_reason_for_call) {  
                 case DLL_PROCESS_ATTACH:  
                         parser_init();  
                         break;  
                 case DLL_THREAD_ATTACH:  
                         break;  
                 case DLL_THREAD_DETACH:  
                         break;  
                 case DLL_PROCESS_DETACH:  
                         break;  
     }  
     return TRUE;  
 }  

Removed from v.1.14  
changed lines
  Added in v.1.35


E-mail: