From 5d5c260ca8ffa707a8b07c7dbceec589f9791b19 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Thu, 18 Apr 2019 08:15:47 +0200 Subject: [PATCH] SEBWIN-226: Implemented custom user agent suffix. --- .../Handlers/RequestHandler.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/SafeExamBrowser.Browser/Handlers/RequestHandler.cs b/SafeExamBrowser.Browser/Handlers/RequestHandler.cs index d52c4e3a..f84b725b 100644 --- a/SafeExamBrowser.Browser/Handlers/RequestHandler.cs +++ b/SafeExamBrowser.Browser/Handlers/RequestHandler.cs @@ -7,6 +7,7 @@ */ using System; +using System.Collections.Specialized; using CefSharp; using CefSharp.Handler; using SafeExamBrowser.Contracts.Configuration; @@ -26,6 +27,23 @@ namespace SafeExamBrowser.Browser.Handlers } public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) + { + AppendCustomUserAgent(request); + 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.ProgramVersion}"; + request.Headers = headers; + } + + private void ReplaceCustomScheme(IRequest request) { var uri = new Uri(request.Url); @@ -37,8 +55,6 @@ namespace SafeExamBrowser.Browser.Handlers { request.Url = new UriBuilder(uri) { Scheme = Uri.UriSchemeHttps }.Uri.AbsoluteUri; } - - return base.OnBeforeResourceLoad(browserControl, browser, frame, request, callback); } } }