@@ -43,12 +43,9 @@ public class HttpCommandExecutor : ICommandExecutor
43
43
private const string RequestAcceptHeader = JsonMimeType + ", " + PngMimeType ;
44
44
private const string RequestContentTypeHeader = JsonMimeType + "; charset=" + Utf8CharsetType ;
45
45
private const string UserAgentHeaderTemplate = "selenium/{0} (.net {1})" ;
46
- private Uri remoteServerUri ;
47
- private TimeSpan serverResponseTimeout ;
48
- private string userAgent ;
49
- private bool enableKeepAlive ;
46
+ private readonly Uri remoteServerUri ;
47
+ private readonly TimeSpan serverResponseTimeout ;
50
48
private bool isDisposed ;
51
- private IWebProxy proxy ;
52
49
private CommandInfoRepository commandInfoRepository = new W3CWireProtocolCommandInfoRepository ( ) ;
53
50
private HttpClient client ;
54
51
@@ -71,6 +68,7 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout)
71
68
/// <param name="timeout">The timeout within which the server must respond.</param>
72
69
/// <param name="enableKeepAlive"><see langword="true"/> if the KeepAlive header should be sent
73
70
/// with HTTP requests; otherwise, <see langword="false"/>.</param>
71
+ /// <exception cref="ArgumentNullException">If <paramref name="addressOfRemoteServer"/> is <see langword="null"/>.</exception>
74
72
public HttpCommandExecutor ( Uri addressOfRemoteServer , TimeSpan timeout , bool enableKeepAlive )
75
73
{
76
74
if ( addressOfRemoteServer == null )
@@ -83,10 +81,10 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
83
81
addressOfRemoteServer = new Uri ( addressOfRemoteServer . ToString ( ) + "/" ) ;
84
82
}
85
83
86
- this . userAgent = string . Format ( CultureInfo . InvariantCulture , UserAgentHeaderTemplate , ResourceUtilities . ProductVersion , ResourceUtilities . PlatformFamily ) ;
84
+ this . UserAgent = string . Format ( CultureInfo . InvariantCulture , UserAgentHeaderTemplate , ResourceUtilities . ProductVersion , ResourceUtilities . PlatformFamily ) ;
87
85
this . remoteServerUri = addressOfRemoteServer ;
88
86
this . serverResponseTimeout = timeout ;
89
- this . enableKeepAlive = enableKeepAlive ;
87
+ this . IsKeepAliveEnabled = enableKeepAlive ;
90
88
}
91
89
92
90
/// <summary>
@@ -100,41 +98,30 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
100
98
/// between this <see cref="HttpCommandExecutor"/> and the remote end WebDriver
101
99
/// implementation.
102
100
/// </summary>
103
- public IWebProxy Proxy
104
- {
105
- get { return this . proxy ; }
106
- set { this . proxy = value ; }
107
- }
101
+ public IWebProxy Proxy { get ; set ; }
108
102
109
103
/// <summary>
110
104
/// Gets or sets a value indicating whether keep-alive is enabled for HTTP
111
105
/// communication between this <see cref="HttpCommandExecutor"/> and the
112
106
/// remote end WebDriver implementation.
113
107
/// </summary>
114
- public bool IsKeepAliveEnabled
115
- {
116
- get { return this . enableKeepAlive ; }
117
- set { this . enableKeepAlive = value ; }
118
- }
108
+ public bool IsKeepAliveEnabled { get ; set ; }
119
109
120
110
/// <summary>
121
111
/// Gets or sets the user agent string used for HTTP communication
122
112
/// batween this <see cref="HttpCommandExecutor"/> and the remote end
123
113
/// WebDriver implementation
124
114
/// </summary>
125
- public string UserAgent
126
- {
127
- get { return this . userAgent ; }
128
- set { this . userAgent = value ; }
129
- }
115
+ public string UserAgent { get ; set ; }
130
116
131
117
/// <summary>
132
118
/// Gets the repository of objects containing information about commands.
133
119
/// </summary>
120
+ /// <exception cref="ArgumentNullException">If the value is set to <see langword="null"/>.</exception>
134
121
protected CommandInfoRepository CommandInfoRepository
135
122
{
136
- get { return this . commandInfoRepository ; }
137
- set { this . commandInfoRepository = value ; }
123
+ get => this . commandInfoRepository ;
124
+ set => this . commandInfoRepository = value ?? throw new ArgumentNullException ( nameof ( value ) , "CommandInfoRepository cannot be null" ) ;
138
125
}
139
126
140
127
/// <summary>
@@ -145,8 +132,7 @@ protected CommandInfoRepository CommandInfoRepository
145
132
/// <returns><see langword="true"/> if the new command has been added successfully; otherwise, <see langword="false"/>.</returns>
146
133
public bool TryAddCommand ( string commandName , CommandInfo info )
147
134
{
148
- HttpCommandInfo commandInfo = info as HttpCommandInfo ;
149
- if ( commandInfo == null )
135
+ if ( info is not HttpCommandInfo commandInfo )
150
136
{
151
137
return false ;
152
138
}
@@ -193,7 +179,7 @@ public virtual async Task<Response> ExecuteAsync(Command commandToExecute)
193
179
}
194
180
195
181
HttpRequestInfo requestInfo = new HttpRequestInfo ( this . remoteServerUri , commandToExecute , info ) ;
196
- HttpResponseInfo responseInfo = null ;
182
+ HttpResponseInfo responseInfo ;
197
183
try
198
184
{
199
185
responseInfo = await this . MakeHttpRequest ( requestInfo ) . ConfigureAwait ( false ) ;
@@ -304,12 +290,11 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
304
290
305
291
using ( HttpResponseMessage responseMessage = await this . client . SendAsync ( requestMessage ) . ConfigureAwait ( false ) )
306
292
{
307
- HttpResponseInfo httpResponseInfo = new HttpResponseInfo ( ) ;
308
- httpResponseInfo . Body = await responseMessage . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
309
- httpResponseInfo . ContentType = responseMessage . Content . Headers . ContentType ? . ToString ( ) ;
310
- httpResponseInfo . StatusCode = responseMessage . StatusCode ;
293
+ var responseBody = await responseMessage . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
294
+ var responseContentType = responseMessage . Content . Headers . ContentType ? . ToString ( ) ;
295
+ var responseStatusCode = responseMessage . StatusCode ;
311
296
312
- return httpResponseInfo ;
297
+ return new HttpResponseInfo ( responseBody , responseContentType , responseStatusCode ) ;
313
298
}
314
299
}
315
300
}
@@ -346,12 +331,15 @@ private Response CreateResponse(HttpResponseInfo responseInfo)
346
331
return response ;
347
332
}
348
333
334
+ #nullable enable
335
+
349
336
/// <summary>
350
337
/// Releases all resources used by the <see cref="HttpCommandExecutor"/>.
351
338
/// </summary>
352
339
public void Dispose ( )
353
340
{
354
341
this . Dispose ( true ) ;
342
+ GC . SuppressFinalize ( this ) ;
355
343
}
356
344
357
345
/// <summary>
@@ -364,10 +352,7 @@ protected virtual void Dispose(bool disposing)
364
352
{
365
353
if ( ! this . isDisposed )
366
354
{
367
- if ( this . client != null )
368
- {
369
- this . client . Dispose ( ) ;
370
- }
355
+ this . client ? . Dispose ( ) ;
371
356
372
357
this . isDisposed = true ;
373
358
}
@@ -377,6 +362,11 @@ private class HttpRequestInfo
377
362
{
378
363
public HttpRequestInfo ( Uri serverUri , Command commandToExecute , HttpCommandInfo commandInfo )
379
364
{
365
+ if ( commandInfo is null )
366
+ {
367
+ throw new ArgumentNullException ( nameof ( commandInfo ) ) ;
368
+ }
369
+
380
370
this . FullUri = commandInfo . CreateCommandUri ( serverUri , commandToExecute ) ;
381
371
this . HttpMethod = commandInfo . Method ;
382
372
this . RequestBody = commandToExecute . ParametersAsJsonString ;
@@ -389,9 +379,16 @@ public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo
389
379
390
380
private class HttpResponseInfo
391
381
{
382
+ public HttpResponseInfo ( string body , string ? contentType , HttpStatusCode statusCode )
383
+ {
384
+ this . Body = body ?? throw new ArgumentNullException ( nameof ( body ) ) ;
385
+ this . ContentType = contentType ;
386
+ this . StatusCode = statusCode ;
387
+ }
388
+
392
389
public HttpStatusCode StatusCode { get ; set ; }
393
390
public string Body { get ; set ; }
394
- public string ContentType { get ; set ; }
391
+ public string ? ContentType { get ; set ; }
395
392
}
396
393
397
394
/// <summary>
@@ -404,7 +401,12 @@ private class DiagnosticsHttpHandler : DelegatingHandler
404
401
public DiagnosticsHttpHandler ( HttpMessageHandler messageHandler , ILogger logger )
405
402
: base ( messageHandler )
406
403
{
407
- _logger = logger ;
404
+ if ( messageHandler is null )
405
+ {
406
+ throw new ArgumentNullException ( nameof ( messageHandler ) ) ;
407
+ }
408
+
409
+ _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
408
410
}
409
411
410
412
/// <summary>
0 commit comments