-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
MapMiddleware.cs
74 lines (63 loc) · 2.31 KB
/
MapMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Builder.Extensions;
/// <summary>
/// Represents a middleware that maps a request path to a sub-request pipeline.
/// </summary>
public class MapMiddleware
{
private readonly RequestDelegate _next;
private readonly MapOptions _options;
/// <summary>
/// Creates a new instance of <see cref="MapMiddleware"/>.
/// </summary>
/// <param name="next">The delegate representing the next middleware in the request pipeline.</param>
/// <param name="options">The middleware options.</param>
public MapMiddleware(RequestDelegate next, MapOptions options)
{
ArgumentNullException.ThrowIfNull(next);
ArgumentNullException.ThrowIfNull(options);
if (options.Branch == null)
{
throw new ArgumentException("Branch not set on options.", nameof(options));
}
_next = next;
_options = options;
}
/// <summary>
/// Executes the middleware.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
/// <returns>A task that represents the execution of this middleware.</returns>
public Task Invoke(HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context.Request.Path.StartsWithSegments(_options.PathMatch, out var matchedPath, out var remainingPath))
{
if (!_options.PreserveMatchedPathSegment)
{
return InvokeCore(context, matchedPath, remainingPath);
}
return _options.Branch!(context);
}
return _next(context);
}
private async Task InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
{
var path = context.Request.Path;
var pathBase = context.Request.PathBase;
// Update the path
context.Request.PathBase = pathBase.Add(matchedPath);
context.Request.Path = remainingPath;
try
{
await _options.Branch!(context);
}
finally
{
context.Request.PathBase = pathBase;
context.Request.Path = path;
}
}
}