-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nats web socket opts improvements (#623)
* NatsWebSocketOpts improvements (#610) * Separate model NatsWebSocketOpts * Сalled NatsTlsOpts.AuthenticateAsClientAsync parameters before passing it to the ConfigureWebSocketOpts * RequestHeaders overwrite the header specified in ConfigureWebSocketOpts * NatsWebSocketOpts improvements (#610) * Added test * Build fixes * Build fixes * Build fixes * dotnet format * #623 reccomendations Signed-off-by: Caleb Lloyd <[email protected]> * NatsWebSocketOpts improvements (#610) * Fence added, an occurs exceptions when add headers to blazor * Revert "NatsWebSocketOpts improvements (#610)" This reverts commit e6b9d1c. * WebSocketSecure tests Signed-off-by: Caleb Lloyd <[email protected]> * fix format Signed-off-by: Caleb Lloyd <[email protected]> * simplify WebSocketOptionsTest Signed-off-by: Caleb Lloyd <[email protected]> --------- Signed-off-by: Caleb Lloyd <[email protected]> Co-authored-by: Ziya Suzen <[email protected]> Co-authored-by: Caleb Lloyd <[email protected]>
- Loading branch information
1 parent
323124a
commit 71dc676
Showing
9 changed files
with
188 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Net.WebSockets; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Microsoft.Extensions.Primitives; | ||
using NATS.Client.Core.Internal; | ||
|
||
namespace NATS.Client.Core; | ||
|
||
/// <summary> | ||
/// Options for ClientWebSocketOptions | ||
/// </summary> | ||
public sealed record NatsWebSocketOpts | ||
{ | ||
public static readonly NatsWebSocketOpts Default = new(); | ||
|
||
/// <summary> | ||
/// An optional dictionary of HTTP request headers to be sent with the WebSocket request. | ||
/// </summary> | ||
/// <remarks> | ||
/// Not supported when running in the Browser, such as when using Blazor WebAssembly, | ||
/// as the underlying Browser implementation does not support adding headers to a WebSocket. | ||
/// </remarks> | ||
public IDictionary<string, StringValues>? RequestHeaders { get; init; } | ||
|
||
/// <summary> | ||
/// An optional async callback handler for manipulation of ClientWebSocketOptions used for WebSocket connections. | ||
/// Implementors should use the passed CancellationToken for async operations called by this handler. | ||
/// </summary> | ||
public Func<Uri, ClientWebSocketOptions, CancellationToken, ValueTask>? ConfigureClientWebSocketOptions { get; init; } = null; | ||
|
||
internal async ValueTask ApplyClientWebSocketOptionsAsync( | ||
ClientWebSocketOptions clientWebSocketOptions, | ||
NatsUri uri, | ||
NatsTlsOpts tlsOpts, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (RequestHeaders != null) | ||
{ | ||
foreach (var entry in RequestHeaders) | ||
{ | ||
// SetRequestHeader overwrites if called multiple times; | ||
// RFC7230 Section 3.2.2 allows for combining them with a comma | ||
// https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2 | ||
clientWebSocketOptions.SetRequestHeader(entry.Key, string.Join(",", entry.Value)); | ||
} | ||
} | ||
|
||
if (tlsOpts.HasTlsCerts) | ||
{ | ||
var authenticateAsClientOptions = await tlsOpts.AuthenticateAsClientOptionsAsync(uri).ConfigureAwait(false); | ||
var collection = new X509CertificateCollection(); | ||
|
||
// must match LoadClientCertFromX509 method in SslClientAuthenticationOptions.cs | ||
#if NET8_0_OR_GREATER | ||
if (authenticateAsClientOptions.ClientCertificateContext != null) | ||
{ | ||
collection.Add(authenticateAsClientOptions.ClientCertificateContext.TargetCertificate); | ||
} | ||
#else | ||
if (authenticateAsClientOptions.ClientCertificates != null) | ||
{ | ||
collection.AddRange(authenticateAsClientOptions.ClientCertificates); | ||
} | ||
#endif | ||
if (collection.Count > 0) | ||
{ | ||
clientWebSocketOptions.ClientCertificates = collection; | ||
} | ||
|
||
#if !NETSTANDARD2_0 | ||
clientWebSocketOptions.RemoteCertificateValidationCallback = authenticateAsClientOptions.RemoteCertificateValidationCallback; | ||
#endif | ||
} | ||
|
||
if (ConfigureClientWebSocketOptions != null) | ||
{ | ||
await ConfigureClientWebSocketOptions(uri.Uri, clientWebSocketOptions, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.