summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/VersionConstraint.cs
blob: 1b1abd815b7d2b4828cc4fa5e18512202531ccd0 (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
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using StardewModdingAPI.Toolkit;

namespace StardewModdingAPI.Web.Framework
{
    /// <summary>Constrains a route value to a valid semantic version.</summary>
    internal class VersionConstraint : IRouteConstraint
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Get whether the URL parameter contains a valid value for this constraint.</summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <param name="route">The router that this constraint belongs to.</param>
        /// <param name="routeKey">The name of the parameter that is being checked.</param>
        /// <param name="values">A dictionary that contains the parameters for the URL.</param>
        /// <param name="routeDirection">An object that indicates whether the constraint check is being performed when an incoming request is being handled or when a URL is being generated.</param>
        /// <returns><c>true</c> if the URL parameter contains a valid value; otherwise, <c>false</c>.</returns>
        public 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));

            return
                values.TryGetValue(routeKey, out object? routeValue)
                && routeValue is string routeStr
                && SemanticVersion.TryParse(routeStr, allowNonStandard: true, out _);
        }
    }
}