home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c14 / lab02 / ex02 / echo.cpp next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.0 KB  |  144 lines

  1. // ECHO.CPP - Implementation file for your Internet Server
  2. //    Echo ISAPI App
  3.  
  4. #include "stdafx.h"
  5. #include "Echo.h"
  6.  
  7. // Do not edit the following lines, which are needed by ClassWizard.
  8. #if 0
  9. BEGIN_MESSAGE_MAP(CEchoExtension, CHttpServer)
  10.     //{{AFX_MSG_MAP(CEchoExtension)
  11.     //}}AFX_MSG_MAP
  12. END_MESSAGE_MAP()
  13. #endif    // 0
  14.  
  15. ///////////////////////////////////////////////////////////////////////
  16. // The one and only CWinApp object
  17. CWinApp theApp;
  18.  
  19. ///////////////////////////////////////////////////////////////////////
  20. // command-parsing map
  21. BEGIN_PARSE_MAP(CEchoExtension, CHttpServer)
  22.     ON_PARSE_COMMAND(EchoRequest, CEchoExtension, ITS_PSTR)
  23.     ON_PARSE_COMMAND_PARAMS("option=full")
  24.     ON_PARSE_COMMAND(Default, CEchoExtension, ITS_EMPTY)
  25.     DEFAULT_PARSE_COMMAND(Default, CEchoExtension)
  26. END_PARSE_MAP(CEchoExtension)
  27.  
  28. ///////////////////////////////////////////////////////////////////////
  29. // The one and only CEchoExtension object
  30. CEchoExtension theExtension;
  31.  
  32. ///////////////////////////////////////////////////////////////////////
  33. // CEchoExtension implementation
  34.  
  35. CEchoExtension::CEchoExtension()
  36. {
  37. }
  38.  
  39. CEchoExtension::~CEchoExtension()
  40. {
  41. }
  42.  
  43. BOOL CEchoExtension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
  44. {
  45.     // Call default implementation for initialization
  46.     CHttpServer::GetExtensionVersion(pVer);
  47.  
  48.     // Load description string
  49.     TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
  50.     ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
  51.             IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
  52.     _tcscpy(pVer->lpszExtensionDesc, sz);
  53.     return TRUE;
  54. }
  55.  
  56. ///////////////////////////////////////////////////////////////////////
  57. // CEchoExtension command handlers
  58.  
  59. void CEchoExtension::Default(CHttpServerContext* pCtxt)
  60. {
  61.     StartContent(pCtxt);
  62.     WriteTitle(pCtxt);
  63.  
  64.     *pCtxt << "<P><H1>Help for Echo.dll</H1><P><HR>";
  65.     EchoHelp(pCtxt);
  66.  
  67.     EndContent(pCtxt);
  68. }
  69.  
  70. void CEchoExtension::EchoRequest(CHttpServerContext *pCtxt, LPCTSTR pstrOption)
  71. {
  72.     StartContent(pCtxt);
  73.     WriteTitle(pCtxt);
  74.  
  75.     *pCtxt << "<P><H1>Echo.dll</H1><P><HR>";
  76.  
  77.     if ( _stricmp(pstrOption, "full") == 0)
  78.     {
  79.         EchoHead(pCtxt);
  80.         *pCtxt << "<BR>";
  81.         EchoBody(pCtxt);
  82.     }
  83.     else if ( _stricmp(pstrOption, "header") == 0)
  84.     {
  85.         EchoHead(pCtxt);
  86.     }
  87.     else if ( _stricmp(pstrOption, "body") == 0)
  88.     {
  89.         EchoBody(pCtxt);
  90.     }
  91.     else
  92.         BadSyntax(pCtxt);
  93.  
  94.     EndContent(pCtxt);
  95. }
  96.  
  97. void CEchoExtension::EchoBody(CHttpServerContext * pCtxt)
  98. {
  99.     char pstrBuffer[4000];
  100.     DWORD dwSize = sizeof(pstrBuffer);
  101.  
  102.     *pCtxt << "<P><H3>Request Body Information</H3><BR>";
  103.  
  104.     BOOL b = pCtxt->ReadClient(pstrBuffer, &dwSize);
  105.     *pCtxt << "Body is " << (long int)dwSize << " bytes <BR>" ;
  106.     if (b == TRUE)
  107.         *pCtxt << pstrBuffer;
  108. }
  109.  
  110. void CEchoExtension::EchoHead(CHttpServerContext * pCtxt)
  111. {
  112.     char pstrBuffer[1000];
  113.     DWORD dwSize = sizeof(pstrBuffer);
  114.  
  115.     *pCtxt << "<P><H3>Request Header Information</H3>";
  116.     *pCtxt << "<BR>Request method: " << pCtxt->m_pECB->lpszMethod;
  117.     *pCtxt << "<BR>Tranlsated path: " << pCtxt->m_pECB->lpszPathTranslated;
  118.     
  119.     pCtxt->GetServerVariable("QUERY_STRING", pstrBuffer, &dwSize);
  120.     *pCtxt << "<BR>Query String: " << pstrBuffer;
  121.  
  122.     dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
  123.     pCtxt->GetServerVariable("REMOTE_ADDR", pstrBuffer, &dwSize);
  124.     *pCtxt << "<BR>Client IP address: " << pstrBuffer;
  125. }
  126.  
  127. void CEchoExtension::BadSyntax(CHttpServerContext* pCtxt)
  128. {
  129.     *pCtxt << "<H3>Bad Syntax Error!</H3><P>";
  130.     *pCtxt << "Proper syntax is as follows:";
  131.     EchoHelp(pCtxt);
  132. }
  133.  
  134. void CEchoExtension::EchoHelp(CHttpServerContext* pCtxt)
  135. {
  136.     *pCtxt << "<P><I>Supported method:</I>";
  137.     *pCtxt << "<BR><B><TT>EchoRequest</TT></B> - currently the only method supported."
  138.            << " Dynamically creates an HTML page containing HTTP request information.";
  139.     *pCtxt << "<P><I>Supported arguments:</I>";
  140.     *pCtxt << "<BR><B><TT>Full</TT></B> - echo back the entire HTTP request message.";
  141.     *pCtxt << "<BR><B><TT>Body</TT></B> - echo back only the HTTP request message body.";
  142.     *pCtxt << "<BR><B><TT>Header</TT></B> - echo back only the HTTP request message head.";
  143. }
  144.