home *** CD-ROM | disk | FTP | other *** search
/ Tutto per Internet / Internet.iso / soft95 / Varie / server / FORMDUMP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-03  |  7.5 KB  |  251 lines

  1. #define WIN32_LEAN_AND_MEAN         // the bare essential Win32 API
  2. #include <windows.h>
  3. #include <ctype.h>                  // for isprint()
  4. #include <httpext.h>
  5.  
  6. #include "keys.h"
  7. #include "html.h"
  8.  
  9. // prototype
  10. void HexDumper (EXTENSION_CONTROL_BLOCK *pECB,
  11.                 HANDLE hContent, DWORD dwOffset, DWORD dwLength);
  12.  
  13. //
  14. // DllEntryPoint allows us to initialize our state variables.
  15. // You might keep state information, as the DLL often remains
  16. // loaded for several client requests.  The server may choose 
  17. // to unload this DLL, and you should save your state to disk,
  18. // and reload it here.  DllEntryPoint is called for both
  19. // loading and unloading.  See the Win32 SDK for more info
  20. // on how DLLs load and unload.
  21. //
  22.  
  23. BOOL WINAPI DllEntryPoint (HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpv)
  24.     {
  25.     // Nothing to do here
  26.     return (TRUE);
  27.     }
  28.  
  29.  
  30. //
  31. //  BOOL WINAPI GetExtensionVersion (HSE_VERSION_INFO *pVersionInfo) 
  32. //
  33. //  Return the version this server is built for.  See <httpext.h> for
  34. //  a prototype.  This function is required by the spec.
  35. //
  36.  
  37. BOOL WINAPI GetExtensionVersion (HSE_VERSION_INFO *pVersionInfo)
  38.     {
  39.     // set version to httpext.h version constants
  40.     pVersionInfo->dwExtensionVersion = MAKELONG (HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
  41.  
  42.     lstrcpyn ((LPTSTR) pVersionInfo->lpszExtensionDesc,
  43.             TEXT("FORMDUMP - A Form Decoder and Dumper"),
  44.             HSE_MAX_EXT_DLL_NAME_LEN);
  45.  
  46.     return TRUE;
  47.     } // GetExtensionVersion()
  48.  
  49.  
  50. //
  51. //  Our entry point:
  52. //
  53. //  BOOL WINAPI HttpExtensionProc (EXTENSION_CONTROL_BLOCK *pECB)
  54. //
  55. //  This function pulls in all inbound data.  A reply page is built
  56. //  so the user can see how forms appear in our key list.
  57. //  This function is also required by the spec.
  58. //
  59.  
  60. DWORD WINAPI HttpExtensionProc (EXTENSION_CONTROL_BLOCK *pECB)
  61.     {
  62.     HKEYLIST hKeyList;
  63.     TCHAR szMsg[128];
  64.     HANDLE hContent;
  65.  
  66.     // Get the keys sent by the client
  67.     hKeyList = GetKeyList (pECB);
  68.  
  69.     // Create a basic HTML page
  70.     HtmlCreatePage (pECB, TEXT("Internet Server API Debugging Tool"));
  71.     HtmlHeading (pECB, 2, TEXT("Data Sent by Your Form"));
  72.  
  73.     // Open the content file
  74.     if (hKeyList)
  75.         {
  76.         hContent = CreateFile (GetContentPath (hKeyList),
  77.                                GENERIC_READ,
  78.                                0,                       // No sharing mode
  79.                                NULL,                    // Default security attribs
  80.                                OPEN_EXISTING,
  81.                                FILE_ATTRIBUTE_NORMAL,   // In this sample, we sequentially access this too
  82.                                NULL                     // No template file
  83.                                );
  84.  
  85.         // Report errors as necessary
  86.         if (hContent == INVALID_HANDLE_VALUE)
  87.             {
  88.             HtmlBold (pECB, TEXT("Can't open content file"));
  89.             FreeKeyList (hKeyList);
  90.             hKeyList = NULL;
  91.             }
  92.         }
  93.  
  94.     // Report errors
  95.     if (!hKeyList)
  96.         {
  97.         HtmlBold (pECB, TEXT("No keys sent or error decoding keys"));
  98.         CloseHandle (hContent);
  99.         }
  100.     else
  101.         {
  102.         HKEYLIST hKey;
  103.  
  104.         // Loop through all of the keys
  105.         hKey = hKeyList;
  106.         while (hKey)
  107.             {
  108.             // Details about the key
  109.             LPCTSTR lpszKeyName;
  110.             DWORD dwOffset;
  111.             DWORD dwLength;
  112.             BOOL bHasCtrlChars;
  113.             int nInstance;
  114.  
  115.             // We get info, and hKey points to next key in list
  116.             hKey = GetKeyInfo (hKey, &lpszKeyName, &dwOffset, &dwLength,
  117.                                 &bHasCtrlChars, &nInstance);
  118.  
  119.             
  120.             // Build web page
  121.             HtmlBold (pECB, TEXT("lpszKeyName: "));
  122.             HtmlWriteText (pECB, lpszKeyName);
  123.             HtmlLineBreak (pECB);
  124.  
  125.             HtmlBold (pECB, TEXT("dwOffset: "));
  126.             wsprintf (szMsg, TEXT("%u"), dwOffset);
  127.             HtmlWriteText (pECB, szMsg);
  128.             HtmlLineBreak (pECB);
  129.  
  130.             HtmlBold (pECB, TEXT("dwLength: "));
  131.             wsprintf (szMsg, TEXT("%u"), dwLength);
  132.             HtmlWriteText (pECB, szMsg);
  133.             HtmlLineBreak (pECB);
  134.  
  135.             HtmlBold (pECB, TEXT("bHasCtrlChars: "));
  136.             wsprintf (szMsg, TEXT("%u"), bHasCtrlChars);
  137.             HtmlWriteText (pECB, szMsg);
  138.             HtmlLineBreak (pECB);
  139.  
  140.             HtmlBold (pECB, TEXT("nInstance: "));
  141.             wsprintf (szMsg, TEXT("%u"), nInstance);
  142.             HtmlWriteText (pECB, szMsg);
  143.  
  144.             if (dwLength)
  145.                 {
  146.                 HtmlLineBreak (pECB);
  147.                 HtmlBold (pECB, TEXT("Data:"));
  148.                 HtmlLineBreak (pECB);
  149.                 
  150.                 HexDumper (pECB, hContent, dwOffset, dwLength);
  151.                 }
  152.  
  153.             HtmlEndParagraph (pECB);
  154.             }
  155.         
  156.         // Finally, write the content path
  157.         HtmlBold (pECB, TEXT("Content Path: "));
  158.         HtmlWriteText (pECB, GetContentPath (hKeyList));
  159.         HtmlEndParagraph (pECB);
  160.  
  161.         // Clean up
  162.         CloseHandle (hContent);
  163.         FreeKeyList (hKeyList);
  164.         }
  165.  
  166.     HtmlEndPage (pECB);
  167.  
  168.     return HSE_STATUS_SUCCESS;
  169.     }
  170.  
  171. //
  172. // Put the inbound data in a hex dump format
  173. //
  174.  
  175. void HexDumper (EXTENSION_CONTROL_BLOCK *pECB,
  176.                 HANDLE hContent, DWORD dwOffset, DWORD dwLength)
  177.     {
  178.     BYTE byLineBuf[16];
  179.     DWORD dwRead;
  180.     DWORD dwSize;
  181.     TCHAR szLine[256];
  182.     TCHAR szHex[3];
  183.     DWORD i;
  184.     DWORD dwPos = 0;
  185.  
  186.     HtmlBeginPreformattedText (pECB);
  187.  
  188.     //
  189.     // We move to the offset, but this sample sequentially
  190.     // accesses the keys anyway.  Because the key data is
  191.     // saved in the same order as the key list, we could
  192.     // omit this as long as we only read dwLength bytes.
  193.     // It is necessary for random-access though.
  194.     //
  195.     SetFilePointer (hContent, dwOffset, NULL, FILE_BEGIN);
  196.     
  197.     while (dwLength)
  198.         {
  199.         // Take min of 16 or dwLength
  200.         dwSize = min (16, dwLength);
  201.  
  202.         // Get data from content file
  203.         if (ReadFile (hContent, byLineBuf, dwSize, &dwRead, NULL))
  204.             {
  205.             // Build text line
  206.             wsprintf (szLine, TEXT("  %04X "), dwPos);
  207.  
  208.             for (i = 0 ; i < dwRead ; i++)
  209.                 {
  210.                 wsprintf (szHex, TEXT("%02X"), byLineBuf[i]);
  211.                 lstrcat (szLine, szHex);
  212.                 lstrcat (szLine, TEXT(" "));
  213.                 }
  214.  
  215.             // Add spaces for short lines
  216.             while (i < 16)
  217.                 {
  218.                 lstrcat (szLine, TEXT("   "));
  219.                 i++;
  220.                 }
  221.  
  222.             // Add ASCII chars
  223.             for (i = 0 ; i < dwRead ; i++)
  224.                 {
  225.                 if (isprint (byLineBuf[i]))
  226.                     {
  227.                     wsprintf (szHex, TEXT("%c"), byLineBuf[i]);
  228.                     lstrcat (szLine, szHex);
  229.                     }
  230.                 else
  231.                     {
  232.                     lstrcat (szLine, TEXT("."));
  233.                     }
  234.                 }
  235.  
  236.             // Write data to web page
  237.             HtmlWriteTextLine (pECB, szLine);
  238.  
  239.             dwLength -= dwRead;
  240.             dwPos += dwRead;
  241.             }
  242.         else
  243.             {
  244.             // Write error to web
  245.             HtmlWriteTextLine (pECB, TEXT("Error reading content file"));
  246.             break;
  247.             }
  248.         }
  249.  
  250.     HtmlEndPreformattedText (pECB);
  251.     }