コード例 #1
0
ファイル: Utility.cs プロジェクト: remy22/DotnetSPA
        public static bool IsRequestShouldBePrerendered(RequestParams requestParams, PrerenderConfig config)
        {
            // Check '_escaped_fragment_' query string parameter

            if(!string.IsNullOrEmpty(requestParams.RequestUri.Query) &&
                requestParams.RequestUri.Query.Contains(EscapedQueryStringParameterName))
            {
                return true;
            }

            // Check ignore file extentions
            var extention = Path.GetExtension(requestParams.RequestUri.AbsoluteUri);
            if (!string.IsNullOrEmpty(extention))
            {
                for (int i = 0; i < config.Configuration.IgnoreExtentions.Count; i++)
                {
                    var extentionElm = config.Configuration.IgnoreExtentions[i];
                    if (string.Equals(extentionElm.Extention, extention, StringComparison.OrdinalIgnoreCase))
                    {
                        return false;
                    }
                }
            }

            // Check crawler user agent

            for (int i = 0; i < config.Configuration.Crawlers.Count; i++)
            {
                var crawlerElm = config.Configuration.Crawlers[i];
                if (crawlerElm.IsMatch(requestParams.UserAgent))
                    return true;
            }

            return false;
        }
コード例 #2
0
ファイル: UtilityTests.cs プロジェクト: remy22/DotnetSPA
        public void TestCorrectPrerenderingIgnoredFile()
        {
            var requestUrl = "http://localhost:4434/myfile.html";
            var userAgent = "Googlebot";

            var requestParams = new RequestParams
            {
                RequestUri = new Uri(requestUrl),
                UserAgent = userAgent
            };

            Assert.IsFalse(Utility.IsRequestShouldBePrerendered(requestParams, _config));
            requestParams.UserAgent = "chrome";
            Assert.IsFalse(Utility.IsRequestShouldBePrerendered(requestParams, _config));
        }
コード例 #3
0
ファイル: UtilityTests.cs プロジェクト: remy22/DotnetSPA
        public void TestCorrectRobotRequestDetection()
        {
            var requestUrl = "http://localhost:4434/path/to/resource?q=2";
            var userAgent = "chrome";

            var requestParams = new RequestParams
            {
                RequestUri = new Uri(requestUrl),
                UserAgent = userAgent
            };

            Assert.IsFalse(Utility.IsRequestShouldBePrerendered(requestParams, _config));

            requestParams.UserAgent = "Googlebot";
            Assert.IsTrue(Utility.IsRequestShouldBePrerendered(requestParams, _config));

            requestParams.UserAgent = "Googlebot-Mobile";
            Assert.IsTrue(Utility.IsRequestShouldBePrerendered(requestParams, _config));
        }
コード例 #4
0
        public override async Task Invoke(IOwinContext context)
        {
            var config = PrerenderConfig.GetCurrent();
            var requestParams = new RequestParams
                                    {
                                        RequestUri = context.Request.Uri,
                                        UserAgent = context.Request.Headers["user-agent"]
                                    };
            if (Utility.IsRequestShouldBePrerendered(requestParams, config))
            {
                var rendererConfig = new WebConfigPrerenderServiceConfiguration(config);
                var renderer = new SnapshotRenderer(rendererConfig);


                var snapshotUrl = Utility.GetSnapshotUrl(context.Request.Uri);
                //render page html
                PrerenderResult response;
                try
                {
                    response = await renderer.RenderPage(snapshotUrl);
                }
                catch (WebException e)
                {
                    context.Response.Write(string.Format("Failed to prerender request '{0}'. Error message: '{1}'. Stack trace: '{2}'", snapshotUrl, e.Message, e.StackTrace));
                    context.Response.StatusCode = 500;
                    return;
                }

                await context.Response.WriteAsync(response.Content);
                context.Response.ContentType = "text/html";
                context.Response.StatusCode = (int)response.StatusCode;
            }
            else
            {
                await Next.Invoke(context);
            }
        }
コード例 #5
0
ファイル: UtilityTests.cs プロジェクト: remy22/DotnetSPA
        public void TestCorrectRobotRequestDetectionByFragment()
        {
            var requestUrl = "http://localhost:4434/?q=2&_escaped_fragment_=/about";
            var userAgent = "Googlebot";

            var requestParams = new RequestParams
            {
                RequestUri = new Uri(requestUrl),
                UserAgent = userAgent
            };

            Assert.IsTrue(Utility.IsRequestShouldBePrerendered(requestParams, _config));
        }