在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例C/C++语言基础 → 支持ASP的WEB服务器源代码,采用MFC编写(web服务器)

支持ASP的WEB服务器源代码,采用MFC编写(web服务器)

C/C++语言基础

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.12M
  • 下载次数:30
  • 浏览次数:322
  • 发布时间:2015-03-30
  • 实例类别:C/C++语言基础
  • 发 布 人:gwf57011444
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 服务器 服务

实例介绍

【实例简介】
【实例截图】

【核心代码】


#include "stdafx.h"
#include "server.h"
#include "ServerObject.h"

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


IMPLEMENT_DYNCREATE(CServerObject, CCmdTarget)

CServerObject::CServerObject()
{
	EnableAutomation();
}

CServerObject::~CServerObject()
{
}


void CServerObject::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(CServerObject, CCmdTarget)
	//{{AFX_MSG_MAP(CServerObject)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CServerObject, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CServerObject)
	DISP_FUNCTION(CServerObject, "URLEncode", URLEncode, VT_BSTR, VTS_BSTR)
	DISP_FUNCTION(CServerObject, "CreateObject", CreateObjectEx, VT_DISPATCH, VTS_BSTR)
	DISP_FUNCTION(CServerObject, "MapPath", MapPath, VT_BSTR, VTS_BSTR)
	DISP_FUNCTION(CServerObject, "HTMLEncode", HTMLEncode, VT_BSTR, VTS_BSTR)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IServerObject to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {7631CBBC-4CD8-4400-90A0-013E1AF29454}
static const IID IID_IServerObject =
{ 0x7631cbbc, 0x4cd8, 0x4400, { 0x90, 0xa0, 0x1, 0x3e, 0x1a, 0xf2, 0x94, 0x54 } };

BEGIN_INTERFACE_MAP(CServerObject, CCmdTarget)
	INTERFACE_PART(CServerObject, IID_IServerObject, Dispatch)
END_INTERFACE_MAP()



/********************************************************************/
/*																	*/
/* Function name : SetCurrentDirectory								*/
/* Description   : Set current directory							*/
/*																	*/
/********************************************************************/
void CServerObject::SetCurrentDirectory(LPCTSTR lpszDirectory)
{
	m_strCurrentDirectory = lpszDirectory;
	m_strCurrentDirectory.TrimRight('\\');
}


/********************************************************************/
/*																	*/
/* Function name : SetRootDirectory									*/
/* Description   : Set root directory								*/
/*																	*/
/********************************************************************/
void CServerObject::SetRootDirectory(LPCTSTR lpszDirectory)
{
	m_strRootDirectory = lpszDirectory;
	m_strRootDirectory.TrimRight('\\');
}


/********************************************************************/
/*																	*/
/* Function name : URLEncode										*/
/* Description   : Convert unsafe characters to %xx sequences		*/
/*																	*/
/********************************************************************/
BSTR CServerObject::URLEncode(LPCTSTR lpszURL) 
{
	CString strResult = "";
	CString strHex;

	char ch;
	while((ch = *lpszURL  ) != '\0')
	{
		// check if it's an unsafe character
		if (AfxIsUnsafeUrlChar(ch))
		{
			if (ch == ' ')
				strResult  = ' ';
			else
			{
				// output the percent, followed by the hex value of the character
				strResult  = '%';
				strHex.Format("%02X", ch);
				strResult  = strHex;
			}
		}
		else 
		// safe character
		{
			strResult  = ch;
		}
	}
	return strResult.AllocSysString();
}


/********************************************************************/
/*																	*/
/* Function name : CreateObjectEx									*/
/* Description   : Create an instance of a server object.			*/
/*																	*/
/********************************************************************/
LPDISPATCH CServerObject::CreateObjectEx(LPCTSTR lpszProgID) 
{
	IDispatchPtr pServerObject;

	// create instance of specified object using smart pointer
	HRESULT hResult = pServerObject.CreateInstance(lpszProgID);
	if (FAILED(hResult))
	{
		return NULL;
	}

	pServerObject->AddRef();
	return pServerObject;
}


/********************************************************************/
/*																	*/
/* Function name : MapPath											*/
/* Description   : Maps the specified relative or virtual path to	*/
/*				   the corresponding physical directory.			*/
/*																	*/
/********************************************************************/
BSTR CServerObject::MapPath(LPCTSTR lpszLogicalPath) 
{
	CString strPhysicalPath;
	CString strLogicalPath = lpszLogicalPath;

	CString strOffset;
	// make unix style
	strLogicalPath.Replace("\\","/");
	while(strLogicalPath.Replace("//","/"));
	
	if (strLogicalPath.Left(1) == '/')
	{
		// absolute path
		strOffset = m_strRootDirectory;
	}
	else
	{
		// relative path
		strOffset = m_strCurrentDirectory;
	}

	if (strOffset.Right(1) != '\\')
		strOffset  = "\\";

	strLogicalPath.TrimLeft('/');
	strPhysicalPath = strOffset   strLogicalPath;

	// make Windows style
	strPhysicalPath.Replace("/", "\\");

	CStringList partList;
	CString strSub;
	int nCount=0;

	// split path in parts
	while(AfxExtractSubString(strSub, strPhysicalPath, nCount  , '\\'))
	{
		if (!strSub.IsEmpty())
			partList.AddTail(strSub);
	}
	
	strPhysicalPath.Empty();

	// fix dots
	while(!partList.IsEmpty())
	{
		CString strPart = partList.GetHead();
		partList.RemoveHead();

		if (strPart == "..")
		{
			// go back one level
			int nPos = strPhysicalPath.ReverseFind('\\');
			if (nPos != -1)
			{
				strPhysicalPath = strPhysicalPath.Left(nPos);
			}
		}
		else
		{
			if (!strPhysicalPath.IsEmpty())
			{
				strPhysicalPath  = "\\";
			}
			strPhysicalPath  = strPart;
		}
	}		
	return strPhysicalPath.AllocSysString();
}


/********************************************************************/
/*																	*/
/* Function name : HTMLEncode										*/
/* Description   : Apply HTML encoding to the specified string.		*/
/*																	*/
/********************************************************************/
BSTR CServerObject::HTMLEncode(LPCTSTR lpszIn) 
{
	CString strEncoded;

	int nLength = lstrlen(lpszIn);

	while (nLength--)
	{
		switch (*lpszIn)
		{
		case '<': 
			strEncoded  = "&lt;";
			break;
		case '>':
			strEncoded  = "&gt;";
			break;
		case '&':
			strEncoded  = "&amp;";
			break;
		case '\'': 
			strEncoded  = "&apos;";
			break;
		case '\"':
			strEncoded  = "&quot;";
			break;
		default:
			// just copy the character
			strEncoded  = *lpszIn;
			break;
		}
		lpszIn  ;
	}
	return strEncoded.AllocSysString();
}

标签: 服务器 服务

实例下载地址

支持ASP的WEB服务器源代码,采用MFC编写(web服务器)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警