Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[dotnet] Possibility to override underlying HttpClient/HttpClientHandler for all HTTP requests #15283

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,39 @@ protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventA
this.SendingRemoteHttpRequest?.Invoke(this, eventArgs);
}

private HttpClient CreateHttpClient()
/// <summary>
/// Creates an instance of <see cref="HttpClientHandler"/> as underlying handler,
/// used by <see cref="CreateHttpClient"/>. Invoked only once when required.
/// </summary>
/// <returns>An instance of <see cref="HttpClientHandler"/>.</returns>
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;
}

httpClientHandler.Proxy = this.Proxy;

return httpClientHandler;
}

/// <summary>
/// Creates an instance of <see cref="HttpClient"/> used by making all HTTP calls to remote end.
/// Invoked only once when required.
/// </summary>
/// <returns>An instance of <see cref="HttpClient"/>.</returns>
protected virtual HttpClient CreateHttpClient()
{
var httpClientHandler = CreateHttpClientHandler()
?? throw new InvalidOperationException($"{nameof(CreateHttpClientHandler)} method returned null");

HttpMessageHandler handler = httpClientHandler;

if (_logger.IsEnabled(LogEventLevel.Trace))
Expand All @@ -242,15 +262,18 @@ private 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;
}

Expand Down
Loading