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

CA认证

开发平台:

Visual C++

  1. // WebServerPage.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "minica.h"
  5. #include "WebServerPage.h"
  6. #include ".GenericClassLanguage.h"
  7. #include "minict.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #ifdef W3MFC_SSL_SUPPORT
  14. CCriticalSection* CWebServerPage::m_pOpenSSLCritSections = NULL;
  15. #endif
  16. CStaticTreeCtrl * CWebServerPage::m_pWebTree = NULL;
  17. CTreeNode * CWebServerPage::m_hWebClient = NULL;
  18. CTreeNode * CWebServerPage::m_hWebServer = NULL;
  19. void CMiniCaHttpServer::OnError(const CString& sError)
  20. {
  21. CWebServerPage::AddMsg(sError, FALSE, FALSE);
  22. }
  23. IMPLEMENT_DYNCREATE(CMiniCAHttpClient, CHttpClient)
  24. void CMiniCAHttpClient::PostLog(int nHTTPStatusCode, DWORD dwBodyLength)
  25. {
  26. //Log each request to the console window using the W3C Common
  27. //log format.
  28. //Get the current date and time
  29. time_t now = time(NULL);
  30. tm* pNow = localtime(&now);
  31. //Get the time zone information
  32. TIME_ZONE_INFORMATION tzi;
  33. GetTimeZoneInformation(&tzi);
  34. //Format the date and time appropiately
  35. TCHAR sDateTime[64];
  36. _tcsftime(sDateTime, 64, _T("[%d/%b/%Y:%H:%M:%S"), pNow);
  37. //Display the connections to the console window
  38. CString sUser(m_Request.m_sUsername);
  39. if (sUser.IsEmpty())
  40. sUser = _T("-");
  41. CString strMsg;
  42. strMsg.Format(_T("%d.%d.%d.%d - %s %s %04d] "%s" %d %dn"), 
  43. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b1,
  44. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b2, 
  45. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b3, 
  46. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b4, 
  47. sUser, sDateTime, tzi.Bias, m_Request.m_sRequest, nHTTPStatusCode, dwBodyLength);
  48. // CWebServerPage::AddMsg(strMsg);
  49. /* _tprintf(_T("%d.%d.%d.%d - %s %s %04d] "%s" %d %dn"), 
  50. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b1,
  51. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b2, 
  52. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b3, 
  53. m_Request.m_ClientAddress.sin_addr.S_un.S_un_b.s_b4, 
  54. sUser, sDateTime, tzi.Bias, m_Request.m_sRequest, nHTTPStatusCode, dwBodyLength);*/
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CWebServerPage property page
  58. IMPLEMENT_DYNCREATE(CWebServerPage, CPropertyPage)
  59. CWebServerPage::CWebServerPage() : CPropertyPage(CWebServerPage::IDD)
  60. {
  61. //{{AFX_DATA_INIT(CWebServerPage)
  62. //}}AFX_DATA_INIT
  63. m_hWebRoot = NULL;
  64. m_hWebServer = NULL;
  65. m_hWebClient = NULL;
  66. }
  67. CWebServerPage::~CWebServerPage()
  68. {
  69. }
  70. void CWebServerPage::DoDataExchange(CDataExchange* pDX)
  71. {
  72. CPropertyPage::DoDataExchange(pDX);
  73. //{{AFX_DATA_MAP(CWebServerPage)
  74. DDX_Control(pDX, IDC_STATIC_WEB, m_WebTree);
  75. //}}AFX_DATA_MAP
  76. }
  77. BEGIN_MESSAGE_MAP(CWebServerPage, CPropertyPage)
  78. //{{AFX_MSG_MAP(CWebServerPage)
  79. ON_BN_CLICKED(IDC_SERVER, OnServer)
  80. ON_BN_CLICKED(IDC_BSTOP, OnBstop)
  81. ON_WM_CLOSE()
  82. ON_WM_DESTROY()
  83. ON_BN_CLICKED(IDC_BRESERT, OnBresert)
  84. ON_BN_CLICKED(IDC_BCLEAR, OnBclear)
  85. //}}AFX_MSG_MAP
  86. END_MESSAGE_MAP()
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CWebServerPage message handlers
  89. CWinThread * WebServiceThread = NULL;
  90. UINT _WebServiceThread(LPVOID lparam)
  91. {
  92. CWebServerPage * p=(CWebServerPage*)lparam;
  93. if(!p->Service())
  94. {
  95. WebServiceThread = NULL;
  96. p->m_WebTree.SetNodeColor(p->m_hWebRoot,RGB(255,0,0));
  97. p->m_WebTree.SetNodeColor(p->m_hWebClient,RGB(255,0,0));
  98. p->m_WebTree.SetNodeColor(p->m_hWebServer,RGB(255,0,0),TRUE);
  99. return 0;
  100. }
  101. else//开放 停止,重启,关闭 启动
  102. {
  103. }
  104. return 1;
  105. }
  106. void CWebServerPage::OnServer() 
  107. {
  108. // TODO: Add your control notification handler code here
  109. if(!WebServiceThread)
  110. WebServiceThread = ::AfxBeginThread(_WebServiceThread,this,THREAD_PRIORITY_IDLE);
  111. }
  112. #ifdef W3MFC_SSL_SUPPORT
  113. void __cdecl CWebServerPage::OpenSSLLockingCallback(int mode, int type, const char* /*file*/, int /*line*/)
  114. {
  115. ASSERT(m_pOpenSSLCritSections);
  116. if (mode & CRYPTO_LOCK)
  117. m_pOpenSSLCritSections[type].Lock();
  118. else
  119. m_pOpenSSLCritSections[type].Unlock();
  120. }
  121. #endif
  122. BOOL CWebServerPage::Service()
  123. {
  124. //Use an ini file in the same location as the exe
  125. /* TCHAR pszIni[_MAX_PATH];
  126. GetModuleFileName(NULL, pszIni, _MAX_PATH);
  127. TCHAR pszDrive[_MAX_DRIVE];
  128. TCHAR pszDir[_MAX_DIR];
  129. TCHAR pszFname[_MAX_FNAME];
  130. _tsplitpath(pszIni, pszDrive, pszDir, pszFname, NULL);
  131. _tmakepath(pszIni, pszDrive, pszDir, pszFname, _T("ini"));
  132. // free((void*)m_pszProfileName);
  133. m_pszProfileName=_tcsdup(pszIni);*/
  134. if(m_hWebRoot)
  135. m_WebTree.DeleteNode(m_hWebRoot);
  136. m_hWebRoot = m_WebTree.InsertChild( HTOPNODE, _T(MiniCT_1000)); //MiniCT_1000 "MiniCA Web Server(Stop)"
  137. m_WebTree.SetNodeColor(m_hWebRoot,RGB(255,0,0));
  138. m_hWebClient = m_WebTree.InsertChild( m_hWebRoot, _T(MiniCT_1001)); //MiniCT_1001 "Client"
  139. m_hWebServer = m_WebTree.InsertChild( m_hWebRoot, _T(MiniCT_1002)); //MiniCT_1002 "Server"
  140. CMiniCaApp * pApp = (CMiniCaApp *)AfxGetApp();
  141. CString lpszIni = pApp->GetAppPath() + "\WebServer.ini";
  142. pApp->m_pszProfileName = lpszIni;
  143. //setup all the virtual directories
  144. CHttpServerSettings settings;
  145. int nDirectories = pApp->GetProfileInt(_T("General"), _T("Directories"), 0);
  146. if (nDirectories == 0)
  147. {
  148. // AddMsg(_T("Usage: W3MFC [Configuration Ini File Path]n"));
  149. AddMsg(_T("Could not read any directory settings from the ini file"), FALSE);
  150. return FALSE; 
  151. }
  152. settings.FreeDirectoryArray();
  153. for (int i=0; i<nDirectories; i++)
  154. {
  155. //Create each directory. Client samples could instantiate a derived version
  156. //to customize the functionality
  157. CHttpDirectory* pDirectory = new CHttpDirectory;
  158. CString sSection;
  159. sSection.Format(_T("Dir%d"), i);
  160. CString sAlias = pApp->GetProfileString(sSection, _T("Alias"), _T(""));
  161. pDirectory->SetAlias(sAlias);
  162. CString sDirectory = pApp->GetProfileString(sSection, _T("Path"), _T(""));
  163. //查找Path是否带有":",如果有就是绝对对路径,否则就是相对路径
  164. if(sDirectory.FindOneOf(":") == -1) //相对路径
  165. {
  166. sDirectory = pApp->GetAppPath() + "\" + sDirectory;
  167. }
  168. pDirectory->SetDirectory(sDirectory);
  169. CString sDefaultFile = pApp->GetProfileString(sSection, _T("DefaultFile"), _T("index.html"));
  170. pDirectory->SetDefaultFile(sDefaultFile);
  171. BOOL bDirectoryListing = (BOOL) pApp->GetProfileInt(sSection, _T("DirectoryListing"), FALSE);
  172. pDirectory->SetDirectoryListing(bDirectoryListing);
  173. BOOL bWritable = (BOOL) pApp->GetProfileInt(sSection, _T("Writable"), FALSE);
  174. pDirectory->SetWritable(bWritable);
  175. BOOL bScript = (BOOL) pApp->GetProfileInt(sSection, _T("Script"), FALSE);
  176. pDirectory->SetScript(bScript);
  177. CString sUsername = pApp->GetProfileString(sSection, _T("Username"), _T(""));
  178. pDirectory->SetUsername(sUsername);
  179. CString sPassword = pApp->GetProfileString(sSection, _T("Password"), _T(""));
  180. pDirectory->SetPassword(sPassword);
  181. CString sRealm = pApp->GetProfileString(sSection, _T("Realm"), _T(""));
  182. pDirectory->SetRealm(sRealm);
  183. settings.m_Directories.SetAtGrow(i, pDirectory);
  184. }
  185. //Do not use port 80 for this sample app. Helps avoid any possible
  186. //conflict with exisiting web servers which may be running
  187. settings.m_nPort = (unsigned short) (pApp->GetProfileInt(_T("General"), _T("Port"), 80));
  188. //Setup the runtime client class
  189. settings.m_pRuntimeClientClass = RUNTIME_CLASS(CMiniCAHttpClient);
  190. //Comment out the following line to not do reverse DNS lookups on client connections
  191. settings.m_bDNSLookup = pApp->GetProfileInt(_T("General"), _T("DNSLookup"), FALSE);
  192. //Should we bind to an IP address
  193. CString sAddress = pApp->GetProfileString(_T("General"), _T("BindAddress"), _T(""));
  194. if (!sAddress.IsEmpty())
  195. {
  196. settings.m_bBind = TRUE;
  197. settings.m_sBindAddress = sAddress;
  198. }
  199. //All the other misc settings
  200. settings.m_dwIdleClientTimeout = pApp->GetProfileInt(_T("General"), _T("Timeout"), settings.m_dwIdleClientTimeout);
  201. settings.m_sServerName = pApp->GetProfileString(_T("General"), _T("ServerName"), settings.m_sServerName);
  202. settings.m_sUsername = pApp->GetProfileString(_T("General"), _T("Username"), settings.m_sUsername);
  203. settings.m_sPassword = pApp->GetProfileString(_T("General"), _T("Password"), settings.m_sPassword);
  204. settings.m_nThreadPoolSize = pApp->GetProfileInt(_T("General"), _T("ThreadPoolSize"), settings.m_nThreadPoolSize);
  205. settings.m_dwWritableTimeout = pApp->GetProfileInt(_T("General"), _T("WritableTimeout"), settings.m_dwWritableTimeout);
  206. settings.m_bEnableThreadLifetime = pApp->GetProfileInt(_T("General"), _T("EnableThreadLifetime"), settings.m_bEnableThreadLifetime);
  207. settings.m_dwThreadLifetime = pApp->GetProfileInt(_T("General"), _T("ThreadLifetime"), settings.m_dwThreadLifetime);
  208. settings.m_bUseIOCPQueue = pApp->GetProfileInt(_T("General"), _T("UseIOCPQueue"), settings.m_bUseIOCPQueue);
  209. settings.m_bKeepAlives = (BOOL) pApp->GetProfileInt(_T("General"), _T("KeepAlives"), settings.m_bKeepAlives);
  210. settings.m_bAutoExpire = (BOOL) pApp->GetProfileInt(_T("General"), _T("AutoExpire"), settings.m_bAutoExpire);
  211. settings.m_bAllowDeleteRequest = (BOOL) pApp->GetProfileInt(_T("General"), _T("AllowDeletes"), settings.m_bAllowDeleteRequest);
  212. #ifndef W3MFC_NO_CGI_SUPPORT
  213. settings.m_dwCGIResponseBufferSize = pApp->GetProfileInt(_T("General"), _T("CGIResponseBufferSize"), settings.m_dwCGIResponseBufferSize);
  214. #endif
  215. settings.m_bAllowAnonymous                   = (BOOL) pApp->GetProfileInt(_T("General"), _T("AllowAnonymous"), settings.m_bAllowAnonymous);
  216. settings.m_bAllowBasicAuthentication         = (BOOL) pApp->GetProfileInt(_T("General"), _T("AllowBasicAuthentication"), settings.m_bAllowBasicAuthentication);
  217. settings.m_bAllowNTLMAuthentication          = (BOOL) pApp->GetProfileInt(_T("General"), _T("AllowNTLMAuthentication"), settings.m_bAllowNTLMAuthentication);
  218. settings.m_bPerformPassthroughAuthentication = (BOOL) pApp->GetProfileInt(_T("General"), _T("PerformPassthroughAuthentication"), settings.m_bPerformPassthroughAuthentication);
  219. #ifdef W3MFC_SSL_SUPPORT
  220. settings.m_SSLProtocol             = (CHttpServerSettings::SSL_PROTOCOL) pApp->GetProfileInt(_T("SSL"), _T("Protocol"), settings.m_SSLProtocol);
  221. settings.m_sServerCertificateFile  = pApp->GetProfileString(_T("SSL"), _T("ServerCertificateFile"), settings.m_sServerCertificateFile);
  222. settings.m_sPrivateKeyFile         = pApp->GetProfileString(_T("SSL"), _T("PrivateKeyFile"), settings.m_sPrivateKeyFile);
  223. settings.m_bReuseSessions          = pApp->GetProfileInt(_T("SSL"), _T("ReuseSessions"), settings.m_bReuseSessions);
  224. settings.m_dwSSLSessionTimeout     = pApp->GetProfileInt(_T("SSL"), _T("SessionTimeout"), settings.m_dwSSLSessionTimeout);
  225. settings.m_dwSSLNegotiationTimeout = pApp->GetProfileInt(_T("SSL"), _T("NegotiationTimeout"), settings.m_dwSSLNegotiationTimeout);
  226. settings.m_sSSLRandomnessFile      = pApp->GetProfileString(_T("SSL"), _T("RandomnessFile"), settings.m_sSSLRandomnessFile);
  227. //Standard OpenSSL initialization
  228. SSL_load_error_strings();
  229. SSL_library_init();
  230. //Setup SSL to work correctly in a multithreaded environment
  231. if (m_pOpenSSLCritSections)
  232. {
  233. delete [] m_pOpenSSLCritSections;
  234. m_pOpenSSLCritSections = NULL;
  235. }
  236. ASSERT(m_pOpenSSLCritSections == NULL);
  237. m_pOpenSSLCritSections = new CCriticalSection[CRYPTO_num_locks()];
  238. if (m_pOpenSSLCritSections == NULL)
  239. {
  240. //Report the error
  241. AddMsg(_T("Failed to create SSL critical sections"));
  242. return FALSE;
  243. }
  244. CRYPTO_set_locking_callback(OpenSSLLockingCallback);
  245. #endif
  246. //Initialize the Mime manager we are going to use with a mime map as taken from the ini file
  247. CIniHttpMimeManager iniMimeManager;
  248. int nSizeOfMap = iniMimeManager.Initialize(pApp->m_pszProfileName, _T("Mime"));
  249. if (nSizeOfMap == 0)
  250. {
  251. AddMsg(_T("Failed to read any MIME map settings from ini file"));
  252. return FALSE; 
  253. }
  254. settings.m_pMimeManager = &iniMimeManager;
  255. #ifndef W3MFC_NO_ISAPI_SUPPORT
  256. //Initialize the ISAPI manager we are going to use with a extension map as taken from the ini file
  257. settings.m_bCacheISAPI                = (BOOL) pApp->GetProfileInt(_T("ISAPI"), _T("Cache"), settings.m_bCacheISAPI);
  258. CIniHttpISAPIManager iniISAPIManager;
  259. iniISAPIManager.Initialize(pApp->m_pszProfileName, _T("ISAPIMap"));
  260. settings.m_pISAPIManager = &iniISAPIManager;
  261. //Also initialize the ISAPI implementation
  262. int nISAPIHashTableSize = pApp->GetProfileInt(_T("ISAPI"), _T("HashTableSize"), 7929); 
  263. CHttpISAPI isapi(nISAPIHashTableSize);
  264. settings.m_pISAPI = &isapi;
  265. #endif
  266. //And the CGI implementation
  267. #ifndef W3MFC_NO_CGI_SUPPORT
  268. CHttpCGI cgi;
  269. settings.m_pCGI = &cgi;
  270. #endif
  271. //and start it up
  272. AddMsg(_T("MiniCA Web server is starting...n"), FALSE);
  273. CMiniCaHttpServer theWebServer;
  274. pTheWebServer = &theWebServer;
  275. if (!theWebServer.Start(&settings))
  276. {
  277. AddMsg(_T("Failed to start up the web servern"), FALSE);
  278. return FALSE;
  279. }
  280. //Display some additional info when the web servering is starting
  281. CString strMsg;
  282. strMsg.Format(_T("Listening for incoming connections on port %dn"), settings.m_nPort);
  283. AddMsg(strMsg, FALSE);
  284. if (settings.m_bDNSLookup)
  285. AddMsg(_T("Reverse DNS lookups will be performed on client requestsn"), FALSE);
  286. if (settings.m_bBind)
  287. {
  288. strMsg.Format(_T("Binding to local address: %sn"), settings.m_sBindAddress);
  289. AddMsg(strMsg, FALSE);
  290. }
  291. strMsg.Format(_T("Client idle timeout: %d msn"), settings.m_dwIdleClientTimeout);
  292. AddMsg(strMsg, FALSE);
  293. strMsg.Format(_T("Client write timeout: %d msn"), settings.m_dwWritableTimeout);
  294. AddMsg(strMsg, FALSE);
  295. strMsg.Format(_T("Configured Server Name: %sn"), settings.m_sServerName);
  296. AddMsg(strMsg, FALSE);
  297. strMsg.Format(_T("Thread pool size: %dn"), settings.m_nThreadPoolSize);
  298. AddMsg(strMsg, FALSE);
  299. if (settings.m_bEnableThreadLifetime)
  300. {
  301. strMsg.Format(_T("Threads in the thread pool will be recycled every %d minute(s)n"), settings.m_dwThreadLifetime);
  302. AddMsg(strMsg, FALSE);
  303. }
  304. if (settings.m_bUseIOCPQueue)
  305. {
  306. AddMsg(_T("Using IOCP Thread pool queuen"), FALSE);
  307. }
  308. else
  309. AddMsg(_T("Using Directed Thread pool queuen"), FALSE);
  310. if (settings.m_bKeepAlives)
  311. AddMsg(_T("Using Keep Alivesn"), FALSE);
  312. if (settings.m_bAutoExpire)
  313. AddMsg(_T("Reponses will be marked with an immediate "Expires" headern"), FALSE);
  314. if (settings.m_bAllowDeleteRequest)
  315. AddMsg(_T("HTTP DELETE verb will be allowedn"), FALSE);
  316. else
  317. AddMsg(_T("HTTP DELETE verb will not be allowedn"), FALSE);
  318. #ifndef W3MFC_NO_CGI_SUPPORT
  319. {
  320. strMsg.Format(_T("CGI Response Buffer size: %dn"), settings.m_dwCGIResponseBufferSize);
  321. AddMsg(strMsg, FALSE);
  322. }
  323. #else
  324. {
  325. strMsg.Format(_T("CGI support is disabled in this configurationn"), FALSE);
  326. AddMsg(strMsg);
  327. }
  328. #endif
  329. #ifndef W3MFC_NO_ISAPI_SUPPORT
  330. AddMsg(_T("ISAPI support is provided in this configurationn"), FALSE);
  331. if (settings.m_bCacheISAPI)
  332. AddMsg(_T("ISAPI dlls will be cached in memoryn"), FALSE);
  333. else
  334. AddMsg(_T("ISAPI dlls will not be cached in memoryn"), FALSE);
  335. #else
  336. AddMsg(_T("ISAPI support is disabled in this configurationn"), FALSE);
  337. #endif
  338. if (settings.m_bAllowAnonymous)
  339. AddMsg(_T("Anonymous Connections allowedn"), FALSE);
  340. else
  341. AddMsg(_T("Anonymous Connections are not allowedn"), FALSE);
  342. if (settings.m_bAllowBasicAuthentication)
  343. AddMsg(_T("Basic Authentication allowedn"), FALSE);
  344. if (settings.m_bAllowNTLMAuthentication)
  345. AddMsg(_T("NTLM Authentication allowedn"), FALSE);
  346. if (settings.m_bPerformPassthroughAuthentication)
  347. AddMsg(_T("Passthrough Authentication will be allowedn"), FALSE);
  348. else
  349. AddMsg(_T("Passthrough Authentication is not allowedn"), FALSE);
  350. #ifdef W3MFC_SSL_SUPPORT
  351. if (settings.m_SSLProtocol != CHttpServerSettings::SSL_NONE)
  352. {
  353. switch (settings.m_SSLProtocol)
  354. {
  355. case CHttpServerSettings::SSL_V2:
  356. {
  357. AddMsg(_T("Server will use SSL v2 protocoln"), FALSE);
  358. break;
  359. }
  360. case CHttpServerSettings::SSL_V3:
  361. {
  362. AddMsg(_T("Server will use SSL v3 protocoln"), FALSE);
  363. break;
  364. }
  365. case CHttpServerSettings::SSL_V2_OR_V3:
  366. {
  367. AddMsg(_T("Server will use SSL v3 protocol (downgrading to V2 if necessary)n"), FALSE);
  368. break;
  369. }
  370. case CHttpServerSettings::TLS_V1:
  371. {
  372. AddMsg(_T("Server will use TLS v1 protocoln"), FALSE);
  373. break;
  374. }
  375. default:
  376. {
  377. break;
  378. }
  379. }
  380. strMsg.Format(_T("Server certificate is %sn"), settings.m_sServerCertificateFile);
  381. AddMsg(strMsg, FALSE);
  382. if (!settings.m_sPrivateKeyFile.IsEmpty())
  383. {
  384. strMsg.Format(_T("Private key is %sn"), settings.m_sPrivateKeyFile);
  385. AddMsg(strMsg, FALSE);
  386. }
  387. if (!settings.m_sSSLRandomnessFile.IsEmpty())
  388. {
  389. strMsg.Format(_T("OpenSSL Randomness file is %sn"), settings.m_sSSLRandomnessFile);
  390. AddMsg(strMsg, FALSE);
  391. }
  392. strMsg.Format(_T("SSL Negotiation timeout will be %d millisecondsn"), settings.m_dwSSLNegotiationTimeout);
  393. AddMsg(strMsg, FALSE);
  394. if (settings.m_bReuseSessions)
  395. {
  396. AddMsg(_T("SSL Sessions can be reusedn"), FALSE);
  397. strMsg.Format(_T("SSL Session timeout will be %d secondsn"), settings.m_dwSSLSessionTimeout);
  398. AddMsg(strMsg, FALSE);
  399. }
  400. else
  401. AddMsg(_T("SSL Sessions can not be reusedn"), FALSE);
  402. }  
  403. #endif
  404. //Check that everything in the thread pool also started up ok
  405. if (!theWebServer.GetThreadPool().WaitForThreadsInitInstance())
  406. return FALSE;
  407. //Register the console handler to allow the program to be gracefully terminated
  408. //Wait until the server finishes
  409. m_WebTree.SetNodeText( m_hWebRoot, _T(MiniCT_1003)); //MiniCT_1003
  410. m_WebTree.SetNodeColor(m_hWebRoot,RGB(0,128,0));
  411. m_WebTree.SetNodeColor(m_hWebClient,RGB(0,128,0));
  412. m_WebTree.SetNodeColor(m_hWebServer,RGB(0,128,0),TRUE);
  413. GetDlgItem(IDC_BRESERT)->EnableWindow(TRUE);
  414. GetDlgItem(IDC_BSTOP)->EnableWindow(TRUE);
  415. GetDlgItem(IDC_SERVER)->EnableWindow(FALSE);
  416. theWebServer.Wait();
  417. AddMsg(_T("Web server has shut down...n"), FALSE);
  418. return TRUE;
  419. }
  420. BOOL CWebServerPage::OnInitDialog() 
  421. {
  422. CPropertyPage::OnInitDialog();
  423. // g_pWebList = &m_WebList;
  424. CString strMsg;
  425. // TODO: Add extra initialization here
  426. //Initialise Sockets
  427. WSADATA wsaData;
  428. if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) 
  429. {
  430. strMsg.Format(_T("Failed to initialise Winsock, GetLastError:%dn"), ::GetLastError());
  431. AddMsg(strMsg);
  432. return FALSE;
  433. }
  434. //Confirm that the WinSock DLL supports 2.0 
  435. if (LOBYTE(wsaData.wVersion) != 2) 
  436. {
  437. strMsg.Format(_T("Failed to initialize a compatible Winsock 2 TCP/IP stack, GetLastError:%dn"), ::GetLastError());
  438. AddMsg(strMsg);
  439. return FALSE; 
  440. }
  441. m_WebTree
  442. .SetTextFont( 9, FALSE, FALSE, "Arial Unicode MS" )
  443. .SetDefaultTextColor( RGB(0,64,255));
  444. m_hWebRoot = m_WebTree.InsertChild( HTOPNODE, _T(MiniCT_1000)); //MiniCT_1000
  445. m_WebTree.SetNodeColor(m_hWebRoot,RGB(255,0,0));
  446. m_hWebClient = m_WebTree.InsertChild( m_hWebRoot, _T(MiniCT_1001)); //MiniCT_1001
  447. m_WebTree.SetNodeColor(m_hWebClient,RGB(255,0,0));
  448. m_hWebServer = m_WebTree.InsertChild( m_hWebRoot, _T(MiniCT_1002)); //MiniCT_1002
  449. m_WebTree.SetNodeColor(m_hWebServer,RGB(255,0,0));
  450. CString strIniPathName = ((CMiniCaApp *)AfxGetApp())->GetAppPath() +  "\WebServer.ini";
  451. CFile file;
  452. if(!file.Open(strIniPathName,CFile::modeRead))
  453. {
  454. /*得到配置*/
  455. HRSRC hRsrc = 0;
  456. hRsrc = FindResource(NULL,MAKEINTRESOURCE(IDR_WEBSERVER_INI),"INI");
  457. DWORD lenCert = SizeofResource(NULL, hRsrc); 
  458. HGLOBAL hgCert=LoadResource(NULL,hRsrc);
  459. LPSTR lpCert=(LPSTR)LockResource(hgCert);
  460. file.Open(strIniPathName,CFile::modeCreate|CFile::modeWrite); //存文件
  461. {
  462. file.Write(lpCert,lenCert);
  463. }
  464. }
  465. file.Close();
  466. return TRUE;  // return TRUE unless you set the focus to a control
  467.               // EXCEPTION: OCX Property Pages should return FALSE
  468. }
  469. void CWebServerPage::AddMsg(CString strMsg, BOOL bClient, BOOL bSucceed) 
  470. {
  471. if(!bClient && !bSucceed) //错误信息
  472. {
  473. HTREENODE hTemp = m_pWebTree->InsertChild(m_hWebServer, strMsg);
  474. m_pWebTree->SetNodeColor(hTemp,RGB(255,0,0));
  475. }
  476. else
  477. {
  478. if(bClient)
  479. m_pWebTree->InsertChild(m_hWebClient, strMsg);
  480. else
  481. m_pWebTree->InsertChild(m_hWebServer, strMsg);
  482. }
  483. }
  484. void CWebServerPage::OnBstop() 
  485. {
  486. // TODO: Add your control notification handler code here
  487. StopServer();
  488. }
  489. void CWebServerPage::OnClose() 
  490. {
  491. // TODO: Add your message handler code here and/or call default
  492. // OnBstop();
  493. // Sleep(2000);
  494. //Close down sockets
  495. // CPropertyPage::OnClose();
  496. }
  497. void CWebServerPage::StopServer()
  498. {
  499. if(WebServiceThread)
  500. {
  501. pTheWebServer->Stop();
  502. m_WebTree.SetNodeText( m_hWebRoot, _T(MiniCT_1000));  //MiniCT_1000
  503. m_WebTree.SetNodeColor(m_hWebRoot,RGB(255,0,0));
  504. m_WebTree.SetNodeColor(m_hWebClient,RGB(255,0,0));
  505. m_WebTree.SetNodeColor(m_hWebServer,RGB(255,0,0),TRUE);
  506. WebServiceThread = NULL;
  507. GetDlgItem(IDC_BRESERT)->EnableWindow(FALSE);
  508. GetDlgItem(IDC_BSTOP)->EnableWindow(FALSE);
  509. GetDlgItem(IDC_SERVER)->EnableWindow(TRUE);
  510. }
  511. }
  512. BOOL CWebServerPage::IsWebRun()
  513. {
  514. return (WebServiceThread == NULL ? FALSE : TRUE);
  515. }
  516. void CWebServerPage::OnDestroy() 
  517. {
  518. CPropertyPage::OnDestroy();
  519. // TODO: Add your message handler code here
  520. /*#ifdef W3MFC_SSL_SUPPORT
  521.   //Clean up the SSL critical sections
  522.   if (m_pOpenSSLCritSections)
  523.   {
  524.     delete [] m_pOpenSSLCritSections;
  525.     m_pOpenSSLCritSections = NULL;
  526.   }
  527. #endif*/
  528. WSACleanup();
  529. }
  530. void CWebServerPage::OnBresert() //重启服务
  531. {
  532. // TODO: Add your control notification handler code here
  533. StopServer();
  534. OnServer();
  535. }
  536. void CWebServerPage::OnBclear() //信息清除
  537. {
  538. // TODO: Add your control notification handler code here
  539. if(m_hWebClient)
  540. m_WebTree.DeleteNode(m_hWebClient);
  541. if(m_hWebServer)
  542. m_WebTree.DeleteNode(m_hWebServer);
  543. m_hWebClient = m_WebTree.InsertChild( m_hWebRoot, _T(MiniCT_1001));
  544. m_hWebServer = m_WebTree.InsertChild( m_hWebRoot, _T(MiniCT_1002));
  545. m_WebTree.SetNodeColor(m_hWebServer,RGB(255,0,0));
  546. }
  547. void CWebServerPage::TranslateCT() //繙譯諸如樹型控件,列錶控件等內容
  548. {
  549. SetDlgItemText(IDC_BCLEAR, MiniCT_11901);
  550. SetDlgItemText(IDC_BSTOP, MiniCT_11902);
  551. SetDlgItemText(IDC_BRESERT, MiniCT_11903);
  552. SetDlgItemText(IDC_SERVER, MiniCT_11904);
  553. }