Skip to content

Commit

Permalink
The PR resolves an issue reported under oras-project#133.
Browse files Browse the repository at this point in the history
While testing against https://hub.docker.com/_/registry, "Docker-Content-Digest" headers were found on response.Headers collection rather than response.Content.Headers.
Aligned internal/private variables to use _camelCase as there was some inconsistencies.

Signed-off-by: Daniel Robinson <daniel.robinson@pebble.tv>
  • Loading branch information
daniel-pebble committed Aug 9, 2024
1 parent c58adc9 commit 309c896
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
6 changes: 3 additions & 3 deletions src/OrasProject.Oras/Content/Digest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ namespace OrasProject.Oras.Content;

internal static class Digest
{
private const string digestRegexPattern = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+";
private static readonly Regex digestRegex = new Regex(digestRegexPattern, RegexOptions.Compiled);
private const string _digestRegexPattern = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+";
private static readonly Regex _digestRegex = new Regex(_digestRegexPattern, RegexOptions.Compiled);

/// <summary>
/// Verifies the digest header and throws an exception if it is invalid.
/// </summary>
/// <param name="digest"></param>
internal static string Validate(string? digest)
{
if (string.IsNullOrEmpty(digest) || !digestRegex.IsMatch(digest))
if (string.IsNullOrEmpty(digest) || !_digestRegex.IsMatch(digest))
{
throw new InvalidDigestException($"Invalid digest: {digest}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace OrasProject.Oras.Registry.Remote;

internal static class HttpResponseMessageExtensions
{
private const string _dockerContentDigestHeader = "Docker-Content-Digest";

/// <summary>
/// Parses the error returned by the remote registry.
/// </summary>
Expand Down Expand Up @@ -75,7 +77,7 @@ public static async Task<Exception> ParseErrorResponseAsync(this HttpResponseMes
/// <exception cref="NotImplementedException"></exception>
public static void VerifyContentDigest(this HttpResponseMessage response, string expected)
{
if (!response.Content.Headers.TryGetValues("Docker-Content-Digest", out var digestValues))
if (!response.Headers.TryGetValues(_dockerContentDigestHeader, out var digestValues))
{
return;
}
Expand Down Expand Up @@ -154,7 +156,7 @@ public static async Task<Descriptor> GenerateDescriptorAsync(this HttpResponseMe

// 4. Validate Server Digest (if present)
string? serverDigest = null;
if (response.Content.Headers.TryGetValues("Docker-Content-Digest", out var serverHeaderDigest))
if (response.Headers.TryGetValues(_dockerContentDigestHeader, out var serverHeaderDigest))
{
serverDigest = serverHeaderDigest.FirstOrDefault();
if (!string.IsNullOrEmpty(serverDigest))
Expand Down
Loading

0 comments on commit 309c896

Please sign in to comment.