/
aprogrammer
/
reverse-proxy-ms-yarp
Обзор
Документация
Войти
/
aprogrammer
/
reverse-proxy-ms-yarp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/ReverseProxy/Routing/HeaderMatcher.cs
68 строк
2 KB
kevbry
Add HeaderMatchMode.NotExists, enable matchers that only match if the… (#1806)
25 июл 2022, 22:17
Не верифицирован
25 июл 2022, 22:17
b0a2452
Код
Авторство
О чём код?
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Linq; using Microsoft.Net.Http.Headers; using Yarp.ReverseProxy.Configuration; namespace Yarp.ReverseProxy.Routing; /// <summary> /// A request header matcher used during routing. /// </summary> internal sealed class HeaderMatcher { /// <summary> /// Creates a new instance. /// </summary> public HeaderMatcher(string name, IReadOnlyList<string>? values, HeaderMatchMode mode, bool isCaseSensitive) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("A header name is required.", nameof(name)); } if ((mode != HeaderMatchMode.Exists && mode != HeaderMatchMode.NotExists) && (values is null || values.Count == 0)) { throw new ArgumentException("Header values must have at least one value.", nameof(values)); } if ((mode == HeaderMatchMode.Exists || mode == HeaderMatchMode.NotExists) && values?.Count > 0) { throw new ArgumentException($"Header values must not be specified when using '{mode}'.", nameof(values)); } if (values is not null && values.Any(string.IsNullOrEmpty)) { throw new ArgumentNullException(nameof(values), "Header values must be not be empty."); } Name = name; Values = values?.ToArray() ?? Array.Empty<string>(); Mode = mode; Comparison = isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; Separator = name.Equals(HeaderNames.Cookie, StringComparison.OrdinalIgnoreCase) ? ';' : ','; } /// <summary> /// Name of the header to look for. /// </summary> public string Name { get; } /// <summary> /// Returns a read-only collection of acceptable header values used during routing. /// At least one value is required unless <see cref="Mode"/> is set to <see cref="HeaderMatchMode.Exists"/> /// or <see cref="HeaderMatchMode.NotExists"/>. /// </summary> public string[] Values { get; } /// <summary> /// Specifies how header values should be compared (e.g. exact matches Vs. by prefix). /// Defaults to <see cref="HeaderMatchMode.ExactHeader"/>. /// </summary> public HeaderMatchMode Mode { get; } public StringComparison Comparison { get; } public char Separator { get; } }