This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
DefaultPageHandlerMethodSelector.cs
195 lines (167 loc) · 6.68 KB
/
DefaultPageHandlerMethodSelector.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
public class DefaultPageHandlerMethodSelector : IPageHandlerMethodSelector
{
private const string Handler = "handler";
private readonly IOptions<RazorPagesOptions> _options;
[Obsolete("This constructor will be removed in a future release. Use the other constructor.")]
public DefaultPageHandlerMethodSelector()
{
}
public DefaultPageHandlerMethodSelector(IOptions<RazorPagesOptions> options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_options = options;
}
private bool AllowFuzzyHttpMethodMatching => _options?.Value.AllowMappingHeadRequestsToGetHandler ?? false;
public HandlerMethodDescriptor Select(PageContext context)
{
var handlers = SelectHandlers(context);
if (handlers == null || handlers.Count == 0)
{
return null;
}
List<HandlerMethodDescriptor> ambiguousMatches = null;
HandlerMethodDescriptor bestMatch = null;
for (var score = 2; score >= 0; score--)
{
for (var i = 0; i < handlers.Count; i++)
{
var handler = handlers[i];
if (GetScore(handler) == score)
{
if (bestMatch == null)
{
bestMatch = handler;
continue;
}
if (ambiguousMatches == null)
{
ambiguousMatches = new List<HandlerMethodDescriptor>();
ambiguousMatches.Add(bestMatch);
}
ambiguousMatches.Add(handler);
}
}
if (ambiguousMatches != null)
{
var ambiguousMethods = string.Join(", ", ambiguousMatches.Select(m => m.MethodInfo));
throw new InvalidOperationException(Resources.FormatAmbiguousHandler(Environment.NewLine, ambiguousMethods));
}
if (bestMatch != null)
{
return bestMatch;
}
}
return null;
}
private List<HandlerMethodDescriptor> SelectHandlers(PageContext context)
{
var handlers = context.ActionDescriptor.HandlerMethods;
var candidates = new List<HandlerMethodDescriptor>();
// Name is optional, may not be provided.
var handlerName = GetHandlerName(context);
// The handler selection process considers handlers according to a few criteria. Handlers
// have a defined HTTP method that they handle, and also optionally a 'name'.
//
// We don't really have a scenario for handler methods without a verb (we don't provide a way
// to create one). If we see one, it will just never match.
//
// The verb must match (with some fuzzy matching) and the handler name must match if
// there is one.
//
// The process is like this:
//
// 1. Match the possible candidates on HTTP method
// 1a. **Added in 2.1** if no candidates matched in 1, then do *fuzzy matching*
// 2. Match the candidates from 1 or 1a on handler name.
// Step 1: match on HTTP method.
var httpMethod = context.HttpContext.Request.Method;
for (var i = 0; i < handlers.Count; i++)
{
var handler = handlers[i];
if (handler.HttpMethod != null &&
string.Equals(handler.HttpMethod, httpMethod, StringComparison.OrdinalIgnoreCase))
{
candidates.Add(handler);
}
}
// Step 1a: do fuzzy HTTP method matching if needed.
if (candidates.Count == 0 && AllowFuzzyHttpMethodMatching)
{
var fuzzyHttpMethod = GetFuzzyMatchHttpMethod(context);
if (fuzzyHttpMethod != null)
{
for (var i = 0; i < handlers.Count; i++)
{
var handler = handlers[i];
if (handler.HttpMethod != null &&
string.Equals(handler.HttpMethod, fuzzyHttpMethod, StringComparison.OrdinalIgnoreCase))
{
candidates.Add(handler);
}
}
}
}
// Step 2: remove candidates with non-matching handlers.
for (var i = candidates.Count - 1; i >= 0; i--)
{
var handler = candidates[i];
if (handler.Name != null &&
!handler.Name.Equals(handlerName, StringComparison.OrdinalIgnoreCase))
{
candidates.RemoveAt(i);
}
}
return candidates;
}
private static int GetScore(HandlerMethodDescriptor descriptor)
{
if (descriptor.Name != null)
{
return 2;
}
else if (descriptor.HttpMethod != null)
{
return 1;
}
else
{
return 0;
}
}
private static string GetHandlerName(PageContext context)
{
var handlerName = Convert.ToString(context.RouteData.Values[Handler]);
if (!string.IsNullOrEmpty(handlerName))
{
return handlerName;
}
if (context.HttpContext.Request.Query.TryGetValue(Handler, out StringValues queryValues))
{
return queryValues[0];
}
return null;
}
private static string GetFuzzyMatchHttpMethod(PageContext context)
{
var httpMethod = context.HttpContext.Request.Method;
// Map HEAD to get.
if (string.Equals("HEAD", httpMethod, StringComparison.OrdinalIgnoreCase))
{
return "GET";
}
return null;
}
}
}