www.pudn.com > www_toolkit.zip > main.cpp, change:1999-06-29,size:3944b


#include "stdafx.h" 
#include "W3Mfc.h" 
#include "HttpClient.h" 
 
 
 
 
//The global variables 
CWinApp theApp; 
CHttpServer theWebServer; 
 
BOOL ConsoleHandlerRoutine(DWORD /*dwCtrlType*/) 
{ 
  _tprintf(_T("Received request to shut down the Web Server\n")); 
  theWebServer.Stop(); 
  return TRUE; 
} 
 
class CMyHttpClient : public CHttpClient 
{ 
public: 
  virtual BOOL AllowThisConnection(); 
 
  DECLARE_DYNCREATE(CMyHttpClient) 
}; 
 
IMPLEMENT_DYNCREATE(CMyHttpClient, CHttpClient) 
 
BOOL CMyHttpClient::AllowThisConnection() 
{ 
  //Create a string representation of the HTTP verb 
  CString sVerb; 
  switch (m_Request.m_Verb) 
  { 
    case CHttpRequest::HTTP_VERB_GET:     sVerb = _T("GET"); break; 
    case CHttpRequest::HTTP_VERB_POST:    sVerb = _T("POST"); break; 
    case CHttpRequest::HTTP_VERB_HEAD:    sVerb = _T("HEAD"); break; 
    case CHttpRequest::HTTP_VERB_PUT:     sVerb = _T("PUT"); break; 
    case CHttpRequest::HTTP_VERB_LINK:    sVerb = _T("LINK"); break; 
    case CHttpRequest::HTTP_VERB_DELETE:  sVerb = _T("DELETE"); break; 
    case CHttpRequest::HTTP_VERB_UNLINK:  sVerb = _T("UNLINK"); break; 
    case CHttpRequest::HTTP_VERB_UNKNOWN: sVerb = _T("UNKNOWN"); break; 
  } 
 
  //Get the current date and time 
  SYSTEMTIME st; 
  GetLocalTime(&st); 
  TCHAR sDate[64]; 
  GetDateFormat(NULL, LOCALE_NOUSEROVERRIDE, &st, NULL, sDate, 64); 
  TCHAR sTime[64]; 
  GetTimeFormat(NULL, LOCALE_NOUSEROVERRIDE, &st, NULL, sTime, 64); 
 
  //Display the connections to the console window 
  CString sUser(m_Request.m_sUsername); 
  if (sUser.IsEmpty()) 
    sUser = _T("Anonymous"); 
 
  _tprintf(_T("%s, %s, %s %s, %s, %d.%d.%d.%d, %d\n"), sDate, 
           sTime, sVerb, m_Request.m_sURL, sUser, 
           m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b1, 
           m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b2,  
           m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b3,  
           m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b4,  
           ntohs(m_Request.m_ClientAddress.sin_port)); 
 
  //Let the parent do its thing 
  return CHttpClient::AllowThisConnection(); 
} 
 
void main() 
{ 
  //Initialize MFC 
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))     
  { 
    _tprintf(_T("Failed to initialise MFC\n")); 
    return; 
  } 
 
  //Initialise Sockets 
  if (!AfxSocketInit()) 
  { 
    _tprintf(_T("Failed to initialise sockets, GetLastError:%d\n"), ::GetLastError()); 
    return; 
  } 
 
  //Setup the root directory 
  CHttpDirectory dir1; 
  dir1.SetAlias(_T('\\')); 
  dir1.SetDirectory(_T("c:\\webshare\\wwwroot")); //change this to suit your system 
  dir1.SetDefaultFile(_T("index.html"));          //change this to suit your system 
  CHttpServerSettings settings; 
  settings.m_Directories.SetAt(0, dir1); 
 
  //setup another virtual directory 
  CHttpDirectory dir2; 
  dir2.SetAlias(_T("~pjn")); 
  dir2.SetDirectory(_T("c:\\webshare\\wwwroot2")); 
  dir2.SetDefaultFile(_T("index.html")); 
  dir2.SetDirectoryListing(TRUE); 
  settings.m_Directories.Add(dir2); 
 
  //Do not use port 80 for this sample app. Helps avoid any possible 
  //conflict with exisiting web servers which may be running 
  settings.m_nPort = 90; 
 
  //Setup the runtime client class 
  settings.m_pRuntimeClientClass = RUNTIME_CLASS(CMyHttpClient); 
 
  //and start it up 
  _tprintf(_T("Web server is starting...\n")); 
  if (!theWebServer.Start(settings)) 
    _tprintf(_T("Failed to start the web server, GetLastError:%d\n"), ::GetLastError()); 
 
  //Register the console handler to allow the program to be gracefully terminated 
  if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) ConsoleHandlerRoutine, TRUE)) 
  { 
    _tprintf(_T("Failed to initialise console handler, GetLastError:%d\n"), ::GetLastError()); 
    return; 
  } 
 
  //Wait until the server finishes 
  theWebServer.Wait(); 
  _tprintf(_T("Web server has shut down...\n")); 
}