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

📄 aspparser.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/********************************************************************/
/*																	*/
/* Function name : ParseInputText									*/
/* Description   : Parse input text -> output ASP script			*/
/*																	*/
/********************************************************************/
BOOL CAspParser::ParseInputText(CString &strText, CString &strResult)
{
    while(!strText.IsEmpty())
	{
        // search for begin tag "<%"
        int nStartPos = strText.Find("<%");
        if (nStartPos == -1)
		{
			// if tag is not found, output remaining text
			strResult += ConvertToCodeBlock(strText);
            break;
        }
        
        // if "<%" has been found, output the preceeding text
		strResult += ConvertToCodeBlock(strText.Left(nStartPos));

		// skip tag
		nStartPos += 2;

        // search for the end tag "%>"
        int nEndPos = strText.Find("%>", nStartPos);
        if (nEndPos == -1)
		{
			// end tag is not found!
			return FALSE;
		}
        
        // get script text
		CString strScript = strText.Mid(nStartPos, nEndPos - nStartPos);
        
		strScript.TrimLeft();

		// if the string begins with = it is translated to a Response.Write
		if (strScript.Left(1) == "=")
		{
			// Replace <%=x%> with 'Response.Write x'
			strResult += "Response.Write ";
			strResult += strScript.Mid(1);
		}
		else
		{
			strResult += strScript;
		}
		strResult += "\r\n";
        
		// get next code segment
        strText = strText.Mid(nEndPos+2);
    }
	return TRUE;        
}


/********************************************************************/
/*																	*/
/* Function name : ConvertToCodeBlock								*/
/* Description   : Convert HTML to ASP code block					*/
/*																	*/
/********************************************************************/
CString CAspParser::ConvertToCodeBlock(LPCTSTR lpszHTML)
{
	CString strHTML = lpszHTML;
	CString strResult;
	
	while(!strHTML.IsEmpty())
	{
		int nIndex = strHTML.Find("\r\n", 0);
		if (nIndex != -1)
		{
			CString strTemp = strHTML.Left(nIndex);
			strTemp.Replace("\"", "\"\"");

			if (!strTemp.IsEmpty())
			{
				strResult += "Response.Write \"";
				strResult += strTemp;
				strResult += "\" & vbCrLf\r\n";
			}
			else
			{
				strResult += "Response.Write \"\" & vbCrLf\r\n";
			}
			// strip previous line
			strHTML = strHTML.Mid(nIndex + 2);
		}
		else
		{
			CString strTemp = strHTML;
			strTemp.Replace("\"", "\"\"");
			
			if (!strTemp.IsEmpty())
			{
				strResult += "Response.Write \"";
				strResult += strTemp;
				strResult += "\"\r\n";
			}
			else
			{
				strResult += "\r\n";
			}
			// we're done!
			break;
		}
	}
	return strResult;
}


/********************************************************************/
/*																	*/
/* Function name : GetResponseBuffer								*/
/* Description   : Return content of response buffer				*/
/*																	*/
/********************************************************************/
BOOL CAspParser::GetResponseBuffer(CString &strBuffer)
{
	strBuffer = m_pResponseObject->GetResponseBuffer();	
	return !strBuffer.IsEmpty();
}


/********************************************************************/
/*																	*/
/* Function name : GetRedirectURL									*/
/* Description   : Return content of redirect URL					*/
/*																	*/
/********************************************************************/
BOOL CAspParser::GetRedirectURL(CString &strURL)
{
	strURL = m_pResponseObject->GetRedirectURL();	
	return !strURL.IsEmpty();
}


/********************************************************************/
/*																	*/
/* Function name : ParseFormVars									*/
/* Description   : Extract parameters from URL						*/
/*																	*/
/********************************************************************/
void CAspParser::ParseFormVars(LPCTSTR lpszParams)
{
	CString strSub;
    int nCount=0;
    
	while(AfxExtractSubString(strSub, lpszParams, nCount++, '&'))
	{
		int nPos = strSub.Find('=');
		if (nPos != -1)
		{
			CString strKey = strSub.Left(nPos);
			m_pRequestObject->AddFormKey(strKey, strSub.Mid(nPos+1));
		}
	}
}


/********************************************************************/
/*																	*/
/* Function name : ParseQueryString									*/
/* Description   : Extract parameters from URL						*/
/*																	*/
/********************************************************************/
void CAspParser::ParseQueryString(LPCTSTR lpszParams)
{
	CString strSub;
    int nCount=0;
    
	while(AfxExtractSubString(strSub, lpszParams, nCount++, '&'))
	{
		int nPos = strSub.Find('=');
		if (nPos != -1)
		{
			CString strKey = strSub.Left(nPos);
			m_pRequestObject->AddQueryStringKey(strKey, strSub.Mid(nPos+1));
		}
	}
}


/********************************************************************/
/*																	*/
/* Function name : ParseRequestCookies								*/
/* Description   : Populate cookie collection						*/
/*																	*/
/********************************************************************/
void CAspParser::ParseRequestCookies(LPCTSTR lpszCookies)
{
	CString strSub;
    int nCount=0;
	BOOL bInCookie = FALSE;
	CCookie cookie;
    
	while(AfxExtractSubString(strSub, lpszCookies, nCount++, ';'))
	{
		int nPos = strSub.Find('=');
		if (nPos != -1)
		{
			CString strName = strSub.Left(nPos);
			strName.TrimLeft();
			strName.TrimRight();

			CString strValue = strSub.Mid(nPos+1);

			if (strName.IsEmpty())
				continue;

			// check for $Version
			if (strName.CompareNoCase("$Version") == 0)
			{
				// ignore for now...
				continue;
			}

			if (strValue.IsEmpty())
				continue;

			if (strName[0] == '$')
			{
				// invalid cookie string
				if (!bInCookie)
					return; 
				else
				{
					// add attribute to current cookie
					strName.TrimLeft('$');
					cookie.AddAttribute(strName, strValue);
				}
			}
			else
			{
				if (!bInCookie)
				{
					bInCookie = TRUE;
					cookie.SetName(strName);
					cookie.ParseValue(strValue);
				}
				else
				{
					// add previous cookie
					CString strPrevName;
					cookie.GetName(strPrevName);

					m_pRequestObject->m_CookieCollection.Add(strPrevName, cookie);

					// empty current cookie and start over
					cookie.Empty();
					cookie.SetName(strName);
					cookie.ParseValue(strValue);
				}
			}
		}
	}

	if (!cookie.IsEmpty())
	{
		CString strName;
		cookie.GetName(strName);
		m_pRequestObject->m_CookieCollection.Add(strName, cookie);
	} 
}


/********************************************************************/
/*																	*/
/* Function name : LoadFile											*/
/* Description   : Load file contents into a CString				*/
/*																	*/
/********************************************************************/
BOOL CAspParser::LoadFile(LPCTSTR lpszFileName, CString &strResult)
{
	try
	{
		CFile aspFile(lpszFileName, CFile::modeRead);
	
		DWORD dwLength = aspFile.GetLength();

		aspFile.Read(strResult.GetBuffer(dwLength), dwLength);
		strResult.ReleaseBuffer();

		aspFile.Close();
	}
	catch(CFileException *e)
	{
		e->Delete();
		return FALSE;
	}
	return TRUE;
}


/********************************************************************/
/*																	*/
/* Function name : SetCurrentDirectory								*/
/* Description   : Set current directory							*/
/*																	*/
/********************************************************************/
void CAspParser::SetCurrentDirectory(LPCTSTR lpszDirectory)
{
	if (m_pServerObject)
		m_pServerObject->SetCurrentDirectory(lpszDirectory);
}



/********************************************************************/
/*																	*/
/* Function name : SetRootDirectory									*/
/* Description   : Set root directory								*/
/*																	*/
/********************************************************************/
void CAspParser::SetRootDirectory(LPCTSTR lpszDirectory)
{
	if (m_pServerObject)
		m_pServerObject->SetRootDirectory(lpszDirectory);
}

⌨️ 快捷键说明

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