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

📄 vhosts.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/Intrface/VHosts.cpp,v $
 * $Date: 2003/05/13 18:42:07 $
 *
 Description:

\*____________________________________________________________________________*/
//$SourceTop:$

#include <iostream.h>
#include <stdio.h>
#include <windowsx.h>

#include "IFaceBse.h"
#include "PIStrStr.h"
#include "StrToken.h"

#include "QConfig.h"
#include "VHosts.hrc"

/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; }
*/
#define D

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define C_IPRESOURCENAME                "IPVHostsPage"
#define C_NONIPRESOURCENAME             "NonIPVHostsPage"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define SECTION         "VirtualHosts"
#define IPVARIABLE      "IP"
#define NONIPVARIABLE   "NonIP"
#define KEY_CONF_FLAG   "Flag"
#define FLAG_IP         "IP"
#define FLAG_NONIP      "NonIP"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
    VHostsPage

Description:
    Creates the eponymous property page and is used by the PropSheet object.

Example:
	<PRE>
	&lt;Object&gt;
		Name VHostsPage
		Class VHostsPageClass
		PropertyPage VHostsPage
	&lt;/Object&gt;

	</PRE>

/*___+++HTMLDOC_END+++___*/
#endif

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
static BOOL CALLBACK fnDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
    LPARAM lParam);

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class VHostsPage : public IFaceBase
{
private:
    QConfig *pConfig;
    PROPSHEETPAGE tPsp;
    Interface *pInterface;

    /*
    ** window private values
    */
    HWND hWndList;
    HWND hWndIPAddress;
    HWND hWndHostname;
    HWND hWndDocumentRoot;
    HWND hWndAdd;
    HWND hWndReplace;
    HWND hWndDelete;
    HWND hWndAdministrator;
    DblList lIPs;
    DblList lHostnames;
    DblList lDocumentRoots;
    DblList lAdministrators;

    /*
    ** Difference between IP and Non-IP virtual hosts
    */
    int iIsIP;
    PIString sVariable;
    PIString sResourceName;

	int Parameter( const char *pVariable, const char *pValue,
		const char *pWhere )
		{
		assert( pVariable && pValue );

		PIOStrStream os;

		os << pWhere << "VHostsPage: ";

        if ( !PIUtil_stricmp( KEY_CONF_FLAG, pVariable ) )
            {
            if ( !PIUtil_stricmp( FLAG_IP, pValue ) )
                {
                iIsIP = 1;
                sVariable = IPVARIABLE;
                sResourceName = C_IPRESOURCENAME;
                }
            else if ( !PIUtil_stricmp( FLAG_NONIP, pValue ) )
                {
                iIsIP = 0;
                sVariable = NONIPVARIABLE;
                sResourceName = C_NONIPRESOURCENAME;
                }
            else
                {
			    os << "Unknown directive '" << pVariable <<
				    "'" << ends;
			    CONFIG_ERR( Object(), os.str() );
			    return 0;
                };
            }
		else
			{
			os << "Unknown directive '" << pVariable <<
				"'" << ends;
			CONFIG_ERR( Object(), os.str() );
			return 0;
			};

		return 1;
		};

public:
	VHostsPage( PIObject *pObject, int iArgc, const char *ppArgv[] )
	:	IFaceBase( pObject ),
        pConfig( FindParentQConfig( PIObject_getDB( pObject ) ) ),
        hWndList( 0 ),
        hWndIPAddress( 0 ),
        hWndHostname( 0 ),
        hWndDocumentRoot( 0 ),
        hWndAdd( 0 ),
        hWndReplace( 0 ),
        hWndDelete( 0 ),
        hWndAdministrator( 0 ),
        pInterface( 0 ),
        iIsIP( -1 )
		{
        /*
        ** Read input parameters
        */
		ReadParameters( iArgc, ppArgv );

        if ( !IsOK() )
            { return; };

        if ( iIsIP==-1 )
            {
            CONFIG_ERR( Object(), "VHostsPage: One of the values 'IP' or \
'NonIP' must be specified for 'Flag'." );
            SetOK( 0 );
            return;
            };

        /*
        ** Setup the PROPSHEETPAGE
        */
        memset( &tPsp, 0, sizeof( PROPSHEETPAGE ) );
        tPsp.dwSize = sizeof( PROPSHEETPAGE );
        tPsp.dwFlags = PSP_DEFAULT;
        tPsp.hInstance = (HINSTANCE)PIDLL_getSystemHandle( PIClass_getLibrary(
            PIObject_getClass( Object() ) ) );

        /* Status page */
        tPsp.pszTemplate = sResourceName;
        tPsp.pfnDlgProc = (DLGPROC)fnDialogProc;
        tPsp.lParam = (LPARAM)this;

        /*
        ** Set the data DB of this object
        */
        PIDB *pDB = PIObject_getDB( Object() );
        PIDB_add( pDB, PIDBTYPE_OPAQUE, INTRFACE_WINDOWTYPE,
            (void *)INTRFACE_PROPPAGE, 0 );
        PIDB_add( pDB, PIDBTYPE_OPAQUE, INTRFACE_PROPSHEETPAGE,
            (void *)&tPsp, 0 );
        
        /*
        ** Done
        */
		};

	~VHostsPage()
		{
		};

    virtual int Handle( Interface &tWindow )
        {
        switch( tWindow.iMessage )
            {
            case INTRFACE_MSG_INIT:
                pInterface = &tWindow;
                break;

            case INTRFACE_MSG_WIN32CREATE:
                return Win32Create( tWindow );

            case INTRFACE_MSG_LOAD:
                return PIAPI_ABORT;

            case INTRFACE_MSG_DEFAULTS:
                break;

            default:
                assert( 0 );
                return PIAPI_ABORT;
            };

        return PIAPI_COMPLETED;
        };

    int Win32Create( Interface &tWindow )
        {
        /* --- property sheet pages should not be created --- */
        return PIAPI_ABORT;
		};

    int Internal_verifyFields( HWND hDlg, char *pszBuf, int iBufSize )
        {
	    int iToLen;
	
	    /*
	    ** Get the 'DocumentRoot' field
	    */
	    SendMessage( hWndDocumentRoot, WM_GETTEXT, iBufSize, (LPARAM)pszBuf );
	
	    /*
	    ** 'DocumentRoot' field must end with '/'
	    */
	    assert( *pszBuf );
	    iToLen = strlen(pszBuf);
	    if ( pszBuf[iToLen-1]!='/' && pszBuf[iToLen-1]!='\\' )
	        {
	        MessageBox( hDlg, "'DocumentRoot' field must end with a \
forward-slash ('/') or a backslash ('\\').",
	            "Error in 'DocumentRoot' Field", MB_OK | MB_ICONEXCLAMATION );
	
	        ::SetFocus( hWndDocumentRoot );
	        ::SendMessage( hWndDocumentRoot, EM_SETSEL, (WPARAM)(INT)0,
                (LPARAM)(INT)-1 );
	
	        goto verify_fail;
	        };
	
	    /*
	    ** Good
	    */
	    return 1;
	
verify_fail:
	    return 0;
        };
	
    void Internal_addLine( const char *pLine )
        {
		assert( pLine );
        if ( iIsIP )
            {
		    assert( lIPs.Size()==lHostnames.Size() );
            };
		assert( lHostnames.Size()==lDocumentRoots.Size() );
		assert( lDocumentRoots.Size()==lAdministrators.Size() );
	
		int i, j;
	    i = 0;
		for( ; pLine[i] && isspace(pLine[i]); i++ );
	    j = i;
		for( ; pLine[i] && !isspace(pLine[i]); i++ );
		
        if ( iIsIP )
            {
            lIPs.Append( (DblList::type)PI_NEW( PIString( &(pLine[j]), i-j)));
	
		    /* --- scan over whitespace to start of second field --- */
		    for( ; pLine[i] && isspace(pLine[i]); i++ );
	        j = i;
		    for( ; pLine[i] && !isspace(pLine[i]); i++ );
            };

		lHostnames.Append((DblList::type)PI_NEW( PIString( &(pLine[j]), i-j)));	
	
		/* --- scan over whitespace to start of second field --- */
		for( ; pLine[i] && isspace(pLine[i]); i++ );
	    j = i;
		for( ; pLine[i] && !isspace(pLine[i]); i++ );
		lDocumentRoots.Append((DblList::type)PI_NEW( PIString( &(pLine[j]),
            i-j)));	
	
		/* --- scan over whitespace to start of third field --- */
		for( ; pLine[i] && isspace(pLine[i]); i++ );
	    j = i;
		for( ; pLine[i] && !isspace(pLine[i]); i++ );
		lAdministrators.Append((DblList::type)PI_NEW( PIString( &(pLine[j]),
            i-j)));	
	
        if ( iIsIP )
            {
		    assert( lIPs.Size()==lHostnames.Size() );
            };
		assert( lHostnames.Size()==lDocumentRoots.Size() );
		assert( lDocumentRoots.Size()==lAdministrators.Size() );
        };
	
    void Internal_clearArrays()
        {
		DblListIterator w( lIPs );
		DblListIterator x( lHostnames );
		DblListIterator y( lDocumentRoots );
		DblListIterator z( lAdministrators );
		for( ; !x.BadIndex(); )
			{
            if ( iIsIP ) { assert( !w.BadIndex() ); };
			assert( !x.BadIndex() && !y.BadIndex() && !z.BadIndex() );
            if ( iIsIP ) { PI_DELETE( (PIString *)w.Current() ); };
			PI_DELETE( (PIString *)x.Current() );
			PI_DELETE( (PIString *)y.Current() );
			PI_DELETE( (PIString *)z.Current() );

            if ( iIsIP ) { w++; };
            x++;
            y++;
            z++;
			};
		lIPs.Clear();
		lHostnames.Clear();
		lDocumentRoots.Clear();
		lAdministrators.Clear();
        };
	
    void Internal_reloadArrays()
        {
		enum { BUF_SIZE=2047 };
		char szBuf[BUF_SIZE+1];
	
		Internal_clearArrays();
	
		/*
		** Get number of items
		*/
		int iCount = SendMessage( hWndList, LB_GETCOUNT, 0, 0 );
	
		/* 
		** Insert from listbox into internal arrays
		*/
		int i;
		for( i=0; i<iCount; i++ )
			{
			SendMessage( hWndList, LB_GETTEXT, i, (LPARAM)szBuf );
			Internal_addLine( szBuf );
			};
        };
	
    void Internal_updateButtons( WPARAM wParam, LPARAM lParam,
	    HWND hWndEdit1, HWND hWndEdit2, const char *pData )
        {
		if ( GET_WM_COMMAND_CMD(wParam, lParam)!=EN_CHANGE )
			{ return; };
	
		int iExistingIndex = -1;
		assert( pData );
		int iChangeToIndex = -1;
		int iIndex = 0;
		if ( *pData )
			{
			for( DblListIterator i( lHostnames );!i.BadIndex(); i++,iIndex++)
				{
				PIString *pString = (PIString *)i.Current();
				assert( pString );
				if ( !PIUtil_strncmpi( (*pString), pData, strlen( pData ) ) )
					{
					iChangeToIndex = iIndex;
					/*
					** If this is a full index, select it
					*/
					if ( !stricmp( (*pString), pData ) )
						{ iExistingIndex = iIndex; };
					break;
					};
				};
			};
	
		/*
		** Remove listbox selection or scroll to correct line
		** as appropriate 
		*/
		SendMessage( hWndList, LB_SETCURSEL, iChangeToIndex, 0 );
	
		/*
		** Set buttons
		*/
		if ( ::SendMessage( hWndEdit1, EM_LINELENGTH, 0, 0 )==0 ||
		    ::SendMessage( hWndDocumentRoot, EM_LINELENGTH, 0, 0 )==0 ||
		    ::SendMessage( hWndAdministrator, EM_LINELENGTH, 0, 0 )==0 )
			{
			if ( IsWindowEnabled( hWndAdd ) ) EnableWindow( hWndAdd, FALSE );
			if ( IsWindowEnabled( hWndReplace ) ) EnableWindow( hWndReplace, FALSE );
			}
		else if ( SendMessage( hWndEdit2, EM_LINELENGTH, 0, 0 )!=0 )
			{
			if ( iExistingIndex>-1 )	/* --- disable add, enable replace --- */
				{

⌨️ 快捷键说明

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