W3Mfc.h
上传用户:dengkfang
上传日期:2008-12-30
资源大小:5233k
文件大小:11k
源码类别:

CA认证

开发平台:

Visual C++

  1. /*
  2. Module : W3Mfc.h
  3. Purpose: Defines the interface for a simple MFC class encapsulation of a HTTP server
  4. Created: PJN / 22-04-1999
  5. Copyright (c) 1999 - 2005 by PJ Naughter.  (Web: www.naughter.com, Email: pjna@naughter.com)
  6. All rights reserved.
  7. Copyright / Usage Details:
  8. You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
  9. when your product is released in binary form. You are allowed to modify the source code in any way you want 
  10. except you cannot modify the copyright details at the top of each module. If you want to distribute source 
  11. code with your application, then you are only allowed to distribute versions released by the author. This is 
  12. to maintain a single distribution point for the source code. 
  13. */
  14. /////////////////////////////// Defines ///////////////////////////////////////
  15. #ifndef __W3MFC_H__
  16. #define __W3MFC_H__
  17. #ifndef W3MFC_EXT_CLASS
  18. #define W3MFC_EXT_CLASS
  19. #endif
  20. /////////////////////////////// Includes //////////////////////////////////////
  21. #include "HttpMimeManager.h"
  22. #ifndef W3MFC_NO_ISAPI_SUPPORT
  23. #include "HttpISAPI.h"
  24. #include "HttpISAPIManager.h"
  25. #endif
  26. #ifndef W3MFC_NO_CGI_SUPPORT
  27. #include "HttpCGI.h"
  28. #endif
  29. #include "HttpDirectory.h"
  30. #include "ThrdPool.h"
  31. #include "DirectedThreadPoolQueue.h"
  32. #include "IOCPThreadPoolQueue.h"
  33. #ifndef W3MFC_NO_SSPI_SUPPORT
  34.   #ifndef __SSPI_H__
  35.   #include <sspi.h>
  36.   #pragma message("To avoid this message please put SSPI.h in your PCH (normally stdafx.h)")
  37.   #endif
  38. #endif
  39. #ifndef _WINSOCK2API_
  40. #include <winsock2.h>
  41. #pragma message("To avoid this message please put winsock2.h in your PCH (normally stdafx.h)")
  42. #endif
  43. /////////////////////////////// Classes ///////////////////////////////////////
  44. //The settings which the web server uses in the call to CHttpServer::Start
  45. class W3MFC_EXT_CLASS CHttpServerSettings
  46. {
  47. public:
  48. //Constructors / Destructors
  49.   CHttpServerSettings();
  50.   virtual ~CHttpServerSettings();
  51. //Methods
  52.   void FreeDirectoryArray();                                                      //Frees up the memory allocated in m_Directories
  53.                                                                                   
  54. //Member variables                                                                
  55.   unsigned short                            m_nPort;                              //The port on which to run the web server
  56.   CArray<CHttpDirectory*, CHttpDirectory*&> m_Directories;                        //Directories served up by this server
  57.   BOOL                                      m_bBind;                              //Should the server be bound to an address
  58.   CString                                   m_sBindAddress;                       //The IP address to bind to (if m_bBind is set)
  59.   DWORD                                     m_dwIdleClientTimeout;                //Timeout in ms to wait for client requests
  60.   CString                                   m_sServerName;                        //The Web server name to return in HTTP headers
  61.   CRuntimeClass*                            m_pRuntimeClientClass;                //The runtime class of the client class to use, normally is CHttpClient
  62.   CString                                   m_sUsername;                          //The account to run the web server under
  63.   CString                                   m_sPassword;                          //The account's password
  64.   int                                       m_nThreadPoolSize;                    //The size of the thread pool to use
  65.   BOOL                                      m_bDNSLookup;                         //Should we do reverse DNS lookups on client addresses when connecting
  66.   DWORD                                     m_dwWritableTimeout;                  //Timeout in ms to wait for client sockets to become writable
  67.   BOOL                                      m_bEnableThreadLifetime;              //TRUE if threads are to be recycled in the thread pool
  68.   DWORD                                     m_dwThreadLifetime;                   //The lifetime for threads (in Minutes) if recycling is enabled
  69.   CHttpMimeManager*                         m_pMimeManager;                       //The Mime manager to use
  70.   BOOL                                      m_bUseIOCPQueue;                      //Should CIOCPThreadPoolQueue be used in preference to CDirectedThreadPoolQueue.
  71.                                                                                   //Please note that the IOCP class does not support thread recycling
  72.   BOOL                                      m_bKeepAlives;                        //Should the server observe client "ConnectionL Keep-Alive" requests 
  73.   BOOL                                      m_bAutoExpire;                        //Should all reponses be marked with an immediate "Expires" header
  74.   BOOL                                      m_bAllowDeleteRequest;                //Should the "DELETE" request be allowed
  75. #ifndef W3MFC_NO_CGI_SUPPORT                                                      
  76.   DWORD                                     m_dwCGIResponseBufferSize;            //The size of the buffer to use when reading the data from CGI programs. Note that
  77.                                                                                   //this is important value as the code will only look for Keep Alive headers
  78.                                                                                   //in the first buffer returned. So make sure this value is bigger than any 
  79.                                                                                   //of the headers which any of your CGI programs generate
  80. #endif                                                                            
  81.   BOOL                                      m_bAllowAnonymous;                    //Should we support anonymous access
  82.   BOOL                                      m_bAllowBasicAuthentication;          //Should we support Basic authentication
  83.   BOOL                                      m_bAllowNTLMAuthentication;           //Should we support NTLM authentication
  84.   BOOL                                      m_bPerformPassthroughAuthentication;  //Should the credentials sent to us by the client, result in us calling thro to the OS to login that user
  85. #ifndef W3MFC_NO_ISAPI_SUPPORT
  86.   BOOL                                      m_bCacheISAPI;                        //Should we cache ISAPI extensions
  87.   CHttpISAPIManager*                        m_pISAPIManager;                      //The ISAPI Extension manager to use
  88.   CHttpISAPI*                               m_pISAPI;                             //The actual ISAPI class to use
  89. #endif                                                                            
  90. #ifndef W3MFC_NO_CGI_SUPPORT                                                      
  91.   CHttpCGI*                                 m_pCGI;                               //The actual CGI class to use
  92. #endif
  93. #ifdef W3MFC_SSL_SUPPORT
  94.   enum SSL_PROTOCOL
  95.   {
  96.    SSL_NONE = 0, 
  97.    SSL_V2 = 1,
  98.    SSL_V3 = 2,
  99.    SSL_V2_OR_V3 = 3,
  100.    TLS_V1 = 4
  101.   }                                         m_SSLProtocol;              //The SSL protocol to use
  102.   CString                                   m_sServerCertificateFile;   //The filename of the PEM file which contains the server's certificate
  103.   CString                                   m_sPrivateKeyFile;          //The filename of the PEM file which contains the server's private 
  104.                                                                         //key, if this value is empty, then the private key is expected to 
  105.                                                                         //be stored in the Server's certificate file "m_sServerCertificateFile"
  106.   BOOL                                      m_bReuseSessions;           //Should SSL sessions be reused
  107.   DWORD                                     m_dwSSLSessionTimeout;      //The SSL Session timeout value in seconds. Default value is 300 seconds.
  108.   DWORD                                     m_dwSSLNegotiationTimeout;  //The timeout in milliseconds to allow for SSL negotiation
  109.   CString                                   m_sSSLRandomnessFile;       //The filename to initialize the SSL PRNG with.
  110. #endif //W3MFC_SSL_SUPPORT
  111. };
  112. //Class which handles Directed requests for the web server
  113. class W3MFC_EXT_CLASS CHttpDirectedThreadPoolQueue : public CDirectedThreadPoolQueue
  114. {
  115. public:
  116. //Constructors / Destructors
  117.   virtual ~CHttpDirectedThreadPoolQueue();
  118. protected:
  119.   DECLARE_DYNCREATE(CHttpDirectedThreadPoolQueue)
  120. };
  121. //The actual web server
  122. class W3MFC_EXT_CLASS CHttpServer : public CObject
  123. {
  124. public:
  125. //Constructors / Destructors
  126.   CHttpServer();
  127.   virtual ~CHttpServer();
  128. //Methods
  129.   virtual BOOL         Start(CHttpServerSettings* pSettings);
  130.   virtual BOOL         Stop();
  131.   virtual BOOL         Wait();
  132.   virtual void         OnError(const CString& sError);
  133.   static BOOL          ReverseDNSLookup(in_addr sin_addr, CString& sDomainName);
  134.   virtual char*        GetFileDeletedHTML(DWORD& dwSize);
  135. #ifdef W3MFC_SSL_SUPPORT
  136.   virtual void LogSSLErrors();
  137. #endif
  138.   virtual void  LoadHTMLResources();
  139.   virtual char* LoadHTML(int nStatusCode, DWORD& dwSize);
  140.   virtual void  FreeHTMLResources();
  141.   virtual BOOL  LoadHTMLResource(UINT nID, char*& pszHTML, DWORD& dwSize);
  142. //Accessors
  143.   CHttpServerSettings* GetSettings() const { return m_pSettings; };
  144.   CThreadPoolServer&   GetThreadPool() { return m_ThreadPool; };
  145.   HANDLE               GetImpersonationHandle() const { return m_hImpersonation; };
  146. #ifndef W3MFC_NO_SSPI_SUPPORT
  147.   CredHandle*          GetCredentialHandle() { return &m_hCredHandle; };
  148. #endif
  149. protected:
  150.   DECLARE_DYNAMIC(CHttpServer)
  151.   static UINT ListenSocketFunction(LPVOID pParam);
  152.   virtual void ListenSocketFunction();
  153. #ifndef W3MFC_NO_SSPI_SUPPORT
  154.   virtual BOOL AcquireSSPI();
  155.   virtual void ReleaseSSPI();
  156. #endif
  157.   CWinThread*          m_pListenThread;
  158.   CCriticalSection     m_csListenThread;
  159.   CEvent               m_ListenStartEvent;
  160.   CEvent               m_StopEvent;
  161.   CEvent               m_SocketEvent;
  162.   BOOL                 m_bListenerRunningOK;
  163.   CHttpServerSettings* m_pSettings;
  164.   CThreadPoolServer    m_ThreadPool;
  165.   HANDLE               m_hImpersonation;
  166. #ifndef W3MFC_NO_SSPI_SUPPORT
  167.   CredHandle           m_hCredHandle;
  168. #endif
  169.   char*                m_psz302HTML;
  170.   DWORD                m_dw302HTML;
  171.   char*                m_psz400HTML;
  172.   DWORD                m_dw400HTML;
  173.   char*                m_psz401HTML;
  174.   DWORD                m_dw401HTML;
  175.   char*                m_psz404HTML;
  176.   DWORD                m_dw404HTML;
  177.   char*                m_psz500HTML;
  178.   DWORD                m_dw500HTML;
  179.   char*                m_pszDeletedHTML;
  180.   DWORD                m_dwDeletedHTML;
  181. private:
  182. CString ErrorString();
  183. };
  184. #endif //__W3MFC_H__