home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / notify.cpp < prev    next >
C/C++ Source or Header  |  1997-10-25  |  5KB  |  210 lines

  1. #include "CkyPch.h"
  2. #include "debug.h"
  3. #include "utils.h"
  4. #include "notify.h"
  5.  
  6.  
  7. //
  8. // ctor
  9. //
  10.  
  11. CNotification::CNotification(
  12.     LPCSTR pszCookie)
  13.     : m_nState(HN_UNDEFINED),
  14.       m_ct(CT_UNDEFINED),
  15.       m_pszUrl(NULL),
  16.       m_pbPartialToken(NULL),
  17.       m_cbPartialToken(0),
  18.       m_pbTokenBuffer(NULL),
  19.       m_cbTokenBuffer(0),
  20.       m_cchContentLength(UINT_MAX), // not specified for .ASPs
  21.       m_fTestCookies(false)
  22. {
  23.     // set the eat cookies and test cookies based on the munge mode.
  24.     // Here's how the flags will affect output of this session
  25.  
  26.     // if EatCookies is true, cookies will be stripped and URLs will
  27.     // be munged with the cookie
  28.     // if TestCookies is true URLs will be munged
  29.     switch ( g_mungeMode )
  30.     {
  31.         case MungeMode_Off:
  32.         {
  33.             m_fEatCookies = false;
  34.         } break;
  35.  
  36.         case MungeMode_On:
  37.         {
  38.             m_fEatCookies = true;
  39.         } break;
  40.  
  41.         // always default to smart
  42.         default:
  43.         {
  44.             m_fEatCookies = false;
  45.             m_fTestCookies = true;
  46.         } break;
  47.     }
  48.  
  49.     *m_szSessionID = '\0';
  50.  
  51.     if (pszCookie != NULL)
  52.     {
  53.         if (!Cookie2SessionID(pszCookie, m_szSessionID))
  54.         {
  55.             TRACE("CNotification(%s): ", pszCookie);
  56.             CopySessionID(pszCookie, m_szSessionID);
  57.         }
  58.     }
  59.  
  60.     TRACE("CNotification(%s)\n", (*m_szSessionID ? m_szSessionID : "<none>"));
  61. }
  62.  
  63.  
  64.  
  65. //
  66. // dtor
  67. //
  68.  
  69. CNotification::~CNotification()
  70. {
  71.     TRACE("~CNotification(%s)\n", m_szSessionID);
  72. }
  73.  
  74.  
  75.  
  76. //
  77. // Create a CNotification for a Filter Context
  78. //
  79.  
  80. CNotification*
  81. CNotification::Create(
  82.     PHTTP_FILTER_CONTEXT pfc,
  83.     LPCSTR               pszCookie)
  84. {
  85.     ASSERT(pfc->pFilterContext == NULL);
  86.     
  87.     TRACE("Notify: ");
  88.  
  89.     // placement new, using ISAPI's fast allocator
  90.     LPBYTE pbNotify =
  91.         static_cast<LPBYTE>(AllocMem(pfc, sizeof(CNotification)));
  92.     CNotification* pNotify = new (pbNotify) CNotification(pszCookie);
  93.  
  94.     pfc->pFilterContext = static_cast<PVOID>(pNotify);
  95.  
  96.     return pNotify;
  97. }
  98.  
  99.  
  100.  
  101. //
  102. // Cleanup
  103. //
  104.  
  105. void
  106. CNotification::Destroy(
  107.     PHTTP_FILTER_CONTEXT pfc)
  108. {
  109.     CNotification* pNotify = Get(pfc);
  110.  
  111.     if (pNotify != NULL)
  112.         pNotify->~CNotification(); // placement destruction
  113.  
  114.     pfc->pFilterContext = NULL;
  115. }
  116.  
  117.  
  118.  
  119. //
  120. // Set the filter context's session ID, creating a CNotification if necessary
  121. //
  122.  
  123. CNotification*
  124. CNotification::SetSessionID(
  125.     PHTTP_FILTER_CONTEXT pfc,
  126.     LPCSTR               pszCookie)
  127. {
  128.     CNotification* pNotify = Get(pfc);
  129.  
  130.     if (pNotify != NULL)
  131.     {
  132.         if (!Cookie2SessionID(pszCookie, pNotify->m_szSessionID))
  133.         {
  134.             TRACE("SetSessionID(%s): ", pszCookie);
  135.             CopySessionID(pszCookie, pNotify->m_szSessionID);
  136.         }
  137.     }
  138.     else
  139.         pNotify = Create(pfc, pszCookie);
  140.  
  141.     return pNotify;
  142. }
  143.  
  144.  
  145.  
  146. //
  147. // Sometimes the data in OnSendRawData ends in a partial token.  We need
  148. // to buffer that partial token for the next call to OnSendRawData.  In
  149. // some cases, it may take several successive calls to OnSendRawData to
  150. // accumulate a complete token.  This routine builds the partial token,
  151. // taking care of memory (re)allocation.  There's always SPARE_BYTES bytes
  152. // unused at the end that callers (esp. OnEndOfRequest) can write into.
  153. //
  154.  
  155. void
  156. CNotification::AppendToken(
  157.     PHTTP_FILTER_CONTEXT pfc,
  158.     LPCSTR               pszNewData,
  159.     int                  cchNewData)
  160. {
  161.     ASSERT(pszNewData != NULL  &&  cchNewData > 0);
  162.  
  163.     // if there's room, append new data to the currently allocated buffer
  164.     if (m_cbPartialToken + cchNewData <= m_cbTokenBuffer - SPARE_BYTES)
  165.     {
  166.         m_pbPartialToken = m_pbTokenBuffer;
  167.         memcpy(m_pbPartialToken + m_cbPartialToken, pszNewData, cchNewData);
  168.         m_cbPartialToken += cchNewData;
  169. #ifdef _DEBUG
  170.         m_pbPartialToken[m_cbPartialToken] = '\0';
  171. #endif
  172.         return;
  173.     }
  174.     
  175.     // We want to allocate some extra space so that we have room to
  176.     // grow for a while before needing to allocate more space.  If we
  177.     // allocated only exactly enough, we'd see O(n^2) memory usage in
  178.     // degenerate cases because AllocMem doesn't return memory to its
  179.     // pool until the transaction is over.
  180.     DWORD cb2 = max(1024, 2 * (cchNewData + m_cbTokenBuffer) + SPARE_BYTES);
  181.  
  182.     if (cb2 > 10000)
  183.         cb2 = 3 * (cchNewData + m_cbTokenBuffer) / 2 + SPARE_BYTES;
  184.  
  185.     m_cbTokenBuffer = cb2;
  186.     LPBYTE pb  = (LPBYTE) AllocMem(pfc, m_cbTokenBuffer);
  187.     LPBYTE pb2 = pb;
  188.  
  189.     // Already have a partial token buffer?  Copy contents, if so.
  190.     if (m_cbPartialToken > 0)
  191.     {
  192.         ASSERT(m_pbPartialToken != NULL
  193.                &&  m_pbPartialToken == m_pbTokenBuffer);
  194.         memcpy(pb, m_pbPartialToken, m_cbPartialToken);
  195.         pb2 += m_cbPartialToken;
  196.     }
  197.     else
  198.         ASSERT(m_pbPartialToken == NULL);
  199.  
  200.     memcpy(pb2, pszNewData, cchNewData);
  201.     m_pbPartialToken = m_pbTokenBuffer = pb;
  202.     m_cbPartialToken += cchNewData;
  203.  
  204. #ifdef _DEBUG
  205.     m_pbPartialToken[m_cbPartialToken] = '\0';
  206. #endif
  207.  
  208.     ASSERT(m_cbPartialToken <= m_cbTokenBuffer - SPARE_BYTES);
  209. }
  210.