| | | 1 | | using Kestrun.Utilities; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Hosting.Options; |
| | | 4 | | /// <summary> |
| | | 5 | | /// Options for mapping a route, including pattern, HTTP verbs, script code, authorization, and metadata. |
| | | 6 | | /// </summary> |
| | | 7 | | public class MapRouteOptions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// The route pattern to match for this option. |
| | | 11 | | /// </summary> |
| | 482 | 12 | | public string? Pattern { get; set; } |
| | | 13 | | /// <summary> |
| | | 14 | | /// The HTTP verbs (methods) that this route responds to. |
| | | 15 | | /// </summary> |
| | 335 | 16 | | public List<HttpVerb> HttpVerbs { get; set; } = []; |
| | | 17 | | /// <summary> |
| | | 18 | | /// Authorization Scheme names required for this route. |
| | | 19 | | /// </summary> |
| | 168 | 20 | | public List<string> RequireSchemes { get; init; } = []; // Authorization scheme name, if any |
| | | 21 | | /// <summary> |
| | | 22 | | /// Authorization policy names required for this route. |
| | | 23 | | /// </summary> |
| | 152 | 24 | | public List<string> RequirePolicies { get; init; } = []; // Authorization policies, if any |
| | | 25 | | /// <summary> |
| | | 26 | | /// Name of the CORS policy to apply, if any. |
| | | 27 | | /// </summary> |
| | 108 | 28 | | public string CorsPolicy { get; set; } = string.Empty; // Name of the CORS policy to apply, if any |
| | | 29 | | /// <summary> |
| | | 30 | | /// If true, short-circuits the pipeline after this route. |
| | | 31 | | /// </summary> |
| | 38 | 32 | | public bool ShortCircuit { get; set; } // If true, short-circuit the pipeline after this route |
| | | 33 | | /// <summary> |
| | | 34 | | /// Status code to return if short-circuiting the pipeline after this route. |
| | | 35 | | /// </summary> |
| | 3 | 36 | | public int? ShortCircuitStatusCode { get; set; } = null; // Status code to return if short-circuiting |
| | | 37 | | /// <summary> |
| | | 38 | | /// If true, allows anonymous access to this route. |
| | | 39 | | /// </summary> |
| | 36 | 40 | | public bool AllowAnonymous { get; set; } |
| | | 41 | | /// <summary> |
| | | 42 | | /// If true, disables antiforgery protection for this route. |
| | | 43 | | /// </summary> |
| | 61 | 44 | | public bool DisableAntiforgery { get; set; } |
| | | 45 | | /// <summary> |
| | | 46 | | /// If true, disables response compression for this route. |
| | | 47 | | /// </summary> |
| | 40 | 48 | | public bool DisableResponseCompression { get; set; } |
| | | 49 | | /// <summary> |
| | | 50 | | /// The name of the rate limit policy to apply to this route, if any. |
| | | 51 | | /// </summary> |
| | 37 | 52 | | public string? RateLimitPolicyName { get; set; } |
| | | 53 | | /// <summary> |
| | | 54 | | /// Endpoints to bind the route to, if any. |
| | | 55 | | /// </summary> |
| | 124 | 56 | | public string[]? Endpoints { get; set; } = []; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Default response content type for this route. |
| | | 60 | | /// </summary> |
| | 73 | 61 | | public string DefaultResponseContentType { get; set; } = "text/html"; // Default response content type for this rout |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// OpenAPI metadata for this route. |
| | | 65 | | /// </summary> |
| | 104 | 66 | | public Dictionary<HttpVerb, OpenAPIMetadata> OpenAPI { get; set; } = []; // OpenAPI metadata for this route |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Path-level OpenAPI common metadata for this route. |
| | | 70 | | /// </summary> |
| | 0 | 71 | | public OpenAPICommonMetadata? PathLevelOpenAPIMetadata { get; set; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Script code and language options for this route. |
| | | 75 | | /// </summary> |
| | 380 | 76 | | public LanguageOptions ScriptCode { get; init; } = new LanguageOptions(); |
| | | 77 | | /// <summary> |
| | | 78 | | /// If true, throws an exception on duplicate routes. |
| | | 79 | | /// </summary> |
| | 7 | 80 | | public bool ThrowOnDuplicate { get; set; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Returns a string representation of the MapRouteOptions. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <returns></returns> |
| | | 86 | | public override string ToString() |
| | | 87 | | { |
| | 1 | 88 | | var verbs = HttpVerbs.Count > 0 ? string.Join(",", HttpVerbs) : "ANY"; |
| | 1 | 89 | | return $"{verbs} {Pattern}"; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Adds security requirement information to this route's authorization settings. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="schemes">the authorization schemes required for this route</param> |
| | | 96 | | /// <param name="policies">the authorization policies required for this route</param> |
| | | 97 | | public void AddSecurityRequirementObject(List<string>? schemes, List<string>? policies) |
| | | 98 | | { |
| | 0 | 99 | | if (schemes is { Count: > 0 }) |
| | | 100 | | { |
| | 0 | 101 | | RequireSchemes.AddRange(schemes); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | if (policies is { Count: > 0 }) |
| | | 105 | | { |
| | 0 | 106 | | RequirePolicies.AddRange(policies); |
| | | 107 | | } |
| | 0 | 108 | | } |
| | | 109 | | } |