diff --git a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs index 85921d73954be..d9c24591fc69a 100644 --- a/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs +++ b/dotnet/src/webdriver/Remote/HttpCommandExecutor.cs @@ -43,12 +43,9 @@ public class HttpCommandExecutor : ICommandExecutor private const string RequestAcceptHeader = JsonMimeType + ", " + PngMimeType; private const string RequestContentTypeHeader = JsonMimeType + "; charset=" + Utf8CharsetType; private const string UserAgentHeaderTemplate = "selenium/{0} (.net {1})"; - private Uri remoteServerUri; - private TimeSpan serverResponseTimeout; - private string userAgent; - private bool enableKeepAlive; + private readonly Uri remoteServerUri; + private readonly TimeSpan serverResponseTimeout; private bool isDisposed; - private IWebProxy proxy; private CommandInfoRepository commandInfoRepository = new W3CWireProtocolCommandInfoRepository(); private HttpClient client; @@ -71,6 +68,7 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout) /// The timeout within which the server must respond. /// if the KeepAlive header should be sent /// with HTTP requests; otherwise, . + /// If is . public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool enableKeepAlive) { if (addressOfRemoteServer == null) @@ -83,10 +81,10 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena addressOfRemoteServer = new Uri(addressOfRemoteServer.ToString() + "/"); } - this.userAgent = string.Format(CultureInfo.InvariantCulture, UserAgentHeaderTemplate, ResourceUtilities.ProductVersion, ResourceUtilities.PlatformFamily); + this.UserAgent = string.Format(CultureInfo.InvariantCulture, UserAgentHeaderTemplate, ResourceUtilities.ProductVersion, ResourceUtilities.PlatformFamily); this.remoteServerUri = addressOfRemoteServer; this.serverResponseTimeout = timeout; - this.enableKeepAlive = enableKeepAlive; + this.IsKeepAliveEnabled = enableKeepAlive; } /// @@ -100,41 +98,30 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena /// between this and the remote end WebDriver /// implementation. /// - public IWebProxy Proxy - { - get { return this.proxy; } - set { this.proxy = value; } - } + public IWebProxy Proxy { get; set; } /// /// Gets or sets a value indicating whether keep-alive is enabled for HTTP /// communication between this and the /// remote end WebDriver implementation. /// - public bool IsKeepAliveEnabled - { - get { return this.enableKeepAlive; } - set { this.enableKeepAlive = value; } - } + public bool IsKeepAliveEnabled { get; set; } /// /// Gets or sets the user agent string used for HTTP communication /// batween this and the remote end /// WebDriver implementation /// - public string UserAgent - { - get { return this.userAgent; } - set { this.userAgent = value; } - } + public string UserAgent { get; set; } /// /// Gets the repository of objects containing information about commands. /// + /// If the value is set to . protected CommandInfoRepository CommandInfoRepository { - get { return this.commandInfoRepository; } - set { this.commandInfoRepository = value; } + get => this.commandInfoRepository; + set => this.commandInfoRepository = value ?? throw new ArgumentNullException(nameof(value), "CommandInfoRepository cannot be null"); } /// @@ -145,8 +132,7 @@ protected CommandInfoRepository CommandInfoRepository /// if the new command has been added successfully; otherwise, . public bool TryAddCommand(string commandName, CommandInfo info) { - HttpCommandInfo commandInfo = info as HttpCommandInfo; - if (commandInfo == null) + if (info is not HttpCommandInfo commandInfo) { return false; } @@ -193,7 +179,7 @@ public virtual async Task ExecuteAsync(Command commandToExecute) } HttpRequestInfo requestInfo = new HttpRequestInfo(this.remoteServerUri, commandToExecute, info); - HttpResponseInfo responseInfo = null; + HttpResponseInfo responseInfo; try { responseInfo = await this.MakeHttpRequest(requestInfo).ConfigureAwait(false); @@ -304,12 +290,11 @@ private async Task MakeHttpRequest(HttpRequestInfo requestInfo using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage).ConfigureAwait(false)) { - HttpResponseInfo httpResponseInfo = new HttpResponseInfo(); - httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); - httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType?.ToString(); - httpResponseInfo.StatusCode = responseMessage.StatusCode; + var responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); + var responseContentType = responseMessage.Content.Headers.ContentType?.ToString(); + var responseStatusCode = responseMessage.StatusCode; - return httpResponseInfo; + return new HttpResponseInfo(responseBody, responseContentType, responseStatusCode); } } } @@ -346,12 +331,15 @@ private Response CreateResponse(HttpResponseInfo responseInfo) return response; } +#nullable enable + /// /// Releases all resources used by the . /// public void Dispose() { this.Dispose(true); + GC.SuppressFinalize(this); } /// @@ -364,10 +352,7 @@ protected virtual void Dispose(bool disposing) { if (!this.isDisposed) { - if (this.client != null) - { - this.client.Dispose(); - } + this.client?.Dispose(); this.isDisposed = true; } @@ -377,6 +362,11 @@ private class HttpRequestInfo { public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo commandInfo) { + if (commandInfo is null) + { + throw new ArgumentNullException(nameof(commandInfo)); + } + this.FullUri = commandInfo.CreateCommandUri(serverUri, commandToExecute); this.HttpMethod = commandInfo.Method; this.RequestBody = commandToExecute.ParametersAsJsonString; @@ -389,9 +379,16 @@ public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo private class HttpResponseInfo { + public HttpResponseInfo(string body, string? contentType, HttpStatusCode statusCode) + { + this.Body = body ?? throw new ArgumentNullException(nameof(body)); + this.ContentType = contentType; + this.StatusCode = statusCode; + } + public HttpStatusCode StatusCode { get; set; } public string Body { get; set; } - public string ContentType { get; set; } + public string? ContentType { get; set; } } /// @@ -404,7 +401,12 @@ private class DiagnosticsHttpHandler : DelegatingHandler public DiagnosticsHttpHandler(HttpMessageHandler messageHandler, ILogger logger) : base(messageHandler) { - _logger = logger; + if (messageHandler is null) + { + throw new ArgumentNullException(nameof(messageHandler)); + } + + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } ///