Path: redb-route/redb.Route.As2/As2Consumer.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.As2/As2Consumer.cs
----
using System.Diagnostics;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MimeKit;
using MimeKit.Cryptography;
using redb.Route.Abstractions;
using redb.Route.As2.Crypto;
using redb.Route.As2.Mdn;
using redb.Route.Components;
using redb.Route.Configuration;
using redb.Route.Core;
using redb.Route.Extensions;
using redb.Route.Http;
using redb.Route.Telemetry;
namespace redb.Route.As2;
///
/// AS2 receive side (server): registers a POST route on the shared Kestrel host; checks the sender's identifiers,
/// decrypts and verifies the inbound S/MIME message against the pinned partner certificate, enforces the agreement,
/// computes the MIC, hands the business payload to the route, and answers with the MDN the request asked for:
/// synchronously, or posted to the receipt URL when the message authenticated and the agreement allows it.
///
internal sealed class As2Consumer : IConsumer
{
// MIME headers that describe the S/MIME wrapper — they belong to the transport plane and must NOT be
// copied into the exchange headers (the business Content-Type lives on Message.ContentType; leaking the
// wrapper into Headers["Content-Type"] would override it on a downstream HTTP produce).
private static readonly HashSet WrapperHeaders = new(StringComparer.OrdinalIgnoreCase)
{
"Content-Type", "Content-Transfer-Encoding", "Content-Disposition", "Content-Length", "Content-Encoding",
"Connection", "Keep-Alive", "Transfer-Encoding", "TE", "Trailer", "Upgrade", "Host",
};
// Credentials of the HTTP hop, dropped as the HTTP consumer drops a checked Authorization: a route that logs, forwards
// or echoes its headers must not carry a caller's token or session on. The principal the host resolved from them is
// on the exchange (ExchangePrincipal).
private static readonly HashSet CredentialHeaders = new(StringComparer.OrdinalIgnoreCase)
{
"Authorization", "Proxy-Authorization", "Cookie",
};
private readonly As2Endpoint _endpoint;
private readonly IProcessor _processor;
private readonly As2EndpointOptions _options;
private readonly IHeaderFilterStrategy _headerFilter;
private readonly IAs2CryptoEngine _engine = new As2CryptoEngine();
private readonly ILogger? _logger;
private RouteRegistration? _registration;
private As2Profile? _profile;
// Delivers asynchronous MDNs; owned by this receiver, bounded by the endpoint's timeout.
private HttpClient? _mdnClient;
// Slots of the asynchronous MDN pool (AsyncMdnPoolSize): taken when a message asking for one is admitted, released
// when its receipt is delivered or given up. Not disposed: a post a forced stop left behind still releases into it.
private SemaphoreSlim? _asyncMdnSlots;
private IIdempotentRepository? _duplicates;
private StreamCacheOptions? _spool;
// Unregistering the route stops new deliveries; a partner message already inside the pipeline
// still has to finish before Stop returns, and the listener stays up for the other routes.
private readonly InflightDrainGuard _drain = new();
public As2Consumer(As2Endpoint endpoint, IProcessor processor, As2EndpointOptions options)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_processor = processor ?? throw new ArgumentNullException(nameof(processor));
_options = options ?? throw new ArgumentNullException(nameof(options));
_logger = endpoint.Logger;
_headerFilter = endpoint.HeaderFilterStrategy;
}
///
public IEndpoint Endpoint => _endpoint;
private SharedHttpServerManager Server =>
(_endpoint.Component as As2Component)?.Server
?? throw new InvalidOperationException("AS2 endpoint has no As2Component/server.");
///
/// The TLS certificate of the receive server: the endpoint's own, else the one on the named
/// connection factory (which is where its password belongs). Nothing here means the host
/// default is asked next, inside the shared host.
///
private (string? Path, string? Password) ResolveServerCertificate()
{
if (!string.IsNullOrEmpty(_options.SslCertPath))
return (_options.SslCertPath, _options.SslCertPassword);
if (!string.IsNullOrEmpty(_options.ConnectionFactory))
{
var factory = _endpoint.Context.GetRequiredFromRegistry(_options.ConnectionFactory);
if (!string.IsNullOrEmpty(factory.SslCertPath))
return (factory.SslCertPath, factory.SslCertPassword);
}
return (null, null);
}
///
public async Task Start(CancellationToken ct = default)
{
// The agreement is resolved once, here: a misnamed or incomplete one stops the route from starting instead of
// answering the partner's first message with a negative MDN.
var profile = As2Profile.Resolve(_endpoint.Context, _options, _endpoint.Uri.Path);
if (profile.MdnMode == As2MdnMode.Async && profile.AsyncMdnAllowedHosts.Count == 0)
throw new InvalidOperationException(
$"AS2 receive endpoint {_endpoint.Uri.Path}: MdnMode is async, so AsyncMdnAllowedHosts must name the hosts receipts may be posted to; " +
"the address itself comes from the partner's request.");
_profile = profile;
_spool = _endpoint.Context.GetStreamCacheOptions();
// Optional here (RFC 4130 does not mandate it, unlike eDelivery AS4); named, as the AS4 and S3 receivers name it.
_duplicates = string.IsNullOrWhiteSpace(_options.IdempotentRepository) ? null
: (_endpoint.Context ?? throw new InvalidOperationException($"AS2 receive endpoint {_endpoint.Uri.Path} has no route context to resolve its idempotent repository from."))
.GetIdempotentRepositoryProvider().Get(_options.IdempotentRepository);
_asyncMdnSlots = new SemaphoreSlim(_options.AsyncMdnPoolSize, _options.AsyncMdnPoolSize);
// The pool already bounds the posts in flight (a slot is held during its post); the handler states the same bound
// for the connections, and renews them, so a partner's address change is picked up.
_mdnClient = new HttpClient(new SocketsHttpHandler
{
MaxConnectionsPerServer = _options.AsyncMdnPoolSize,
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
})
{
Timeout = _options.Timeout > 0 ? TimeSpan.FromMilliseconds(_options.Timeout) : Timeout.InfiniteTimeSpan,
};
_drain.Start(ct);
var host = _options.Host;
var port = _options.Port;
var path = _endpoint.Uri.Path; // e.g. "/inbound/orders" — kept intact by the DSL
// The TLS server certificate travels with the registration. Earlier it did not, so an
// as2s:// receiver opened a PLAINTEXT port while PartnerUrl advertised https:// to the
// trading partner.
var (certPath, certPassword) = ResolveServerCertificate();
_registration = Server.RegisterRoute(host, port, path, "POST", HandleRequest,
_options.UseTls, certPath, certPassword,
maxRequestBodySize: _options.MaxRequestBodySize,
concurrencyLimit: ConcurrencyLimitOptions.FromEndpoint(
_options.MaxConcurrentRequests, _options.RequestQueueLimit,
_options.RejectStatusCode, _options.RetryAfterSeconds,
onRejected: _endpoint.RecordRejected));
try
{
await Server.EnsureStarted(host, port, ct).ConfigureAwait(false);
}
catch
{
// The listener did not come up: the route leaves it and the MDN client goes, so a later Start
// registers once and builds one client.
await Stop(CancellationToken.None).ConfigureAwait(false);
throw;
}
_logger?.LogInformation("AS2 consumer started: {Host}:{Port}{Path}", host, port, path);
}
///
public async Task Stop(CancellationToken ct = default)
{
if (_registration is not null)
{
Server.UnregisterRoute(_registration);
_registration = null;
// The drain also waits for asynchronous MDNs still being posted: each is counted like a request.
await _drain.DrainAsync(ct, _logger, $"as2://{_options.Host}:{_options.Port}").ConfigureAwait(false);
await Server.StopIfEmpty(_options.Host, _options.Port, ct).ConfigureAwait(false);
}
_mdnClient?.Dispose();
_mdnClient = null;
_logger?.LogInformation("AS2 consumer stopped: {Host}:{Port}", _options.Host, _options.Port);
}
private async Task HandleRequest(HttpContext http)
{
// Counted for the drain: unregistering the route stops new deliveries, but a partner's
// message already being verified, decrypted and routed must finish before Stop returns.
_drain.Increment();
AsyncMdnSlot? slot = null;
try
{
// A message that asks for an asynchronous MDN takes its slot now, before any work: with every slot taken it
// is refused, as the admission limit refuses, and the partner resends it later. RFC 4130 §7 puts the
// response before the MDN, so the receiver cannot post the receipt in the caller's place instead.
if (_profile!.MdnMode == As2MdnMode.Async && !string.IsNullOrEmpty(http.Request.Headers[As2Headers.ReceiptDeliveryOption].ToString()))
{
slot = AsyncMdnSlot.TryTake(_asyncMdnSlots!);
if (slot is null)
{
RefuseBusy(http, "every asynchronous MDN slot is taken");
As2Metrics.AsyncMdnRejected.Add(1);
return;
}
}
await HandleRequestCore(http, slot).ConfigureAwait(false);
}
finally
{
// Handed over: the post releases it when the receipt is delivered or given up.
if (slot is { HandedOver: false }) slot.Release();
_drain.Decrement();
}
}
///
/// Answers a request the receiver will not take now with and
/// , the answer of the admission limit.
///
private void RefuseBusy(HttpContext http, string reason)
{
_endpoint.RecordRejected();
_logger?.LogWarning("AS2 receiver {Path}: message {MessageId} refused with {Status}, {Reason} (ref {Reference}).",
http.Request.Path, http.Request.Headers[As2Headers.MessageId].ToString(), _options.RejectStatusCode, reason, http.TraceIdentifier);
http.Response.StatusCode = _options.RejectStatusCode;
if (_options.RetryAfterSeconds > 0)
http.Response.Headers.RetryAfter = _options.RetryAfterSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
///
/// A slot of the asynchronous MDN pool. The request holds it until it answers; when the receipt is posted after the
/// answer, the post takes it over and releases it once the receipt is delivered or given up.
///
private sealed class AsyncMdnSlot
{
private readonly SemaphoreSlim _pool;
private int _released;
private AsyncMdnSlot(SemaphoreSlim pool) => _pool = pool;
public static AsyncMdnSlot? TryTake(SemaphoreSlim pool) => pool.Wait(0) ? new AsyncMdnSlot(pool) : null;
public bool HandedOver { get; set; }
public void Release()
{
if (Interlocked.Exchange(ref _released, 1) == 0) _pool.Release();
}
}
private async Task HandleRequestCore(HttpContext http, AsyncMdnSlot? slot)
{
using var span = StartReceiveSpan(_endpoint, http.Request);
var request = http.Request;
var profile = _profile!;
var reference = http.TraceIdentifier;
var originalMessageId = request.Headers[As2Headers.MessageId].ToString();
var receipt = ReceiptRequest.Of(request, profile);
if (receipt.RequestedMicAlgs is { Count: > 0 } asked && !asked.Contains(receipt.MicAlg, StringComparer.OrdinalIgnoreCase))
_logger?.LogWarning("AS2 receiver {Path}: message {MessageId} asks for micalg {Asked}; none is usable here, the MIC is {Used}.",
request.Path, originalMessageId, string.Join(", ", asked), receipt.MicAlg);
As2Mic? mic = null;
var signatureValid = false;
var wasSigned = false;
var wasEncrypted = false;
var authenticated = false; // the sender is the agreed partner, as far as the agreement can tell
string? failure = null; // RFC 4130 §7.4.3 modifier of a message that was not processed
IExchange? exchange = null;
var claimed = false; // this request holds the duplicate-detection claim of its Message-ID
string? claimToken = null; // the id of that claim, so a takeover's claim is never settled by this request
var duplicate = false; // the Message-ID was received and processed before
var inFlight = false; // an earlier copy of the Message-ID is still being processed
var processed = false; // the route processed the document
// The request and every stage unwrapped from it live in spools (the core stream cache: memory, or a temporary
// file past the threshold), parsed by MimeKit in place; all of them go when the request is answered.
StreamCache? mime = null;
var spools = new List();
try
{
// The MIME headers of the wrapper and the body, in one spool; bounded by maxRequestBodySize.
mime = await As2Http.ReadBodyAsync(http, _options.MaxRequestBodySize, MimeHeaderPrefix(request), _spool!, http.RequestAborted)
.ConfigureAwait(false);
if (mime is null)
{
_logger?.LogWarning("AS2 receiver {Path}: request refused, its body is over maxRequestBodySize {Limit} bytes (ref {Reference}).",
request.Path, _options.MaxRequestBodySize, reference);
_endpoint.RecordError(new InvalidOperationException(
$"AS2 receiver {request.Path}: request refused, its body is over maxRequestBodySize {_options.MaxRequestBodySize} bytes (ref {reference})."));
span.Activity?.SetStatus(ActivityStatusCode.Error, "request body over maxRequestBodySize");
return;
}
// The agreement names both parties (RFC 4130 §6.2): a message from another sender, or for another
// recipient, is not ours to process, whatever it carries.
var from = request.Headers[As2Headers.As2From].ToString();
var to = request.Headers[As2Headers.As2To].ToString();
if (!IsAgreedIdentifier(from, profile.As2To) || !IsAgreedIdentifier(to, profile.As2From))
throw new As2DispositionException(As2Disposition.AuthenticationFailed,
$"AS2-From '{from}' and AS2-To '{to}' are not the agreement's '{profile.As2To}' and '{profile.As2From}'.");
// A receipt option the sender marked required and this receiver cannot honour (RFC 4130 §7.5.3): the MDN is
// failed/Failure and the message is not processed.
if (receipt.Failure is { } receiptFailure)
throw new As2DispositionException(receiptFailure,
$"Disposition-Notification-Options '{request.Headers[As2Headers.DispositionNotificationOptions]}' requires what this receiver cannot honour.");
// Unwrap: decrypt (outer), then the signature and the compression in the order they were applied: compress
// then sign (RFC 5402 §3.2/3.5) or sign then compress (§3.3/3.6), each at most once.
var entity = MimeEntity.Load(mime, persistent: true);
var wasCompressed = false;
if (entity is ApplicationPkcs7Mime { SecureMimeType: SecureMimeType.EnvelopedData } enveloped)
{
if (profile.OurCertificate is null)
throw new As2DispositionException(As2Disposition.DecryptionFailed,
"the message is encrypted and the agreement has no certificate of ours to decrypt it.");
try { entity = Unwrap(spools, s => _engine.DecryptTo(enveloped, profile.OurCertificate, s)); }
catch (Exception e) when (e is not OperationCanceledException)
{
throw new As2DispositionException(As2Disposition.DecryptionFailed, $"decryption failed: {e.Message}", e);
}
wasEncrypted = true;
}
while (true)
{
if (!wasSigned && entity is MultipartSigned signed)
{
if (profile.PartnerCertificate is null)
throw new As2DispositionException(As2Disposition.AuthenticationFailed,
"the message is signed and the agreement has no partner certificate to verify it.");
if (As2CertificateValidity.Problem(profile.PartnerCertificate, DateTimeOffset.UtcNow, "The partner's signing certificate") is { } invalid)
throw new As2DispositionException(As2Disposition.AuthenticationFailed, invalid);
try { signatureValid = _engine.Verify(signed, profile.PartnerCertificate); }
catch (Exception e) when (e is not OperationCanceledException)
{
throw new As2DispositionException(As2Disposition.AuthenticationFailed, $"signature verification failed: {e.Message}", e);
}
// RFC 5402 §4.1: over the data that was signed, compressed or not.
mic = _engine.ComputeReceivedMic(signed[0], receipt.MicAlg);
entity = signed[0];
wasSigned = true;
}
else if (!wasCompressed && entity is ApplicationPkcs7Mime { SecureMimeType: SecureMimeType.CompressedData } compressed)
{
// Bounded while it inflates: a small body can expand to gigabytes.
try { entity = Unwrap(spools, s => _engine.DecompressTo(compressed, new BoundedWriteStream(s, _options.DecompressedLimit, "maxDecompressedSize"))); }
catch (Exception e) when (e is not OperationCanceledException)
{
throw new As2DispositionException(As2Disposition.DecompressionFailed, $"decompression failed: {e.Message}", e);
}
wasCompressed = true;
}
else
break;
}
// Unsigned: over the (uncompressed) part with its MIME headers, as it arrived, when it was encrypted (RFC 4130
// §7.3.1, RFC 5402 §4.2); over its content alone when it was not, compressed or plain (RFC 4130 §7.3.1, and
// what OpenAS2 computes for a compressed unencrypted message).
mic ??= wasEncrypted
? _engine.ComputeReceivedMic(entity, receipt.MicAlg)
: _engine.ComputeMic(entity, receipt.MicAlg, includeHeaders: false);
// ENFORCE the profile before handing the payload to the route: a message that the partnership
// requires to be signed/encrypted but is not, or whose signature did not verify, is rejected with a
// negative MDN — never processed as if trusted.
if (wasSigned && !signatureValid)
throw new As2DispositionException(As2Disposition.AuthenticationFailed,
"the signature does not verify against the partner certificate (another signer, or altered content).");
if (profile.Encrypt && !wasEncrypted)
throw new As2DispositionException(As2Disposition.InsufficientMessageSecurity,
"the message is not encrypted and the agreement requires encryption.");
if (profile.Sign && !wasSigned)
throw new As2DispositionException(As2Disposition.InsufficientMessageSecurity,
"the message is not signed and the agreement requires a signature.");
authenticated = true;
// Duplicate detection, the AS4 receiver's two-phase claim (Lease, then Confirm or Release of that claim): claimed
// only now, so a forged copy cannot burn the id of the real message; taken uncancelled, as Confirm and Release are.
if (_duplicates is not null)
{
if (string.IsNullOrWhiteSpace(originalMessageId))
throw new As2DispositionException(As2Disposition.UnexpectedProcessingError,
"the message has no Message-ID (RFC 4130 requires one), and duplicate detection needs it.");
// A copy whose first one is still in a route is not a duplicate yet: the first may fail and release its
// claim, and a duplicate-document MDN would have told the partner the document arrived. It is refused as
// busy, and the partner resends it. A claim older than inFlightClaimTimeout was left by a stopped node.
var lease = await _duplicates.Lease(originalMessageId, TimeSpan.FromMilliseconds(_options.InFlightClaimTimeout), CancellationToken.None)
.ConfigureAwait(false);
claimToken = lease.Token;
switch (lease.Outcome)
{
case IdempotentClaim.Claimed:
claimed = true;
break;
case IdempotentClaim.TakenOver:
claimed = true;
_logger?.LogWarning("AS2 receiver {Path}: message {MessageId} had a claim older than inFlightClaimTimeout {Timeout} ms " +
"that was never confirmed; taken over and processed.", request.Path, originalMessageId, _options.InFlightClaimTimeout);
break;
case IdempotentClaim.InFlight:
inFlight = true;
break;
default:
duplicate = true;
_logger?.LogInformation("AS2 receiver {Path}: message {MessageId} is a duplicate; answered, not delivered again.",
request.Path, originalMessageId);
break;
}
}
if (!duplicate && !inFlight)
{
// Extract the business payload.
var (payload, payloadContentType) = ExtractPayload(entity, _spool!, _options.StreamBody);
// Build the exchange. Content-Type comes from the INNER part (Message.ContentType); the wrapper
// Content-Type is deliberately NOT copied into the headers.
var message = new Message(payload) { ContentType = payloadContentType };
exchange = Exchange.Create(message, _endpoint.ScopeFactory);
CopyInboundHeaders(request, message, _headerFilter, exchange);
// The header reports a verification: an unsigned message had none.
message.Headers[As2Headers.SignatureValid] = wasSigned && signatureValid;
if (mic is not null)
{
message.Headers[As2Headers.Mic] = mic.Value.Digest;
message.Headers[As2Headers.MicAlg] = mic.Value.Algorithm;
}
if (http.Connection.RemoteIpAddress is not null)
message.Headers[As2Headers.RemoteAddress] = http.Connection.RemoteIpAddress.ToString();
if (!string.IsNullOrEmpty(_options.ConnectionFactory))
message.Headers[As2Headers.PartnerName] = _options.ConnectionFactory;
exchange.Pattern = ExchangePattern.InOut;
ExchangePrincipal.Set(exchange, SharedHttpServerManager.GetResolvedPrincipal(http));
// Cancelled when the sender goes away, and when a stop outlasts the drain timeout.
using (var processing = CancellationTokenSource.CreateLinkedTokenSource(http.RequestAborted, _drain.ProcessingToken))
await _processor.Process(exchange, processing.Token).ConfigureAwait(false);
if (exchange.EndedInFailure())
{
// No RecordError: the core StatisticsProcessor saw the same exchange.Exception.
_logger?.LogWarning(exchange.Exception, "AS2 receiver {Path}: route failed for message {MessageId} (ref {Reference}).",
request.Path, originalMessageId, reference);
if (exchange.Exception is { } routeFailure) span.Activity.RecordFailure(routeFailure);
failure = As2Disposition.UnexpectedProcessingError;
}
else
{
processed = true;
}
}
}
catch (As2DispositionException e)
{
_logger?.LogWarning(e, "AS2 receiver {Path}: message {MessageId} refused with {Disposition} (ref {Reference}): {Detail}",
request.Path, originalMessageId, e.Modifier, reference, e.Message);
_endpoint.RecordError(e);
span.Activity.RecordFailure(e);
failure = e.Modifier;
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_logger?.LogError(ex, "AS2 receiver {Path}: message {MessageId} not processed (ref {Reference}).",
request.Path, originalMessageId, reference);
_endpoint.RecordError(ex);
span.Activity.RecordFailure(ex);
failure = As2Disposition.UnexpectedProcessingError;
}
finally
{
if (exchange is not null)
await exchange.DisposeAsync().ConfigureAwait(false);
foreach (var spool in spools)
await spool.DisposeAsync().ConfigureAwait(false);
if (mime is not null)
await mime.DisposeAsync().ConfigureAwait(false);
// Processed: the id stays. Anything else (the route failed, threw, or was cancelled): released, so the
// partner's resend is delivered and not taken for a duplicate.
if (claimed)
await SettleClaimAsync(originalMessageId, claimToken, processed).ConfigureAwait(false);
}
if (inFlight)
{
RefuseBusy(http, "an earlier copy of it is still being processed");
return;
}
// RFC 4130 §7.3: an MDN only when the sender asked for one (Disposition-Notification-To), and never when the
// agreement says none.
if (profile.MdnMode == As2MdnMode.None || !receipt.Requested)
{
http.Response.StatusCode = StatusCodes.Status200OK;
return;
}
// Signed when the request asks for a signed receipt, with the micalg it asked for. A failure carries its code
// and our reference only. A receipt that cannot be made as required (receipt.Failure) goes unsigned: it reports
// exactly that no MDN as asked could be generated.
var sign = receipt.Signed && receipt.Failure is null && profile.OurCertificate is not null;
if (receipt.Signed && receipt.Failure is null && profile.OurCertificate is null)
_logger?.LogWarning("AS2 receiver {Path}: message {MessageId} asks for a signed MDN and the agreement has no certificate of ours; " +
"the MDN is unsigned (ref {Reference}).", request.Path, originalMessageId, reference);
var (mdnContentType, mdnCte, mdnBody) = MdnBuilder.Build(
originalMessageId, profile.As2From, profile.As2To, mic, failure, reference,
warning: duplicate ? "duplicate-document" : null,
signer: sign ? _engine : null,
signerCert: sign ? profile.OurCertificate : null,
signAlg: receipt.MicAlg);
// Async MDN: posted only for a sender that authenticated, in an agreement that says async, to a host the
// agreement names — the address comes from the request, so anything less lets any caller make us POST (a
// receipt signed by our key) wherever it likes. Otherwise the receipt is the response to this request.
var receiptUrl = http.Request.Headers[As2Headers.ReceiptDeliveryOption].ToString();
if (!string.IsNullOrEmpty(receiptUrl) && AsyncReceiptRefusal(receiptUrl, authenticated, profile) is { } refusal)
{
_logger?.LogWarning("AS2 receiver {Path}: asynchronous MDN for message {MessageId} not posted to '{Url}': {Reason}; " +
"the MDN is returned in the response (ref {Reference}).", request.Path, originalMessageId, receiptUrl, refusal, reference);
receiptUrl = "";
}
if (!string.IsNullOrEmpty(receiptUrl) && slot is not null)
{
http.Response.StatusCode = StatusCodes.Status200OK;
// Answered first, posted after: the sender's wait for 200 must not depend on its own MDN endpoint. The
// post is counted for the drain and cancelled by a forced stop, not by this request's end; it keeps the
// request's slot until the receipt is delivered or given up.
_drain.Increment();
slot.HandedOver = true;
_ = Task.Run(async () =>
{
try { await PostAsyncMdn(receiptUrl, mdnContentType, mdnCte, mdnBody, profile, originalMessageId, _drain.ProcessingToken).ConfigureAwait(false); }
finally
{
slot.Release();
_drain.Decrement();
}
});
}
else
{
var response = http.Response;
response.StatusCode = StatusCodes.Status200OK;
response.ContentType = mdnContentType;
response.Headers[As2Headers.As2Version] = "1.2";
if (!string.IsNullOrEmpty(profile.As2From)) response.Headers[As2Headers.As2From] = As2Headers.FormatName(profile.As2From);
if (!string.IsNullOrEmpty(profile.As2To)) response.Headers[As2Headers.As2To] = As2Headers.FormatName(profile.As2To);
response.Headers[As2Headers.MessageId] = $"<{Guid.NewGuid():N}@redb.route>";
if (!string.IsNullOrEmpty(mdnCte)) response.Headers["Content-Transfer-Encoding"] = mdnCte;
await response.Body.WriteAsync(mdnBody, http.RequestAborted).ConfigureAwait(false);
}
}
///
/// Why an asynchronous MDN is not posted to , or null when it is. The host must be one the
/// agreement lists () — a name is trusted as the operator
/// wrote it; what it resolves to is not checked again.
///
private static string? AsyncReceiptRefusal(string url, bool authenticated, As2Profile profile)
{
if (!authenticated)
return "the message did not authenticate";
if (profile.MdnMode != As2MdnMode.Async)
return $"the agreement's MDN mode is {profile.MdnMode}";
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps))
return "it is not an absolute http(s) URL";
if (!string.IsNullOrEmpty(uri.UserInfo))
return "it carries user information";
var host = uri.Host.Trim('[', ']');
if (!profile.AsyncMdnAllowedHosts.Any(h => string.Equals(h.Trim('[', ']'), host, StringComparison.OrdinalIgnoreCase)))
return $"host '{host}' is not in AsyncMdnAllowedHosts";
return null;
}
///
/// Confirms the claim of a processed message, or releases one the route did not process (as the AS4 and S3
/// receivers do). The answer goes out either way, so a failure here is logged: a failed confirm leaves the claim in
/// place; a failed release leaves the id marked until it is cleaned up. Only this request's claim is settled: when
/// processing outlived inFlightClaimTimeout and another node took the claim over, nothing of its claim is touched.
///
private async Task SettleClaimAsync(string messageId, string? token, bool processed)
{
try
{
var settled = processed
? await _duplicates!.Confirm(messageId, token, CancellationToken.None).ConfigureAwait(false)
: await _duplicates!.Release(messageId, token, CancellationToken.None).ConfigureAwait(false);
if (!settled)
_logger?.LogWarning("AS2 receiver {Path}: the claim of message {MessageId} was taken over while it was processed " +
"(longer than inFlightClaimTimeout {Timeout} ms); the new holder's claim is left as it is.",
_endpoint.Uri.Path, messageId, _options.InFlightClaimTimeout);
}
catch (Exception e) when (e is not OperationCanceledException)
{
_logger?.LogWarning(e, "AS2 receiver {Path}: {Action} the duplicate claim of message {MessageId} failed.",
_endpoint.Uri.Path, processed ? "confirming" : "releasing", messageId);
}
}
///
/// Whether a received AS2-From/AS2-To is the agreed identifier: RFC 4130 §6.2 allows a quoted-string
/// and compares case-sensitively.
///
internal static bool IsAgreedIdentifier(string? received, string agreed)
{
if (string.IsNullOrEmpty(agreed) || string.IsNullOrWhiteSpace(received)) return false;
var value = received.Trim();
if (value.Length >= 2 && value[0] == '"' && value[^1] == '"')
value = value[1..^1].Replace("\\\"", "\"", StringComparison.Ordinal).Replace("\\\\", "\\", StringComparison.Ordinal);
return string.Equals(value, agreed, StringComparison.Ordinal);
}
///
/// Posts an asynchronous MDN, at most times: again after a
/// connection failure, a timeout, 408, 429 or a 5xx answer, waiting
/// doubled for each retry; another 4xx is the partner refusing the receipt itself, and sending it again changes
/// nothing. Every attempt carries the same Message-ID, so the partner can tell a resend. A receipt given up is logged
/// and counted (redb.route.as2.mdn.async_failed).
///
private async Task PostAsyncMdn(string url, string contentType, string? transferEncoding, byte[] body, As2Profile profile,
string originalMessageId, CancellationToken ct)
{
var mdnMessageId = $"<{Guid.NewGuid():N}@redb.route>";
var delay = _options.AsyncMdnRetryDelay;
// Runs detached from the request, so nothing above it would see an exception: every outcome is logged here.
for (var attempt = 1; ; attempt++)
{
string outcome;
Exception? failure = null;
try
{
using var content = new ByteArrayContent(body);
content.Headers.TryAddWithoutValidation("Content-Type", contentType);
if (!string.IsNullOrEmpty(transferEncoding))
content.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", transferEncoding);
using var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = content };
if (!string.IsNullOrEmpty(profile.As2From)) request.Headers.TryAddWithoutValidation(As2Headers.As2From, As2Headers.FormatName(profile.As2From));
if (!string.IsNullOrEmpty(profile.As2To)) request.Headers.TryAddWithoutValidation(As2Headers.As2To, As2Headers.FormatName(profile.As2To));
request.Headers.TryAddWithoutValidation(As2Headers.MessageId, mdnMessageId);
var client = _mdnClient ?? throw new OperationCanceledException("the receiver is stopped.");
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
if (attempt > 1)
_logger?.LogInformation("AS2 asynchronous MDN for message {MessageId} to {Url} delivered on attempt {Attempt}.",
originalMessageId, url, attempt);
return;
}
var status = (int)response.StatusCode;
outcome = $"answered {status}";
if (status is not (408 or 429) && status < 500)
{
GiveUp(originalMessageId, url, attempt, outcome, null);
return;
}
}
catch (OperationCanceledException) when (ct.IsCancellationRequested || _mdnClient is null)
{
_logger?.LogWarning("AS2 asynchronous MDN for message {MessageId} to {Url} not delivered: the receiver stopped.",
originalMessageId, url);
As2Metrics.AsyncMdnFailed.Add(1);
return;
}
catch (Exception ex)
{
// A connection failure, or the endpoint's timeout (HttpClient reports it as a cancellation).
outcome = "not delivered";
failure = ex;
}
if (attempt >= _options.AsyncMdnAttempts)
{
GiveUp(originalMessageId, url, attempt, outcome, failure);
return;
}
_logger?.LogWarning(failure, "AS2 asynchronous MDN for message {MessageId} to {Url} {Outcome} on attempt {Attempt} of {Attempts}; " +
"tried again in {Delay} ms.", originalMessageId, url, outcome, attempt, _options.AsyncMdnAttempts, delay);
try { await Task.Delay(delay, ct).ConfigureAwait(false); }
catch (OperationCanceledException)
{
_logger?.LogWarning("AS2 asynchronous MDN for message {MessageId} to {Url} not delivered: the receiver stopped.",
originalMessageId, url);
As2Metrics.AsyncMdnFailed.Add(1);
return;
}
delay = (int)Math.Min((long)delay * 2, int.MaxValue);
}
}
private void GiveUp(string originalMessageId, string url, int attempts, string outcome, Exception? failure)
{
_logger?.LogError(failure, "AS2 asynchronous MDN for message {MessageId} to {Url} given up: {Outcome} after {Attempts} attempt(s).",
originalMessageId, url, outcome, attempts);
_endpoint.RecordError(failure ?? new InvalidOperationException($"asynchronous MDN to {url} {outcome}."));
As2Metrics.AsyncMdnFailed.Add(1);
}
// ── Helpers ──────────────────────────────────────────────────────────────
///
/// The Consumer span of a received request, on the core contract ():
/// the host's request span when the application traces ASP.NET Core, else the sender's traceparent, else a
/// root; the sender's baggage is put back; EnableTelemetry=false opens nothing. Named after its destination,
/// the receive path.
///
internal static TransportSpan StartReceiveSpan(As2Endpoint endpoint, HttpRequest request) =>
RouteTelemetryExtensions.StartConsumerSpan(
endpoint.Context, $"{endpoint.Uri.Path} receive", ActivityKind.Consumer,
"messaging.system", "as2", endpoint.Uri.NormalizedKey,
request.Headers, static (headers, name) => headers.TryGetValue(name, out var value) ? value.ToString() : null,
InboundParent.HostRequest, destination: endpoint.Uri.Path, operation: "receive");
/// The MIME headers of the inbound entity, rebuilt from the HTTP headers that carry them.
private static byte[] MimeHeaderPrefix(HttpRequest request)
{
var sb = new StringBuilder();
sb.Append("Content-Type: ").Append(request.ContentType ?? "application/octet-stream").Append("\r\n");
if (request.Headers.TryGetValue("Content-Transfer-Encoding", out var cte))
sb.Append("Content-Transfer-Encoding: ").Append(cte.ToString()).Append("\r\n");
if (request.Headers.TryGetValue("Content-Disposition", out var cd))
sb.Append("Content-Disposition: ").Append(cd.ToString()).Append("\r\n");
sb.Append("\r\n");
return Encoding.ASCII.GetBytes(sb.ToString());
}
///
/// Runs one unwrapping step into a new spool and parses its output in place (MimeKit's persistent mode keeps the
/// parts as views of the spool). The spool lives until the request is answered; a failed step leaves nothing behind.
///
private MimeEntity Unwrap(List spools, Action step)
{
var spool = new StreamCache(_spool!);
spools.Add(spool);
step(spool);
spool.CompleteWriting();
spool.Position = 0;
return MimeEntity.Load(spool, persistent: true);
}
///
/// The business payload, decoded into a spool of its own. With the spool is the body
/// (the exchange closes it); otherwise its bytes are, and the spool goes at once.
///
private static (object payload, string? contentType) ExtractPayload(MimeEntity entity, StreamCacheOptions options, bool streamBody)
{
var spool = new StreamCache(options);
var keep = false;
try
{
string? contentType;
if (entity is MimePart { Content: not null } part)
{
part.Content.DecodeTo(spool);
contentType = part.ContentType?.MimeType;
}
else
{
// Non-leaf payload (rare): the whole entity.
entity.WriteTo(spool);
contentType = entity.ContentType?.MimeType;
}
spool.CompleteWriting();
spool.Position = 0;
if (streamBody)
{
keep = true;
return (spool, contentType);
}
var bytes = new byte[spool.Length];
spool.ReadExactly(bytes);
return (bytes, contentType);
}
finally
{
if (!keep) spool.Dispose();
}
}
///
/// Copies AS2/business request headers verbatim, skipping the S/MIME wrapper + hop-by-hop set and the hop's
/// credentials; decides the rest.
///
private static void CopyInboundHeaders(HttpRequest request, IMessage message, IHeaderFilterStrategy headerFilter,
IExchange exchange)
{
foreach (var header in request.Headers)
{
if (header.Key.StartsWith(':')) continue; // pseudo-headers
if (WrapperHeaders.Contains(header.Key)) continue; // wrapper / hop-by-hop
if (CredentialHeaders.Contains(header.Key)) continue; // they authenticated the hop, not the document
var value = header.Value.Count == 1 ? header.Value[0] : header.Value.ToString();
if (headerFilter.FilterIncoming(header.Key, value, exchange)) continue;
message.Headers[header.Key] = value;
}
}
}