From 8adc13bdc85880c85b9710d3d7a5d1a8eed9666c Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:45:29 +0300 Subject: [PATCH 1/5] Expose handler and client --- dotnet/src/webdriver/Remote/HttpCommandExecutor.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index 2bcee9c7673ad..4f14c16ca377f 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -221,7 +221,7 @@ protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventA this.SendingRemoteHttpRequest?.Invoke(this, eventArgs); } - private HttpClient CreateHttpClient() + protected virtual HttpClientHandler CreateHttpClientHandler() { HttpClientHandler httpClientHandler = new HttpClientHandler(); string userInfo = this.remoteServerUri.UserInfo; @@ -234,6 +234,13 @@ private HttpClient CreateHttpClient() httpClientHandler.Proxy = this.Proxy; + return httpClientHandler; + } + + protected virtual HttpClient CreateHttpClient() + { + var httpClientHandler = CreateHttpClientHandler(); + HttpMessageHandler handler = httpClientHandler; if (_logger.IsEnabled(LogEventLevel.Trace)) From 2bf54c6041ea88ce27a0117c5de60a4c90805fa7 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:50:02 +0300 Subject: [PATCH 2/5] Docs --- dotnet/src/webdriver/Remote/HttpCommandExecutor.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index 4f14c16ca377f..93dea4a179ccf 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -221,6 +221,11 @@ protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventA this.SendingRemoteHttpRequest?.Invoke(this, eventArgs); } + /// + /// Creates an instance of as underlying handler, + /// used by . + /// + /// An instance of . protected virtual HttpClientHandler CreateHttpClientHandler() { HttpClientHandler httpClientHandler = new HttpClientHandler(); @@ -236,7 +241,10 @@ protected virtual HttpClientHandler CreateHttpClientHandler() return httpClientHandler; } - + /// + /// Creates an instance of used by making all HTTP calls to remote end. + /// + /// An instance of . protected virtual HttpClient CreateHttpClient() { var httpClientHandler = CreateHttpClientHandler(); From 36f4aa39ab358b9e5ccef511a93df0ae136ff8e9 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:14:05 +0300 Subject: [PATCH 3/5] Null check for handler --- dotnet/src/webdriver/Remote/HttpCommandExecutor.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index 93dea4a179ccf..f197a7c937772 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -241,13 +241,14 @@ protected virtual HttpClientHandler CreateHttpClientHandler() return httpClientHandler; } + /// /// Creates an instance of used by making all HTTP calls to remote end. /// /// An instance of . protected virtual HttpClient CreateHttpClient() { - var httpClientHandler = CreateHttpClientHandler(); + var httpClientHandler = CreateHttpClientHandler() ?? throw new InvalidOperationException($"{nameof(CreateHttpClientHandler)} method returned null"); HttpMessageHandler handler = httpClientHandler; From a2fcaa60471dc75e2aff24fb243bd4c9b6f902ce Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:33:01 +0300 Subject: [PATCH 4/5] More docs --- dotnet/src/webdriver/Remote/HttpCommandExecutor.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index f197a7c937772..16c1f88a8795d 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -223,7 +223,7 @@ protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventA /// /// Creates an instance of as underlying handler, - /// used by . + /// used by . Invoked only once when required. /// /// An instance of . protected virtual HttpClientHandler CreateHttpClientHandler() @@ -244,6 +244,7 @@ protected virtual HttpClientHandler CreateHttpClientHandler() /// /// Creates an instance of used by making all HTTP calls to remote end. + /// Invoked only once when required. /// /// An instance of . protected virtual HttpClient CreateHttpClient() From 3e91b05a18fc211823ab21f8f773bccc24067e00 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:36:19 +0300 Subject: [PATCH 5/5] Enhance code syntax while I am here --- dotnet/src/webdriver/Remote/HttpCommandExecutor.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index 16c1f88a8795d..6743984a8eb22 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -228,11 +228,13 @@ protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventA /// An instance of . protected virtual HttpClientHandler CreateHttpClientHandler() { - HttpClientHandler httpClientHandler = new HttpClientHandler(); + HttpClientHandler httpClientHandler = new(); + string userInfo = this.remoteServerUri.UserInfo; - if (!string.IsNullOrEmpty(userInfo) && userInfo.Contains(":")) + + if (!string.IsNullOrEmpty(userInfo) && userInfo.Contains(':')) { - string[] userInfoComponents = this.remoteServerUri.UserInfo.Split(new char[] { ':' }, 2); + string[] userInfoComponents = this.remoteServerUri.UserInfo.Split([':'], 2); httpClientHandler.Credentials = new NetworkCredential(userInfoComponents[0], userInfoComponents[1]); httpClientHandler.PreAuthenticate = true; } @@ -249,7 +251,8 @@ protected virtual HttpClientHandler CreateHttpClientHandler() /// An instance of . protected virtual HttpClient CreateHttpClient() { - var httpClientHandler = CreateHttpClientHandler() ?? throw new InvalidOperationException($"{nameof(CreateHttpClientHandler)} method returned null"); + var httpClientHandler = CreateHttpClientHandler() + ?? throw new InvalidOperationException($"{nameof(CreateHttpClientHandler)} method returned null"); HttpMessageHandler handler = httpClientHandler; @@ -259,15 +262,18 @@ protected virtual HttpClient CreateHttpClient() } var client = new HttpClient(handler); + client.DefaultRequestHeaders.UserAgent.ParseAdd(this.UserAgent); client.DefaultRequestHeaders.Accept.ParseAdd(RequestAcceptHeader); client.DefaultRequestHeaders.ExpectContinue = false; + if (!this.IsKeepAliveEnabled) { client.DefaultRequestHeaders.Connection.ParseAdd("close"); } client.Timeout = this.serverResponseTimeout; + return client; }