home *** CD-ROM | disk | FTP | other *** search
/ Tutto per Internet / Internet.iso / soft95 / Varie / server / srch.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-05  |  5.7 KB  |  220 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "httpext.h"
  6. #include "srch.h"     
  7.  
  8. // constructor
  9. CSrch::CSrch(char* cData, EXTENSION_CONTROL_BLOCK* pEcb)
  10. {
  11.     bUseCase = TRUE;
  12.     bOverflow = FALSE;
  13.  
  14.     bHitSomething = FALSE;
  15.     sCounter = 0;
  16.     sHitCount = -1;
  17.     
  18.     strcpy(pszAlias, "");
  19.  
  20. // swap '+' to ' ' (put the space char back in)
  21. // this gets substituted by WWW server
  22.     char* pData = cData;
  23.     while (*pData)
  24.         {
  25.         if ((*pData == 0x0D) || (*pData == 0x0A)) *pData = '\0';
  26.         pData++;
  27.         }
  28.  
  29.     pData = cData;
  30.     while (*pData)
  31.         {
  32.         if (*pData == '+') *pData = ' ';
  33.         pData++;
  34.         }
  35.  
  36.     pExtContext = pEcb;
  37.     DecodeHex(cData);           
  38.     strcpy(cBuffer, cData);
  39.  
  40.     wsprintf(cPrintBuffer,"Content-Type: text/html\r\n"     "\r\n"
  41.              "<head>"
  42.             "<title>Search Results</title></head>\n\n\r"
  43.             "<body><H1><p align=center>Full Text Search</H1><p>"
  44.             "<p align=center>Search string - %s<p>"
  45.             "<p><hr>"
  46.             ,
  47.             cData);
  48.          {
  49.           DWORD dwLen=lstrlen(cPrintBuffer);
  50.  
  51.           pEcb->ServerSupportFunction(pEcb->ConnID,
  52.                                       HSE_REQ_SEND_RESPONSE_HEADER,
  53.                                       "200 OK",
  54.                                       &dwLen,
  55.                                       (LPDWORD) cPrintBuffer );
  56.          }
  57.  
  58.     cParsedData = _strupr(cData);       
  59.  
  60. }
  61.  
  62. // destructor
  63. CSrch::~CSrch()
  64. {                    
  65.     unsigned long ulCount;
  66.     if (bOverflow)
  67.         {
  68.         char pTemp[] = "<p><b>The search criteria returned > 255 hits. Please narrow your search value down and retry.</b><p>";
  69.  
  70.         ulCount = sizeof(pTemp);
  71.         pExtContext->WriteClient( pExtContext->ConnID,
  72.                            pTemp,
  73.                            &ulCount,
  74.                            0 );
  75.  
  76.  
  77. // clean up the allocated memory
  78.         short sCount;    
  79.         for (sCount = 0; sCount <= sHitCount; sCount++)
  80.             VirtualFree((LPVOID)sHitStruct[sCount].cHREF, 
  81.                         0, MEM_DECOMMIT | MEM_RELEASE); 
  82.  
  83.         return;
  84.         }
  85.  
  86.     if (!bHitSomething) 
  87.         {
  88.         char pTemp[] = "<p><b>No hits found.</b><p></body>";
  89.         ulCount = sizeof(pTemp);
  90.         pExtContext->WriteClient( pExtContext->ConnID,
  91.                            pTemp,
  92.                            &ulCount,
  93.                            0 );
  94.         }
  95.     else 
  96.         {
  97.         Sort();
  98.         short sCount;    
  99.         for (sCount = 0; sCount <= sHitCount; sCount++)
  100.             {
  101.             ulCount = wsprintf(cPrintBuffer,
  102.                 "<a href=%s>%s</a> - %d hit(s)<p>\n\r", 
  103.                 sHitStruct[sCount].cHREF,
  104.                 sHitStruct[sCount].cHREF, 
  105.                 sHitStruct[sCount].sHits);
  106.  
  107.             VirtualFree((LPVOID)sHitStruct[sCount].cHREF, 
  108.                         0, MEM_DECOMMIT | MEM_RELEASE); 
  109.                         
  110.             pExtContext->WriteClient( pExtContext->ConnID,
  111.                                cPrintBuffer,
  112.                                &ulCount,
  113.                                0 );
  114.  
  115.             }
  116.             ulCount = wsprintf(cPrintBuffer,"<p><p>"
  117.             "<p><b>%d hit(s) found.</b><p>"
  118.             , 
  119.             sCounter); 
  120.         
  121.                ulCount++;
  122.                pExtContext->WriteClient( pExtContext->ConnID,
  123.                                   cPrintBuffer,
  124.                                   &ulCount,
  125.                                   0 );
  126.                           
  127.         }
  128.                  {
  129.                   DWORD dwLen=0;
  130.  
  131.                   pExtContext->ServerSupportFunction( pExtContext,
  132.                                                       HSE_REQ_DONE_WITH_SESSION,
  133.                                                       NULL,
  134.                                                       &dwLen,
  135.                                                       0 );
  136.                  }
  137.  
  138. }
  139.  
  140. // selection sort
  141. void CSrch::Sort()
  142. {
  143.     short sCurrent,sCompare,sMax;
  144.  
  145.     for (sCurrent = 0; sCurrent <= sHitCount; sCurrent++)
  146.             {
  147.             sMax = sCurrent;
  148.             for (sCompare = (sCurrent + 1); sCompare <= sHitCount; sCompare++)
  149.                     if (sHitStruct[sCompare].sHits > sHitStruct[sMax].sHits) sMax = sCompare;
  150.  
  151.             Swap(sCurrent, sMax);            
  152.             }
  153. }
  154.  
  155. // swap locations of minimum value and current
  156. void CSrch::Swap(short sCurrent,short sMax)
  157. {
  158.     sSwap.sHits = sHitStruct[sMax].sHits;
  159.     sHitStruct[sMax].sHits = sHitStruct[sCurrent].sHits;
  160.        sHitStruct[sCurrent].sHits = sSwap.sHits;
  161.  
  162.     sSwap.cHREF = sHitStruct[sMax].cHREF;
  163.     sHitStruct[sMax].cHREF = sHitStruct[sCurrent].cHREF;
  164.        sHitStruct[sCurrent].cHREF = sSwap.cHREF;
  165.  
  166. }
  167.  
  168. // version information
  169.  extern "C" BOOL WINAPI GetExtensionVersion( HSE_VERSION_INFO *version)
  170. {
  171.     version->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
  172.  
  173.     strcpy(version->lpszExtensionDesc, "Srchd.dll v1.0");
  174.  
  175.     return TRUE;
  176. }
  177.  
  178. // DLL Entry point
  179. extern "C"  DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK* pEcb)
  180. {
  181. // first parse out the search string
  182.     char* cData;
  183. //    char* cBuffer = (char*)VirtualAlloc(0, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  184.     char* cBuffer = (char*)malloc(pEcb->cbAvailable);
  185.  
  186.     wsprintf(cBuffer,"%s", pEcb->lpbData);
  187.     cData = strstr(cBuffer,"=");
  188.     cData++;
  189.  
  190.     CSrch* cHTTPIn = new CSrch(cData, pEcb);
  191.  
  192. // get the web tree location dir from the DLL load location
  193.       char pszCurDir[256];
  194.     char pszSetDir[256];
  195.  
  196.     GetModuleFileName(GetModuleHandle("srchd.dll"), pszCurDir, 256);
  197.  
  198.     strncpy(pszSetDir, pszCurDir, strlen(pszCurDir) - strlen("srchd.dll"));
  199.     *(pszSetDir + ((strlen(pszCurDir) - strlen("srchd.dll")) - 1)) = 0;
  200.     SetCurrentDirectory(pszSetDir);
  201.                             
  202.     cHTTPIn->cStartDir = pszSetDir;
  203.                           
  204.     DWORD dwSize = 100;
  205.                               
  206.       pEcb->GetServerVariable(pEcb->ConnID, "QUERY_STRING", cHTTPIn->pszAlias, &dwSize);
  207.  
  208.      cHTTPIn->ListDirectoryContents(pszSetDir, "*.htm", cHTTPIn->cParsedData);
  209.            
  210. // clean up before we get run again.
  211. //    VirtualFree((LPVOID)cBuffer, 0, MEM_DECOMMIT | MEM_RELEASE);
  212.     free(cBuffer);
  213.     delete cHTTPIn;
  214.  
  215.     return HSE_STATUS_SUCCESS;
  216. }
  217.  
  218.  
  219.      
  220.