home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / internet / mfcucase / mfcucase.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.1 KB  |  202 lines

  1. // mfcucase.cpp : implements the filter class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1997-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. // Note that your filters probably won't have an #ifdef like this one.
  14. // The project file for this sample allows you to build the sample as
  15. // a statically linked regular MFC DLL (that is, with _AFXDLL defined)
  16. // or as a DLL that doesn't use MFC classes aside from the ISAPI
  17. // support classes (that is, without _AFXDLL defined).
  18.  
  19. #ifdef _AFXDLL
  20. #include <afx.h>
  21. #include <afxwin.h>
  22. #endif
  23.  
  24. #include <afxisapi.h>
  25. #include "mfcucase.h"
  26. #include "resource.h"
  27.  
  28.  
  29. ///////////////////////////////////////////////////////////////////////
  30. // The one and only CUpCaseFilter object
  31.  
  32. CUpCaseFilter theFilter;
  33.  
  34.  
  35. ///////////////////////////////////////////////////////////////////////
  36. // CUpCaseFilter implementation
  37.  
  38. CUpCaseFilter::CUpCaseFilter()
  39. {
  40. }
  41.  
  42. CUpCaseFilter::~CUpCaseFilter()
  43. {
  44. }
  45.  
  46.  
  47. ///////////////////////////////////////////////////////////////////////
  48. // CUpCaseFilter object
  49.  
  50. BOOL CUpCaseFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
  51. {
  52.     // call default implmentation to initialize
  53.     CHttpFilter::GetFilterVersion(pVer);
  54.  
  55.     // set flags that interest us
  56.     pVer->dwFlags |=
  57.         (SF_NOTIFY_NONSECURE_PORT |
  58.          SF_NOTIFY_URL_MAP | SF_NOTIFY_SEND_RAW_DATA);
  59.  
  60.     // load description string
  61.     TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
  62.     ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
  63.         IDS_MFCUCASE_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
  64.     _tcscpy(pVer->lpszFilterDesc, sz);
  65.     return TRUE;
  66. }
  67.  
  68. DWORD CUpCaseFilter::OnSendRawData(CHttpFilterContext* pCtxt,
  69.     PHTTP_FILTER_RAW_DATA pRawData)
  70. {
  71.     LPTSTR  pstrIn;
  72.     DWORD   cbBuffer;
  73.     DWORD   cbTemp;
  74.  
  75.     // if we previously identified the data requested as being
  76.     // interesting, let's try to parse it
  77.  
  78.     if (pCtxt->m_pFC->pFilterContext != NULL)
  79.     {
  80.         // gain a pointer to the actual data
  81.         pstrIn = (LPTSTR) pRawData->pvInData;
  82.         cbBuffer = 0;
  83.         cbTemp = 0;
  84.  
  85.         // are we waiting for the header?
  86.  
  87.         if (pCtxt->m_pFC->pFilterContext == (VOID *) 1)
  88.         {
  89.             // rip through the header to the end
  90.             while (cbBuffer < pRawData->cbInData)
  91.             {
  92.                 if (pstrIn[cbBuffer] == '\n' &&
  93.                     pstrIn[cbBuffer+2] == '\n')
  94.                 {
  95.                     cbBuffer += 3;
  96.                     break;
  97.                 }
  98.                 cbBuffer++;
  99.             }
  100.  
  101.             // does the header identify HTML content?
  102.             while (cbTemp < cbBuffer)
  103.             {
  104.                 if (pstrIn[cbTemp] == '/' && pstrIn[cbTemp+1] == 'h' &&
  105.                     pstrIn[cbTemp+2] == 't' && pstrIn[cbTemp+3] == 'm')
  106.                 {
  107.                     // yes, it's HTML, set our flag
  108.                     pCtxt->m_pFC->pFilterContext = (VOID*) 2;
  109.                     break;
  110.                 }
  111.  
  112.                 cbTemp++;
  113.             }
  114.  
  115.             // if it wasn't HTML--clear the flag
  116.             if (cbTemp == cbBuffer)
  117.                 pCtxt->m_pFC->pFilterContext = NULL;
  118.         }
  119.  
  120.         // if there's anything else left in the buffer,
  121.         // convert it all to upper case!
  122.         if (pCtxt->m_pFC->pFilterContext != NULL)
  123.         {
  124.             while (cbBuffer < pRawData->cbInData)
  125.             {
  126.                 if (isleadbyte(pstrIn[cbBuffer]))
  127.                     cbBuffer += 2;
  128.                 else
  129.                 {
  130.                     pstrIn[cbBuffer] = _totupper(pstrIn[cbBuffer]);
  131.                     cbBuffer++;
  132.                 }
  133.             }
  134.         }
  135.     }
  136.  
  137.     return SF_STATUS_REQ_NEXT_NOTIFICATION;
  138. }
  139.  
  140. DWORD CUpCaseFilter::OnUrlMap(CHttpFilterContext* pCtxt,
  141.     PHTTP_FILTER_URL_MAP pUrlMap)
  142. {
  143.     LPTSTR pstrPhysPath = pUrlMap->pszPhysicalPath;
  144.     pCtxt->m_pFC->pFilterContext = NULL;
  145.  
  146.     // if we get a URL, try to see if it has the string "\UC\"
  147.     while (*pstrPhysPath != '\0')
  148.     {
  149.         if (pstrPhysPath[0] == '\\' &&
  150.             (pstrPhysPath[1] == 'u' || pstrPhysPath[1] == 'U') &&
  151.             (pstrPhysPath[2] == 'c' || pstrPhysPath[2] == 'C') &&
  152.             pstrPhysPath[3] == '\\')
  153.         {
  154.             // we found a "\UC\", so the user wants
  155.             // upper case.  remove the "\UC\" substring
  156.             // so the server can find the real path
  157.  
  158.             while (pstrPhysPath[3] != '\0')
  159.             {
  160.                 pstrPhysPath[0] = pstrPhysPath[3];
  161.                 pstrPhysPath = _tcsinc(pstrPhysPath);
  162.             }
  163.             pstrPhysPath[0] = '\0';
  164.  
  165.             // set a flag to remind us to try and uppercase
  166.             pCtxt->m_pFC->pFilterContext = (VOID*) 1;
  167.             break;
  168.         }
  169.  
  170.         pstrPhysPath = _tcsinc(pstrPhysPath);
  171.     }
  172.  
  173.     return SF_STATUS_REQ_NEXT_NOTIFICATION;
  174. }
  175.  
  176. // If you're building a filter that doesn't use any MFC classes, you'll
  177. // need to implement your own version of AfxGetResourceHandle() so that
  178. // the MFC ISAPI support classes can find resources they may need.
  179. // If you're using MFC classes besides the ISAPI support classes, you'll
  180. // automatically get the normal MFC implementation of
  181. // AfxGetResourceHandle() and won't need this code.
  182.  
  183. #ifndef _AFXDLL
  184.  
  185. static HINSTANCE g_hInstance;
  186.  
  187. HINSTANCE AFXISAPI AfxGetResourceHandle()
  188. {
  189.     return g_hInstance;
  190. }
  191.  
  192. BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
  193.                     LPVOID lpReserved)
  194. {
  195.     if (ulReason == DLL_PROCESS_ATTACH)
  196.         g_hInstance = hInst;
  197.  
  198.     return TRUE;
  199. }
  200.  
  201. #endif
  202.