private void WarmupUrl(string baseUrl, string url, ICollection <ReportEntry> reportEntries)
        {
            var relativeUrl = url.Trim().Replace(baseUrl, string.Empty);

            try {
                var filename = WarmupUtility.EncodeUrl(url.TrimEnd('/'));
                var path     = _appDataFolder.Combine(BaseFolder, filename);

                var download = _webDownloader.Download(url);

                if (download != null)
                {
                    if (download.StatusCode == HttpStatusCode.OK)
                    {
                        // success
                        _appDataFolder.CreateFile(path, download.Content);

                        reportEntries.Add(new ReportEntry {
                            RelativeUrl = relativeUrl,
                            Filename    = filename,
                            StatusCode  = (int)download.StatusCode,
                            CreatedUtc  = _clock.UtcNow
                        });

                        // if the base url contains http://www, then also render the www-less one);

                        if (url.StartsWith("http://www.", StringComparison.OrdinalIgnoreCase))
                        {
                            url      = "http://" + url.Substring("http://www.".Length);
                            filename = WarmupUtility.EncodeUrl(url.TrimEnd('/'));
                            path     = _appDataFolder.Combine(BaseFolder, filename);
                            _appDataFolder.CreateFile(path, download.Content);
                        }
                    }
                    else
                    {
                        reportEntries.Add(new ReportEntry {
                            RelativeUrl = relativeUrl,
                            Filename    = filename,
                            StatusCode  = (int)download.StatusCode,
                            CreatedUtc  = _clock.UtcNow
                        });
                    }
                }
                else
                {
                    // download failed
                    reportEntries.Add(new ReportEntry {
                        RelativeUrl = relativeUrl,
                        Filename    = filename,
                        StatusCode  = 0,
                        CreatedUtc  = _clock.UtcNow
                    });
                }
            } catch (Exception e) {
                Logger.Error(e, "Could not extract warmup page content for: ", url);
            }
        }
Exemple #2
0
        public void ClearingUrlsShouldDeleteContent()
        {
            _settings.Urls = @" /
                                /About";

            ((StubWorkContextAccessor.WorkContextImpl.StubSite)_orchardServices.WorkContext.CurrentSite).BaseUrl = "http://www.orchardproject.net/";

            _webDownloader
            .Setup(w => w.Download("http://www.orchardproject.net/"))
            .Returns(new DownloadResult {
                Content = "Foo", StatusCode = HttpStatusCode.OK
            });

            _webDownloader
            .Setup(w => w.Download("http://www.orchardproject.net/About"))
            .Returns(new DownloadResult {
                Content = "Bar", StatusCode = HttpStatusCode.OK
            });

            _warmupUpdater.Generate();
            var files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://www.orchardproject.net"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://www.orchardproject.net/About"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net/About"))));

            _settings.Urls = @"";

            _warmupUpdater.Generate();
            files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files.Count, Is.EqualTo(0));
        }
Exemple #3
0
        public void ShouldNotDeleteOtherFiles()
        {
            _settings.Urls = @" /
                                /About";

            ((StubWorkContextAccessor.WorkContextImpl.StubSite)_orchardServices.WorkContext.CurrentSite).BaseUrl = "http://www.orchardproject.net/";

            _webDownloader
            .Setup(w => w.Download("http://www.orchardproject.net/"))
            .Returns(new DownloadResult {
                Content = "Foo", StatusCode = HttpStatusCode.OK
            });

            _webDownloader
            .Setup(w => w.Download("http://www.orchardproject.net/About"))
            .Returns(new DownloadResult {
                Content = "Bar", StatusCode = HttpStatusCode.OK
            });

            // Create a static file in the warmup folder
            _appDataFolder.CreateFile(_appDataFolder.Combine(WarmupFolder, "foo.txt"), "Foo");

            _warmupUpdater.Generate();
            var files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, "foo.txt")));

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://www.orchardproject.net"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://www.orchardproject.net/About"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net/About"))));
        }
Exemple #4
0
        public void ShouldProcessIfDelayHasExpired()
        {
            _settings.Urls      = @"/";
            _settings.Delay     = 90;
            _settings.Scheduled = true;

            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/"))
            .Returns(new DownloadResult {
                Content = "Foo", StatusCode = HttpStatusCode.OK
            });

            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/About"))
            .Returns(new DownloadResult {
                Content = "Bar", StatusCode = HttpStatusCode.OK
            });

            _warmupUpdater.Generate();

            var warmupContent = _appDataFolder.ReadFile(_warmupFilename);

            Assert.That(warmupContent, Is.EqualTo(XmlConvert.ToString(_clock.UtcNow, XmlDateTimeSerializationMode.Utc)));

            _settings.Urls = @" /
                                /About";
            _clock.Advance(TimeSpan.FromMinutes(91));
            _warmupUpdater.EnsureGenerate();

            var files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net/About"))));

            warmupContent = _appDataFolder.ReadFile(_warmupFilename);
            Assert.That(warmupContent, Is.EqualTo(XmlConvert.ToString(_clock.UtcNow, XmlDateTimeSerializationMode.Utc)));
        }
Exemple #5
0
        public void ShouldProcessValidRequestsOnly()
        {
            _settings.Urls = @" /
                                <>@\\";
            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/"))
            .Returns(new DownloadResult {
                Content = "Foo", StatusCode = HttpStatusCode.OK
            });

            _warmupUpdater.Generate();
            var files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files.Count, Is.EqualTo(1));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net"))));
        }
Exemple #6
0
        public void ShouldCreateFilesForOkStatusOnly()
        {
            _settings.Urls = @" /
                                /About";
            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/"))
            .Returns(new DownloadResult {
                Content = "Foo", StatusCode = HttpStatusCode.OK
            });

            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/About"))
            .Returns(new DownloadResult {
                Content = "Bar", StatusCode = HttpStatusCode.NotFound
            });

            _warmupUpdater.Generate();
            var files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net"))));
            Assert.That(files, Has.None.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net/About"))));
        }
Exemple #7
0
        public void ShouldDownloadConfiguredUrls()
        {
            _settings.Urls = @" /
                                /About";
            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/"))
            .Returns(new DownloadResult {
                Content = "Foo", StatusCode = HttpStatusCode.OK
            });

            _webDownloader
            .Setup(w => w.Download("http://orchardproject.net/About"))
            .Returns(new DownloadResult {
                Content = "Bar", StatusCode = HttpStatusCode.OK
            });

            _warmupUpdater.Generate();
            var files = _appDataFolder.ListFiles(WarmupFolder).ToList();

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net"))));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net/About"))));

            files = _appDataFolder.ListFiles(TenantFolder).ToList();

            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(TenantFolder, "warmup.txt")));
            Assert.That(files, Has.Some.Matches <string>(x => x == _appDataFolder.Combine(TenantFolder, "warmup.xml")));

            var homepageContent = _appDataFolder.ReadFile(_appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net")));
            var aboutcontent    = _appDataFolder.ReadFile(_appDataFolder.Combine(WarmupFolder, WarmupUtility.EncodeUrl("http://orchardproject.net/About")));

            Assert.That(homepageContent, Is.EqualTo("Foo"));
            Assert.That(aboutcontent, Is.EqualTo("Bar"));
        }
Exemple #8
0
 public void EmptyStringsAreNotAllowed()
 {
     Assert.Throws <ArgumentException>(() => WarmupUtility.EncodeUrl(""));
     Assert.Throws <ArgumentException>(() => WarmupUtility.EncodeUrl(null));
 }
Exemple #9
0
 public void EncodedUrlsShouldPreserveQueryStrings()
 {
     Assert.That(WarmupUtility.EncodeUrl("http://www.microsoft.com/foo?bar=baz"), Is.EqualTo("bar"));
     Assert.That(WarmupUtility.EncodeUrl("http://www.microsoft.com/foo?bar=baz"), Is.EqualTo("baz"));
     Assert.That(WarmupUtility.EncodeUrl("http://www.microsoft.com/foo?bar=baz"), Is.EqualTo("foo"));
 }
Exemple #10
0
 public void EncodedUrlsShouldBeValidFilenames()
 {
     Assert.That(WarmupUtility.EncodeUrl("http://www.microsoft.com"), Is.EqualTo("http_3A_2F_2Fwww_2Emicrosoft_2Ecom"));
     Assert.That(WarmupUtility.EncodeUrl("http://www.microsoft.com/foo?bar=baz"), Is.EqualTo("http_3A_2F_2Fwww_2Emicrosoft_2Ecom_2Ffoo_3Fbar_3Dbaz"));
 }
        public void EnsureGenerate()
        {
            var baseUrl = _orchardServices.WorkContext.CurrentSite.BaseUrl;
            var part    = _orchardServices.WorkContext.CurrentSite.As <WarmupSettingsPart>();

            // do nothing while the base url setting is not defined
            if (String.IsNullOrWhiteSpace(baseUrl))
            {
                return;
            }

            // prevent multiple appdomains from rebuilding the static page concurrently (e.g., command line)
            ILockFile lockFile = null;

            if (!_lockFileManager.TryAcquireLock(_lockFilename, ref lockFile))
            {
                return;
            }

            using (lockFile) {
                // check if we need to regenerate the pages by reading the last time it has been done
                // 1- if the warmup file doesn't exists, generate the pages
                // 2- otherwise, if the scheduled generation option is on, check if the delay is over
                if (_appDataFolder.FileExists(_warmupPath))
                {
                    try {
                        var warmupContent = _appDataFolder.ReadFile(_warmupPath);
                        var expired       = XmlConvert.ToDateTimeOffset(warmupContent).AddMinutes(part.Delay);
                        if (expired > _clock.UtcNow)
                        {
                            return;
                        }
                    }
                    catch {
                        // invalid file, delete continue processing
                        _appDataFolder.DeleteFile(_warmupPath);
                    }
                }

                // delete peviously generated pages, by reading the Warmup Report file
                try {
                    var encodedPrefix = WarmupUtility.EncodeUrl("http://www.");

                    foreach (var reportEntry in _reportManager.Read())
                    {
                        try {
                            // use FileName as the SiteBaseUrl could have changed in the meantime
                            var path = _appDataFolder.Combine(BaseFolder, reportEntry.Filename);
                            _appDataFolder.DeleteFile(path);

                            // delete the www-less version too if it's available
                            if (reportEntry.Filename.StartsWith(encodedPrefix, StringComparison.OrdinalIgnoreCase))
                            {
                                var filename = WarmupUtility.EncodeUrl("http://") + reportEntry.Filename.Substring(encodedPrefix.Length);
                                path = _appDataFolder.Combine(BaseFolder, filename);
                                _appDataFolder.DeleteFile(path);
                            }
                        }
                        catch (Exception e) {
                            Logger.Error(e, "Could not delete specific warmup file: ", reportEntry.Filename);
                        }
                    }
                }
                catch (Exception e) {
                    Logger.Error(e, "Could not read warmup report file");
                }

                var reportEntries = new List <ReportEntry>();

                if (!String.IsNullOrEmpty(part.Urls))
                {
                    // loop over every relative url to generate the contents
                    using (var urlReader = new StringReader(part.Urls)) {
                        string relativeUrl;
                        while (null != (relativeUrl = urlReader.ReadLine()))
                        {
                            if (String.IsNullOrWhiteSpace(relativeUrl))
                            {
                                continue;
                            }

                            string url = null;
                            relativeUrl = relativeUrl.Trim();

                            try {
                                url = VirtualPathUtility.RemoveTrailingSlash(baseUrl) + relativeUrl;
                                var filename = WarmupUtility.EncodeUrl(url.TrimEnd('/'));
                                var path     = _appDataFolder.Combine(BaseFolder, filename);

                                var download = _webDownloader.Download(url);

                                if (download != null)
                                {
                                    if (download.StatusCode == HttpStatusCode.OK)
                                    {
                                        // success
                                        _appDataFolder.CreateFile(path, download.Content);

                                        reportEntries.Add(new ReportEntry {
                                            RelativeUrl = relativeUrl,
                                            Filename    = filename,
                                            StatusCode  = (int)download.StatusCode,
                                            CreatedUtc  = _clock.UtcNow
                                        });

                                        // if the base url contains http://www, then also render the www-less one);

                                        if (url.StartsWith("http://www.", StringComparison.OrdinalIgnoreCase))
                                        {
                                            url      = "http://" + url.Substring("http://www.".Length);
                                            filename = WarmupUtility.EncodeUrl(url.TrimEnd('/'));
                                            path     = _appDataFolder.Combine(BaseFolder, filename);
                                            _appDataFolder.CreateFile(path, download.Content);
                                        }
                                    }
                                    else
                                    {
                                        reportEntries.Add(new ReportEntry {
                                            RelativeUrl = relativeUrl,
                                            Filename    = filename,
                                            StatusCode  = (int)download.StatusCode,
                                            CreatedUtc  = _clock.UtcNow
                                        });
                                    }
                                }
                                else
                                {
                                    // download failed
                                    reportEntries.Add(new ReportEntry {
                                        RelativeUrl = relativeUrl,
                                        Filename    = filename,
                                        StatusCode  = 0,
                                        CreatedUtc  = _clock.UtcNow
                                    });
                                }
                            }
                            catch (Exception e) {
                                Logger.Error(e, "Could not extract warmup page content for: ", url);
                            }
                        }
                    }
                }

                _reportManager.Create(reportEntries);

                // finally write the time the generation has been executed
                _appDataFolder.CreateFile(_warmupPath, XmlConvert.ToString(_clock.UtcNow, XmlDateTimeSerializationMode.Utc));
            }
        }