protected void Session_Start(object sender, EventArgs e)
{
#if DEBUG
//debug 登陆默认设置
#endif
} protected void Application_BeginRequest(object sender, EventArgs e)
{ } protected void Application_End(object sender, EventArgs e)
{
Brotli.Brolib.FreeLibrary();
} protected void Application_Error(object sender, EventArgs e)
{
//错误日志记录
} protected void AddCompressSupport(HttpContext context)
{
Boolean doCompress = true;
String compressMode = System.Web.Configuration.WebConfigurationManager.AppSettings["CompressMode"];
if (!String.IsNullOrEmpty(compressMode))
{
Boolean.TryParse(compressMode, out doCompress);
}
//don't use compress for filehandler
if (context.Request.Url.AbsoluteUri.Contains("FileHandler.ashx")) return;
if (context.Response.ContentType.IndexOf("json", StringComparison.CurrentCultureIgnoreCase) >= 0
|| (context.Handler is System.Web.SessionState.IRequiresSessionState)
)
{
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetMaxAge(TimeSpan.Zero);
context.Response.Cache.SetExpires(new DateTime(2000, 1, 1));
} if (doCompress)
{
var app = context.ApplicationInstance;
String acceptEncodings = app.Request.Headers.Get("Accept-Encoding"); if (!String.IsNullOrEmpty(acceptEncodings))
{
System.IO.Stream baseStream = app.Response.Filter;
acceptEncodings = acceptEncodings.ToLower(); if (acceptEncodings.Contains("br") || acceptEncodings.Contains("brotli"))
{
app.Response.Filter = new Brotli.BrotliStream(baseStream, System.IO.Compression.CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "br");
}
else
if (acceptEncodings.Contains("deflate"))
{
app.Response.Filter = new System.IO.Compression.DeflateStream(baseStream, System.IO.Compression.CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
}
else if (acceptEncodings.Contains("gzip"))
{
app.Response.Filter = new System.IO.Compression.GZipStream(baseStream, System.IO.Compression.CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
} }
}
} protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
//压缩Response请求
AddCompressSupport(Context);
}