⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 general.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/EnhPi3/General.cpp,v $
 * $Date: 2003/05/13 18:41:57 $
 *
 Description:

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <assert.h>
#include <stdio.h>

#include "PICompat.h"
#include "PIString.h"

#include "resrc1.h"
#include "Common.h"

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
static HWND hWndHost = 0;
static HWND hWndIP = 0;
static HWND hWndLocal = 0;
static HWND hWndEdit = 0;
static HWND hWndPort = 0;
static HWND hWndURL = 0;
static HWND hWndAdmin = 0;
static HWND hWndSsl = 0;
static HWND hWndRemote = 0;
static HWND hWndVerbose = 0;
static PIString *pHost;
static PIString *pIP;
static PIString *pLocal;
static int __iCurrentRadioButton = 0;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static PIString *Internal_stringFromRadioButton( int iId )
{
	switch( iId )
		{
		case IDC_REMOTEHOST:	return pHost;
		case IDC_REMOTEIP:	return pIP;
		case IDC_LOCAL:	return pLocal;
		default:
			assert( 0 );
		};

	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static void Internal_radioButtonChange( int iNewId )
{
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	/* --- Get current value --- */
	*szBuf = '\0';
	SendMessage( hWndEdit, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
	*(Internal_stringFromRadioButton( __iCurrentRadioButton )) = szBuf;

	__iCurrentRadioButton = iNewId;

	SendMessage( hWndEdit, WM_SETTEXT, 0, (LPARAM)(const char *)
		*(Internal_stringFromRadioButton( __iCurrentRadioButton )) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static void Internal_updateURL( HWND hDlg )
{
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	/* --- Get current value --- */
	assert( AD_lookupValue( __pConfig, "Internal", "URLProtocol" ) );
	strcpy( szBuf, AD_lookupValue( __pConfig, "Internal", "URLProtocol" ) );
	int iLen = strlen( szBuf );
	SendMessage( hWndEdit, WM_GETTEXT, BUF_SIZE-7, (LPARAM)&(szBuf[iLen]) );
	iLen = strlen( szBuf );
	szBuf[iLen++] = ':';
	SendMessage( hWndPort, WM_GETTEXT, BUF_SIZE-iLen, (LPARAM)&(szBuf[iLen]) );
	assert( AD_lookupValue( __pConfig, "Internal", "DefaultPort" ) );
	if ( atoi( &(szBuf[iLen]) )==atoi(AD_lookupValue( __pConfig, "Internal", 
			"DefaultPort" )) )
		{ szBuf[--iLen] = '\0'; };

	/* --- now update the URL --- */
	SendMessage( hWndURL, WM_SETTEXT, 0, (LPARAM)szBuf );
	
	/* --- mark property sheet as modified --- */
	AD_Changed( hDlg );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static BOOL CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
	LPARAM lParam)
{
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	switch (uMsg)
		{
		case WM_INITDIALOG:
			{
			const char *pA = AD_lookupValue( __pConfig, "General", "Admin" );
			hWndHost = GetDlgItem( hDlg, IDC_REMOTEHOST );
			hWndIP = GetDlgItem( hDlg, IDC_REMOTEIP );
			hWndLocal = GetDlgItem( hDlg, IDC_LOCAL );
			hWndEdit = GetDlgItem( hDlg, IDC_HOST );
			hWndPort = GetDlgItem( hDlg, IDC_PORT );
			hWndURL = GetDlgItem( hDlg, IDC_URL );
			hWndAdmin = GetDlgItem( hDlg, IDC_ADMIN );
			hWndSsl = GetDlgItem( hDlg, IDC_SSL );
			hWndRemote = GetDlgItem( hDlg, IDC_REMOTE );
			hWndVerbose = GetDlgItem( hDlg, IDC_VERBOSE );
			pHost = PI_NEW( PIString );
			pIP = PI_NEW( PIString );
			pLocal = PI_NEW( PIString );

			assert( hWndHost && hWndIP && hWndLocal );
			assert( hWndEdit && hWndPort && hWndURL && hWndAdmin );

			assert( pA );

			::SendMessage( hWndAdmin, WM_SETTEXT, 0, (LPARAM)pA );

			::SendMessage( hWndHost, BM_SETCHECK, BST_UNCHECKED, 0 );
			::SendMessage( hWndIP, BM_SETCHECK, BST_UNCHECKED, 0 );
			::SendMessage( hWndLocal, BM_SETCHECK, BST_UNCHECKED, 0 );
			::SendMessage( hWndSsl, BM_SETCHECK, BST_UNCHECKED, 0 );
			::SendMessage( hWndRemote, BM_SETCHECK, BST_UNCHECKED, 0 );
			::SendMessage( hWndVerbose, BM_SETCHECK, BST_UNCHECKED, 0 );

			/* --- just for sanity --- */
			::SendMessage( hWndEdit, EM_SETLIMITTEXT, 256, 0 );
			::SendMessage( hWndPort, EM_SETLIMITTEXT, 256, 0 );

			::SendMessage( ::GetDlgItem( hDlg, IDC_INSTALLDIR ), WM_SETTEXT, 0,
				(LPARAM)AD_lookupValue( __pConfig, "General", "InstallDir" ));
			::SendMessage( ::GetDlgItem( hDlg, IDC_OS ), WM_SETTEXT, 0,
				(LPARAM)AD_lookupValue( __pConfig, "General", "OS" ));

			/*
			** Get local network information
			*/	
			*szBuf = '\0';
			if ( ::gethostname( szBuf, BUF_SIZE ) )
				{
				/*
				** Error
				*/
				*pHost = "localhost";
				*pIP = "127.0.0.1";
				}
			else
				{
				struct hostent *pEnt = ::gethostbyname( szBuf );
				*pHost = szBuf;
				if ( !pEnt )
					{
					/*
					** Error	
					*/
					*pIP = "127.0.0.1";
					}
				else
					{
					*pIP = inet_ntoa( *((in_addr *)(pEnt->h_addr_list)[0]) );	
					};
				};

			*pLocal = "localhost";

			const char *pUse = AD_lookupValue( __pConfig, "Identity", "Use" );
			const char *pTheHost = AD_lookupValue( __pConfig, "General", "Host" );
			SendMessage( hWndEdit, WM_SETTEXT, 0, (LPARAM)(const char *)pTheHost );
			if ( !stricmp( pUse, "local" ) )
				{
				SendMessage( hWndLocal, BM_SETCHECK, BST_CHECKED, 0 );
				__iCurrentRadioButton = IDC_LOCAL;
				SendMessage( hWndEdit, EM_SETREADONLY, (BOOL)TRUE, 0 );
				}
			else if ( !stricmp( pUse, "remote" ) )
				{
				SendMessage( hWndHost, BM_SETCHECK, BST_CHECKED, 0 );
				__iCurrentRadioButton = IDC_REMOTEHOST;
				SendMessage( hWndEdit, EM_SETREADONLY, (BOOL)FALSE, 0 );
				}
			else if ( !stricmp( pUse, "ip" ) )
				{
				SendMessage( hWndIP, BM_SETCHECK, BST_CHECKED, 0 );
				__iCurrentRadioButton = IDC_REMOTEIP;
				SendMessage( hWndEdit, EM_SETREADONLY, (BOOL)FALSE, 0 );
				};

			const char *pTest = AD_lookupValue( __pConfig, "Internal", "HandlerLine" );
			if ( strstr( pTest, "TopSiteRoot" ) )
				{
				SendMessage( hWndRemote, BM_SETCHECK, BST_CHECKED, 0 );
				}

			if ( strstr( AD_lookupValue( __pConfig, "General", "ErrorMessages" ), "Verbose" ))
				{
				SendMessage( hWndVerbose, BM_SETCHECK, BST_CHECKED, 0 );
				}

			// Port and SSL flag are set and URL is updated
			// in PSN_SETACTIVE handler

			AD_UnChanged( hDlg );
			};
			break;

		case WM_COMMAND:
			if ( GET_WM_COMMAND_CMD(wParam, lParam)==BN_CLICKED )
				{
				switch( GET_WM_COMMAND_ID( wParam, lParam) )
					{
					case IDC_REMOTEHOST:
						Internal_radioButtonChange( IDC_REMOTEHOST );	
						SendMessage( hWndEdit, EM_SETREADONLY, (BOOL)FALSE, 0 );
						break;

					case IDC_REMOTEIP:	
						Internal_radioButtonChange( IDC_REMOTEIP );	
						SendMessage( hWndEdit, EM_SETREADONLY, (BOOL)FALSE, 0 );
						break;

					case IDC_LOCAL:	
						Internal_radioButtonChange( IDC_LOCAL );	
						SendMessage( hWndEdit, EM_SETREADONLY, (BOOL)TRUE, 0 );
						break;

					case IDC_SSL:
						*szBuf = '\0';
						SendMessage( hWndPort, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
						if ( ::SendMessage( hWndSsl, BM_GETCHECK, 0, 0 )==BST_CHECKED )
							{
							AD_replaceValue( __pConfig, "Internal", "URLProtocol", "https://" );
							AD_replaceValue( __pConfig, "Internal", "DefaultPort", "443" );
							if (!stricmp(szBuf, "80")) {
								SendMessage( hWndPort, WM_SETTEXT, 0, (LPARAM)"443");
								};
							}
						else
							{
							AD_replaceValue( __pConfig, "Internal", "URLProtocol", "http://" );
							AD_replaceValue( __pConfig, "Internal", "DefaultPort", "80" );
							if (!stricmp(szBuf, "443")) {
								SendMessage( hWndPort, WM_SETTEXT, 0, (LPARAM)"80");
								};
							};
						Internal_updateURL( hDlg );
						break;

					case IDC_REMOTE:
					case IDC_VERBOSE:
						AD_Changed( hDlg );
						break;

					default:;
					};
				}
			else if ( GET_WM_COMMAND_CMD(wParam, lParam)==EN_CHANGE )
				{ Internal_updateURL( hDlg ); };
			break;

		case WM_DESTROY:
			PI_DELETE( pHost );
			PI_DELETE( pIP );
			PI_DELETE( pLocal );
			break;

		case WM_NOTIFY:
			switch( ((NMHDR *)lParam)->code )
				{
				case PSN_SETACTIVE:
					{
					int iSaveChanged = AD_IsChanged();
					SendMessage( hWndPort, WM_SETTEXT, 0, (LPARAM)
						AD_lookupValue( __pConfig, "General", "Port" ) );

					const char *pTest = AD_lookupValue( __pConfig, "Internal", \
						"ServerIOObjectLine" );
					SendMessage( hWndSsl, BM_SETCHECK, (WPARAM)strstr( pTest, "SSLIO" ), 0 );
					Internal_updateURL( hDlg );
					if ( !iSaveChanged ) { AD_UnChanged( hDlg ); };
					SetWindowLong( hDlg, DWL_MSGRESULT, FALSE );
					return (TRUE);
					}

				case PSN_QUERYCANCEL:
					return AD_cancel( hDlg );
				
				case PSN_KILLACTIVE:
					if ( AD_IsChanged() )
					{
					*szBuf = '\0';
					::SendMessage( ::GetDlgItem( hDlg, IDC_INSTALLDIR ),
						WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "General", "InstallDir", szBuf ); 

					*szBuf = '\0';
					::SendMessage( ::GetDlgItem( hDlg, IDC_OS ),
						WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "General", "OS", szBuf ); 

					*szBuf = '\0';
					SendMessage( hWndPort, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					int iPort = atoi( szBuf );
					if ( iPort<0 || iPort>65535 )
						{
						/*
						** Give message and abort change
						*/
						MessageBox( hDlg, "Port must be in the range [0..65536]",
							"Port Field Error", MB_OK );
						SetFocus( hWndPort );
						SendMessage( hWndPort, EM_SETSEL, 0, -1 );
						SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
						return (TRUE);
						};
					AD_replaceValue( __pConfig, "General", "Port", szBuf ); 
					SendMessage( hWndAdmin, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "General", "Admin", szBuf ); 
					SendMessage( hWndEdit, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "General", "Host", szBuf ); 
					SendMessage( hWndURL, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
					AD_replaceValue( __pConfig, "Identity", "URL", szBuf );
					switch( __iCurrentRadioButton )
						{
						case IDC_LOCAL:
							AD_replaceValue( __pConfig, "Identity", "Use", "Local" );	
							break;

						case IDC_REMOTEHOST:
							AD_replaceValue( __pConfig, "Identity", "Use", "Remote" );	
							break;

						case IDC_REMOTEIP:
							AD_replaceValue( __pConfig, "Identity", "Use", "IP" );	
							break;

						default:
							assert( 0 );
						};

					if ( ::SendMessage( hWndSsl, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{
						AD_replaceValue( __pConfig, "Internal", "ServerIOObjectLine", \
							"\tIOObject SSLIOObject" );
						}
					else
						{
						AD_replaceValue( __pConfig, "Internal", "ServerIOObjectLine", \
							"\tIOObject ServerIOObject" );
						}

					if ( ::SendMessage( hWndRemote, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{
						AD_replaceValue( __pConfig, "Internal", "HandlerLine", \
							"\tHandlers TopSiteRoot" );
  						}
					else
						{
						AD_replaceValue( __pConfig, "Internal", "HandlerLine", \
							DEFAULT_HANDLERS );
						}

					if ( ::SendMessage( hWndVerbose, BM_GETCHECK, 0, 0 )==BST_CHECKED )
						{
							/*
							** Give warning message
							*/
						if ( __iCurrentRadioButton != IDC_LOCAL )
							{
							MessageBox( hDlg, "This kind of error messages are extremely verbose. \
Don't even think about using them in an internet server.", "Verbose Errors Warning", MB_OK | MB_ICONWARNING );
							}
						AD_replaceValue( __pConfig, "General", "ErrorMessages",
							"Verbose" );
						}
					else
						{
						AD_replaceValue( __pConfig, "General", "ErrorMessages",
							"Short" );
						}
					}
					SetWindowLong( hDlg, DWL_MSGRESULT, FALSE );
					return (TRUE);

				default:
					return (FALSE);
				};
			break;

		default:
			return (FALSE);
		};

	return (TRUE);
}

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
DLGPROC fnGeneral = (DLGPROC)DialogProc;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -