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

📄 io.cpp

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

 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/IO.cpp,v $
 * $Date: 2003/06/01 13:47:42 $
 *
 Description:

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

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

#include "DblList.h"
#include "PIString.h"

#include "resrc1.h"
#include "Dialogs.h"
#include "Config.h"


/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
static HWND hWndList = 0;
static HWND hWndType = 0;
static HWND hWndHost = 0;
static HWND hWndPort = 0;
static HWND hWndSend = 0;
static HWND hWndRecv = 0;
static HWND hWndAdd = 0;
static HWND hWndReplace = 0;
static HWND hWndDelete = 0;
static HWND hWndKeys = 0;
static DblList lTypes;
static DblList lHosts;
static DblList lPorts;
static DblList lSends;
static DblList lRecvs;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static int Internal_verifyFields( HWND hDlg, char *pszBuf, int iBufSize )
{
	assert( pszBuf && iBufSize );
	*pszBuf = '\0';

	/*
	** Scan Hostname for spaces
	*/
	SendMessage( hWndHost, WM_GETTEXT, iBufSize, (LPARAM)pszBuf );
	int iLen = strlen(pszBuf);
	for( int iTest=0; iTest<iLen; iTest++)
		{
		if ( isspace( pszBuf[iTest] ) )
			{
			/*
			** Give message and abort change
			*/
			MessageBox( hDlg, "'Hostname' field may not contain spaces or tabs.",
				"Configuration Error", MB_OK | MB_ICONEXCLAMATION );
			SetFocus( hWndHost );
			SendMessage( hWndHost, EM_SETSEL, 0, -1 );
			SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
			goto verify_fail;
			};
		};

	/*
	** Check Port
	*/
	SendMessage( hWndPort, WM_GETTEXT, iBufSize, (LPARAM)pszBuf );
	iTest = atoi( pszBuf );
	if ( iTest<0 || iTest>65535 )
		{
		/*
		** Give message and abort change
		*/
		MessageBox( hDlg, "Port must be in the range [0..65536]",
			"Configuration Error", MB_OK | MB_ICONEXCLAMATION );
		SetFocus( hWndPort );
		SendMessage( hWndPort, EM_SETSEL, 0, -1 );
		SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
		goto verify_fail;
		};

	/*
	** Send Timeout
	*/
	::SendMessage( hWndSend, WM_GETTEXT, iBufSize, (LPARAM)pszBuf );
	iLen = strlen(pszBuf);
	for( iTest=0; iTest<iLen; iTest++)
		{
		if ( isalpha( pszBuf[iTest] ) )
			{
			/*
			** Give message and abort change
			*/
			MessageBox( hDlg, "Field contains alphanumeric characters.",
				"Configuration Error", MB_OK | MB_ICONEXCLAMATION );
			SetFocus( hWndSend );
			SendMessage( hWndSend, EM_SETSEL, 0, -1 );
			SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
			goto verify_fail;
			};
		};

	iTest = atoi( pszBuf );
	if ( iTest<1 && iTest!=-1 )
		{
		/*
		** Give message and abort change
		*/
		MessageBox( hDlg, "Send timeout must be in the range [-1,1..n]",
			"Configuration Error' Field", MB_OK );
		SetFocus( hWndSend );
		SendMessage( hWndSend, EM_SETSEL, 0, -1 );
		SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
		goto verify_fail;
		};

	/*
	** Receive Timeout
	*/
	::SendMessage( hWndRecv, WM_GETTEXT, iBufSize, (LPARAM)pszBuf );
	iLen = strlen(pszBuf);
	for( iTest=0; iTest<iLen; iTest++)
		{
		if ( isalpha( pszBuf[iTest] ) )
			{
			/*
			** Give message and abort change
			*/
			MessageBox( hDlg, "Field contains alphanumeric characters.",
				"Configuration Error", MB_OK | MB_ICONEXCLAMATION );
			SetFocus( hWndRecv );
			SendMessage( hWndRecv, EM_SETSEL, 0, -1 );
			SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
			goto verify_fail;
			};
		};

	iTest = atoi( pszBuf );
	if ( iTest<1 && iTest!=-1 )
		{
		/*
		** Give message and abort change
		*/
		MessageBox( hDlg, "Receive timeout must be in the range [-1,1..n]",
			"Configuration Error' Field", MB_OK );
		SetFocus( hWndRecv );
		SendMessage( hWndSend, EM_SETSEL, 0, -1 );
		SetWindowLong( hDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE );
		goto verify_fail;
		};

	/*
	** Good
	*/
	return 1;

verify_fail:

	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Insert values from listbox into values array. Sort
	the lines in reverse order of virtual path component length.
\*____________________________________________________________________________*/
static void Internal_saveValues( HWND hDlg )
{
	enum { BUF_SIZE=1023 };
	char pszBuf[BUF_SIZE+1];

	for(;;)
		{
		int iCount = SendMessage( hWndList, LB_GETCOUNT, 0, 0 );

		if ( !iCount )
			{ return; };

		int iBiggest = -1;
		int iBiggestIndex = -1;

		for( int j=0; j<iCount; j++ )	
			{
			/*
			** Get string
			*/
			if ( SendMessage( hWndList, LB_GETTEXTLEN, j, 0 ) >= BUF_SIZE )
				{
				MessageBox( hDlg, "Line too long", "Internal Error", MB_OK );
				return;
				};
			SendMessage( hWndList, LB_GETTEXT, j, (LPARAM)pszBuf );

			
			/*
			** Get number of components in path
			*/
			int iNumComponents = 0;
			int i=0;
			for( ; pszBuf[i] && pszBuf[i]!='\t'; i++ );
			for( ; pszBuf[i] && isspace(pszBuf[i]); i++ );
			for( ; pszBuf[i] && !isspace(pszBuf[i]); i++ )
				{
				if ( pszBuf[i]=='/' )
					{
					iNumComponents++;
					i++; 
					if ( !pszBuf[i] || isspace(pszBuf[i]) )
						{ break; };
					};
				};

			if ( iNumComponents>iBiggest )
				{
				iBiggestIndex = j;
				iBiggest = iNumComponents;
				};
			};

		/*
		** Add biggest line to list and remove it from
		** listbox
		*/
		SendMessage( hWndList, LB_GETTEXT, iBiggestIndex, (LPARAM)pszBuf );
		SendMessage( hWndList, LB_DELETESTRING, iBiggestIndex, 0 );
		AD_addValue( __pConfig, "IOObjects", "IOObject", pszBuf );
		};
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static void Internal_addLine( const char *pLine )
{
	assert( pLine );
	assert( lTypes.Size()==lHosts.Size() );
	assert( lHosts.Size()==lPorts.Size() );
	assert( lPorts.Size()==lSends.Size() );
	assert( lSends.Size()==lRecvs.Size() );

	int i = 0;
	/* --- scan to tab --- */
	for( ; pLine[i] && pLine[i]!='\t'; i++ );
	lTypes.Append( (DblList::type)PI_NEW( PIString( pLine, i ) ) );

	/* --- scan over whitespace to start of Host --- */
	for( ; pLine[i] && isspace(pLine[i]); i++ );
	pLine = &( pLine[i] );	
	i=0;
	/* --- scan to tab --- */
	for( ; pLine[i] && pLine[i]!='\t'; i++ );
	lHosts.Append( (DblList::type)PI_NEW( PIString( pLine, i ) ) );	

	/* --- scan over whitespace to start of Port --- */
	for( ; pLine[i] && isspace(pLine[i]); i++ );
	pLine = &( pLine[i] );	
	i=0;
	/* --- scan to tab --- */
	for( ; pLine[i] && pLine[i]!='\t'; i++ );
	lPorts.Append( (DblList::type)PI_NEW( PIString( pLine, i ) ) );	

	/* --- scan over whitespace to start of Send --- */
	for( ; pLine[i] && isspace(pLine[i]); i++ );
	pLine = &( pLine[i] );	
	i=0;
	/* --- scan to tab --- */
	for( ; pLine[i] && pLine[i]!='\t'; i++ );
	lSends.Append( (DblList::type)PI_NEW( PIString( pLine, i ) ) );	

	/* --- scan over whitespace to start of Recv --- */
	for( ; pLine[i] && isspace(pLine[i]); i++ );
	pLine = &( pLine[i] );	
	i=0;
	/* --- scan to tab --- */
	for( ; pLine[i] && pLine[i]!='\t'; i++ );
	lRecvs.Append( (DblList::type)PI_NEW( PIString( pLine, i ) ) );	

	assert( lTypes.Size()==lHosts.Size() );
	assert( lHosts.Size()==lPorts.Size() );
	assert( lPorts.Size()==lSends.Size() );
	assert( lSends.Size()==lRecvs.Size() );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static void Internal_updateButtons( WPARAM wParam, LPARAM lParam, 
	const char *pHostname, const char *pPort, int iTypeChanged )
{
	int iCBSel = SendMessage( hWndType, CB_GETCURSEL, 0, 0 );
	const char *pType = 0;
	if ( iCBSel>-1 )
		{
		pType = AD_lookupValueEx( __pConfig, "Internal", 
			"IOTypes", iCBSel );
		}
	else
		{
		return;
		}

	int iExistingIndex = -1;
	assert( pType && pHostname && pPort );
	int iIndex = 0;
	if ( *pType )
		{
		if ( !stricmp( pType, "SSL" ) && iTypeChanged
		&& ( !stricmp( pPort, "" ) || !stricmp( pPort, "80" )))
			{
			SendMessage( hWndPort, WM_SETTEXT, 1, (LPARAM)"443" );
			return;
			}

		if ( !stricmp( pType, "TCP" ) && iTypeChanged
		&& ( !stricmp( pPort, "" ) || !stricmp( pPort, "443" )))
			{
			SendMessage( hWndPort, WM_SETTEXT, 1, (LPARAM)"80" );
			return;
			}

		/*
		** If the 'type', 'hostname' or 'port' changed, check is it an existing one.
		*/
		DblListIterator i( lTypes );
		DblListIterator j( lHosts );
		DblListIterator k( lPorts );
		for( ; !i.BadIndex(); i++, j++, k++, iIndex++ )
			{
			PIString *pT = (PIString *)i.Current();
			PIString *pH = (PIString *)j.Current();
			PIString *pP = (PIString *)k.Current();
			assert( pT && pH && pP);
			if ( (*pT)==pType && (*pH)==pHostname && (*pP)==pPort )
				{
				iExistingIndex = iIndex;
				break;
				};
			};
		};

	/*
	** Remove listbox selection or scroll to correct line
	** as appropriate 
	*/
	SendMessage( hWndList, LB_SETCURSEL, iExistingIndex, 0 );

	/*
	** Set buttons
	*/
	if ( iCBSel<0 || SendMessage( hWndType, CB_GETLBTEXTLEN, iCBSel, 0 )==0 ||
			SendMessage( hWndHost, EM_LINELENGTH, 0, 0 )==0 ||
			SendMessage( hWndPort, EM_LINELENGTH, 0, 0 )==0 )
		{
		if ( IsWindowEnabled( hWndAdd ) ) EnableWindow( hWndAdd, FALSE );
		if ( IsWindowEnabled( hWndReplace ) ) EnableWindow( hWndReplace, FALSE );
		}
	else
		{
		if ( iExistingIndex>-1 )	/* --- disable add, enable replace --- */
			{
			if ( IsWindowEnabled( hWndAdd ) ) EnableWindow( hWndAdd, FALSE );
			if ( !IsWindowEnabled( hWndReplace ) ) EnableWindow( hWndReplace, TRUE );
			}
		else						/* --- disable replace, enable add --- */
			{
			if ( !IsWindowEnabled( hWndAdd ) ) EnableWindow( hWndAdd, TRUE );
			if ( IsWindowEnabled( hWndReplace ) ) EnableWindow( hWndReplace, FALSE );
			};
		};

	/*
	** If there is a selected item in the listbox then
	** enable delete, otherwise disable delete	
	*/
	if ( SendMessage( hWndList, LB_GETCURSEL, 0, 0 )==LB_ERR )
		{
		EnableWindow( hWndDelete, FALSE );
		EnableWindow( hWndKeys, !stricmp( pType, "SSL" ) &&
			IsWindowEnabled( hWndAdd )
			);
		}
	else
		{
		EnableWindow( hWndDelete, TRUE );
		EnableWindow( hWndKeys, !stricmp( pType, "SSL" ));
		};
}

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

	switch (uMsg)
		{
		case WM_INITDIALOG:
			AD_hourglassOn();
			{
			int aTabstop[5];
			hWndList = GetDlgItem( hDlg, IDC_LIST );
			hWndType = GetDlgItem( hDlg, IDC_TYPE );
			hWndHost = GetDlgItem( hDlg, IDC_HOSTNAME );
			hWndPort = GetDlgItem( hDlg, IDC_PORT );
			hWndSend = GetDlgItem( hDlg, IDC_SEND );
			hWndRecv = GetDlgItem( hDlg, IDC_RECV );

⌨️ 快捷键说明

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