Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpMethodRouteConstraint.cs « Constraints « src « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 827947f7f0b1293577cf6653d3339f74c662a29d (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Routing.Constraints;

/// <summary>
/// Constrains the HTTP method of request or a route.
/// </summary>
public class HttpMethodRouteConstraint : IRouteConstraint
{
    /// <summary>
    /// Creates a new instance of <see cref="HttpMethodRouteConstraint"/> that accepts the HTTP methods specified
    /// by <paramref name="allowedMethods"/>.
    /// </summary>
    /// <param name="allowedMethods">The allowed HTTP methods.</param>
    public HttpMethodRouteConstraint(params string[] allowedMethods)
    {
        if (allowedMethods == null)
        {
            throw new ArgumentNullException(nameof(allowedMethods));
        }

        AllowedMethods = new List<string>(allowedMethods);
    }

    /// <summary>
    /// Gets the HTTP methods allowed by the constraint.
    /// </summary>
    public IList<string> AllowedMethods { get; }

    /// <inheritdoc />
    public virtual bool Match(
        HttpContext? httpContext,
        IRouter? route,
        string routeKey,
        RouteValueDictionary values,
        RouteDirection routeDirection)
    {
        if (routeKey == null)
        {
            throw new ArgumentNullException(nameof(routeKey));
        }

        if (values == null)
        {
            throw new ArgumentNullException(nameof(values));
        }

        switch (routeDirection)
        {
            case RouteDirection.IncomingRequest:
                // Only required for constraining incoming requests
                if (httpContext == null)
                {
                    throw new ArgumentNullException(nameof(httpContext));
                }

                return AllowedMethods.Contains(httpContext.Request.Method, StringComparer.OrdinalIgnoreCase);

            case RouteDirection.UrlGeneration:
                // We need to see if the user specified the HTTP method explicitly.  Consider these two routes:
                //
                // a) Route: template = "/{foo}", Constraints = { httpMethod = new HttpMethodRouteConstraint("GET") }
                // b) Route: template = "/{foo}", Constraints = { httpMethod = new HttpMethodRouteConstraint("POST") }
                //
                // A user might know ahead of time that a URI he/she is generating might be used with a particular HTTP
                // method.  If a URI will be used for an HTTP POST but we match on (a) while generating the URI, then
                // the HTTP GET-specific route will be used for URI generation, which might have undesired behavior.
                //
                // To prevent this, a user might call GetVirtualPath(..., { httpMethod = "POST" }) to
                // signal that they are generating a URI that will be used for an HTTP POST, so they want the URI
                // generation to be performed by the (b) route instead of the (a) route, consistent with what would
                // happen on incoming requests.
                if (!values.TryGetValue(routeKey, out var obj))
                {
                    return true;
                }

                return AllowedMethods.Contains(Convert.ToString(obj, CultureInfo.InvariantCulture), StringComparer.OrdinalIgnoreCase);

            default:
                throw new ArgumentOutOfRangeException(nameof(routeDirection));
        }
    }
}