< Summary

Information
Class: NanoRoute.Internals.RequestPipeline
Assembly: NanoRoute.dll
File(s): /home/runner/work/nanoroute/nanoroute/Src/NanoRoute/Private/RequestPipeline.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 113
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
RequestPipeline(...)20

File(s)

/home/runner/work/nanoroute/nanoroute/Src/NanoRoute/Private/RequestPipeline.cs

#LineLine coverage
 1/********************************************************************************
 2* RequestPipeline.cs                                                            *
 3*                                                                               *
 4* Author: Denes Solti                                                           *
 5********************************************************************************/
 6using System;
 7using System.Collections.Generic;
 8using System.Net;
 9using System.Net.Http;
 10using System.Threading;
 11using System.Threading.Tasks;
 12
 13namespace NanoRoute.Internals
 14{
 15    using Properties;
 16
 217    internal sealed class RequestPipeline(RouteNode root, MatchingPrecedence matchingPrecedence)
 18    {
 19        /// <summary>
 20        /// Executes an <see cref="HttpRequestMessage"/> through the configured route handler pipeline.
 21        /// </summary>
 22        /// <param name="request">The request to process.</param>
 23        /// <param name="services">The service provider exposed to value parsers and handlers.</param>
 24        /// <param name="cancellation">A token that can cancel request processing.</param>
 25        /// <returns>The <see cref="HttpResponseMessage"/> produced by the matching handlers.</returns>
 26        /// <remarks>
 27        /// Prefix routes can participate in the same pipeline as exact routes. Consecutive <c>/</c> separators in the
 28        /// request path are treated as a single separator during matching. When several handlers match, NanoRoute
 29        /// evaluates compatible matches from shorter prefixes toward more specific matches and honors
 30        /// <see cref="MatchingPrecedence"/> when both literal and parameterized segments are available at the same dept
 31        /// Once a branch is selected at a given depth, NanoRoute does not return to sibling branches later in the pipel
 32        /// </remarks>
 33        /// <exception cref="HttpRequestException">Thrown when no handler matches the request path.</exception>
 34        /// <exception cref="ArgumentNullException">
 35        /// Thrown when <paramref name="request"/> or <paramref name="services"/> is <see langword="null"/>.
 36        /// </exception>
 37        /// <exception cref="InvalidOperationException">Thrown when the request uses an unsupported HTTP method.</except
 38        /// <exception cref="OperationCanceledException">
 39        /// Thrown when the caller cancels the <paramref name="cancellation"/>.
 40        /// </exception>
 41        /// <example>
 42        /// <code>
 43        /// using HttpResponseMessage response = await pipeline.ExecuteAsync(request, services, cancellation);
 44        /// </code>
 45        /// </example>
 46        public async Task<HttpResponseMessage> ExecuteAsync(HttpRequestMessage request, IServiceProvider services, Cance
 47        {
 48            Ensure.NotNull(request);
 49            Ensure.NotNull(services);
 50
 51            if (!HttpVerb.TryParseFast(request.Method.Method, out HttpVerb verb))
 52                throw new InvalidOperationException
 53                (
 54                    string.Format(Resources.Culture, Resources.ERR_INVALID_VERB, request.Method.Method)
 55                );
 56
 57            RouterEventSource.Info.Write("RequestProcessingStarted", static request => new
 58            {
 59                RequestUri = request.RequestUri.OriginalString,
 60                Verb = request.Method.Method
 61            }, request);
 62
 63            using RouteMatchCursor cursor = new
 64            (
 65                root,
 66                verb,
 67                request.RequestUri,
 68                services,
 69                new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase),
 70                matchingPrecedence,
 71                cancellation
 72            );
 73
 74            return await CallNextHandler().ConfigureAwait(false);
 75
 76            async Task<HttpResponseMessage> CallNextHandler()
 77            {
 78                if (!await cursor.MoveNextAsync().ConfigureAwait(false))
 79                {
 80                    RouterEventSource.Info.Write("NoMatchingHandler", static request => new
 81                    {
 82                        RequestUri = request.RequestUri.OriginalString,
 83                        Verb = request.Method.Method
 84                    }, request);
 85
 86                    HttpRequestException.Throw(HttpStatusCode.NotFound, Resources.ERR_NOT_FOUND);
 87                }
 88
 89                RouterEventSource.Info.Write("MatchingHandler", static (request, cursor) => new
 90                {
 91                    RequestUri = request.RequestUri.OriginalString,
 92                    Verb = request.Method.Method,
 93                    cursor.HandlerRegistration.Pattern,
 94                    ParameterCount = cursor.Parameters.Count
 95                }, request, cursor);
 96
 97                RequestContext requestContext = new()
 98                {
 99                    Parameters = cursor.Parameters,
 100                    RemainingPath = cursor.RemainingPath,
 101                    Services = services,
 102                    Request = request,
 103                    Cancellation = cancellation
 104                };
 105
 106                return await cursor
 107                    .HandlerRegistration
 108                    .Handler(requestContext, CallNextHandler)
 109                    .ConfigureAwait(false);
 110            }
 111        }
 112    }
 113}