zoukankan      html  css  js  c++  java
  • 某墙尼妹,用个Response.Filter来解决StackExchange.Exceptional中google cdn的问题

    某墙墙了古古路,一些开源的东东里用了古古路CDN,比如Exceptional,Opserver ,导致服务要么慢要么用不了

    必须要替换之

    Exceptional就只要用Response.Filter替换个页面了,因为自己维护个版本还要定期合并什么的,操心

     internal class ResponseStream : MemoryStream
            {
    
                #region ctor
    
                private Stream Output { get; set; }
                public HttpContextBase Context { get; set; }
    
                /// <summary>
                /// 页面输出的Stream Buffer
                /// </summary>
                public List<byte> BytesArray { get; set; }
    
                public ResponseStream(HttpContextBase context)
                {
                    Context = context;
                    Output = context.Response.Filter;
                    context.Response.BufferOutput = true;
                    context.Response.Buffer = true;
                    BytesArray = new List<byte>();
                }
    
                #endregion
    
    
                public override void Write(byte[] buffer, int offset, int count)
                {
                    if (Context.Response.ContentType != "text/html")
                    {
                        Output.Write(buffer, offset, count);
                        return;
                    }
                    BytesArray.AddRange(buffer);
                }
    
                public override void Close()
                {
                    if (BytesArray.Count > 0)
                        CloseByReplace();
                    Output.Close();
                    base.Close();
                }
    
                private void CloseByReplace()
                {
                    var html = Encoding.UTF8.GetString(BytesArray.ToArray(), 0, BytesArray.Count);
                    var sb = new StringBuilder(html);
    
                    sb.Replace("//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", "http://libs.baidu.com/jquery/1.7.2/jquery.min.js");
                    var outputBytes = Encoding.UTF8.GetBytes(sb.ToString());
                    Output.Write(outputBytes, 0, outputBytes.Length);
                }
    
    
            }

    Action对应的改为:

            public ActionResult Exceptions()
            {
                var context = System.Web.HttpContext.Current;
                context.Response.Filter = new ResponseStream(HttpContext);
                var page = new HandlerFactory().GetHandler(context, Request.RequestType, Request.Url.ToString(),
                    Request.PathInfo);
                page.ProcessRequest(context);
    
                return null;
            }
  • 相关阅读:
    mysql GRANT ALL PRIVILEGES 限制某个或所有客户端都可以连接至mysql
    MySql开启远程用户登录GRANTALLPRIVILEGESON*.*TO'root'@'%'I MySql开启远程用户登录GRANTALLPRIVILEGESON*.*TO'root'@'%'I
    php中 -> 和 => 和 :: 的用法 以及 self 和 $this 的用法
    mysql case when then else end 的用法
    C/C++ 程序的build过程
    Git 笔记
    English Snippets
    Ubuntu 使用笔记
    在CentOS上安装Sublime Text
    hihoCoder #1379 Emulator
  • 原文地址:https://www.cnblogs.com/chsword/p/StackExchange_Exceptional_response_filter.html
Copyright © 2011-2022 走看看