feat(grpc-data): X0 server parses x-otopcua-data-features on every RPC and records requested/effective (effective always empty)

This commit is contained in:
Joseph Doherty
2026-11-23 17:16:24 -05:00
parent 984b1e122a
commit 331350e2f4
2 changed files with 66 additions and 1 deletions
@@ -16,6 +16,29 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
{
private readonly DataConnectionOptions _options = options.Value;
/// <summary>
/// The optional features this server honours on a call that requests them. X0 reserves the negotiation and
/// supports nothing, so every call's effective set is empty and every RPC keeps its v1 behaviour.
/// </summary>
internal static DataFeatures SupportedFeatures => DataFeatures.None;
/// <summary>The feature set recorded for this call by <see cref="NegotiateFeatures"/> (v1 when absent).</summary>
internal static DataCallFeatures CallFeatures(ServerCallContext context) =>
context.UserState.TryGetValue(typeof(DataCallFeatures), out var value) && value is DataCallFeatures features
? features : DataCallFeatures.None;
/// <summary>
/// Parses <see cref="DataConnectionProtoContract.FeaturesHeader"/> on every RPC (after authentication) and records
/// requested ∩ supported. A binary or oversized value is an <see cref="ArgumentException"/> ⇒ InvalidArgument.
/// </summary>
private static DataCallFeatures NegotiateFeatures(ServerCallContext context)
{
var requested = DataFeatureHeader.Parse(context.RequestHeaders);
var features = requested == DataFeatures.None ? DataCallFeatures.None : new DataCallFeatures(requested, requested & SupportedFeatures);
context.UserState[typeof(DataCallFeatures)] = features;
return features;
}
public override Task<W.CapabilitiesResponse> GetCapabilities(W.CapabilitiesRequest request, ServerCallContext context) =>
Execute(context, async (connection, caller, token) =>
{
@@ -133,6 +156,7 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
context.WriteOptions = new(WriteFlags.NoCompress);
responses.WriteOptions = new(WriteFlags.NoCompress);
var (caller, target) = AuthenticateCall(context);
NegotiateFeatures(context);
await using var connection = factory.Create(caller, target);
DataCapabilities caps;
IDataSubscription subscription;
@@ -254,6 +278,7 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
context.WriteOptions = new(WriteFlags.NoCompress);
var http = context.GetHttpContext();
var (caller, target) = AuthenticateCall(context);
NegotiateFeatures(context);
if (!http.Items.ContainsKey(typeof(DataConnectionUnaryLifetime)))
http.Items[typeof(DataConnectionUnaryLifetime)] = new DataConnectionUnaryLifetime(TimeProvider.System, _options.UnaryTimeout);
var timeout = new CancellationTokenSource(_options.UnaryTimeout);
@@ -53,6 +53,39 @@ public sealed class GrpcDataV1CompatibilityTests
actual.ShouldBe(GrpcDataV1Goldens.Responses[rpc], $"{rpc} response must not change in X0 when features are requested");
}
[Theory]
[MemberData(nameof(Rpcs))]
public async Task Every_rpc_records_the_requested_and_effective_feature_set(string rpc)
{
var (_, absent) = await V1CompatibilityHarness.DriveWithContextAsync(rpc, new Metadata());
DataConnectionGrpcService.CallFeatures(absent).ShouldBe(DataCallFeatures.None);
absent.UserState.ContainsKey(typeof(DataCallFeatures)).ShouldBeTrue();
var (_, requested) = await V1CompatibilityHarness.DriveWithContextAsync(rpc,
new Metadata { { "x-otopcua-data-features", "history" }, { "x-otopcua-data-features", " conditions, future" } });
// X0: the server supports no feature, so whatever is asked for, nothing is effective.
DataConnectionGrpcService.CallFeatures(requested).ShouldBe(new DataCallFeatures(DataFeatures.History | DataFeatures.Conditions, DataFeatures.None));
DataConnectionGrpcService.SupportedFeatures.ShouldBe(DataFeatures.None);
}
public static TheoryData<string, string> MalformedHeaders()
{
var data = new TheoryData<string, string>();
foreach (var rpc in RpcNames) { data.Add(rpc, "binary"); data.Add(rpc, "oversized"); }
return data;
}
[Theory]
[MemberData(nameof(MalformedHeaders))]
public async Task Binary_or_oversized_feature_header_is_rejected_invalid_argument(string rpc, string shape)
{
var headers = shape == "binary"
? new Metadata { { "x-otopcua-data-features-bin", new byte[] { 0x68 } } }
: new Metadata { { "x-otopcua-data-features", "history," + new string(' ', 256) } };
var failure = await Should.ThrowAsync<RpcException>(() => V1CompatibilityHarness.DriveAsync(rpc, headers));
failure.StatusCode.ShouldBe(StatusCode.InvalidArgument);
}
internal static string Hex(byte[] bytes) => Convert.ToHexString(bytes);
/// <summary>X0: no field varies. Add explicit, listed normalisations here only if a later phase needs one.</summary>
@@ -105,9 +138,16 @@ internal static class V1Requests
/// </summary>
internal static class V1CompatibilityHarness
{
internal static async Task<string> DriveAsync(string rpc, Metadata headers)
internal static async Task<string> DriveAsync(string rpc, Metadata headers) => (await DriveWithContextAsync(rpc, headers)).Bytes;
internal static async Task<(string Bytes, ServerCallContext Context)> DriveWithContextAsync(string rpc, Metadata headers)
{
var (service, context) = Create(rpc, headers);
return (await DriveAsync(service, context, rpc), context);
}
private static async Task<string> DriveAsync(DataConnectionGrpcService service, ServerCallContext context, string rpc)
{
var bytes = V1Requests.Bytes(rpc);
IMessage response = rpc switch
{