Skip to content

Commit

Permalink
Fix request cache key building to use entire querystring for now
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Mar 19, 2024
1 parent 5515592 commit 62e72ee
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 20 deletions.
91 changes: 72 additions & 19 deletions src/Imazen.Routing/HttpAbstractions/IHttpResponseStreamAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Reflection;
using System.Runtime.InteropServices;
using Imazen.Abstractions.Blobs;
using Imazen.Routing.Requests;
Expand Down Expand Up @@ -32,14 +33,42 @@ public interface IHttpResponseStreamAdapter

public static class BufferWriterExtensions
{




public static void WriteWtf16String(this IBufferWriter<byte> writer, StringValues stringValues, char delimiter = ',')
{
switch (stringValues.Count)
{
case 0:
writer.WriteWtf16String("");
return;
case 1:
writer.WriteWtf16String(stringValues.ToString());
return;
case > 1:
{
var arr = stringValues.ToArray();
foreach (var str in arr)
{
writer.WriteWtf16String(str);
if (arr[^1] != str)
writer.WriteWtf16Char(delimiter);
}

break;
}
}
}




public static void WriteWtf16String(this IBufferWriter<byte> writer, string str)
public static void WriteWtf16String(this IBufferWriter<byte> writer, string? str)
{
if (str == null)
{
writer.WriteWtf16String("(null)");
return;
}
var byteCount = str.Length * 2;
var buffer = writer.GetSpan(byteCount);
// marshal
Expand Down Expand Up @@ -117,27 +146,51 @@ public static void WriteInt(this IBufferWriter<byte> writer, int i)
writer.Advance(4);
}

public static void WriteWtf16Dictionary(this IBufferWriter<byte> writer, IDictionary<string, string>? dict)
{
if (dict == null)
{
writer.WriteWtf16String("(null)");
return;
}
//serialize into {key=value,key2=value2,} syntax
writer.WriteWtf16Char('{');
foreach (var pair in dict)
{
writer.WriteWtf16String(pair.Key);
writer.WriteWtf16Char('=');
writer.WriteWtf16String(pair.Value);
writer.WriteWtf16Char(',');
}
}
public static void WriteWtf16Dictionary(this IBufferWriter<byte> writer, IDictionary<string, StringValues>? dict)
{
if (dict == null)
{
writer.WriteWtf16String("(null)");
return;
}
//serialize into {key=value,key2=value2,} syntax
writer.WriteWtf16Char('{');
foreach (var pair in dict)
{
writer.WriteWtf16String(pair.Key);
writer.WriteWtf16Char('=');
writer.WriteWtf16String(pair.Value);
writer.WriteWtf16Char(',');
}
}

// Write IGetRequestSnapshot
public static void WriteWtf16PathAndRouteValuesCacheKeyBasis(this IBufferWriter<byte> writer, IGetResourceSnapshot request)
{
//path = request.Path
//route values = request.RouteValues

writer.WriteWtf16String("|path=");
writer.WriteWtf16String("\npath=");
writer.WriteWtf16String(request.Path);
writer.WriteWtf16String("|\n|routeValues(");
writer.WriteWtf16Char((char)(request.ExtractedData?.Count ?? 0));
writer.WriteWtf16String(")|");
if (request.ExtractedData != null)
{
foreach (var pair in request.ExtractedData)
{
writer.WriteWtf16String(pair.Key);
writer.WriteWtf16String("=");
writer.WriteWtf16String(pair.Value);
writer.WriteWtf16String("|");
}
}
writer.WriteWtf16String("\nrouteValues=");
writer.WriteWtf16Dictionary(request.ExtractedData);

}

Expand Down Expand Up @@ -207,7 +260,7 @@ public static void WriteWtf<T>(this IBufferWriter<byte> writer, T? b) where T :
{
if (b == null)
{
WriteWtf16String(writer, "null");
WriteWtf16String(writer, "(null)");
return;
}
WriteAsInMemory(writer, b.Value);
Expand Down
19 changes: 18 additions & 1 deletion src/Imazen.Routing/Requests/IRequestSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,25 @@ public record RequestSnapshot(bool MutationsComplete) : IRequestSnapshot{
public void WriteCacheKeyBasisPairsTo(IBufferWriter<byte> writer)
{
if (!MutationsComplete) throw new InvalidOperationException("Mutations must be complete before writing cache key basis pairs");

// We must trust the layers to extract any vary-by data from OriginatingRequest (such as host, headers, etc) and put it in ExtractedData.

//Storage tags aren't currently hashed.

// TODO: write all pairs using memcasting and wtf16
if (ParentRequest != null)
{
writer.WriteWtf16String("[CHILD REQUEST]"); // watermark images are subject to different authorization rules, and must be cached separately
}

writer.WriteWtf16String("method=");
writer.WriteWtf16String(HttpMethod);
writer.WriteWtf16String("\npath=");
writer.WriteWtf16String(Path);
writer.WriteWtf16String("\nExtractedData=");
writer.WriteWtf16Dictionary(ExtractedData);
writer.WriteWtf16String("\nQueryString=");
writer.WriteWtf16Dictionary(QueryString);

}

public override string ToString()
Expand Down

0 comments on commit 62e72ee

Please sign in to comment.