| | 1 | | using System.Text; |
| | 2 | |
|
| | 3 | | namespace Kestrun.Models; |
| | 4 | |
|
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Represents a request model for Kestrun, containing HTTP method, path, query, headers, body, authorization, cookies, |
| | 8 | | /// </summary> |
| | 9 | | public partial class KestrunRequest |
| | 10 | | { |
| 79 | 11 | | private KestrunRequest(HttpRequest request, Dictionary<string, string>? formDict, string body) |
| | 12 | | { |
| 79 | 13 | | Request = request; |
| 79 | 14 | | Query = request.Query |
| 99 | 15 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| 79 | 16 | | Headers = request.Headers |
| 149 | 17 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| 79 | 18 | | Cookies = request.Cookies |
| 83 | 19 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| 79 | 20 | | Form = formDict; |
| 79 | 21 | | Body = body; |
| 79 | 22 | | RouteValues = request.RouteValues |
| 81 | 23 | | .ToDictionary(x => x.Key, x => x.Value?.ToString() ?? string.Empty); |
| 79 | 24 | | } |
| | 25 | |
|
| 125 | 26 | | private HttpRequest Request { get; init; } |
| | 27 | | /// <summary> |
| | 28 | | /// Gets the HTTP method for the request (e.g., GET, POST). |
| | 29 | | /// </summary> |
| 14 | 30 | | public string Method => Request.Method; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Gets the host header value for the request. |
| | 34 | | /// </summary> |
| 1 | 35 | | public string Host => Request.Host.ToUriComponent(); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets the query string for the request (e.g., "?id=123"). |
| | 39 | | /// </summary> |
| 1 | 40 | | public string QueryString => Request.QueryString.ToUriComponent(); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets the content type of the request (e.g., "application/json"). |
| | 44 | | /// </summary> |
| 1 | 45 | | public string? ContentType => Request.ContentType; |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Gets the protocol used for the request (e.g., "HTTP/1.1"). |
| | 49 | | /// </summary> |
| 1 | 50 | | public string Protocol => Request.Protocol; |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Gets a value indicating whether the request is made over HTTPS. |
| | 54 | | /// </summary> |
| 1 | 55 | | public bool IsHttps => Request.IsHttps; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Gets the content length of the request, if available. |
| | 59 | | /// </summary> |
| 2 | 60 | | public long? ContentLength => Request.ContentLength; |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets a value indicating whether the request has a form content type. |
| | 64 | | /// </summary> |
| 1 | 65 | | public bool HasFormContentType => Request.HasFormContentType; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Gets the request scheme (e.g., "http", "https"). |
| | 69 | | /// </summary> |
| 1 | 70 | | public string Scheme => Request.Scheme; |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets the request path (e.g., "/api/resource"). |
| | 74 | | /// </summary> |
| 15 | 75 | | public string Path => Request.Path.ToString(); |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Gets the base path for the request (e.g., "/api"). |
| | 79 | | /// </summary> |
| 1 | 80 | | public string PathBase => Request.PathBase.ToString(); |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Gets the query parameters for the request as a dictionary of key-value pairs. |
| | 84 | | /// </summary> |
| 85 | 85 | | public Dictionary<string, string> Query { get; init; } |
| | 86 | |
|
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets the headers for the request as a dictionary of key-value pairs. |
| | 90 | | /// </summary> |
| 167 | 91 | | public Dictionary<string, string> Headers { get; init; } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Gets the body content of the request as a string. |
| | 95 | | /// </summary> |
| 85 | 96 | | public string Body { get; init; } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Gets the authorization header value for the request, if present. |
| | 100 | | /// </summary> |
| 7 | 101 | | public string? Authorization => Request.Headers.Authorization.ToString(); |
| | 102 | | /// <summary> |
| | 103 | | /// Gets the cookies for the request as an <see cref="IRequestCookieCollection"/>, if present. |
| | 104 | | /// </summary> |
| 83 | 105 | | public Dictionary<string, string> Cookies { get; init; } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Gets the form data for the request as a dictionary of key-value pairs, if present. |
| | 109 | | /// </summary> |
| 84 | 110 | | public Dictionary<string, string>? Form { get; init; } |
| | 111 | |
|
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Gets the route values for the request as a <see cref="RouteValueDictionary"/>, if present. |
| | 115 | | /// </summary> |
| 80 | 116 | | public Dictionary<string, string> RouteValues { get; init; } |
| | 117 | |
|
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Creates a new <see cref="KestrunRequest"/> instance from the specified <see cref="HttpContext"/>. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="context">The HTTP context containing the request information.</param> |
| | 123 | | /// <returns>A task that represents the asynchronous operation. The task result contains the constructed <see cref=" |
| | 124 | | public static async Task<KestrunRequest> NewRequest(HttpContext context) |
| | 125 | | { |
| | 126 | | // ① Allow the body to be read multiple times |
| 79 | 127 | | context.Request.EnableBuffering(); |
| | 128 | |
|
| | 129 | | // ② Read the raw body into a string, then rewind |
| | 130 | | string body; |
| 79 | 131 | | using (var reader = new StreamReader( |
| 79 | 132 | | context.Request.Body, |
| 79 | 133 | | encoding: Encoding.UTF8, |
| 79 | 134 | | detectEncodingFromByteOrderMarks: false, |
| 79 | 135 | | leaveOpen: true)) |
| | 136 | | { |
| 79 | 137 | | body = await reader.ReadToEndAsync(); |
| 79 | 138 | | context.Request.Body.Position = 0; |
| 79 | 139 | | } |
| | 140 | |
|
| | 141 | | // ③ If it's a form, read it safely |
| 79 | 142 | | var formDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 79 | 143 | | if (context.Request.HasFormContentType) |
| | 144 | | { |
| 1 | 145 | | var form = await context.Request.ReadFormAsync(); |
| 6 | 146 | | foreach (var kv in form) |
| | 147 | | { |
| 2 | 148 | | formDict[kv.Key] = kv.Value.ToString(); |
| | 149 | | } |
| | 150 | | } |
| | 151 | |
|
| 79 | 152 | | return new KestrunRequest(request: context.Request, formDict: formDict, body: body); |
| 79 | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Synchronous helper for tests and simple call sites that prefer not to use async/await. |
| | 157 | | /// Avoid in ASP.NET request pipelines; intended for unit tests only. |
| | 158 | | /// </summary> |
| 49 | 159 | | public static KestrunRequest NewRequestSync(HttpContext context) => NewRequest(context).GetAwaiter().GetResult(); |
| | 160 | | } |