aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Essentials/src/Middleware
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-11-02 01:49:02 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-11-02 01:49:02 -0400
commit9e3dd9be0f0ec7aaef1a719f09f96425e66369df (patch)
tree59b8bd4ace8750327db80823fa1e5eccdf44bc74 /lib/Plugins.Essentials/src/Middleware
parenteafefadc4b858e5b5be481662a2b0c8e47a43bf4 (diff)
may have gottem carried away
Diffstat (limited to 'lib/Plugins.Essentials/src/Middleware')
-rw-r--r--lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs2
-rw-r--r--lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs8
-rw-r--r--lib/Plugins.Essentials/src/Middleware/MiddlewareImplAttribute.cs48
-rw-r--r--lib/Plugins.Essentials/src/Middleware/MiddlewareImplOptions.cs46
-rw-r--r--lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs24
5 files changed, 110 insertions, 18 deletions
diff --git a/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs b/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs
index 3c56866..83e6a06 100644
--- a/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs
+++ b/lib/Plugins.Essentials/src/Middleware/IHttpMiddleware.cs
@@ -24,9 +24,9 @@
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.
diff --git a/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs b/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs
index ace0c86..0a05c70 100644
--- a/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs
+++ b/lib/Plugins.Essentials/src/Middleware/IHttpMiddlewareChain.cs
@@ -43,13 +43,7 @@ namespace VNLib.Plugins.Essentials.Middleware
/// 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);
+ void Add(IHttpMiddleware middleware);
/// <summary>
/// Removes a middleware handler from the chain
diff --git a/lib/Plugins.Essentials/src/Middleware/MiddlewareImplAttribute.cs b/lib/Plugins.Essentials/src/Middleware/MiddlewareImplAttribute.cs
new file mode 100644
index 0000000..d5a66a4
--- /dev/null
+++ b/lib/Plugins.Essentials/src/Middleware/MiddlewareImplAttribute.cs
@@ -0,0 +1,48 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: MiddlewareImplAttribute.cs
+*
+* MiddlewareImplAttribute.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;
+
+namespace VNLib.Plugins.Essentials.Middleware
+{
+ /// <summary>
+ /// Specifies optional implementation flags for a middleware instance
+ /// that loaders may use during soriting of the middleware chain
+ /// </summary>
+ [AttributeUsage(AttributeTargets.Class)]
+ public sealed class MiddlewareImplAttribute : Attribute
+ {
+ /// <summary>
+ /// The option flags for a middleware instance
+ /// </summary>
+ public MiddlewareImplOptions ImplOptions { get; }
+
+ /// <summary>
+ /// Creates a new <see cref="MiddlewareImplAttribute"/> instance
+ /// with the specified <see cref="MiddlewareImplOptions"/>
+ /// </summary>
+ /// <param name="implOptions">Implementation option flags</param>
+ public MiddlewareImplAttribute(MiddlewareImplOptions implOptions) => ImplOptions = implOptions;
+ }
+}
diff --git a/lib/Plugins.Essentials/src/Middleware/MiddlewareImplOptions.cs b/lib/Plugins.Essentials/src/Middleware/MiddlewareImplOptions.cs
new file mode 100644
index 0000000..1e325e3
--- /dev/null
+++ b/lib/Plugins.Essentials/src/Middleware/MiddlewareImplOptions.cs
@@ -0,0 +1,46 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: MiddlewareImplOptions.cs
+*
+* MiddlewareImplOptions.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;
+
+
+namespace VNLib.Plugins.Essentials.Middleware
+{
+ /// <summary>
+ /// Implementation flags for midleware implementations
+ /// </summary>
+ [Flags]
+ public enum MiddlewareImplOptions
+ {
+ /// <summary>
+ /// No flags
+ /// </summary>
+ None = 0x00,
+ /// <summary>
+ /// Prioritizes a middleware instance in the chain because
+ /// it is required for security purposes
+ /// </summary>
+ SecurityCritical = 0x01
+ }
+}
diff --git a/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs b/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs
index 1e1db22..5d0c472 100644
--- a/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs
+++ b/lib/Plugins.Essentials/src/Middleware/SemiConistentMiddlewareChain.cs
@@ -22,6 +22,7 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
+using System.Reflection;
using System.Collections.Generic;
namespace VNLib.Plugins.Essentials.Middleware
@@ -36,20 +37,23 @@ namespace VNLib.Plugins.Essentials.Middleware
private LinkedList<IHttpMiddleware> _middlewares = new();
///<inheritdoc/>
- public void AddFirst(IHttpMiddleware middleware)
+ public void Add(IHttpMiddleware middleware)
{
- lock (_middlewares)
- {
- _middlewares.AddFirst(middleware);
- }
- }
+ //Get security critical flag
+ bool isSecCritical = middleware.GetType().GetCustomAttribute<MiddlewareImplAttribute>()
+ ?.ImplOptions.HasFlag(MiddlewareImplOptions.SecurityCritical) ?? false;
- ///<inheritdoc/>
- public void AddLast(IHttpMiddleware middleware)
- {
lock (_middlewares)
{
- _middlewares.AddLast(middleware);
+ //Always add security critical middleware to the front of the chain
+ if (isSecCritical)
+ {
+ _middlewares.AddFirst(middleware);
+ }
+ else
+ {
+ _middlewares.AddLast(middleware);
+ }
}
}