Skip to content

Commit f285ee9

Browse files
committed
refactor(middlware): split concerns
move the middleware implementation into its own class, separate from the app builder extensions
1 parent 806f58f commit f285ee9

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,17 @@
1+
using JsonApiDotNetCore.Middleware;
12
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.AspNetCore.Http;
3-
using Microsoft.Extensions.Primitives;
43

54
namespace JsonApiDotNetCore.Routing
65
{
76
public static class IApplicationBuilderExtensions
87
{
98
public static IApplicationBuilder UseJsonApi(this IApplicationBuilder app)
109
{
11-
app.Use(async (context, next) =>
12-
{
13-
if (IsValid(context))
14-
await next.Invoke();
15-
});
10+
app.UseMiddleware<RequestMiddleware>();
1611

1712
app.UseMvc();
1813

1914
return app;
2015
}
21-
22-
private static bool IsValid(HttpContext context)
23-
{
24-
return IsValidContentTypeHeader(context) && IsValidAcceptHeader(context);
25-
}
26-
27-
private static bool IsValidContentTypeHeader(HttpContext context)
28-
{
29-
var contentType = context.Request.ContentType;
30-
if (contentType != null && ContainsMediaTypeParameters(contentType))
31-
{
32-
FlushResponse(context, 415);
33-
return false;
34-
}
35-
return true;
36-
}
37-
38-
private static bool IsValidAcceptHeader(HttpContext context)
39-
{
40-
var acceptHeaders = new StringValues();
41-
if (context.Request.Headers.TryGetValue("Accept", out acceptHeaders))
42-
{
43-
foreach (var acceptHeader in acceptHeaders)
44-
{
45-
if (ContainsMediaTypeParameters(acceptHeader))
46-
{
47-
FlushResponse(context, 406);
48-
return false;
49-
}
50-
}
51-
}
52-
return true;
53-
}
54-
55-
private static bool ContainsMediaTypeParameters(string mediaType)
56-
{
57-
var mediaTypeArr = mediaType.Split(';');
58-
return (mediaTypeArr[0] == "application/vnd.api+json" && mediaTypeArr.Length == 2);
59-
}
60-
61-
private static void FlushResponse(HttpContext context, int statusCode)
62-
{
63-
context.Response.StatusCode = statusCode;
64-
context.Response.Body.Flush();
65-
}
6616
}
6717
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.Extensions.Primitives;
4+
5+
namespace JsonApiDotNetCore.Middleware
6+
{
7+
public class RequestMiddleware
8+
{
9+
private readonly RequestDelegate _next;
10+
11+
public RequestMiddleware(RequestDelegate next)
12+
{
13+
_next = next;
14+
}
15+
16+
public async Task Invoke(HttpContext context)
17+
{
18+
if (IsValid(context))
19+
await _next(context);
20+
}
21+
22+
private static bool IsValid(HttpContext context)
23+
{
24+
return IsValidContentTypeHeader(context) && IsValidAcceptHeader(context);
25+
}
26+
27+
private static bool IsValidContentTypeHeader(HttpContext context)
28+
{
29+
var contentType = context.Request.ContentType;
30+
if (contentType != null && ContainsMediaTypeParameters(contentType))
31+
{
32+
FlushResponse(context, 415);
33+
return false;
34+
}
35+
return true;
36+
}
37+
38+
private static bool IsValidAcceptHeader(HttpContext context)
39+
{
40+
var acceptHeaders = new StringValues();
41+
if (context.Request.Headers.TryGetValue("Accept", out acceptHeaders))
42+
{
43+
foreach (var acceptHeader in acceptHeaders)
44+
{
45+
if (ContainsMediaTypeParameters(acceptHeader))
46+
{
47+
FlushResponse(context, 406);
48+
return false;
49+
}
50+
}
51+
}
52+
return true;
53+
}
54+
55+
private static bool ContainsMediaTypeParameters(string mediaType)
56+
{
57+
var mediaTypeArr = mediaType.Split(';');
58+
return (mediaTypeArr[0] == "application/vnd.api+json" && mediaTypeArr.Length == 2);
59+
}
60+
61+
private static void FlushResponse(HttpContext context, int statusCode)
62+
{
63+
context.Response.StatusCode = statusCode;
64+
context.Response.Body.Flush();
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)