コード例 #1
0
ファイル: IWebProxy.cs プロジェクト: nguyenhuy2911/CMS
        protected string GenerateProxyUrl(ProxyRenderContext proxyRenderContext, string url, bool isForm)
        {
            if (string.IsNullOrEmpty(url))
            {
                return("");
            }
            if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                var destinationURI = new Uri(url);
                var host           = proxyRenderContext.ProxyPosition.HostUri;
                if (destinationURI.Host.ToLower() == host.Host.ToLower())
                {
                    url = destinationURI.PathAndQuery;
                }
            }

            if (!url.StartsWith("#") && !url.StartsWith("javascript:") && !Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                var encodedModuleUrl = ModuleUrlHelper.Encode(url);
                var routeValues      = proxyRenderContext.PageRequestContext.ModuleUrlContext.GetRouteValuesWithModuleUrl(proxyRenderContext.ProxyPosition.PagePositionId, encodedModuleUrl, false);
                var urlHelper        = new UrlHelper(proxyRenderContext.ControllerContext.RequestContext);
                var pageUrl          = urlHelper.FrontUrl(proxyRenderContext.PageRequestContext.Site, proxyRenderContext.PageRequestContext.RequestChannel).PageUrl(proxyRenderContext.PageRequestContext.Page.FullName, routeValues).ToString();
                if (isForm)
                {
                    pageUrl = Kooboo.Web.Url.UrlUtility.AddQueryParam(pageUrl, Kooboo.CMS.Sites.View.ModuleUrlContext.PostModuleParameter, proxyRenderContext.ProxyPosition.PagePositionId);
                }

                return(pageUrl);
            }
            else
            {
                return(url);
            }
        }
コード例 #2
0
ファイル: IWebProxy.cs プロジェクト: nguyenhuy2911/CMS
        public IHtmlString ProcessRequest(ProxyRenderContext proxyRenderContext)
        {
            Func <string, bool, string> proxyUrl = (url, isForm) => new Uri(proxyRenderContext.ProxyPosition.HostUri, url).ToString();

            if (!proxyRenderContext.ProxyPosition.NoProxy && proxyRenderContext.PageRequestContext != null)
            {
                proxyUrl = (url, isForm) => GenerateProxyUrl(proxyRenderContext, url, isForm);
            }
            IHtmlString htmlString = new HtmlString("");
            var         html       = _httpProcessor.ProcessRequest(proxyRenderContext.ControllerContext.HttpContext, proxyRenderContext.RequestUri.ToString(), proxyRenderContext.HttpMethod, proxyUrl);

            if (!string.IsNullOrEmpty(html))
            {
                htmlString = _htmlParser.Parse(proxyRenderContext, html, proxyUrl);
            }
            return(htmlString);
        }
コード例 #3
0
ファイル: IWebProxy.cs プロジェクト: eyouyou/Bsc
        public IHtmlString ProcessRequest(ProxyRenderContext proxyRenderContext)
        {
            Func<string, bool, string> proxyUrl =
                (url, isForm) => new Uri(proxyRenderContext.ProxyPosition.HostUri, url).ToString();

            if (!proxyRenderContext.ProxyPosition.NoProxy && proxyRenderContext.PageRequestContext != null)
            {
                proxyUrl = (url, isForm) => GenerateProxyUrl(proxyRenderContext, url, isForm);
            }
            IHtmlString htmlString = new HtmlString("");
            var html = _httpProcessor.ProcessRequest(proxyRenderContext.ControllerContext.HttpContext,
                proxyRenderContext.RequestUri.ToString(), proxyRenderContext.HttpMethod, proxyUrl);
            if (!string.IsNullOrEmpty(html))
            {
                htmlString = _htmlParser.Parse(proxyRenderContext, html, proxyUrl);
            }
            return htmlString;
        }
コード例 #4
0
ファイル: IWebProxy.cs プロジェクト: eyouyou/Bsc
        protected string GenerateProxyUrl(ProxyRenderContext proxyRenderContext, string url, bool isForm)
        {
            if (string.IsNullOrEmpty(url))
            {
                return "";
            }
            if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                var destinationURI = new Uri(url);
                var host = proxyRenderContext.ProxyPosition.HostUri;
                if (destinationURI.Host.ToLower() == host.Host.ToLower())
                {
                    url = destinationURI.PathAndQuery;
                }
            }

            if (!url.StartsWith("#") && !url.StartsWith("javascript:") &&
                !Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                var encodedModuleUrl = ModuleUrlHelper.Encode(url);
                var routeValues =
                    proxyRenderContext.PageRequestContext.ModuleUrlContext.GetRouteValuesWithModuleUrl(
                        proxyRenderContext.ProxyPosition.PagePositionId, encodedModuleUrl, false);
                var urlHelper = new UrlHelper(proxyRenderContext.ControllerContext.RequestContext);
                var pageUrl =
                    urlHelper.FrontUrl(proxyRenderContext.PageRequestContext.Site,
                        proxyRenderContext.PageRequestContext.RequestChannel)
                        .PageUrl(proxyRenderContext.PageRequestContext.Page.FullName, routeValues)
                        .ToString();
                if (isForm)
                {
                    pageUrl = UrlUtility.AddQueryParam(pageUrl,
                        View.ModuleUrlContext.PostModuleParameter,
                        proxyRenderContext.ProxyPosition.PagePositionId);
                }

                return pageUrl;
            }
            else
            {
                return url;
            }
        }
コード例 #5
0
ファイル: ProxyRender.cs プロジェクト: eyouyou/Bsc
        public virtual IHtmlString Render(ProxyRenderContext proxyRenderContext)
        {
            var positionId = proxyRenderContext.ProxyPosition.PagePositionId;

            Func<IHtmlString> getHtml = () =>
            {
                var html = _webProxy.ProcessRequest(proxyRenderContext);

                return html;
            };
            var cacheSetting = proxyRenderContext.ProxyPosition.OutputCache;
            if (cacheSetting != null && cacheSetting.EnableCaching != null && cacheSetting.EnableCaching == true && proxyRenderContext.HttpMethod.ToUpper() == "GET")
            {
                string cacheKey = string.Format("{0}||{1}||{2}", positionId, proxyRenderContext.RequestUri.ToString(), proxyRenderContext.ProxyPosition.NoProxy);
                return proxyRenderContext.PageRequestContext.Site.ObjectCache().GetCache(cacheKey, getHtml, cacheSetting.ToCachePolicy());
            }
            else
            {
                return getHtml();
            }
        }
コード例 #6
0
ファイル: IProxyHtmlParser.cs プロジェクト: eyouyou/Bsc
        public IHtmlString Parse(ProxyRenderContext proxyRenderContext, string rawHtml
            , Func<string, bool, string> proxyUrlFunc)
        {
            var stylesheets = new List<string>();

            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(rawHtml);

            HtmlNode headNode = null;
            HtmlNode bodyNode = null;

            foreach (var node in htmlDoc.DocumentNode.Descendants().ToArray())
            {
                //if (IsStylesheet(node) && proxyRenderContext.ProxyPosition.ProxyStylesheet == true)
                //{
                //    stylesheets.Add(node.Attributes["href"].Value);
                //    node.Remove();
                //    continue;
                //}
                var nodeName = node.Name.ToLower();
                var elementName = node.Attributes["name"] == null ? "" : node.Attributes["name"].Value.ToLower();
                if (elementName == "__viewstate") { continue; }
                if (elementName == "__eventvalidation") { continue; }
                if (nodeName == "head")
                {
                    headNode = node;
                }
                if (nodeName == "body")
                {
                    bodyNode = node;
                }
                foreach (var attr in node.Attributes)
                {
                    if (IsW3CUrlAttribute(attr.Name.ToLower()) || attr.Value.StartsWith("/"))
                    {
                        var isStaticResource = IsStaticResource(node, attr);
                        if (isStaticResource)
                        {
                            attr.Value = GenerateAbsoluteUrl(proxyRenderContext.ProxyPosition.HostUri.ToString(), attr.Value);
                        }
                        else
                        {
                            var isForm = node.Name.ToLower() == "form";
                            attr.Value = proxyUrlFunc(attr.Value, isForm);
                        }
                    }
                }
            }

            string newHtml = "";
            if (bodyNode == null)
            {
                newHtml = htmlDoc.DocumentNode.InnerHtml;
            }
            else
            {
                if (headNode != null)
                {
                    newHtml = headNode.InnerHtml;
                }
                newHtml = newHtml + bodyNode.InnerHtml;
            }

            string injectScript = "";
            if (proxyRenderContext.PageRequestContext != null)
            {
                injectScript = InjectScriptForAjax(proxyRenderContext.PageRequestContext.Site, proxyRenderContext.PageRequestContext.Page);
                newHtml = newHtml + injectScript;
            }

            return new HtmlString(newHtml);
        }
コード例 #7
0
ファイル: IProxyHtmlParser.cs プロジェクト: nguyenhuy2911/CMS
        public IHtmlString Parse(ProxyRenderContext proxyRenderContext, string rawHtml
                                 , Func <string, bool, string> proxyUrlFunc)
        {
            var stylesheets = new List <string>();

            HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(rawHtml);

            HtmlNode headNode = null;
            HtmlNode bodyNode = null;

            foreach (var node in htmlDoc.DocumentNode.Descendants().ToArray())
            {
                //if (IsStylesheet(node) && proxyRenderContext.ProxyPosition.ProxyStylesheet == true)
                //{
                //    stylesheets.Add(node.Attributes["href"].Value);
                //    node.Remove();
                //    continue;
                //}
                var nodeName    = node.Name.ToLower();
                var elementName = node.Attributes["name"] == null ? "" : node.Attributes["name"].Value.ToLower();
                if (elementName == "__viewstate")
                {
                    continue;
                }
                if (elementName == "__eventvalidation")
                {
                    continue;
                }
                if (nodeName == "head")
                {
                    headNode = node;
                }
                if (nodeName == "body")
                {
                    bodyNode = node;
                }
                foreach (var attr in node.Attributes)
                {
                    if (IsW3CUrlAttribute(attr.Name.ToLower()) || attr.Value.StartsWith("/"))
                    {
                        var isStaticResource = IsStaticResource(node, attr);
                        if (isStaticResource)
                        {
                            attr.Value = GenerateAbsoluteUrl(proxyRenderContext.ProxyPosition.HostUri.ToString(), attr.Value);
                        }
                        else
                        {
                            var isForm = node.Name.ToLower() == "form";
                            attr.Value = proxyUrlFunc(attr.Value, isForm);
                        }
                    }
                }
            }

            string newHtml = "";

            if (bodyNode == null)
            {
                newHtml = htmlDoc.DocumentNode.InnerHtml;
            }
            else
            {
                if (headNode != null)
                {
                    newHtml = headNode.InnerHtml;
                }
                newHtml = newHtml + bodyNode.InnerHtml;
            }

            string injectScript = "";

            if (proxyRenderContext.PageRequestContext != null)
            {
                injectScript = InjectScriptForAjax(proxyRenderContext.PageRequestContext.Site, proxyRenderContext.PageRequestContext.Page);
                newHtml      = newHtml + injectScript;
            }

            //if (proxyRenderContext.ProxyPosition.ProxyStylesheet == true && stylesheets.Count > 0)
            //{
            //    newHtml = ProxyStyleSheet(proxyRenderContext, stylesheets, newHtml);
            //}

            return(new HtmlString(newHtml));
        }