/* * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ using System; using System.Collections.Specialized; using CefSharp; using SafeExamBrowser.Configuration.Contracts; namespace SafeExamBrowser.Browser.Handlers { internal class ResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler { private AppConfig appConfig; internal ResourceRequestHandler(AppConfig appConfig) { this.appConfig = appConfig; } protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { // TODO: CEF does not yet support intercepting requests from service workers, thus the user agent must be statically set at browser // startup for now. Once CEF has full support of service workers, the static user agent should be removed and the method below // reactivated. See https://bitbucket.org/chromiumembedded/cef/issues/2622 for the current status of development. // AppendCustomUserAgent(request); if (IsMailtoUrl(request.Url)) { return CefReturnValue.Cancel; } ReplaceCustomScheme(request); return base.OnBeforeResourceLoad(browserControl, browser, frame, request, callback); } private void AppendCustomUserAgent(IRequest request) { var headers = new NameValueCollection(request.Headers); var userAgent = request.Headers["User-Agent"]; headers["User-Agent"] = $"{userAgent} SEB/{appConfig.ProgramInformationalVersion}"; request.Headers = headers; } private bool IsMailtoUrl(string url) { return url.StartsWith(Uri.UriSchemeMailto); } private void ReplaceCustomScheme(IRequest request) { if (Uri.IsWellFormedUriString(request.Url, UriKind.RelativeOrAbsolute)) { var uri = new Uri(request.Url); if (uri.Scheme == appConfig.SebUriScheme) { request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttp }.Uri.AbsoluteUri; } else if (uri.Scheme == appConfig.SebUriSchemeSecure) { request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttps }.Uri.AbsoluteUri; } } } } }