zoukankan      html  css  js  c++  java
  • 通过IHttpModule,IHttpHandler扩展IIS

    • IIS对Http Request的处理流程

    当Windows Server收到从浏览器发送过来的http请求,处理流程如下(用自官方文档):

    最终请求会被w3wp.exe处理,处理过程如下:

    左边蓝色的部分是对Request的处理流程(从上到下),IHttpModule就是用来跟踪和控制这整个过程的接口。

    • IHttpModule

    我们通过自定义IHttpModule的实现,订阅上述所有所有事件来跟踪和控制请求的处理过程。这样我们可以在请求被真正处理(Handler Excecution)之前,对请求进行自定义的预处理,比如说对Url的重写。代码如下:

    public class UrlRewriteModule:IHttpModule
        {
            public void Dispose()
            {
                //throw new NotImplementedException();
            }
    
            public void Init(HttpApplication context)
            {
                context.PostAuthenticateRequest += OnPostAuthenticatRequest;
            }
    
            private void OnPostAuthenticatRequest(object sender, EventArgs arg)
            {
                var app = sender as HttpApplication;
                if (app != null)
                {
                    var regex = new Regex(@"^/w+/HelloWorld.jhtml$");
                    if(regex.IsMatch(app.Request.Path))
                    {
                        app.Context.RewritePath(regex.Replace(app.Request.Path, "/HelloWorld.jhtml"));
                        //将http://{serverUrl}/xxxxxxx/HelloWorld.jhtml Rewrite 到 http://{serverUrl}/HelloWorld.jhtml
                    }
                }
            }
        }
    

      

    • IHttpHandler 

    那IHttpHandler又是什么鬼?IHttpHandler是真正为每一个请求获取最终输出给Client的资源的接口。IIS默认有了很多的Handler来处理各种类型的请求,我们打开IIS中网站的Handler Mapping可以看到:

    这里很多的Module同时充当了Handler. 可以自己实现IHttpHandler接口来支持新的请求类型。上面IHttpModule的例子中,Client访问了一个扩展名为.jhtml的资源,这个IIS默认是不支持的。可以实现IHttpHandler来支持这种类型,代码如下:

    public class JHtmlHandler:IHttpHandler
        {
    
            public bool IsReusable
            {
                get { return true; }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.WriteFile(context.Request.PhysicalPath);
            }
        }
    

    这里只是简单的将请求的物理文件返回给Client.

    • 自定义IHttpModule和IHttpHandler的部署

    要将开发的IHttpModule与IHttpHandler部署到网站,有很多种方法:

    1. 将IHttpModule与IHttpHandler的实现编译为Dll,将Dll放入网站的bin目录

    2. 将IHttpModule与IHttpHandler的实现编译为强名Dll, 将Dll注册到GAC

    最后修改网站的web.config,应用自定义的IHttpModule和IHttpHandler,配置如下(网站Application Pool配置为集成模式运行),此处是以将Dll注册到GAC:

    <system.webServer>
            <modules>
                <add name="rewriteModule" type="ModuleSample.UrlRewriteModule,ModuleSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1c9f38745200151a,processorArchitecture=MSIL" />
            </modules>
            <handlers>
                <add name="JHtmlHandler" path="*.jhtml" verb="*" type="ModuleSample.JHtmlHandler,ModuleSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1c9f38745200151a" />
            </handlers>
    </system.webServer>

      

  • 相关阅读:
    hive查询语句合并问题
    hive isnull或ifnull的替代方法if()方法
    hive科学计数法引发的问题
    科学计数法转字符串
    shell命令执行结果$?
    shell脚本中变量接受hive语句的返回值问题
    shell简单命令
    js切换图片
    js点击图片切换
    操作节点
  • 原文地址:https://www.cnblogs.com/Code-life/p/6592070.html
Copyright © 2011-2022 走看看