using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using XiaoFeng.UrlRewrite;
namespace XiaoFeng.Web
{
public class HttpModule : IHttpModule, IDisposable
{
public HttpModule() { }
public void Dispose() { GC.SuppressFinalize(this); }
~HttpModule() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
context.Error += new EventHandler(Application_OnError);
context.EndRequest += new EventHandler(Application_EndRequest);
context.PreSendRequestHeaders += new EventHandler(OnPreSendRequestHeaders);
}
protected void Application_OnError(Object sender, EventArgs e)
{
StringBuilder Sbr = new StringBuilder();
Exception ex = HttpContext.Current.Server.GetLastError();
Exception iex = ex.InnerException;
LogHelper.WriteLog(iex);
HttpException httpException = ex as HttpException;
if (httpException.GetHttpCode() == 404)
{
HttpContext.Current.Server.ClearError();
Sbr.Append(@"
<tr>
<th>错误信息:</th>
<td>当前请求地址不存在,或已被管理员删除。</td>
</tr>
");
Web.Error.writeContent("404", Sbr.ToString(), true);
}
string _Error = Config.GetConfig("Error");
Boolean f = true;
if (_Error == "1")
{
Sbr.Append(@"
<tr>
<th>错误源:</th>
<td>" + iex.Source + @"</td>
</tr>
<tr>
<th>所属类:</th>
<td>" + iex.TargetSite.DeclaringType.FullName + @"[" + iex.TargetSite.DeclaringType.Namespace + @"]</td>
</tr>
<tr>
<th>错误信息:</th>
<td>" + iex.Message + @"</td>
</tr>
<tr>
<th>错误堆栈:</th>
<td>" + Regex.Replace(Regex.Replace(iex.StackTrace, @"[\n]", "<br/>"), @"(行号\s*\d*)", "<span>$1</span>", RegexOptions.IgnoreCase) + @"</td>
</tr>
");
f = false;
}
else if (_Error == "0" || _Error.IsNullOrEmpty())
{
Sbr.Append(@"
<tr>
<th>错误信息:</th>
<td>当前请求出现了异常,如果您是管理员,请到空间相关目录查看当前异常信息。</td>
</tr>
");
f = true;
}
if (_Error != "2")
{
HttpContext.Current.Server.ClearError();
Web.Error.writeContent("500", Sbr.ToString(), f);
}
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication context = (HttpApplication)sender;
HttpRequest Request = context.Request;
string root = System.AppDomain.CurrentDomain.BaseDirectory;
string curPath = Request.Url.AbsolutePath;
string MyHost = Request.Url.Host;
string goUrl = curPath;
string QueryString = Request.Url.Query.Trim('?');
string _curPath = root + curPath.Replace("/", "\\").TrimStart('\\');
if (!curPath.isPattern(@"\.asmx/?"))
{
if (!File.Exists(_curPath))
{
List<ConfigElement> MyConfig = XiaoFeng.UrlRewrite.Config.GetData();
string SendType = "";
foreach (ConfigElement Element in MyConfig)
{
string LookFor = Element.LookFor.TrimStart('~'), SendTo = Element.SendTo.TrimStart('~');
if (curPath == "/")
{
if (LookFor == "/" || LookFor == MyHost) { goUrl = SendTo;SendType = Element.SendType; break; }
}
else
{
if (LookFor == "/" || LookFor == MyHost) continue;
if (Regex.IsMatch(curPath, @"^" + LookFor + @"$", RegexOptions.IgnoreCase))
{
if (LookFor.IndexOf("(", StringComparison.OrdinalIgnoreCase) > -1 && LookFor.IndexOf(")") > -1)
{
if (Regex.IsMatch(SendTo, @"\$\d+"))
{
goUrl = Regex.Replace(curPath, LookFor, SendTo + (QueryString == "" ? "" : ((SendTo.IndexOf("?", StringComparison.OrdinalIgnoreCase) == -1 ? "?" : "&") + QueryString)), RegexOptions.IgnoreCase);
}
else
goUrl = SendTo + (SendTo.IndexOf("?", StringComparison.OrdinalIgnoreCase) == -1 ? "?" : "&") + QueryString;
}
else
goUrl = SendTo;
SendType = Element.SendType;
break;
}
}
}
if (MyConfig != null && MyConfig.Count > 0)
{
if (SendType == "301")
{
context.Context.Response.StatusCode = 301;
context.Context.Response.Status = "301 Moved Permanently";
context.Context.Response.Headers.Add("Location", goUrl);
}
else if (SendType == "302")
context.Context.Response.Redirect(goUrl, true);
else
context.Context.RewritePath(goUrl, true);
}
}
}
}
protected void Application_EndRequest(object sender, EventArgs e) { }
protected void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication context = (HttpApplication)sender;
var headers = context.Context.Response.Headers;
if (headers != null) headers.Remove("server");
}
}
}