home *** CD-ROM | disk | FTP | other *** search
- // ECHO.CPP - Implementation file for your Internet Server
- // Echo ISAPI App
-
- #include <afx.h>
- #include <afxwin.h>
- #include <afxisapi.h>
- #include "resource.h"
- #include "Echo.h"
-
- ///////////////////////////////////////////////////////////////////////
- // command-parsing map
-
- BEGIN_PARSE_MAP(CEchoExtension, CHttpServer)
- // TODO: insert your ON_PARSE_COMMAND() and
- // ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
- // For example:
-
- //Lab 10.2, ex 2 - Implementing Echo
- ON_PARSE_COMMAND(EchoRequest, CEchoExtension, ITS_PSTR)
- ON_PARSE_COMMAND_PARAMS("option=full")
-
- ON_PARSE_COMMAND(Default, CEchoExtension, ITS_EMPTY)
- DEFAULT_PARSE_COMMAND(Default, CEchoExtension)
- END_PARSE_MAP(CEchoExtension)
-
-
- ///////////////////////////////////////////////////////////////////////
- // The one and only CEchoExtension object
-
- CEchoExtension theExtension;
-
-
- ///////////////////////////////////////////////////////////////////////
- // CEchoExtension implementation
-
- CEchoExtension::CEchoExtension()
- {
- }
-
- CEchoExtension::~CEchoExtension()
- {
- }
-
- BOOL CEchoExtension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
- {
- // Call default implementation for initialization
- CHttpServer::GetExtensionVersion(pVer);
-
- // Load description string
- TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
- ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
- IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
- _tcscpy(pVer->lpszExtensionDesc, sz);
- return TRUE;
- }
-
- ///////////////////////////////////////////////////////////////////////
- // CEchoExtension command handlers
-
- //Lab 10.2, ex 2 - Implementing Echo
- void CEchoExtension::Default(CHttpServerContext* pCtxt)
- {
- StartContent(pCtxt);
- WriteTitle(pCtxt);
-
- *pCtxt << "<P><H1>Help for Echo.dll</H1><P><HR>";
- *pCtxt << "You have called the Echo ISAPI application without supplying "
- << "a method and an argument.";
- EchoHelp(pCtxt);
- *pCtxt << "\r\n";
-
- EndContent(pCtxt);
- }
-
-
- //Lab 10.2, ex 2 - Implementing Echo
- void CEchoExtension::EchoRequest(CHttpServerContext* pCtxt, LPCTSTR pstrOption)
- {
- pCtxt->m_pECB->dwHttpStatusCode = 200;
- StartContent(pCtxt);
- WriteTitle(pCtxt);
-
- *pCtxt << "<P><H1>Echo.dll</H1><P><HR>";
-
- if ( _stricmp(pstrOption, "full") == 0)
- {
- EchoHead(pCtxt);
- *pCtxt << "<BR>";
- EchoBody(pCtxt);
- }
- else if ( _stricmp(pstrOption, "header") == 0)
- {
- EchoHead(pCtxt);
- }
- else if ( _stricmp(pstrOption, "body") == 0)
- {
- EchoBody(pCtxt);
- }
- else
- BadSyntax(pCtxt);
-
- EndContent(pCtxt);
- }
-
- ///////////////////////////////////////////////////////////////////////
- // Implementation member functions
- //Lab 10.2, ex 2 - Implementing Echo
-
- void CEchoExtension::EchoBody(CHttpServerContext* pCtxt)
- {
- *pCtxt << "<P><H3>Request Body Information</H3><BR>";
-
- DWORD dwContentLength = pCtxt->m_pECB->cbTotalBytes;
- //If there is no HTTP body, then don't process.
- if ( dwContentLength == 0)
- {
- *pCtxt << "<I>No Request Body Present.</I>";
- return;
- }
-
- //Else, allocate stack buffer, read in, then write out.
- char pstrBuffer[4000];
- DWORD dwSize = sizeof(pstrBuffer);
- BOOL b = pCtxt->ReadClient(pstrBuffer, &dwSize);
- if (b == TRUE)
- {
- *pCtxt << "<I>Request Body, First " << (long int)dwSize
- << " of " << (long int)dwContentLength
- << " Total Bytes Follows:</I> <BR>";
- *pCtxt << pstrBuffer;
- }
- else //report error back and set status code
- {
- *pCtxt << "<I>Error in Reading Request Body!</I>";
- pCtxt->m_pECB->dwHttpStatusCode = 500;
- }
- }
-
- //EchoHead actually picks off the typical information from the
- //HTTP header after it is parsed by the Web server; it is not a
- //true echo.
- void CEchoExtension::EchoHead(CHttpServerContext* pCtxt)
- {
- char pstrBuffer[1000];
- DWORD dwSize = sizeof(pstrBuffer);
- #define MECB pCtxt->m_pECB
-
- *pCtxt << "<P><H3>Request Header Information</H3>";
- *pCtxt << "<BR>Request method: " << MECB->lpszMethod;
- *pCtxt << "<BR>Tranlsated path: " << MECB->lpszPathTranslated;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("QUERY_STRING", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Query String: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("REMOTE_USER", pstrBuffer, &dwSize);
- *pCtxt << "<P>Client User name: "
- << (pstrBuffer[0]==0?"Anonymous":pstrBuffer);
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("REMOTE_HOST", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Client host name: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("REMOTE_ADDR", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Client IP address: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("HTTP_ACCEPT", pstrBuffer, &dwSize);
- *pCtxt << "<P>Accept fields: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("CONTENT_TYPE", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Content type (POST): " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("CONTENT_LENGTH", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Content length: " << pstrBuffer;
-
- *pCtxt << "<P><B>Gratuitous Server Information</B>";
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("SERVER_PROTOCOL", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Service protocol: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("SERVER_NAME", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Server Name: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- pCtxt->GetServerVariable("SERVER_SOFTWARE", pstrBuffer, &dwSize);
- *pCtxt << "<BR>Server Software: " << pstrBuffer;
-
- dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
- *pCtxt << "<P>";
-
- #undef MECB //pCtxt->m_pECB
- }
-
- void CEchoExtension::BadSyntax(CHttpServerContext* pCtxt)
- {
- *pCtxt << "<H3>Bad Syntax Error!</H3><P>";
- *pCtxt << "You have called the Echo ISAPI application without supplying "
- << "the proper argument. You must specify a method and a single"
- << " argument.";
- EchoHelp(pCtxt);
- }
-
- void CEchoExtension::EchoHelp(CHttpServerContext* pCtxt)
- {
- *pCtxt << "<P><I>Supported method:</I>";
- *pCtxt << "<BR><B><TT>EchoRequest</TT></B> - currently the only method supported."
- << " Dynamically creates an HTML page containing HTTP request information.";
- *pCtxt << "<P><I>Supported arguments:</I>";
- *pCtxt << "<BR><B><TT>Full</TT></B> - echo back the entire HTTP request message.";
- *pCtxt << "<BR><B><TT>Body</TT></B> - echo back only the HTTP request message body.";
- *pCtxt << "<BR><B><TT>Header</TT></B> - echo back only the HTTP request message head.";
- *pCtxt << "<P>For example, if you have installed Echo.dll on the server \"WebSvr\""
- << "then the following URL would display information on the HTTP request "
- << "message header and body:";
- *pCtxt << "<BR><B><TT>http://WebSvr/Scripts/Echo.dll?EchoRequest&Full</TT></B>";
- }
- ///////////////////////////////////////////////////////////////////////
- // If your extension will not use MFC... [remainder deleted]
-