diff options
author | vnugent <public@vaughnnugent.com> | 2023-08-19 23:44:53 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-08-19 23:44:53 -0400 |
commit | f541b853c738a076d2a85ef6b269c1b140353b85 (patch) | |
tree | ae52056036d22963613903181029c3ff9bed0f5a /lib/Plugins.Essentials/src/Middleware | |
parent | 3f5eb61fc7166674a5424d5f8e8c23a775c27614 (diff) |
Essentials middleware first addition, websocket feature and generics
Diffstat (limited to 'lib/Plugins.Essentials/src/Middleware')
4 files changed, 239 insertions, 0 deletions
diff --git a/lib/Plugins.Essentials/src/Middleware/HttpMiddlewareResult.cs b/lib/Plugins.Essentials/src/Middleware/HttpMiddlewareResult.cs new file mode 100644 index 0000000..6054a6e --- /dev/null +++ b/lib/Plugins.Essentials/src/Middleware/HttpMiddlewareResult.cs @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Essentials +* File: HttpMiddlewareResult.cs +* +* HttpMiddlewareResult.cs is part of VNLib.Plugins.Essentials which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Essentials is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Plugins.Essentials is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +namespace VNLib.Plugins.Essentials.Middleware +{ + /// <summary> + /// The result of a <see cref="IHttpMiddleware"/> process. + /// </summary> + public enum HttpMiddlewareResult + { + /// <summary> + /// The request has not been completed and should continue to be processed. + /// </summary> + Continue, + + /// <summary> + /// The request has been handled and no further processing should occur. + /// </summary> + Complete + } +} diff --git a/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs b/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs new file mode 100644 index 0000000..a5a3949 --- /dev/null +++ b/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Essentials +* File: IHttpMiddleware.cs +* +* IHttpMiddleware.cs is part of VNLib.Plugins.Essentials which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Essentials is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Plugins.Essentials is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using System.Threading.Tasks; + + +namespace VNLib.Plugins.Essentials.Middleware +{ + /// <summary> + /// Represents a low level intermediate request processor with high privilages, meant to add + /// functionality to entity processing. + /// </summary> + public interface IHttpMiddleware + { + /// <summary> + /// Processes the <see cref="HttpEntity"/> and returns a <see cref="HttpMiddlewareResult"/> + /// indicating whether the request should continue to be processed. + /// </summary> + /// <param name="entity">The entity to process</param> + /// <returns>The result of the operation</returns> + ValueTask<HttpMiddlewareResult> ProcessAsync(HttpEntity entity); + } +} diff --git a/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs b/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs new file mode 100644 index 0000000..54da6c1 --- /dev/null +++ b/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs @@ -0,0 +1,67 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Essentials +* File: IHttpMiddlewareChain.cs +* +* IHttpMiddlewareChain.cs is part of VNLib.Plugins.Essentials which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Essentials is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Plugins.Essentials is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using System.Collections.Generic; + +#nullable enable + +namespace VNLib.Plugins.Essentials.Middleware +{ + /// <summary> + /// Represents a chain of <see cref="IHttpMiddleware"/> instances that + /// will be called by an <see cref="EventProcessor"/> during + /// entity processing. + /// </summary> + public interface IHttpMiddlewareChain + { + /// <summary> + /// Gets the current head of the middleware chain + /// </summary> + /// <returns>A <see cref="LinkedListNode{T}"/> that points to the head of the current chain</returns> + LinkedListNode<IHttpMiddleware>? GetCurrentHead(); + + /// <summary> + /// Adds a middleware handler to the end of the chain + /// </summary> + /// <param name="middleware">The middleware processor to add</param> + void AddLast(IHttpMiddleware middleware); + + /// <summary> + /// Adds a middleware handler to the beginning of the chain + /// </summary> + /// <param name="middleware">The middleware processor to add</param> + void AddFirst(IHttpMiddleware middleware); + + /// <summary> + /// Removes a middleware handler from the chain + /// </summary> + /// <param name="middleware">The middleware instance to remove</param> + void RemoveMiddleware(IHttpMiddleware middleware); + + /// <summary> + /// Removes all middleware handlers from the chain + /// </summary> + void Clear(); + } +}
\ No newline at end of file diff --git a/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs b/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs new file mode 100644 index 0000000..197ba12 --- /dev/null +++ b/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs @@ -0,0 +1,86 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Essentials +* File: SemiConistentMiddlewareChain.cs +* +* SemiConistentMiddlewareChain.cs is part of VNLib.Plugins.Essentials which +* is part of the larger VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Essentials is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Plugins.Essentials is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using System.Collections.Generic; + +#nullable enable + +namespace VNLib.Plugins.Essentials.Middleware +{ + /// <summary> + /// A default implementation of <see cref="IHttpMiddlewareChain"/> that + /// maintains a semi-conistant chain of middleware handlers, for infrequent + /// chain updates + /// </summary> + internal sealed class SemiConistentMiddlewareChain : IHttpMiddlewareChain + { + private LinkedList<IHttpMiddleware> _middlewares = new(); + + ///<inheritdoc/> + public void AddFirst(IHttpMiddleware middleware) + { + lock (_middlewares) + { + _middlewares.AddFirst(middleware); + } + } + + ///<inheritdoc/> + public void AddLast(IHttpMiddleware middleware) + { + lock (_middlewares) + { + _middlewares.AddLast(middleware); + } + } + + ///<inheritdoc/> + public void Clear() + { + lock (_middlewares) + { + _middlewares.Clear(); + } + } + + ///<inheritdoc/> + public LinkedListNode<IHttpMiddleware>? GetCurrentHead() => _middlewares.First; + + ///<inheritdoc/> + public void RemoveMiddleware(IHttpMiddleware middleware) + { + lock (_middlewares) + { + //Clone current table + LinkedList<IHttpMiddleware> newTable = new(_middlewares); + + //Remove the middleware + newTable.Remove(middleware); + + //Replace the current table with the new one + _middlewares = newTable; + } + } + } +}
\ No newline at end of file |