| | | 1 | | using System.Text; |
| | | 2 | | using Microsoft.Net.Http.Headers; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Models; |
| | | 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 | | { |
| | 181 | 11 | | private KestrunRequest(HttpContext context, Dictionary<string, string>? formDict, string body) |
| | | 12 | | { |
| | 181 | 13 | | HttpContext = context ?? throw new ArgumentNullException(nameof(context)); |
| | 181 | 14 | | Request = context.Request; |
| | 181 | 15 | | Query = Request.Query |
| | 203 | 16 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | 181 | 17 | | Headers = Request.Headers |
| | 381 | 18 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | 181 | 19 | | Cookies = Request.Cookies |
| | 187 | 20 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | 181 | 21 | | Form = formDict; |
| | 181 | 22 | | Body = body; |
| | 181 | 23 | | RouteValues = Request.RouteValues |
| | 183 | 24 | | .ToDictionary(x => x.Key, x => x.Value?.ToString() ?? string.Empty); |
| | 181 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the <see cref="Microsoft.AspNetCore.Http.HttpContext"/> associated with the request. |
| | | 29 | | /// </summary> |
| | 356 | 30 | | public HttpContext HttpContext { get; init; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the <see cref="HttpRequest"/> associated with the request. |
| | | 34 | | /// </summary> |
| | 1296 | 35 | | public HttpRequest Request { get; init; } |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the HTTP method for the request (e.g., GET, POST). |
| | | 38 | | /// </summary> |
| | 345 | 39 | | public string Method => Request.Method; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the host header value for the request. |
| | | 43 | | /// </summary> |
| | 1 | 44 | | public HostString Host => Request.Host; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the query string for the request (e.g., "?id=123"). |
| | | 48 | | /// </summary> |
| | 1 | 49 | | public string QueryString => Request.QueryString.ToUriComponent(); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the content type of the request (e.g., "application/json"). |
| | | 53 | | /// </summary> |
| | 10 | 54 | | public string? ContentType => Request.ContentType; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the protocol used for the request (e.g., "HTTP/1.1"). |
| | | 58 | | /// </summary> |
| | 1 | 59 | | public string Protocol => Request.Protocol; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets a value indicating whether the request is made over HTTPS. |
| | | 63 | | /// </summary> |
| | 1 | 64 | | public bool IsHttps => Request.IsHttps; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the content length of the request, if available. |
| | | 68 | | /// </summary> |
| | 2 | 69 | | public long? ContentLength => Request.ContentLength; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets a value indicating whether the request has a form content type. |
| | | 73 | | /// </summary> |
| | 1 | 74 | | public bool HasFormContentType => Request.HasFormContentType; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the request scheme (e.g., "http", "https"). |
| | | 78 | | /// </summary> |
| | 1 | 79 | | public string Scheme => Request.Scheme; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the request path (e.g., "/api/resource"). |
| | | 83 | | /// </summary> |
| | 20 | 84 | | public string Path => Request.Path.ToString(); |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the base path for the request (e.g., "/api"). |
| | | 88 | | /// </summary> |
| | 1 | 89 | | public string PathBase => Request.PathBase.ToString(); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets the query parameters for the request as a dictionary of key-value pairs. |
| | | 93 | | /// </summary> |
| | 189 | 94 | | public Dictionary<string, string> Query { get; init; } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Gets the headers for the request as a dictionary of key-value pairs. |
| | | 98 | | /// </summary> |
| | 387 | 99 | | public Dictionary<string, string> Headers { get; init; } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Gets the body content of the request as a string. |
| | | 103 | | /// </summary> |
| | 197 | 104 | | public string Body { get; init; } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets the authorization header value for the request, if present. |
| | | 108 | | /// </summary> |
| | 7 | 109 | | public string? Authorization => Request.Headers.Authorization.ToString(); |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets the cookies for the request as an <see cref="IRequestCookieCollection"/>, if present. |
| | | 112 | | /// </summary> |
| | 185 | 113 | | public Dictionary<string, string> Cookies { get; init; } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the form data for the request as a dictionary of key-value pairs, if present. |
| | | 117 | | /// </summary> |
| | 206 | 118 | | public Dictionary<string, string>? Form { get; init; } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Gets the route values for the request as a <see cref="RouteValueDictionary"/>, if present. |
| | | 122 | | /// </summary> |
| | 182 | 123 | | public Dictionary<string, string> RouteValues { get; init; } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Creates a new <see cref="KestrunRequest"/> instance from the specified <see cref="Microsoft.AspNetCore.Http.Http |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="context">The HTTP context containing the request information.</param> |
| | | 129 | | /// <returns>A task that represents the asynchronous operation. The task result contains the constructed <see cref=" |
| | | 130 | | public static async Task<KestrunRequest> NewRequest(HttpContext context) |
| | | 131 | | { |
| | | 132 | | // If request decompression runs later in the pipeline (after the PowerShell middleware), |
| | | 133 | | // the body is still encoded here. Avoid reading/parsing forms at this stage in that case. |
| | | 134 | | // Also avoid enabling buffering up-front for encoded bodies; the decompression middleware |
| | | 135 | | // expects to read the raw encoded stream from position 0. |
| | 181 | 136 | | var contentEncoding = context.Request.Headers[HeaderNames.ContentEncoding].ToString(); |
| | | 137 | | |
| | | 138 | | // Allow the body to be read multiple times for non-encoded requests. |
| | 181 | 139 | | if (string.IsNullOrWhiteSpace(contentEncoding)) |
| | | 140 | | { |
| | 181 | 141 | | context.Request.EnableBuffering(); |
| | | 142 | | } |
| | | 143 | | |
| | 181 | 144 | | var contentType = context.Request.ContentType; |
| | 181 | 145 | | var hasFormContentType = context.Request.HasFormContentType; |
| | 181 | 146 | | var isMultipart = contentType?.StartsWith("multipart/", StringComparison.OrdinalIgnoreCase) ?? false; |
| | | 147 | | |
| | | 148 | | // ② Read the raw body into a string (best-effort), then rewind. |
| | | 149 | | // IMPORTANT: Avoid reading encoded bodies and multipart payloads here. |
| | | 150 | | // - For Content-Encoding (e.g. gzip), RequestDecompression will decode later in the pipeline. |
| | | 151 | | // - For multipart, KrFormParser handles parsing/streaming. |
| | | 152 | | // Reading those payloads as UTF-8 here can interfere with later middleware/parsers. |
| | 181 | 153 | | var body = string.Empty; |
| | 181 | 154 | | if (!isMultipart && string.IsNullOrWhiteSpace(contentEncoding)) |
| | | 155 | | { |
| | 181 | 156 | | using var reader = new StreamReader( |
| | 181 | 157 | | context.Request.Body, |
| | 181 | 158 | | encoding: Encoding.UTF8, |
| | 181 | 159 | | detectEncodingFromByteOrderMarks: false, |
| | 181 | 160 | | leaveOpen: true); |
| | | 161 | | |
| | 181 | 162 | | body = await reader.ReadToEndAsync().ConfigureAwait(false); |
| | 181 | 163 | | context.Request.Body.Position = 0; |
| | 181 | 164 | | } |
| | | 165 | | |
| | | 166 | | // ③ If it's a form, read it safely |
| | 181 | 167 | | Dictionary<string, string>? formDict = null; |
| | 181 | 168 | | if (hasFormContentType) |
| | | 169 | | { |
| | | 170 | | // Only parse application/x-www-form-urlencoded here. |
| | | 171 | | // Multipart (form-data/mixed/etc) is handled by KrFormParser / Add-KrFormRoute. |
| | | 172 | | // Also skip parsing when a non-empty Content-Encoding header is present; in that case |
| | | 173 | | // request decompression middleware may not have run yet. |
| | 4 | 174 | | if (string.IsNullOrWhiteSpace(contentEncoding) |
| | 4 | 175 | | && (context.Request.ContentType?.StartsWith("application/x-www-form-urlencoded", StringComparison.Ordina |
| | | 176 | | { |
| | 4 | 177 | | formDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 4 | 178 | | var form = await context.Request.ReadFormAsync().ConfigureAwait(false); |
| | 18 | 179 | | foreach (var kv in form) |
| | | 180 | | { |
| | 5 | 181 | | formDict[kv.Key] = kv.Value.ToString(); |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | 181 | 186 | | return new KestrunRequest(context: context, formDict: formDict, body: body); |
| | 181 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Synchronous helper for tests and simple call sites that prefer not to use async/await. |
| | | 191 | | /// Avoid in ASP.NET request pipelines; intended for unit tests only. |
| | | 192 | | /// </summary> |
| | 175 | 193 | | public static KestrunRequest NewRequestSync(HttpContext context) => NewRequest(context).GetAwaiter().GetResult(); |
| | | 194 | | } |