home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / source / chap14 / lst14_03 / lst14_03.cpp next >
Encoding:
C/C++ Source or Header  |  1996-11-19  |  3.7 KB  |  169 lines

  1. // LST14_03.CPP - Implementation file for your Internet Server
  2. //    lst14_03 Extension
  3.  
  4. #include <afx.h>
  5. #include <afxwin.h>
  6. #include <afxisapi.h>
  7. #include "resource.h"
  8. #include "lst14_03.h"
  9.  
  10.  
  11.  
  12. ///////////////////////////////////////////////////////////////////////
  13. // The one and only CLst14_03Extension object
  14.  
  15. CLst14_03Extension theExtension;
  16. CWinApp BugFix;
  17.  
  18. ///////////////////////////////////////////////////////////////////////
  19. // CLst14_03Extension implementation
  20.  
  21. CLst14_03Extension::CLst14_03Extension()
  22. {
  23.     m_pnCounter = new UINT;
  24.     (*m_pnCounter) = 0;
  25.  
  26.     m_lpCriticalExtension = new CRITICAL_SECTION;
  27.     InitializeCriticalSection(m_lpCriticalExtension);
  28. }
  29.  
  30. CLst14_03Extension::~CLst14_03Extension()
  31. {
  32.     delete m_pnCounter;
  33.  
  34.     DeleteCriticalSection(m_lpCriticalExtension);
  35.     delete m_lpCriticalExtension;
  36. }
  37.  
  38. BOOL CLst14_03Extension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
  39. {
  40.     // Call default implementation for initialization
  41.     CHttpServer::GetExtensionVersion(pVer);
  42.  
  43.     // Load description string
  44.     TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
  45.     ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
  46.             IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
  47.     _tcscpy(pVer->lpszExtensionDesc, sz);
  48.     return TRUE;
  49. }
  50.  
  51. int CLst14_03Extension::CallFunction(CHttpServerContext* pCtxt,LPTSTR pszQuery, LPTSTR pszCommand)
  52. {
  53.     int nRet=callOK;
  54.  
  55.     ISAPIASSERT(pCtxt->m_pStream == NULL);
  56.     pCtxt->m_pStream = ConstructStream();
  57.     if (pCtxt->m_pStream == NULL)
  58.         nRet = callNoStream;
  59.     else
  60.     {
  61.         pCtxt->m_pStream->InitStream();
  62.         Enterance(pCtxt,pszQuery);        
  63.     }
  64.  
  65.     return(nRet);
  66. }
  67.  
  68. void  CLst14_03Extension::Enterance(CHttpServerContext* pCtxt,LPTSTR pszQuery)
  69. {
  70.     ThreadPool();
  71.     (*pCtxt) << _T("<HTML><BODY>Name: "); 
  72.  
  73.     LPTSTR lpszValue;
  74.     if (lpszValue=FindNameValue(pszQuery,_T("Name"),_T('='),_T('&')))
  75.     {
  76.         (*pCtxt) << lpszValue;
  77.         delete lpszValue;
  78.     }
  79.  
  80.     (*pCtxt) << _T("</BODY</HTML>");
  81.  
  82.     LeavePool();
  83. };
  84.  
  85. void CLst14_03Extension::ThreadPool()
  86. {
  87.     BOOL bGoodToGo=FALSE;
  88.  
  89.     while (!bGoodToGo)
  90.     {
  91.         EnterCriticalSection(m_lpCriticalExtension);
  92.  
  93.         bGoodToGo=((*m_pnCounter)<15);
  94.  
  95.         if(bGoodToGo)
  96.             (*m_pnCounter)++;
  97.  
  98.          LeaveCriticalSection(m_lpCriticalExtension);
  99.  
  100.     }
  101.  
  102. }
  103.  
  104. void CLst14_03Extension::LeavePool()
  105. {
  106.     EnterCriticalSection(m_lpCriticalExtension);
  107.     (*m_pnCounter)--;
  108.      LeaveCriticalSection(m_lpCriticalExtension);
  109. };
  110.  
  111. // The Caller must delete the memory
  112. LPTSTR CLst14_03Extension::FindNameValue(LPCTSTR lpszString,LPCTSTR lpszName,TCHAR cSeparator,TCHAR cDelimiter)
  113. {
  114.     LPTSTR    lpszIndex;
  115.     LPTSTR    lpszEnd;
  116.     DWORD    dwValueSize=0;
  117.        LPTSTR    lpszValue;
  118.  
  119.     LPTSTR     lpszStringCopy;
  120.       LPTSTR     lpszNameCopy;
  121.     DWORD     dwNameLength;
  122.  
  123.     lpszStringCopy = new TCHAR[_tcslen(lpszString)+1];
  124.     _tcscpy(lpszStringCopy,lpszString);
  125.  
  126.        lpszNameCopy = new TCHAR[_tcslen(lpszName)+2];
  127.     _tcscpy(lpszNameCopy,lpszName);
  128.     dwNameLength=_tcslen(lpszNameCopy);
  129.  
  130.      lpszNameCopy[dwNameLength]=cSeparator;
  131.     lpszNameCopy[dwNameLength+1]=(TCHAR)_T('\0');
  132.  
  133.     // Find The Name in the Query String
  134.     lpszIndex=_tcsstr(lpszStringCopy,lpszNameCopy);
  135.  
  136.     delete lpszNameCopy;
  137.     
  138.     // Error: The Name part of the Name value pair doesn't exist
  139.     if (!lpszIndex)
  140.     {
  141.         delete lpszStringCopy;
  142.         return (NULL);
  143.     }
  144.  
  145.     // Increase the pointer passed the Name and Get to the Value
  146.     lpszIndex+=_tcslen(lpszName)+1;
  147.  
  148.     // Find the End of the Value by looking for the Demiliter
  149.     lpszEnd=_tcschr(lpszIndex,_T(cDelimiter));
  150.  
  151.     // if we find a Demiliter set it as the end
  152.     if (lpszEnd)
  153.         (*lpszEnd)='\0';
  154.  
  155.     // Remove the CGI Syntax
  156.     PreprocessString(lpszIndex);
  157.  
  158.     // Calculate the Value Size
  159.     dwValueSize=_tcslen(lpszIndex);
  160.  
  161.     lpszValue = new TCHAR[dwValueSize+1];
  162.  
  163.     _tcscpy(lpszValue,lpszIndex);
  164.  
  165.     delete lpszStringCopy;
  166.  
  167.     return lpszValue;
  168. }
  169.