From dab71d5597fdfbe71f6ac310a240835716e952a5 Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 6 Mar 2023 01:53:26 -0500 Subject: FBM and caching non-breaking updates --- .../src/Extensions/ConnectionInfoExtensions.cs | 32 ++++++++++++- .../src/Extensions/HttpCookie.cs | 53 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 lib/Plugins.Essentials/src/Extensions/HttpCookie.cs (limited to 'lib/Plugins.Essentials') 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 { + /// /// Provides 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); } + + /// + /// Sets a cookie with an infinite (session life-span) + /// + /// + /// The cookie to set for the server + /// + [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); + } + /// /// Is the current connection a "browser" ? /// 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 +{ + /// + /// A structure for defining an HTTP cookie + /// + /// The cookie name + /// The cookie value + 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; + + /// + /// Configures the default + /// + public HttpCookie():this(string.Empty, string.Empty) + { } + } +} \ No newline at end of file -- cgit