#17610: Adding validation on warmup pages

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-03-30 16:16:24 -07:00
parent 1f4a996735
commit d59b7162c2
2 changed files with 9 additions and 79 deletions

View File

@@ -51,7 +51,14 @@ namespace Orchard.Warmup.Controllers {
var warmupPart = Services.WorkContext.CurrentSite.As<WarmupSettingsPart>();
if(TryUpdateModel(warmupPart)) {
Services.Notifier.Information(T("Warmup updated successfully."));
using (var urlReader = new StringReader(warmupPart.Urls)) {
string relativeUrl;
while (null != (relativeUrl = urlReader.ReadLine())) {
if(!Uri.IsWellFormedUriString(relativeUrl, UriKind.Relative) || !(relativeUrl.StartsWith("/"))) {
AddModelError("Urls", T("{0} is an invalid warmup url.", relativeUrl));
}
}
}
}
if (warmupPart.Scheduled) {
@@ -60,6 +67,7 @@ namespace Orchard.Warmup.Controllers {
}
}
Services.Notifier.Information(T("Warmup updated successfully."));
return View(warmupPart);
}
@@ -76,83 +84,6 @@ namespace Orchard.Warmup.Controllers {
return result;
}
[FormValueRequired("submit.Extract")]
[HttpPost, ActionName("Index")]
public ActionResult IndexPostExtract() {
var baseUrl = Services.WorkContext.CurrentSite.BaseUrl;
baseUrl = VirtualPathUtility.AppendTrailingSlash(baseUrl);
var part = Services.WorkContext.CurrentSite.As<WarmupSettingsPart>();
if (String.IsNullOrWhiteSpace(baseUrl) || String.IsNullOrWhiteSpace(part.Urls)) {
return RedirectToAction("Index");
}
var regex = new Regex(@"<link\s[^>]*href=""(?<url>[^""]*\.css)""|<script\s[^>]*src=""(?<url>[^""]*\.js)""", RegexOptions.IgnoreCase);
var resources = new List<string>();
// add the already registered urls to remove duplicates
using (var urlReader = new StringReader(part.Urls)) {
string relativeUrl;
while (null != (relativeUrl = urlReader.ReadLine())) {
if (String.IsNullOrWhiteSpace(relativeUrl)) {
continue;
}
relativeUrl = relativeUrl.Trim();
resources.Add(relativeUrl);
try {
var contentUrl = VirtualPathUtility.RemoveTrailingSlash(baseUrl) + relativeUrl;
var filename = WarmupUtility.EncodeUrl(contentUrl.TrimEnd('/'));
var path = _appDataFolder.Combine("Warmup", filename);
if(!_appDataFolder.FileExists(path)) {
continue;
}
var content = _appDataFolder.ReadFile(path);
// process only html files
if (!content.Contains("<html") && !content.Contains("</html")) {
continue;
}
var localPrefix = Request.ApplicationPath ?? "/";
var matches = regex.Matches(content);
foreach (Match m in matches) {
var url = m.Groups["url"].Value;
if (url.StartsWith(localPrefix, StringComparison.OrdinalIgnoreCase)) {
resources.Add(url.Substring(localPrefix.Length));
}
else if (url.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase)) {
resources.Add("/" + url.Substring(baseUrl.Length));
}
else if (!url.StartsWith("http://") && !url.StartsWith("/")) {
// relative urls e.g., ../, foo.js, ...
relativeUrl = VirtualPathUtility.AppendTrailingSlash(relativeUrl);
url = VirtualPathUtility.Combine(relativeUrl, url);
resources.Add(url);
}
}
}
catch {
// if something unexpected happens, process next file
continue;
}
}
}
// extract unique urls
var uniqueResources = resources.GroupBy(x => x.ToLowerInvariant()).Select(x => x.First()).ToArray();
part.Urls = String.Join(System.Environment.NewLine, uniqueResources);
return RedirectToAction("Index");
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}

View File

@@ -33,6 +33,5 @@
<fieldset>
<button class="primaryAction" name="submit" value="@T("Save")" type="submit">@T("Save")</button>
<button class="primaryAction" name="submit.Generate" value="@T("Save and generate")" type="submit">@T("Save and generate")</button>
<button class="primaryAction" name="submit.Extract" value="@T("Extract resources")" type="submit">@T("Extract resources")</button>
</fieldset>
}