Skip to content

Commit

Permalink
move key value parser to own project
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Dec 9, 2024
1 parent c0a2c04 commit b57766b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 304 deletions.
52 changes: 0 additions & 52 deletions src/Nager.EmailAuthentication.UnitTest/KeyValueParserTest.cs

This file was deleted.

62 changes: 35 additions & 27 deletions src/Nager.EmailAuthentication/DmarcRecordParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,53 +40,61 @@ public static bool TryParse(
return false;
}

var keyValueParser = new KeyValueParser.MemoryEfficientKeyValueParser(';', '=');
if (!keyValueParser.TryParse(dmarcRaw, out var parseResult))
{
dmarcDataFragment = null;
return false;
}

if (parseResult == null)
{
dmarcDataFragment = null;
return false;
}

var dataFragment = new DmarcDataFragment();
var internalUnrecognizedParts = new List<string>();

var unrecognizedHandlers = new List<string>();

var handlers = new Dictionary<string, Action<string>>
{
{ "v=", value => dataFragment.Version = value },
{ "p=", value => dataFragment.DomainPolicy = value },
{ "sp=", value => dataFragment.SubdomainPolicy = value },
{ "rua=", value => dataFragment.AggregateReportUri = value },
{ "ruf=", value => dataFragment.ForensicReportUri = value },
{ "rf=", value => dataFragment.ReportFormat = value },
{ "fo=", value => dataFragment.FailureOptions = value },
{ "pct=", value => dataFragment.PolicyPercentage = value },
{ "ri=", value => dataFragment.ReportingInterval = value },
{ "adkim=", value => dataFragment.DkimAlignmentMode = value },
{ "aspf=", value => dataFragment.SpfAlignmentMode = value }
{ "v", value => dataFragment.Version = value },
{ "p", value => dataFragment.DomainPolicy = value },
{ "sp", value => dataFragment.SubdomainPolicy = value },
{ "rua", value => dataFragment.AggregateReportUri = value },
{ "ruf", value => dataFragment.ForensicReportUri = value },
{ "rf", value => dataFragment.ReportFormat = value },
{ "fo", value => dataFragment.FailureOptions = value },
{ "pct", value => dataFragment.PolicyPercentage = value },
{ "ri", value => dataFragment.ReportingInterval = value },
{ "adkim", value => dataFragment.DkimAlignmentMode = value },
{ "aspf", value => dataFragment.SpfAlignmentMode = value }
};

var parts = dmarcRaw.Split(";", StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
foreach (var keyValue in parseResult.KeyValues)
{
var cleanPart = part.AsSpan().TrimStart(' ');
var keyValueSeparatorIndex = cleanPart.IndexOf('=');

if (keyValueSeparatorIndex <= 0)
if (string.IsNullOrEmpty(keyValue.Key))
{
internalUnrecognizedParts.Add(cleanPart.ToString());
continue;
}

var key = cleanPart[..(keyValueSeparatorIndex + 1)];
var value = cleanPart[(keyValueSeparatorIndex + 1)..];

if (handlers.TryGetValue(key.ToString().ToLowerInvariant(), out var handler))
if (handlers.TryGetValue(keyValue.Key.ToLowerInvariant(), out var handler))
{
handler(value.ToString());
handler(keyValue.Value ?? "");
continue;
}

internalUnrecognizedParts.Add(cleanPart.ToString());
unrecognizedHandlers.Add($"{keyValue.Key} {keyValue.Value}");
}

if (internalUnrecognizedParts.Count > 0)
if (parseResult.UnrecognizedParts != null && parseResult.UnrecognizedParts.Length > 0)
{
unrecognizedParts = [.. internalUnrecognizedParts];
unrecognizedHandlers.AddRange(parseResult.UnrecognizedParts);
}

unrecognizedParts = unrecognizedHandlers.Count == 0 ? null : [.. unrecognizedHandlers];

dmarcDataFragment = dataFragment;

return true;
Expand Down
15 changes: 0 additions & 15 deletions src/Nager.EmailAuthentication/KeyValueParser/IKeyValueParser.cs

This file was deleted.

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions src/Nager.EmailAuthentication/KeyValueParser/ParseResult.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Description>Email Authentication, DMARC</Description>

<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<ImplicitUsings>enable</ImplicitUsings>

Expand All @@ -24,4 +23,8 @@
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nager.KeyValueParser" Version="1.0.0" />
</ItemGroup>

</Project>

0 comments on commit b57766b

Please sign in to comment.