Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
folder: [Bones, Bones.Akka, Bones.Converters, Bones.Flow, Bones.Grpc, Bones.Tests, Bones.X509, Bones.Akka.Monitoring, Bones.Monitoring]
folder: [Bones, Bones.Akka, Bones.AspNetCore, Bones.Converters, Bones.Flow, Bones.Grpc, Bones.Tests, Bones.X509, Bones.Akka.Monitoring, Bones.Monitoring]
steps:
# Checking out repository
- name: Checkout 🛎️
Expand Down
14 changes: 14 additions & 0 deletions src/Bones.AspNetCore/Bones.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\Bones.Monitoring\Bones.Monitoring.csproj" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>$(VERSION)</Version>
<Authors>dative-gpi</Authors>
<Company>dative-gpi</Company>
</PropertyGroup>

</Project>
75 changes: 75 additions & 0 deletions src/Bones.AspNetCore/PostAsGetMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

using Bones.Monitoring.Core;

namespace Bones.AspNetCore
{
public class PostAsGetMiddleware : MonitoredMiddleware
{
public PostAsGetMiddleware(
IServiceProvider serviceProvider,
RequestDelegate next) : base(next, serviceProvider)
{
}

protected override async Task HandleAsync(HttpContext context, RequestDelegate next)
{
if (context.Request.Method == "POST"
&& context.Request.Query.ContainsKey("_method")
&& context.Request.Query["_method"] == "GET"
&& context.Request.HasJsonContentType())
{
// Convert the request to a GET request
context.Request.Method = "GET";
var doc = await JsonSerializer.DeserializeAsync<Dictionary<string, JsonElement>>(context.Request.Body);
if (doc != null)
{
var props = Flat(doc);
var query = $"?{String.Join("&", props.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"))}";
context.Request.QueryString = new QueryString(query);
context.Request.Body = Stream.Null;
context.Request.ContentLength = 0;
}
}
await next(context);
return;
}

private Dictionary<string, StringValues> Flat(Dictionary<string, JsonElement> payload)
{
Dictionary<string, StringValues> result = new Dictionary<string, StringValues>();
foreach (var item in payload)
{
var flatted = DeepFlat(item.Key, item.Value);
foreach (var flat in flatted)
{
result.Add(flat.Key, flat.Value);
}
}
return result;
}

private IEnumerable<KeyValuePair<string, string>> DeepFlat(string key, JsonElement value)
{
switch (value.ValueKind)
{
case JsonValueKind.Array when value.GetArrayLength() == 0:
return new[] { new KeyValuePair<string, string>($"{key}[]", "") };
case JsonValueKind.Array when value.GetArrayLength() != 0:
return value.EnumerateArray().SelectMany((item, index) => DeepFlat($"{key}[{index}]", item));
case JsonValueKind.Object:
return value.EnumerateObject().SelectMany(prop => DeepFlat($"{key}.{prop.Name}", prop.Value));
default:
return new[] { new KeyValuePair<string, string>(key, value.ToString()) };
}
}
}
}