private TResolver GetResolver(RouteMatcher matcher) { var resolver = _resolvers.FirstOrDefault(r => r.Matcher.Equals(matcher)); if (resolver != null) { return(resolver); } resolver = CreateResolver(matcher); _resolvers.Add(resolver ?? throw SelfCheck.Failure($"{nameof(CreateResolver)} returned null.")); return(resolver); }
/// <summary> /// Associates all requests matching a route to a synchronous handler. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> public static RoutingModule OnAny(this RoutingModule @this, RouteMatcher matcher, SyncRouteHandlerCallback handler) { @this.Add(HttpVerb.Any, matcher, handler); return(@this); }
/// <summary> /// Adds a synchronous handler to a <see cref="RoutingModule"/>. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="verb">A <see cref="HttpVerb"/> constant representing the HTTP method /// to associate with <paramref name="handler"/>, or <see cref="HttpVerb.Any"/> /// if <paramref name="handler"/> can handle all HTTP methods.</param> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <seealso cref="RoutingModule.Add(HttpVerb,RouteMatcher,SyncRouteHandlerCallback)"/> public static RoutingModule Handle(this RoutingModule @this, HttpVerb verb, RouteMatcher matcher, SyncRouteHandlerCallback handler) { @this.Add(verb, matcher, handler); return(@this); }
/// <summary> /// Associates <c>OPTIONS</c> requests matching a route to a handler. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> public static RoutingModule OnOptions(this RoutingModule @this, RouteMatcher matcher, RouteHandlerCallback handler) { @this.Add(HttpVerb.Options, matcher, handler); return(@this); }
/// <summary> /// Associates <c>OPTIONS</c> requests matching a route to a handler. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="route">The route to match URL paths against.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="route"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="FormatException"><paramref name="route"/> is not a valid route.</exception> public static RoutingModule OnOptions(this RoutingModule @this, string route, RouteHandlerCallback handler) { @this.Add(HttpVerb.Options, RouteMatcher.Parse(route, false), handler); return(@this); }
/// <summary> /// Associates <c>HEAD</c> requests matching a route to a synchronous handler. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void OnHead(RouteMatcher matcher, SyncRouteHandlerCallback handler) => _resolvers.Add(HttpVerbs.Head, matcher, handler);
/// <summary> /// Adds a handler to a <see cref="RoutingModule"/>. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="verb">A <see cref="HttpVerbs"/> constant representing the HTTP method /// to associate with <paramref name="handler"/>, or <see cref="HttpVerbs.Any"/> /// if <paramref name="handler"/> can handle all HTTP methods.</param> /// <param name="route">The route to match URL paths against.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="route"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="FormatException"><paramref name="route"/> is not a valid route.</exception> /// <seealso cref="RoutingModule.Add(HttpVerbs,RouteMatcher,RouteHandlerCallback)"/> public static RoutingModule Handle(this RoutingModule @this, HttpVerbs verb, string route, RouteHandlerCallback handler) { @this.Add(verb, RouteMatcher.Parse(route, false), handler); return(@this); }
/// <summary> /// Associates some data and a route to a synchronous handler. /// </summary> /// <param name="data">Data used to determine which contexts are /// suitable to be handled by <paramref name="handler"/>.</param> /// <param name="matcher">The <see cref="RouteMatcher"/>to match URL paths against.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="InvalidOperationException">The <see cref="CreateResolver"/> method /// returned <see langword="null"/>.</exception> /// <seealso cref="ResolveAsync"/> /// <seealso cref="Add(TData,RouteMatcher,RouteHandlerCallback)"/> /// <seealso cref="RouteResolverBase{TData}.Add(TData,SyncRouteHandlerCallback)"/> public void Add(TData data, RouteMatcher matcher, SyncRouteHandlerCallback handler) { matcher = Validate.NotNull(nameof(matcher), matcher); handler = Validate.NotNull(nameof(handler), handler); GetResolver(matcher).Add(data, handler); }
/// <summary> /// Associates <c>PATCH</c> requests matching a route to a handler. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void OnPatch(RouteMatcher matcher, RouteHandlerCallback handler) => _resolvers.Add(HttpVerb.Patch, matcher, handler);
/// <summary> /// Associates <c>OPTIONS</c> requests matching a route to a synchronous handler. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void OnOptions(RouteMatcher matcher, SyncRouteHandlerCallback handler) => _resolvers.Add(HttpVerb.Options, matcher, handler);
/// <summary> /// Associates <c>DELETE</c> requests matching a route to a synchronous handler. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void OnDelete(RouteMatcher matcher, SyncRouteHandlerCallback handler) => _resolvers.Add(HttpVerb.Delete, matcher, handler);
/// <summary> /// Associates <c>POST</c> requests matching a route to a handler. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> public static RoutingModule OnPost(this RoutingModule @this, RouteMatcher matcher, RouteHandlerCallback handler) { @this.Add(HttpVerbs.Post, matcher, handler); return(@this); }
/// <summary> /// Associates all requests matching a route to a handler. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="route">The route to match URL paths against.</param> /// <param name="isBaseRoute"><see langword="true"/> if <paramref name="route"/> /// is a base route; <see langword="false"/> if <paramref name="route"/> /// is a terminal (non-base) route.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="route"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="FormatException"><paramref name="route"/> is not a valid route.</exception> public static RoutingModule OnAny(this RoutingModule @this, string route, bool isBaseRoute, RouteHandlerCallback handler) { @this.Add(HttpVerb.Any, RouteMatcher.Parse(route, isBaseRoute), handler); return(@this); }
/// <summary> /// Adds a synchronous handler to a <see cref="RoutingModule"/>. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="verb">A <see cref="HttpVerb"/> constant representing the HTTP method /// to associate with <paramref name="handler"/>, or <see cref="HttpVerb.Any"/> /// if <paramref name="handler"/> can handle all HTTP methods.</param> /// <param name="route">The route to match URL paths against.</param> /// <param name="isBaseRoute"><see langword="true"/> if <paramref name="route"/> /// is a base route; <see langword="false"/> if <paramref name="route"/> /// is a terminal (non-base) route.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="route"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="FormatException"><paramref name="route"/> is not a valid route.</exception> /// <seealso cref="RoutingModule.Add(HttpVerb,RouteMatcher,RouteHandlerCallback)"/> public static RoutingModule Handle(this RoutingModule @this, HttpVerb verb, string route, bool isBaseRoute, SyncRouteHandlerCallback handler) { @this.Add(verb, RouteMatcher.Parse(route, isBaseRoute), handler); return(@this); }
/// <summary> /// Associates <c>POST</c> requests matching a route to a handler. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void OnPost(RouteMatcher matcher, RouteHandlerCallback handler) => _resolvers.Add(HttpVerbs.Post, matcher, handler);
/// <summary> /// <para>Called by <see cref="Add(TData,RouteMatcher,RouteHandlerCallback)"/> /// and <see cref="Add(TData,RouteMatcher,SyncRouteHandlerCallback)"/> to create an instance /// of <typeparamref name="TResolver"/> that can resolve the specified route.</para> /// <para>If this method returns <see langword="null"/>, an <see cref="InvalidOperationException"/> /// is thrown by the calling method.</para> /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/>to match URL paths against.</param> /// <returns>A newly-constructed instance of <typeparamref name="TResolver"/>.</returns> protected abstract TResolver CreateResolver(RouteMatcher matcher);
/// <summary> /// Associates <c>PUT</c> requests matching a route to a synchronous handler. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void OnPut(RouteMatcher matcher, SyncRouteHandlerCallback handler) => _resolvers.Add(HttpVerb.Put, matcher, handler);
/// <summary> /// Associates a HTTP method and a route to a synchronous handler. /// </summary> /// <param name="verb">A <see cref="HttpVerb"/> constant representing the HTTP method /// to associate with <paramref name="handler"/>, or <see cref="HttpVerb.Any"/> /// if <paramref name="handler"/> can handle all HTTP methods.</param> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> protected void AddHandler(HttpVerb verb, RouteMatcher matcher, SyncRouteHandlerCallback handler) => _resolvers.Add(verb, matcher, handler);
/// <summary> /// Initializes a new instance of the <see cref="RouteAttribute"/> class. /// </summary> /// <param name="isBaseRoute"><see langword="true"/> if this attribute represents a base route; /// <see langword="false"/> (the default) if it represents a terminal (non-base) route.</param> /// <param name="verb">The verb.</param> /// <param name="route">The route.</param> /// <exception cref="ArgumentNullException"><paramref name="route"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"> /// <para><paramref name="route"/> is empty.</para> /// <para>- or -</para> /// <para><paramref name="route"/> does not start with a slash (<c>/</c>) character.</para> /// <para>- or -</para> /// <para><paramref name="route"/> does not comply with route syntax.</para> /// </exception> /// <seealso cref="Routing.Route.IsValid"/> public RouteAttribute(HttpVerb verb, string route, bool isBaseRoute = false) { Matcher = RouteMatcher.Parse(route, isBaseRoute); Verb = verb; }
/// <summary> /// Associates a HTTP method and a route to a synchronous handler. /// </summary> /// <param name="verb">A <see cref="HttpVerb"/> constant representing the HTTP method /// to associate with <paramref name="handler"/>, or <see cref="HttpVerb.Any"/> /// if <paramref name="handler"/> can handle all HTTP methods.</param> /// <param name="matcher">The <see cref="RouteMatcher"/> used to match URL paths.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> public void Add(HttpVerb verb, RouteMatcher matcher, SyncRouteHandlerCallback handler) => AddHandler(verb, matcher, handler);
/// <summary> /// Associates all requests matching a route to a synchronous handler. /// </summary> /// <param name="this">The <see cref="RoutingModule"/> on which this method is called.</param> /// <param name="route">The route to match URL paths against.</param> /// <param name="handler">A callback used to handle matching contexts.</param> /// <returns><paramref name="this"/> with the handler added.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"> /// <para><paramref name="route"/> is <see langword="null"/>.</para> /// <para>- or -</para> /// <para><paramref name="handler"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="FormatException"><paramref name="route"/> is not a valid route.</exception> public static RoutingModule OnAny(this RoutingModule @this, string route, SyncRouteHandlerCallback handler) { @this.Add(HttpVerbs.Any, RouteMatcher.Parse(route, false), handler); return(@this); }
/// <summary> /// Initializes a new instance of the <see cref="RouteVerbResolver"/> class. /// </summary> /// <param name="matcher">The <see cref="RouteMatcher"/> to match URL paths against.</param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="matcher"/> is <see langword="null"/>.</para> /// </exception> public RouteVerbResolver(RouteMatcher matcher) : base(matcher) { }