diff options
author | vnugent <public@vaughnnugent.com> | 2023-04-08 16:09:51 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-04-08 16:09:51 -0400 |
commit | 228e1b59a2fc24c2e4ce57aa8171a13a1a18c199 (patch) | |
tree | 4088332a1670e3fc718694da73fdc22a83c06945 /lib/Plugins.Essentials | |
parent | cf45636b6216d1b2beaec83bb129a1927f24f608 (diff) |
Minor updates and API changes
Diffstat (limited to 'lib/Plugins.Essentials')
4 files changed, 45 insertions, 22 deletions
diff --git a/lib/Plugins.Essentials/src/Accounts/AccountUtils.cs b/lib/Plugins.Essentials/src/Accounts/AccountUtils.cs index 13cff7b..33da10e 100644 --- a/lib/Plugins.Essentials/src/Accounts/AccountUtils.cs +++ b/lib/Plugins.Essentials/src/Accounts/AccountUtils.cs @@ -53,7 +53,7 @@ namespace VNLib.Plugins.Essentials.Accounts /// <summary> /// The size in bytes of the random passwords generated when invoking the <see cref="SetRandomPasswordAsync(PasswordHashing, IUserManager, IUser, int)"/> /// </summary> - public const int RANDOM_PASS_SIZE = 128; + public const int RANDOM_PASS_SIZE = 240; /// <summary> /// The origin string of a local user account. This value will be set if an @@ -409,8 +409,7 @@ namespace VNLib.Plugins.Essentials.Accounts } /// <summary> - /// Attempts to encrypt the supplied data with session stored client information. The user must - /// be authorized + /// Attempts to encrypt client data against the supplied security information instance, without a secured session. /// </summary> /// <param name="entity"></param> /// <param name="secInfo">Used for unauthorized connections to encrypt client data based on client security info</param> diff --git a/lib/Plugins.Essentials/src/Endpoints/ResourceEndpointBase.cs b/lib/Plugins.Essentials/src/Endpoints/ResourceEndpointBase.cs index 7411789..dfb94f7 100644 --- a/lib/Plugins.Essentials/src/Endpoints/ResourceEndpointBase.cs +++ b/lib/Plugins.Essentials/src/Endpoints/ResourceEndpointBase.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials @@ -54,32 +54,26 @@ namespace VNLib.Plugins.Essentials.Endpoints try { ERRNO preProc = PreProccess(entity); + if (preProc == ERRNO.E_FAIL) { return VfReturnType.Forbidden; } + //Entity was responded to by the pre-processor if (preProc < 0) { return VfReturnType.VirtualSkip; } + //If websockets are quested allow them to be processed in a logged-in/secure context if (entity.Server.IsWebSocketRequest) { return await WebsocketRequestedAsync(entity); } - ValueTask<VfReturnType> op = entity.Server.Method switch - { - //Get request to get account - HttpMethod.GET => GetAsync(entity), - HttpMethod.POST => PostAsync(entity), - HttpMethod.DELETE => DeleteAsync(entity), - HttpMethod.PUT => PutAsync(entity), - HttpMethod.PATCH => PatchAsync(entity), - HttpMethod.OPTIONS => OptionsAsync(entity), - _ => AlternateMethodAsync(entity, entity.Server.Method), - }; - return await op; + + //Call process method + return await OnProcessAsync(entity); } catch (InvalidJsonRequestException ije) { @@ -178,6 +172,29 @@ namespace VNLib.Plugins.Essentials.Endpoints } /// <summary> + /// Called by the process method to process a connection after it has been pre-processed. + /// By default, this method simply selects the request method type and invokes the desired + /// handler + /// </summary> + /// <param name="entity">The entity to process</param> + /// <returns>A value task that completes when the processing has completed</returns> + protected virtual ValueTask<VfReturnType> OnProcessAsync(HttpEntity entity) + { + ValueTask<VfReturnType> op = entity.Server.Method switch + { + //Get request to get account + HttpMethod.GET => GetAsync(entity), + HttpMethod.POST => PostAsync(entity), + HttpMethod.DELETE => DeleteAsync(entity), + HttpMethod.PUT => PutAsync(entity), + HttpMethod.PATCH => PatchAsync(entity), + HttpMethod.OPTIONS => OptionsAsync(entity), + _ => AlternateMethodAsync(entity, entity.Server.Method), + }; + return op; + } + + /// <summary> /// This method gets invoked when an incoming POST request to the endpoint has been requested. /// </summary> /// <param name="entity">The entity to be processed</param> diff --git a/lib/Plugins.Essentials/src/Endpoints/VirtualEndpoint.cs b/lib/Plugins.Essentials/src/Endpoints/VirtualEndpoint.cs index 5beb4b9..4ffe288 100644 --- a/lib/Plugins.Essentials/src/Endpoints/VirtualEndpoint.cs +++ b/lib/Plugins.Essentials/src/Endpoints/VirtualEndpoint.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials @@ -42,7 +42,7 @@ namespace VNLib.Plugins.Essentials.Endpoints /// <summary> /// An <see cref="ILogProvider"/> to write logs to /// </summary> - protected ILogProvider Log { get; private set; } + protected ILogProvider Log { get; set; } /// <summary> /// Sets the log and path and checks the values @@ -61,6 +61,7 @@ namespace VNLib.Plugins.Essentials.Endpoints //Store log Log = log ?? throw new ArgumentNullException(nameof(log)); } + ///<inheritdoc/> public abstract ValueTask<VfReturnType> Process(T entity); } diff --git a/lib/Plugins.Essentials/src/EventProcessor.cs b/lib/Plugins.Essentials/src/EventProcessor.cs index 820af30..a207e35 100644 --- a/lib/Plugins.Essentials/src/EventProcessor.cs +++ b/lib/Plugins.Essentials/src/EventProcessor.cs @@ -503,15 +503,19 @@ namespace VNLib.Plugins.Essentials default: break; } + //If the file was not set or the request method is not a GET (or HEAD), return not-found if (filename == null || (entity.Server.Method & (HttpMethod.GET | HttpMethod.HEAD)) == 0) { CloseWithError(HttpStatusCode.NotFound, entity); return; } + DateTime fileLastModified = File.GetLastWriteTimeUtc(filename); + //See if the last modifed header was set DateTimeOffset? ifModifedSince = entity.Server.LastModified(); + //If the header was set, check the date, if the file has been modified since, continue sending the file if (ifModifedSince.HasValue && ifModifedSince.Value > fileLastModified) { @@ -519,20 +523,24 @@ namespace VNLib.Plugins.Essentials entity.CloseResponse(HttpStatusCode.NotModified); return; } + //Get the content type of he file ContentType fileType = HttpHelpers.GetContentTypeFromFile(filename); + //Make sure the client accepts the content type if (entity.Server.Accepts(fileType)) { //set last modified time as the files last write time entity.Server.LastModified(fileLastModified); + //try to open the selected file for reading and allow sharing FileStream fs = new (filename, FileMode.Open, FileAccess.Read, FileShare.Read); + //Check for range if (entity.Server.Range != null && entity.Server.Range.Item1 > 0) { //Seek the stream to the specified position - fs.Position = entity.Server.Range.Item1; + fs.Seek(entity.Server.Range.Item1, SeekOrigin.Begin); entity.CloseResponse(HttpStatusCode.PartialContent, fileType, fs); } else @@ -540,13 +548,11 @@ namespace VNLib.Plugins.Essentials //send the whole file entity.CloseResponse(HttpStatusCode.OK, fileType, fs); } - return; } else { //Unacceptable CloseWithError(HttpStatusCode.NotAcceptable, entity); - return; } } catch (IOException ioe) @@ -557,7 +563,7 @@ namespace VNLib.Plugins.Essentials } catch (Exception ex) { - Log.Information(ex, "Unhandled exception during file opening."); + Log.Error(ex, "Unhandled exception during file opening."); //Invoke the root error handler CloseWithError(HttpStatusCode.InternalServerError, entity); return; |