| | | 1 | | /******************************************************************************** |
| | | 2 | | * RouterBase.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Net.Http; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace NanoRoute |
| | | 12 | | { |
| | | 13 | | using Internals; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides shared configuration storage and request-pipeline execution for transport-specific router adapters. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="TConfig">The immutable router configuration type used by the adapter.</typeparam> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// Derive from this type when a custom transport needs to translate its native request model into an |
| | | 21 | | /// <see cref="HttpRequestMessage"/> while reusing NanoRoute's matching, value parsing, middleware, and handler |
| | | 22 | | /// pipeline. The constructor captures an immutable route snapshot, and derived adapters can call |
| | | 23 | | /// <see cref="Route(HttpRequestMessage, IServiceProvider, CancellationToken)"/> from their public entry point. |
| | | 24 | | /// </remarks> |
| | | 25 | | /// <example> |
| | | 26 | | /// <code> |
| | | 27 | | /// public sealed class MyRouter : RouterBase<RouterConfig> |
| | | 28 | | /// { |
| | | 29 | | /// private MyRouter(RouteScopeBuilder routes, RouterConfig config) |
| | | 30 | | /// : base(routes, config) |
| | | 31 | | /// { |
| | | 32 | | /// } |
| | | 33 | | /// |
| | | 34 | | /// public async Task<HttpResponseMessage> Route(MyRequest request, IServiceProvider services, Cancellatio |
| | | 35 | | /// { |
| | | 36 | | /// using HttpRequestMessage httpRequest = request.ToHttpRequestMessage(); |
| | | 37 | | /// return await Route(httpRequest, services, cancellation).ConfigureAwait(false); |
| | | 38 | | /// } |
| | | 39 | | /// } |
| | | 40 | | /// </code> |
| | | 41 | | /// </example> |
| | | 42 | | public abstract class RouterBase<TConfig> where TConfig : RouterConfig |
| | | 43 | | { |
| | | 44 | | private readonly RequestPipeline _pipeline; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Initializes a router from the current route scope and configuration. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="routes">The route scope whose registered routes are captured by the router.</param> |
| | | 50 | | /// <param name="config">The configuration assigned to the router.</param> |
| | | 51 | | /// <exception cref="ArgumentNullException"> |
| | | 52 | | /// Thrown when <paramref name="routes"/> or <paramref name="config"/> is <see langword="null"/>. |
| | | 53 | | /// </exception> |
| | 2 | 54 | | protected RouterBase(RouteScopeBuilder routes, TConfig config) |
| | 2 | 55 | | { |
| | 2 | 56 | | Ensure.NotNull(routes); |
| | 2 | 57 | | Ensure.NotNull(config); |
| | | 58 | | |
| | 2 | 59 | | Config = config; |
| | 2 | 60 | | _pipeline = new RequestPipeline(routes.CreateSnapshot(), config.MatchingPrecedence); |
| | 2 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the configuration assigned to this router instance. |
| | | 65 | | /// </summary> |
| | | 66 | | public TConfig Config { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Routes a single HTTP request message and returns the produced response. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="request">The request to process.</param> |
| | | 72 | | /// <param name="services">The service provider exposed to handlers through <see cref="RequestContext.Services"/ |
| | | 73 | | /// <param name="cancellation">A token that can cancel request processing.</param> |
| | | 74 | | /// <returns>The response produced by the matching handlers.</returns> |
| | | 75 | | /// <exception cref="ArgumentNullException"> |
| | | 76 | | /// Thrown when <paramref name="request"/> or <paramref name="services"/> is <see langword="null"/>. |
| | | 77 | | /// </exception> |
| | | 78 | | /// <exception cref="InvalidOperationException">Thrown when the request uses an unsupported HTTP method.</except |
| | | 79 | | /// <exception cref="HttpRequestException"> |
| | | 80 | | /// Thrown when no handler matches the request path or a matched handler signals an HTTP failure that is not |
| | | 81 | | /// translated by middleware. |
| | | 82 | | /// </exception> |
| | | 83 | | /// <exception cref="OperationCanceledException"> |
| | | 84 | | /// Thrown when the caller cancels <paramref name="cancellation"/>. |
| | | 85 | | /// </exception> |
| | | 86 | | /// <remarks> |
| | | 87 | | /// The returned <see cref="HttpResponseMessage"/> is not disposed by the router. Callers should dispose it |
| | | 88 | | /// after reading the response body. |
| | | 89 | | /// </remarks> |
| | | 90 | | protected Task<HttpResponseMessage> Route(HttpRequestMessage request, IServiceProvider services, CancellationTok |
| | 2 | 91 | | { |
| | 2 | 92 | | Ensure.NotNull(request); |
| | 2 | 93 | | Ensure.NotNull(services); |
| | | 94 | | |
| | 2 | 95 | | return _pipeline.ExecuteAsync(request, services, cancellation); |
| | 2 | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |