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

📄 aspparser.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/********************************************************************/
/*																	*/
/*  AspParser.cpp													*/
/*																	*/
/*  Implementation of the CAspParser class.							*/
/*																	*/
/*  Programmed by Pablo van der Meer								*/
/*	This code is stolen from: http://www.pablovandermeer.nl			*/
/*																	*/
/*  Last updated: July 4, 2003										*/
/*																	*/
/********************************************************************/

#include "stdafx.h"
#include "AspParser.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// Script Engine CLSIDs...
#include <initguid.h>
DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0,
   0xaa, 0x0, 0x4a, 0x55, 0xe8);
DEFINE_GUID(CLSID_JScript, 0xf414c260, 0x6ac0, 0x11cf, 0xb6, 0xd1, 0x00,
   0xaa, 0x00, 0xbb, 0xbb, 0x58);


IMPLEMENT_DYNCREATE(CAspParser, CScriptEngine)

CAspParser::CAspParser()
{
	m_pRequestObject = NULL;
	m_pResponseObject = NULL;
	m_pServerObject = NULL;
}

CAspParser::~CAspParser()
{
}


/********************************************************************/
/*																	*/
/* Function name : OnGetItemInfo									*/
/* Description   : Allows the scripting engine to obtain			*/
/*				   information about an item added with the			*/
/*				   IActiveScript::AddNamedItem method.				*/
/*																	*/
/********************************************************************/
HRESULT CAspParser::OnGetItemInfo(LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppUnknownItem, ITypeInfo** ppTypeInfo)
{
	HRESULT hResult = S_OK;

	CString strName(pstrName);

	// valid ppUnknownItem pointer?
	if (ppUnknownItem != NULL) 
	{
		*ppUnknownItem = NULL;
	}

	// valid ppTypeInfo pointer ?
	if (ppTypeInfo != NULL) 
	{
		*ppTypeInfo = NULL;
	}

	// Check for Response object
	if (wcsicmp(pstrName, L"Response") == 0) 
	{
		// get the dispatch interface
		IDispatch* pDispatch = m_pResponseObject->GetIDispatch(FALSE);

		// is script engine looking for an IUnknown for our object?
		if (dwReturnMask & SCRIPTINFO_IUNKNOWN) 
		{
			// Provide 'Response' object.
			*ppUnknownItem = (IUnknown*)m_pResponseObject->GetIDispatch(TRUE);
		}

		// is script engine expecting an ITypeInfo?
		if (dwReturnMask & SCRIPTINFO_ITYPEINFO) 
		{
			// default to NULL
			*ppTypeInfo = NULL;
		}
	} 
	else
	// Check for Request object
	if (wcsicmp(pstrName, L"Request") == 0) 
	{
		// get the dispatch interface
		IDispatch* pDispatch = m_pRequestObject->GetIDispatch(FALSE);

		// is script engine looking for an IUnknown for our object?
		if (dwReturnMask & SCRIPTINFO_IUNKNOWN) 
		{
			// Provide 'Request' object.
			*ppUnknownItem = (IUnknown*)m_pRequestObject->GetIDispatch(TRUE);
		}

		// is script engine expecting an ITypeInfo?
		if (dwReturnMask & SCRIPTINFO_ITYPEINFO) 
		{
			// default to NULL
			*ppTypeInfo = NULL;
		}
	} 
	else
	// Check for Server object
	if (wcsicmp(pstrName, L"Server") == 0) 
	{
		// get the dispatch interface
		IDispatch* pDispatch = m_pServerObject->GetIDispatch(FALSE);

		// is script engine looking for an IUnknown for our object?
		if (dwReturnMask & SCRIPTINFO_IUNKNOWN) 
		{
			// Provide 'Server' object.
			*ppUnknownItem = (IUnknown*)m_pServerObject->GetIDispatch(TRUE);
		}

		// is script engine expecting an ITypeInfo?
		if (dwReturnMask & SCRIPTINFO_ITYPEINFO) 
		{
			// default to NULL
			*ppTypeInfo = NULL;
		}
	} 
	else
	// Check for Session object
/*	if (wcsicmp(pstrName, L"Session") == 0) 
	{
		// get the dispatch interface
		IDispatch* pDispatch = m_pSessionObject->GetIDispatch(FALSE);

		// is script engine looking for an IUnknown for our object?
		if (dwReturnMask & SCRIPTINFO_IUNKNOWN) 
		{
			// Provide 'Session' object.
			*ppUnknownItem = (IUnknown*)m_pSessionObject->GetIDispatch(TRUE);
		}

		// is script engine expecting an ITypeInfo?
		if (dwReturnMask & SCRIPTINFO_ITYPEINFO) 
		{
			// default to NULL
			*ppTypeInfo = NULL;
		}
	} 
	else */
	{
		CString strError;
		
		strError.Format("The script engine asked for information\nfor an unknown object named \"%s\".", (LPCSTR)strName);
		AfxMessageBox(strError, MB_ICONSTOP | MB_OK);
		hResult = E_UNEXPECTED;
	}
	return hResult;
}


/********************************************************************/
/*																	*/
/* Function name : Initialize										*/
/* Description   : Initialize ASP parser							*/
/*																	*/
/********************************************************************/
BOOL CAspParser::Initialize()
{
	if (!Create(CLSID_VBScript))
		return FALSE;

	m_pRequestObject = new CRequestObject;
	m_pResponseObject = new CResponseObject;
	m_pServerObject = new CServerObject;

	// Add an IResponse item to the engine's name space...
	if (!AddNamedItem(L"Response", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISPERSISTENT))
		return FALSE;

	// Add an IRequest item to the engine's name space...
	if (!AddNamedItem(L"Request", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISPERSISTENT | SCRIPTITEM_GLOBALMEMBERS))
		return FALSE;

	// Add an IServer item to the engine's name space...
	if (!AddNamedItem(L"Server", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISPERSISTENT))
		return FALSE;

	// share cookie collection
	m_pResponseObject->m_pCookieCollection = &m_pRequestObject->m_CookieCollection;
	return TRUE;
}


/********************************************************************/
/*																	*/
/* Function name : CleanUp											*/
/* Description   : Clean up											*/
/*																	*/
/********************************************************************/
void CAspParser::CleanUp()
{
	if (m_pRequestObject)
	{
		delete m_pRequestObject;
	}
	m_pRequestObject = NULL;

	if (m_pResponseObject)
	{
		delete m_pResponseObject;
	}
	m_pResponseObject = NULL;

	if (m_pServerObject)
	{
		delete m_pServerObject;
	}
	m_pServerObject = NULL;

	CScriptEngine::CleanUp();
}


/********************************************************************/
/*																	*/
/* Function name : Execute											*/
/* Description   : Execute provided script							*/
/*																	*/
/********************************************************************/
BOOL CAspParser::Execute(CString &strScript)
{
	CString strResult, strCode;

	if (PreProcessScript(strScript, strResult))
	{
		if (ParseInputText(strResult, strCode))
		{
			// clear the response buffer
			m_pResponseObject->Clear();

			// Parse the code script
			EXCEPINFO ei;
			BSTR pParseText = strCode.AllocSysString();

			if (ParseScriptText(pParseText, NULL, NULL, NULL, 0, 0, 0L, NULL, &ei))
			{
				// Set the engine state. This line actually triggers the execution of the script.
				BOOL bResult = Connect();

				// close the script engine
				Close();
				return bResult;
			}
		}
	}
	// for some reason we have to call these or it will crash...
	Connect();
	Close();
	return FALSE;
}


/********************************************************************/
/*																	*/
/* Function name : PreProcessScript									*/
/* Description   : Pre process script:								*/
/*				   Evaluate SSI (Server Side Includes)				*/
/*																	*/
/********************************************************************/
BOOL CAspParser::PreProcessScript(CString &strInput, CString &strOutput)
{
	CString strTag, strType, strBuff, strFileName;

	strOutput = strInput;

	int nStartPos = strOutput.Find("<!--", 0);

	while(nStartPos != -1)
	{
        int nEndPos = strOutput.Find("-->", nStartPos);

		if (nEndPos != -1)
		{
			// check for #include
			strTag = strOutput.Mid(nStartPos, nEndPos - nStartPos + 3);
			
			strBuff = strTag;
			strBuff.MakeLower();

			int nPos = strBuff.Find("#include");
			if (nPos != -1)
			{
				AfxExtractSubString(strBuff, strTag, 1, '"');
				
				// TODO: check for 'file' or 'virtual'
				if (m_pServerObject)
					strFileName = m_pServerObject->GetCurrentDirectory();

				strFileName += "\\";
				strFileName += strBuff;
				if (!LoadFile(strFileName, strBuff))
				{
					m_strLastError.Format("Couldn't load file: %s", strFileName);
					return FALSE;
				}
				strOutput.Replace(strTag, strBuff);
			}
			// TODO: #echo, #exec, #if
			else
			{
				nStartPos = nEndPos;
			}
		}
		nStartPos = strOutput.Find("<!--", nStartPos);
	}
	return TRUE;
}


⌨️ 快捷键说明

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