using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;
using Everest.CmsServices.Models;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Web{
public enum CmsVisitType
{
Unknow,
Normal,
Host,
HostNVirtual
}
public class KoobooHttpRequestWrapper : HttpRequestWrapper
{
const string PreviewUrl = "/c-";
private bool isApplicationResolved = false;
private readonly HttpRequest _request;
public KoobooHttpRequestWrapper(HttpRequest httpRequest)
: base(httpRequest)
{
_request = httpRequest;
ResolveApplicationByHost();
}
private void ResolveApplicationByHost()
{
appRelativeCurrentExecutionFilePath = base.AppRelativeCurrentExecutionFilePath;
if (!_request.RawUrl.StartsWith(PreviewUrl, StringComparison.InvariantCultureIgnoreCase))
{
var host = _request.GetRawHostWithoutPort();
var virtualPath = appRelativeCurrentExecutionFilePath.Substring(2);
if (!string.IsNullOrEmpty(virtualPath))
{
var path = virtualPath.Split('/');
app = CachedData.GetApplicationByHostNameNVirtualPath(host, path[0]);
if (app != null)
{
visitType = CmsVisitType.HostNVirtual;
CmsApplicationVirtualPath = path[0];
if (path.Length < 2)
{
appRelativeCurrentExecutionFilePath = "~/";
}
else
{
appRelativeCurrentExecutionFilePath = "~/" + string.Join("/", path.Skip(1).ToArray());
}
}
}
if (app == null)
{
app = CachedData.GetApplicationByHostName(host);
if (app != null)
{
visitType = CmsVisitType.Host;
}
}
}
}
private void ResolveApplication()
{
if (isApplicationResolved == false && app == null)
{
string cmsApplicationName = "";
var requestContext = RequestContext;
if (requestContext.RouteData.Values["application"] != null && !StringExtensions.IsNullOrEmptyTrim(requestContext.RouteData.Values["application"].ToString()))
{
cmsApplicationName = requestContext.RouteData.Values["application"].ToString();
if (!StringExtensions.IsNullOrEmptyTrim(cmsApplicationName))
{
app = CachedData.GetApplication(cmsApplicationName);
visitType = CmsVisitType.Normal;
}
}
isApplicationResolved = true;
}
}
private aspnet_Applications app;
public aspnet_Applications CmsApplication
{
get
{
ResolveApplication();
return app;
}
set
{
app = value;
isApplicationResolved = true;
}
}
private CmsVisitType visitType;
public CmsVisitType VisitType
{
get
{
ResolveApplication();
return visitType;
}
}
public string CmsApplicationVirtualPath { get; private set; }
public RequestContext RequestContext { get; set; }
string appRelativeCurrentExecutionFilePath;
public override string AppRelativeCurrentExecutionFilePath
{
get
{
return appRelativeCurrentExecutionFilePath;
}
}
}
}
|