Path: redb-route/redb.Route.As4/Mime/PayloadConsistency.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.As4/Mime/PayloadConsistency.cs
----
using redb.Route.As4.Messaging;
namespace redb.Route.As4.Mime;
///
/// The payload references of a user message and the MIME parts of its message must match one to one
/// (ebMS 3.0 Core §5.2.2.13, AS4 profile): every cid: reference names a part, every part is referenced.
/// A reference outside the message is not supported.
///
internal static class PayloadConsistency
{
/// Upper bound on attachments per message; beyond it the message is refused before any crypto work.
public const int DefaultMaxParts = 100;
/// Part property naming the character set of an XML payload before compression.
public const string CharacterSetProperty = "CharacterSet";
///
/// Whether is an encoding name of XML 1.0 §4.3.3 (EncName: a Latin letter, then
/// Latin letters, digits, ., _ and -), which the profile requires of the
/// CharacterSet part property (eDelivery AS4 1.16 §3.2.3).
///
public static bool IsCharacterSetName(string? value)
{
if (string.IsNullOrEmpty(value) || !char.IsAsciiLetter(value[0]))
return false;
foreach (var c in value)
if (!(char.IsAsciiLetterOrDigit(c) || c is '.' or '_' or '-'))
return false;
return true;
}
///
/// Throws when a part of names a
/// CharacterSet that is not an encoding name.
///
public static void CheckCharacterSets(UserMessage message)
{
ArgumentNullException.ThrowIfNull(message);
foreach (var info in message.PayloadInfo)
foreach (var property in info.Properties)
if (property.Name == CharacterSetProperty && !IsCharacterSetName(property.Value))
throw new As4ProcessingException(As4ErrorCode.ValueInconsistent,
$"part '{info.Href}' names CharacterSet '{property.Value}', which is not an encoding name of XML 1.0 §4.3.3.");
}
///
/// Pairs each eb:PartInfo of with its part, or throws
/// / .
/// A PartInfo without href stands for the SOAP body and is paired with null.
///
public static IReadOnlyList<(PartInfo Info, SwaPart? Part)> Match(UserMessage message, SwaMessage swa, int maxParts = DefaultMaxParts)
{
ArgumentNullException.ThrowIfNull(message);
ArgumentNullException.ThrowIfNull(swa);
if (swa.Parts.Count > maxParts)
throw new As4ProcessingException(As4ErrorCode.MimeInconsistency, $"the message has {swa.Parts.Count} parts; at most {maxParts} are accepted.");
var pairs = new List<(PartInfo, SwaPart?)>();
var referenced = new HashSet(StringComparer.Ordinal);
foreach (var info in message.PayloadInfo)
{
if (info.Href is null)
{
pairs.Add((info, null));
continue;
}
if (!info.Href.StartsWith("cid:", StringComparison.OrdinalIgnoreCase))
throw new As4ProcessingException(As4ErrorCode.ExternalPayloadError, $"payload reference '{info.Href}' points outside the message.");
var id = Uri.UnescapeDataString(info.Href["cid:".Length..]);
if (!referenced.Add(id))
throw new As4ProcessingException(As4ErrorCode.MimeInconsistency, $"part '{id}' is referenced more than once.");
if (!swa.Parts.TryGetValue(id, out var part))
throw new As4ProcessingException(As4ErrorCode.MimeInconsistency, $"the header references part '{id}', which the message does not carry.");
pairs.Add((info, part));
}
var unreferenced = swa.Parts.Keys.FirstOrDefault(id => !referenced.Contains(id));
if (unreferenced is not null)
throw new As4ProcessingException(As4ErrorCode.MimeInconsistency, $"part '{unreferenced}' is not referenced by the header.");
return pairs;
}
}