// ========================================================================== // Class Implementation : CHelloScript // ========================================================================== // Source file : helloscp.cpp // Source : Dundas Software // // This sample implements a simple write back script. This type of script // requires that the onGet and onPost functions be defined. These are the // only functions that needed to be defined for this script. // ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" // standard MFC include #include "helloscp.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CWinApp theApp; // dummy app CHelloScript::CHelloScript() { } CHelloScript::~CHelloScript() { } // This function is only so that you will see something when you debug // this demo from within DevStudio or on a command line. void CHelloScript::OnRunCommandLine(int argc, char** argv) { _tprintf(_T("This program is designed to be run from the HTTP Service\n")); _tprintf(_T("Running it from the command line does nothing interesting\n")); int index = 1; while(index < argc) { _tprintf(_T("Command line Parameter %d : %s\n"), index, argv[index]); index++; } } // This member function is used whenever the client makes a request // for information from the server. // This is virtual function which must be defined in any derived class // One must override these functions because each implementation // will be different. void CHelloScript::OnGet(LPCTSTR pszParam) { UNREFERENCED_PARAMETER(pszParam); // Tell the client that it will be receiving HTML formatted text _tprintf(_T("Content-type: text/HTML\n")); _tprintf(_T("\n")); LPCTSTR pszAction = GetPathInfo(); if (_tcsicmp(pszAction, _T("/report")) == 0) { // When called by a get this script will return information // about the server, client and connection. // These functions can be used internally for processing of _tprintf(_T("

GetPathInfo() = %s

\n"), GetPathInfo()); _tprintf(_T("

GetScriptName() = %s

\n"), GetScriptName()); _tprintf(_T("

GetRemoteAddress() = %s

\n"), GetRemoteAddress()); _tprintf(_T("

GetServerName() = %s

\n"), GetServerName()); _tprintf(_T("

GetServerProtocol() = %s

\n"), GetServerProtocol()); _tprintf(_T("

GetServerSoftware() = %s

\n"), GetServerSoftware()); _tprintf(_T("

GetServerPort() = %i

\n"), GetServerPort()); _tprintf(_T("

GetClientInfo(ACCEPT) = %s

\n"), GetClientInfo(_T("ACCEPT"))); _tprintf(_T("

GetClientInfo(USER_AGENT) = %s

\n"), GetClientInfo(_T("USER_AGENT"))); _tprintf(_T("

GetClientInfo(REFERER) = %s

\n"), GetClientInfo(_T("REFERER"))); _tprintf(_T("
")); } else { _tprintf(_T("This is not a valid parameter :\n")); _tprintf(_T("

%s\n"), pszAction); } _tprintf(_T("\n\n")); } void CHelloScript::OnPost(LPCTSTR pszType, LPCTSTR pszData) { // The client needs to know what type of file it is receiving // This statement tells it that. // _tprintf is used to simply print the text to the stdout port. _tprintf(_T("Content-type: text/HTML\n\n")); _tprintf(_T("")); if (_tcscmp(pszType, _T("application/x-www-form-urlencoded")) != 0) { _tprintf(_T("This is not a valid Post parameter\n")); _tprintf(_T("\n\n")); return; } LPTSTR pszResult = NULL; // CGI converts certain characters like spaces, slashes into other // characters. They need to be returned to their initial characters. // The UUDecode function takes care of this. UUDecode(pszData, pszResult); // Check for the required fields. If they do not exist then // write back an error message to the client CString sSubject, sDescription, sFileName, sEMail; if (!Extract(sSubject, pszResult, 0, _T("Subject")) || !Extract(sDescription, pszResult, 1, _T("Descr")) || !Extract(sFileName, pszResult, 2, _T("Filename")) || !Extract(sEMail, pszResult, 3, _T("MailAddr"))) { _tprintf(_T("Data is corrupt, please try again\n")); _tprintf(_T("\n\n")); return; } // The following if statements test the required fields for // information. If they are empty then an error message is // written back to the client. if (sFileName.IsEmpty()) { _tprintf(_T("The Filename is empty, please try again\n")); _tprintf(_T("

Try again\n")); _tprintf(_T("\n\n")); if (pszResult != NULL) delete[] pszResult; return; } if (sEMail.IsEmpty()) { _tprintf(_T("Your Mail address is empty, please try again\n")); _tprintf(_T("

Try again\n")); _tprintf(_T("\n\n")); if (pszResult != NULL) delete[] pszResult; return; } if (pszResult != NULL) delete[] pszResult; // The following _tprintf calls send a message back to the client. // If the source of the document is viewed in the cients browser // it does not appear to be layed out nicely. However it is readable. // The printing of new line (\n) on its own causes the document to be // restarted. _tprintf(_T("

Your message has been received\n")); _tprintf(_T("

Hello %s\n"), sEMail); _tprintf(_T("

The subject you entered is '%s'\n"), sSubject); _tprintf(_T("

The description you entered is '%s'\n"), sDescription); _tprintf(_T("

And here is the filename you entered '%s'\n"), sFileName); _tprintf(_T("

Back to Home Page\n")); _tprintf(_T("

This is just a demonstration of a write back script.
")); _tprintf(_T("

")); _tprintf(_T("

\n\n")); } int main(int argc, char *argv[]) { CHelloScript script; return script.DoMain(argc, argv); }