SEBWIN-467: Fixed issue where the browser rendered configuration files instead of downloading them.

This commit is contained in:
Damian Büchel 2021-04-13 17:12:09 +02:00
parent 42c614cf24
commit 62b72c85e1

View file

@ -7,6 +7,7 @@
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
using CefSharp;
using SafeExamBrowser.Browser.Contracts.Filters;
@ -23,6 +24,7 @@ namespace SafeExamBrowser.Browser.Handlers
{
internal class RequestHandler : CefSharp.Handler.RequestHandler
{
private AppConfig appConfig;
private IRequestFilter filter;
private ILogger logger;
private string quitUrlPattern;
@ -42,6 +44,7 @@ namespace SafeExamBrowser.Browser.Handlers
WindowSettings windowSettings,
IText text)
{
this.appConfig = appConfig;
this.filter = filter;
this.logger = logger;
this.resourceHandler = resourceHandler;
@ -91,6 +94,13 @@ namespace SafeExamBrowser.Browser.Handlers
return true;
}
if (IsConfigurationFile(request, out var downloadUrl))
{
browser.GetHost().StartDownload(downloadUrl);
return true;
}
return base.OnBeforeBrowse(webBrowser, browser, frame, request, userGesture, isRedirect);
}
@ -109,6 +119,31 @@ namespace SafeExamBrowser.Browser.Handlers
}
}
private bool IsConfigurationFile(IRequest request, out string downloadUrl)
{
var uri = new Uri(request.Url);
var uriExtension = Path.GetExtension(uri.AbsolutePath);
var isConfigurationFile = string.Equals(appConfig.ConfigurationFileExtension, uriExtension, StringComparison.OrdinalIgnoreCase);
downloadUrl = request.Url;
if (isConfigurationFile)
{
if (uri.Scheme == appConfig.SebUriScheme)
{
downloadUrl = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttp }.Uri.AbsoluteUri;
}
else if (uri.Scheme == appConfig.SebUriSchemeSecure)
{
downloadUrl = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttps }.Uri.AbsoluteUri;
}
logger.Debug($"Detected configuration file {(windowSettings.UrlPolicy.CanLog() ? $"'{uri}'" : "")}.");
}
return isConfigurationFile;
}
private bool IsQuitUrl(IRequest request)
{
var isQuitUrl = false;