Community Server专题三:HttpModule

从专题三开始分析Community Server的一些具体的技术实现,根据IIS对请求的处理流程,从HttpModule& HttpHandler切入话题,同时你也可以通过一系列的专题了解CS的运行过程,不只如此,所有的.Net 1.1 构架的Web App都是以同样的顺序执行的。
先了解一下IIS系统。它是一个程序,负责对网站的内容进行管理并且处理对客户的请求做出反应。当用户对一个页面提出请求时,IIS做如下反应(不考虑权限问题):
1.把对方请求的虚拟路径转换成物理路径
2.根据物理路径搜索请求的文件
3.找到文件后,获取文件的内容
4.生成Http头信息。
5.向客户端发送所有的文件内容:首先是头信息,然后是Html内容,最后是其它文件的内容。
6.客户端IE浏览器获得信息后,解析文件内容,找出其中的引用文件,如.js .css .gif等,向IIS请求这些文件。
7.IIS获取请求后,发送文件内容。
8.当浏览器获取所有内容后,生成内容界面,客户就看到图像/文本/其它内容了。
但是IIS本身是不支持动态页面的,也就是说它仅仅支持静态html页面的内容,对于如.asp,.aspx,.cgi,.php等,IIS并不会处理这些标记,它就会把它当作文本,丝毫不做处理发送到客户端。为了解决这个问题。IIS有一种机制,叫做ISAPI的筛选器,这个东西是一个标准组件(COM组件),当在在访问IIS所不能处理的文件时,如asp.net 1.1 中的IIS附加ISAPI筛选器如图:
Community Server专题三:HttpModule
Asp.net服务在注册到IIS的时候,会把每个扩展可以处理的文件扩展名注册到IIS里面(如:*.ascx、*.aspx等)。扩展启动后,就根据定义好的方式来处理IIS所不能处理的文件,然后把控制权跳转到专门处理代码的进程中。让这个进程开始处理代码,生成标准的HTML代码,生成后把这些代码加入到原有的Html中,最后把完整的Html返回给IIS,IIS再把内容发送到客户端。
有上面对ISAPI的简单描述,我们把HttpModule& HttpHandler分开讨论,并且结合CS进行具体的实现分析。
HttpModule
HttpModule实现了ISAPI Filter的功能,是通过对IhttpModule接口的继承来处理。下面打开CS中的CommunityServerComponents项目下的CSHttpModule.cs文件(放在HttpModule目录)
Community Server专题三:HttpModule//------------------------------------------------------------------------------
Community Server专题三:HttpModule
//<copyrightcompany="TelligentSystems">
Community Server专题三:HttpModule
//Copyright(c)TelligentSystemsCorporation.Allrightsreserved.
Community Server专题三:HttpModule
//</copyright>
Community Server专题三:HttpModule
//------------------------------------------------------------------------------
Community Server专题三:HttpModule

Community Server专题三:HttpModule
usingSystem;
Community Server专题三:HttpModule
usingSystem.IO;
Community Server专题三:HttpModule
usingSystem.Web;
Community Server专题三:HttpModule
usingCommunityServer.Components;
Community Server专题三:HttpModule
usingCommunityServer.Configuration;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
namespaceCommunityServer
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//*********************************************************************
Community Server专题三:HttpModule
//CSHttpModule
Community Server专题三:HttpModule
//
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
/**////<summary>
Community Server专题三:HttpModule
///ThisHttpModuleencapsulatesalltheforumsrelatedeventsthatoccur
Community Server专题三:HttpModule
///duringASP.NETapplicationstart-up,errors,andendrequest.
Community Server专题三:HttpModule
///</summary>

Community Server专题三:HttpModule//***********************************************************************/
Community Server专题三:HttpModule
publicclassCSHttpModule:IHttpModule
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Membervariablesandinheritedproperties/methods#regionMembervariablesandinheritedproperties/methods
Community Server专题三:HttpModule
Community Server专题三:HttpModule
publicStringModuleName
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
getCommunity Server专题三:HttpModule{return"CSHttpModule";}
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//*********************************************************************
Community Server专题三:HttpModule
//ForumsHttpModule
Community Server专题三:HttpModule
//
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
/**////<summary>
Community Server专题三:HttpModule
///InitializestheHttpModuleandperformsthewireupofallapplication
Community Server专题三:HttpModule
///events.
Community Server专题三:HttpModule
///</summary>
Community Server专题三:HttpModule
///<paramname="application">Applicationthemoduleisbeingrunfor</param>

Community Server专题三:HttpModulepublicvoidInit(HttpApplicationapplication)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
//Wire-upapplicationevents
Community Server专题三:HttpModule
//
Community Server专题三:HttpModule
application.BeginRequest+=newEventHandler(this.Application_BeginRequest);
Community Server专题三:HttpModuleapplication.AuthenticateRequest
+=newEventHandler(Application_AuthenticateRequest);
Community Server专题三:HttpModuleapplication.Error
+=newEventHandler(this.Application_OnError);
Community Server专题三:HttpModuleapplication.AuthorizeRequest
+=newEventHandler(this.Application_AuthorizeRequest);
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//settingsID=SiteSettingsManager.GetSiteSettings(application.Context).SettingsID;
Community Server专题三:HttpModule
Jobs.Instance().Start();
Community Server专题三:HttpModule
//CSExceptionex=newCSException(CSExceptionType.ApplicationStart,"AppicationStarted"+AppDomain.CurrentDomain.FriendlyName);
Community Server专题三:HttpModule
//ex.Log();
Community Server专题三:HttpModule
}

Community Server专题三:HttpModule
Community Server专题三:HttpModule
//intsettingsID;
Community Server专题三:HttpModule
publicvoidDispose()
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
//CSExceptionex=newCSException(CSExceptionType.ApplicationStop,"ApplicationStopping"+AppDomain.CurrentDomain.FriendlyName);
Community Server专题三:HttpModule
//ex.Log(settingsID);
Community Server专题三:HttpModule
Jobs.Instance().Stop();
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Installer#regionInstaller
Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
#endregion

Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
#endregion

Community Server专题三:HttpModule
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
ApplicationOnError#regionApplicationOnError
Community Server专题三:HttpModule
privatevoidApplication_OnError(Objectsource,EventArgse)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModuleHttpApplicationapplication
=(HttpApplication)source;
Community Server专题三:HttpModuleHttpContextcontext
=application.Context;
Community Server专题三:HttpModule
Community Server专题三:HttpModuleCSExceptioncsException
=context.Server.GetLastError()asCSException;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
if(csException==null)
Community Server专题三:HttpModulecsException
=context.Server.GetLastError().GetBaseException()asCSException;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
try
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
if(csException!=null)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
switch(csException.ExceptionType)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
caseCSExceptionType.UserInvalidCredentials:
Community Server专题三:HttpModule
caseCSExceptionType.AccessDenied:
Community Server专题三:HttpModule
caseCSExceptionType.AdministrationAccessDenied:
Community Server专题三:HttpModule
caseCSExceptionType.ModerateAccessDenied:
Community Server专题三:HttpModule
caseCSExceptionType.PostDeleteAccessDenied:
Community Server专题三:HttpModule
caseCSExceptionType.PostProblem:
Community Server专题三:HttpModule
caseCSExceptionType.UserAccountBanned:
Community Server专题三:HttpModule
caseCSExceptionType.ResourceNotFound:
Community Server专题三:HttpModule
caseCSExceptionType.UserUnknownLoginError:
Community Server专题三:HttpModule
caseCSExceptionType.SectionNotFound:
Community Server专题三:HttpModulecsException.Log();
Community Server专题三:HttpModule
break;
Community Server专题三:HttpModule}

Community Server专题三:HttpModule}

Community Server专题三:HttpModule
else
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModuleExceptionex
=context.Server.GetLastError();
Community Server专题三:HttpModule
if(ex.InnerException!=null)
Community Server专题三:HttpModuleex
=ex.InnerException;
Community Server专题三:HttpModule
Community Server专题三:HttpModulecsException
=newCSException(CSExceptionType.UnknownError,ex.Message,context.Server.GetLastError());
Community Server专题三:HttpModule
Community Server专题三:HttpModuleSystem.Data.SqlClient.SqlExceptionsqlEx
=exasSystem.Data.SqlClient.SqlException;
Community Server专题三:HttpModule
if(sqlEx==null||sqlEx.Number!=-2)//don'tlogtimeouts
Community Server专题三:HttpModule
csException.Log();
Community Server专题三:HttpModule}

Community Server专题三:HttpModule}

Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
catchCommunity Server专题三:HttpModule{}//notmuchtodohere,butwewanttopreventinfiniteloopingwithourerrorhandles
Community Server专题三:HttpModule

Community Server专题三:HttpModuleCSEvents.CSException(csException);
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
#endregion

Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
ApplicationAuthenticateRequest#regionApplicationAuthenticateRequest
Community Server专题三:HttpModule
Community Server专题三:HttpModule
privatevoidApplication_AuthenticateRequest(Objectsource,EventArgse)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModuleHttpContextcontext
=HttpContext.Current;
Community Server专题三:HttpModuleProviderp
=null;
Community Server专题三:HttpModuleExtensionModulemodule
=null;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//Iftheinstallerismakingtherequestterminateearly
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
if(CSConfiguration.GetConfig().AppLocation.CurrentApplicationType==ApplicationType.Installer)Community Server专题三:HttpModule{
Community Server专题三:HttpModule
return;
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModule
//Onlycontinueifwehaveavalidcontext
Community Server专题三:HttpModule
//
Community Server专题三:HttpModule
if((context==null)||(context.User==null))
Community Server专题三:HttpModule
return;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
try
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
//Logictohandlevariousauthenticationtypes
Community Server专题三:HttpModule
//
Community Server专题三:HttpModule
switch(context.User.Identity.GetType().Name.ToLower())
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//Microsoftpassport
Community Server专题三:HttpModule
case"passportidentity":
Community Server专题三:HttpModulep
=(Provider)CSConfiguration.GetConfig().Extensions["PassportAuthentication"];
Community Server专题三:HttpModulemodule
=ExtensionModule.Instance(p);
Community Server专题三:HttpModule
if(module!=null)
Community Server专题三:HttpModulemodule.ProcessRequest();
Community Server专题三:HttpModule
else
Community Server专题三:HttpModule
gotodefault;
Community Server专题三:HttpModule
break;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//Windows
Community Server专题三:HttpModule
case"windowsidentity":
Community Server专题三:HttpModulep
=(Provider)CSConfiguration.GetConfig().Extensions["WindowsAuthentication"];
Community Server专题三:HttpModulemodule
=ExtensionModule.Instance(p);
Community Server专题三:HttpModule
if(module!=null)
Community Server专题三:HttpModulemodule.ProcessRequest();
Community Server专题三:HttpModule
else
Community Server专题三:HttpModule
gotodefault;
Community Server专题三:HttpModule
break;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//Forms
Community Server专题三:HttpModule
case"formsidentity":
Community Server专题三:HttpModulep
=(Provider)CSConfiguration.GetConfig().Extensions["FormsAuthentication"];
Community Server专题三:HttpModulemodule
=ExtensionModule.Instance(p);
Community Server专题三:HttpModule
if(module!=null)
Community Server专题三:HttpModulemodule.ProcessRequest();
Community Server专题三:HttpModule
else
Community Server专题三:HttpModule
gotodefault;
Community Server专题三:HttpModule
break;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//Custom
Community Server专题三:HttpModule
case"customidentity":
Community Server专题三:HttpModulep
=(Provider)CSConfiguration.GetConfig().Extensions["CustomAuthentication"];
Community Server专题三:HttpModulemodule
=ExtensionModule.Instance(p);
Community Server专题三:HttpModule
if(module!=null)
Community Server专题三:HttpModulemodule.ProcessRequest();
Community Server专题三:HttpModule
else
Community Server专题三:HttpModule
gotodefault;
Community Server专题三:HttpModule
break;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
default:
Community Server专题三:HttpModuleCSContext.Current.UserName
=context.User.Identity.Name;
Community Server专题三:HttpModule
break;
Community Server专题三:HttpModule
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
catch(Exceptionex)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModuleCSExceptionforumEx
=newCSException(CSExceptionType.UnknownError,"ErrorinAuthenticateRequest",ex);
Community Server专题三:HttpModuleforumEx.Log();
Community Server专题三:HttpModule
Community Server专题三:HttpModule
throwforumEx;
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModule
////Gettherolestheuserbelongsto
Community Server专题三:HttpModule
////
Community Server专题三:HttpModule
//Rolesroles=newRoles();
Community Server专题三:HttpModule
//roles.GetUserRoles();
Community Server专题三:HttpModule
}

Community Server专题三:HttpModule
#endregion

Community Server专题三:HttpModule
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
ApplicationAuthorizeRequest#regionApplicationAuthorizeRequest
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
privatevoidApplication_AuthorizeRequest(Objectsource,EventArgse)Community Server专题三:HttpModule{
Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModule
if(CSConfiguration.GetConfig().AppLocation.CurrentApplicationType==ApplicationType.Installer)
Community Server专题三:HttpModuleCommunity Server专题三:HttpModule
Community Server专题三:HttpModule{
Community Server专题三:HttpModule
//CSContext.Create(context);
Community Server专题三:HttpModule
return;
Community Server专题三:HttpModule}

Community Server专题三:HttpModule
Community Server专题三:HttpModule
Community Server专题三:HttpModuleHttpApplicationapplication
=(HttpApplication)source;
Community Server专题三:HttpModuleHttpContextcontext
=application.Context;
Community Server专题三:HttpModule
Community Server专题三:HttpModuleCSContextcsContext
=CSContext.Current;
Community Server专题三:HttpModule
//boolenableBannedUsersToLogin=CSContext.Current.SiteSettings.EnableBannedUsersToLogin;
Community Server专题三:HttpModule
Community Server专题三:HttpModule
////Iftheinstallerismakingtherequestterminateearly
Community Server专题三:HttpModule
//if(csContext.ApplicationType==ApplicationType.Installer){
Community Server专题三:HttpModule
//return;
Community Server专题三:HttpModule
//}
Community Server专题三:HttpModule
Community Server专题三:HttpModule
//csContext.User=CSContext.Current.User;
Community Server专题三:HttpModule