| | 1 | |
|
| | 2 | |
|
| | 3 | | using System.Security.Claims; |
| | 4 | | using Kestrun.Models; |
| | 5 | | using Microsoft.AspNetCore.Http.Features; |
| | 6 | |
|
| | 7 | | namespace Kestrun.Hosting; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Represents the context for a Kestrun request, including the request, response, HTTP context, and host. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="Request">The Kestrun request.</param> |
| | 13 | | /// <param name="Response">The Kestrun response.</param> |
| | 14 | | /// <param name="HttpContext">The associated HTTP context.</param> |
| 100 | 15 | | public sealed record KestrunContext(KestrunRequest Request, KestrunResponse Response, HttpContext HttpContext) |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Returns the ASP.NET Core session if the Session middleware is active; otherwise null. |
| | 19 | | /// </summary> |
| 8 | 20 | | public ISession? Session => HttpContext.Features.Get<ISessionFeature>()?.Session; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// True if Session middleware is active for this request. |
| | 24 | | /// </summary> |
| 4 | 25 | | public bool HasSession => Session is not null; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Try pattern to get session without exceptions. |
| | 29 | | /// </summary> |
| | 30 | | public bool TryGetSession(out ISession? session) |
| | 31 | | { |
| 2 | 32 | | session = Session; |
| 2 | 33 | | return session is not null; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the cancellation token that is triggered when the HTTP request is aborted. |
| | 38 | | /// </summary> |
| 1 | 39 | | public CancellationToken Ct => HttpContext.RequestAborted; |
| | 40 | | /// <summary> |
| | 41 | | /// Gets the collection of key/value pairs associated with the current HTTP context. |
| | 42 | | /// </summary> |
| 3 | 43 | | public IDictionary<object, object?> Items => HttpContext.Items; |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets the user associated with the current HTTP context. |
| | 47 | | /// </summary> |
| 2 | 48 | | public ClaimsPrincipal User => HttpContext.User; |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Returns a string representation of the KestrunContext, including path, user, and session status. |
| | 52 | | /// </summary> |
| | 53 | | public override string ToString() |
| 2 | 54 | | => $"KestrunContext{{ Path={HttpContext.Request.Path}, User={User?.Identity?.Name ?? "<anon>"}, HasSession={HasS |
| | 55 | | } |