aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Essentials
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Plugins.Essentials')
-rw-r--r--lib/Plugins.Essentials/src/Extensions/ConnectionInfoExtensions.cs32
-rw-r--r--lib/Plugins.Essentials/src/Extensions/HttpCookie.cs53
2 files changed, 83 insertions, 2 deletions
diff --git a/lib/Plugins.Essentials/src/Extensions/ConnectionInfoExtensions.cs b/lib/Plugins.Essentials/src/Extensions/ConnectionInfoExtensions.cs
index ba01132..a91f196 100644
--- a/lib/Plugins.Essentials/src/Extensions/ConnectionInfoExtensions.cs
+++ b/lib/Plugins.Essentials/src/Extensions/ConnectionInfoExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2022 Vaughn Nugent
+* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Essentials
@@ -23,8 +23,8 @@
*/
using System;
-using System.Diagnostics.CodeAnalysis;
using System.Net;
+using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using VNLib.Net.Http;
@@ -33,6 +33,7 @@ using VNLib.Net.Http;
namespace VNLib.Plugins.Essentials.Extensions
{
+
/// <summary>
/// Provides <see cref="ConnectionInfo"/> extension methods
/// for common use cases
@@ -234,6 +235,33 @@ namespace VNLib.Plugins.Essentials.Extensions
server.SetCookie(name, value, domain, path, expires, sameSite, httpOnly, secure);
}
+
+ /// <summary>
+ /// Sets a cookie with an infinite (session life-span)
+ /// </summary>
+ /// <param name="server"></param>
+ /// <param name="cookie">The cookie to set for the server</param>
+ /// <exception cref="ArgumentException"></exception>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void SetCookie(this IConnectionInfo server, in HttpCookie cookie)
+ {
+ //Cookie name is required
+ if(string.IsNullOrWhiteSpace(cookie.Name))
+ {
+ throw new ArgumentException("A nonn-null cookie name is required");
+ }
+
+ //Set the cookie
+ server.SetCookie(cookie.Name,
+ cookie.Value,
+ cookie.Domain,
+ cookie.Path,
+ cookie.ValidFor,
+ cookie.SameSite,
+ cookie.HttpOnly,
+ cookie.Secure);
+ }
+
/// <summary>
/// Is the current connection a "browser" ?
/// </summary>
diff --git a/lib/Plugins.Essentials/src/Extensions/HttpCookie.cs b/lib/Plugins.Essentials/src/Extensions/HttpCookie.cs
new file mode 100644
index 0000000..d7f73a3
--- /dev/null
+++ b/lib/Plugins.Essentials/src/Extensions/HttpCookie.cs
@@ -0,0 +1,53 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: HttpCookie.cs
+*
+* HttpCookie.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;
+
+using VNLib.Net.Http;
+
+#nullable enable
+
+namespace VNLib.Plugins.Essentials.Extensions
+{
+ /// <summary>
+ /// A structure for defining an HTTP cookie
+ /// </summary>
+ /// <param name="Name">The cookie name</param>
+ /// <param name="Value">The cookie value</param>
+ public readonly record struct HttpCookie (string Name, string Value)
+ {
+ public readonly TimeSpan ValidFor { get; init; } = TimeSpan.MaxValue;
+ public readonly string Domain { get; init; } = "";
+ public readonly string Path { get; init; } = "/";
+ public readonly CookieSameSite SameSite { get; init; } = CookieSameSite.None;
+ public readonly bool HttpOnly { get; init; } = false;
+ public readonly bool Secure { get; init; } = false;
+
+ /// <summary>
+ /// Configures the default <see cref="HttpCookie"/>
+ /// </summary>
+ public HttpCookie():this(string.Empty, string.Empty)
+ { }
+ }
+} \ No newline at end of file