Skip to content

Commit

Permalink
refactor middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Ilias Politsopoulos <[email protected]>
  • Loading branch information
IliasP91 committed Oct 4, 2023
1 parent cb811e6 commit 004a985
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Dapr.AspNetCore/CloudEventPropertyNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Dapr
{
internal static class CloudEventPropertyNames
{
public const string Data = "data";
public const string DataContentType = "datacontenttype";
public const string DataBase64 = "data_base64";
}
}
30 changes: 23 additions & 7 deletions src/Dapr.AspNetCore/CloudEventsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal class CloudEventsMiddleware
// are included in the headers by other components of Dapr earlier in the pipeline
private static readonly string[] ExcludedPropertiesFromHeaders =
{
"datacontenttype", "data", "data_base64", "pubsubname", "traceparent"
CloudEventPropertyNames.DataContentType, CloudEventPropertyNames.Data,
CloudEventPropertyNames.DataBase64, "pubsubname", "traceparent"
};

private readonly RequestDelegate next;
Expand Down Expand Up @@ -100,14 +101,28 @@ private async Task ProcessBodyAsync(HttpContext httpContext, string charSet)

var dataPropName = jsonPropNames
.Select(d => d.Name)
.FirstOrDefault(d => d.Equals("data", StringComparison.OrdinalIgnoreCase)) ?? "";
.FirstOrDefault(d => d.Equals(CloudEventPropertyNames.Data, StringComparison.OrdinalIgnoreCase));

var dataBase64PropName = jsonPropNames
.Select(d => d.Name)
.FirstOrDefault(d => d.Equals("data_base64", StringComparison.OrdinalIgnoreCase)) ?? "";
.FirstOrDefault(d =>
d.Equals(CloudEventPropertyNames.DataBase64, StringComparison.OrdinalIgnoreCase));

var isDataSet = json.TryGetProperty(dataPropName, out var data);
var isBinaryDataSet = json.TryGetProperty(dataBase64PropName, out var binaryData);
var isDataSet = false;
var isBinaryDataSet = false;
JsonElement data = default;

if (!string.IsNullOrWhiteSpace(dataPropName))
{
isDataSet = true;
data = json.TryGetProperty(dataPropName, out var dataJsonElement) ? dataJsonElement : data;
}

if (!string.IsNullOrWhiteSpace(dataBase64PropName))
{
isBinaryDataSet = true;
data = json.TryGetProperty(dataBase64PropName, out var dataJsonElement) ? dataJsonElement : data;
}

if (isDataSet && isBinaryDataSet)
{
Expand Down Expand Up @@ -144,7 +159,7 @@ private async Task ProcessBodyAsync(HttpContext httpContext, string charSet)
// As per the spec, if the implementation determines that the type of data is Binary,
// the value MUST be represented as a JSON string expression containing the Base64 encoded
// binary value, and use the member name data_base64 to store it inside the JSON object.
var decodedBody = binaryData.GetBytesFromBase64();
var decodedBody = data.GetBytesFromBase64();
body = new MemoryStream(decodedBody);
body.Seek(0L, SeekOrigin.Begin);
contentType = GetDataContentType(json, out _);
Expand Down Expand Up @@ -196,7 +211,8 @@ private static string GetDataContentType(JsonElement json, out bool isJson)
var dataContentTypePropName = json.EnumerateObject()
.Select(d => d.Name)
.FirstOrDefault(d =>
d.Equals("datacontenttype", StringComparison.OrdinalIgnoreCase))
d.Equals(CloudEventPropertyNames.DataContentType,
StringComparison.OrdinalIgnoreCase))
?? "";
string contentType;

Expand Down
5 changes: 5 additions & 0 deletions src/Dapr.AspNetCore/CloudEventsMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class CloudEventsMiddlewareOptions
/// <remarks>
/// <para>
/// Setting this property to <c>true</c> will forward the CloudEvent properties as Request Headers in the following format.
/// </para>
/// <para>
/// Property names will always be prefixed with 'Cloudevent.' and lower case.
/// </para>
/// <para>
/// ie. A CloudEvent property <c>"type": "Example.Type"</c> will be added as <c>"Cloudevent.type": "Example.Type"</c> request header.
/// </para>
/// </remarks>
Expand Down

0 comments on commit 004a985

Please sign in to comment.