Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 33e6b7e

Browse files
moved allowed methods into own middleware
1 parent 92b8d56 commit 33e6b7e

2 files changed

Lines changed: 60 additions & 56 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.Primitives;
8+
9+
namespace SqlStreamStore.HAL
10+
{
11+
internal static class AllowedMethodsMiddleware
12+
{
13+
public static IApplicationBuilder UseAllowedMethods(this IApplicationBuilder builder, IResource resource)
14+
{
15+
var allowed = ResourceMethods.Discover(resource);
16+
17+
var allowedMethodsHeaderValue = allowed.Aggregate(
18+
StringValues.Empty,
19+
(previous, method) => StringValues.Concat(previous, method.Method));
20+
21+
var allowedHeadersHeaderValue = new StringValues(new[]
22+
{
23+
Constants.Headers.ContentType,
24+
Constants.Headers.XRequestedWith,
25+
Constants.Headers.Authorization
26+
});
27+
28+
Task Options(HttpContext context, Func<Task> next)
29+
{
30+
context.Response.Headers.AppendCommaSeparatedValues(
31+
Constants.Headers.AccessControl.AllowMethods,
32+
allowedMethodsHeaderValue);
33+
context.Response.Headers.AppendCommaSeparatedValues(
34+
Constants.Headers.AccessControl.AllowHeaders,
35+
allowedHeadersHeaderValue);
36+
context.Response.Headers.AppendCommaSeparatedValues(
37+
Constants.Headers.AccessControl.AllowOrigin,
38+
"*");
39+
40+
return Task.CompletedTask;
41+
}
42+
43+
Task AllowedMethods(HttpContext context, Func<Task> next)
44+
{
45+
if(!allowed.Contains(new HttpMethod(context.Request.Method)))
46+
{
47+
context.Response.StatusCode = 405;
48+
context.Response.Headers.Add(Constants.Headers.Allowed, allowedMethodsHeaderValue);
49+
return Task.CompletedTask;
50+
}
51+
52+
return next();
53+
}
54+
55+
return builder
56+
.MapWhen(HttpMethod.Options, inner => inner.Use(Options))
57+
.Use(AllowedMethods);
58+
}
59+
}
60+
}
Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
namespace SqlStreamStore.HAL
22
{
33
using System;
4-
using System.Linq;
54
using System.Net.Http;
6-
using System.Threading.Tasks;
75
using Microsoft.AspNetCore.Builder;
8-
using Microsoft.AspNetCore.Http;
9-
using Microsoft.Extensions.Primitives;
10-
using MidFunc = System.Func<
11-
Microsoft.AspNetCore.Http.HttpContext,
12-
System.Func<System.Threading.Tasks.Task>,
13-
System.Threading.Tasks.Task
14-
>;
156

167
internal static class ApplicationBuilderExtensions
178
{
@@ -24,52 +15,5 @@ public static IApplicationBuilder MapWhen(
2415
method.Method,
2516
StringComparison.OrdinalIgnoreCase),
2617
configure);
27-
28-
public static IApplicationBuilder UseAllowedMethods(this IApplicationBuilder builder, IResource resource)
29-
{
30-
var allowed = ResourceMethods.Discover(resource);
31-
32-
var allowedMethodsHeaderValue = allowed.Aggregate(
33-
StringValues.Empty,
34-
(previous, method) => StringValues.Concat(previous, method.Method));
35-
36-
var allowedHeadersHeaderValue = new StringValues(new[]
37-
{
38-
Constants.Headers.ContentType,
39-
Constants.Headers.XRequestedWith,
40-
Constants.Headers.Authorization
41-
});
42-
43-
Task Options(HttpContext context, Func<Task> next)
44-
{
45-
context.Response.Headers.AppendCommaSeparatedValues(
46-
Constants.Headers.AccessControl.AllowMethods,
47-
allowedMethodsHeaderValue);
48-
context.Response.Headers.AppendCommaSeparatedValues(
49-
Constants.Headers.AccessControl.AllowHeaders,
50-
allowedHeadersHeaderValue);
51-
context.Response.Headers.AppendCommaSeparatedValues(
52-
Constants.Headers.AccessControl.AllowOrigin,
53-
"*");
54-
55-
return Task.CompletedTask;
56-
}
57-
58-
Task AllowedMethods(HttpContext context, Func<Task> next)
59-
{
60-
if(!allowed.Contains(new HttpMethod(context.Request.Method)))
61-
{
62-
context.Response.StatusCode = 405;
63-
context.Response.Headers.Add(Constants.Headers.Allowed, allowedMethodsHeaderValue);
64-
return Task.CompletedTask;
65-
}
66-
67-
return next();
68-
}
69-
70-
return builder
71-
.MapWhen(HttpMethod.Options, inner => inner.Use(Options))
72-
.Use(AllowedMethods);
73-
}
7418
}
7519
}

0 commit comments

Comments
 (0)