| | | 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 | | { |
| | 104 | 11 | | private KestrunRequest(HttpContext context, Dictionary<string, string>? formDict, string body) |
| | | 12 | | { |
| | 104 | 13 | | HttpContext = context ?? throw new ArgumentNullException(nameof(context)); |
| | 104 | 14 | | Request = context.Request; |
| | 104 | 15 | | Query = Request.Query |
| | 124 | 16 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | 104 | 17 | | Headers = Request.Headers |
| | 210 | 18 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | 104 | 19 | | Cookies = Request.Cookies |
| | 110 | 20 | | .ToDictionary(x => x.Key, x => x.Value.ToString() ?? string.Empty); |
| | 104 | 21 | | Form = formDict; |
| | 104 | 22 | | Body = body; |
| | 104 | 23 | | RouteValues = Request.RouteValues |
| | 106 | 24 | | .ToDictionary(x => x.Key, x => x.Value?.ToString() ?? string.Empty); |
| | 104 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the <see cref="Microsoft.AspNetCore.Http.HttpContext"/> associated with the request. |
| | | 29 | | /// </summary> |
| | 201 | 30 | | public HttpContext HttpContext { get; init; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the <see cref="HttpRequest"/> associated with the request. |
| | | 34 | | /// </summary> |
| | 568 | 35 | | public HttpRequest Request { get; init; } |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the HTTP method for the request (e.g., GET, POST). |
| | | 38 | | /// </summary> |
| | 15 | 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> |
| | 1 | 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> |
| | 16 | 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> |
| | 110 | 94 | | public Dictionary<string, string> Query { get; init; } |
| | | 95 | | |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets the headers for the request as a dictionary of key-value pairs. |
| | | 99 | | /// </summary> |
| | 218 | 100 | | public Dictionary<string, string> Headers { get; init; } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets the body content of the request as a string. |
| | | 104 | | /// </summary> |
| | 110 | 105 | | public string Body { get; init; } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets the authorization header value for the request, if present. |
| | | 109 | | /// </summary> |
| | 7 | 110 | | public string? Authorization => Request.Headers.Authorization.ToString(); |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets the cookies for the request as an <see cref="IRequestCookieCollection"/>, if present. |
| | | 113 | | /// </summary> |
| | 108 | 114 | | public Dictionary<string, string> Cookies { get; init; } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Gets the form data for the request as a dictionary of key-value pairs, if present. |
| | | 118 | | /// </summary> |
| | 109 | 119 | | public Dictionary<string, string>? Form { get; init; } |
| | | 120 | | |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets the route values for the request as a <see cref="RouteValueDictionary"/>, if present. |
| | | 124 | | /// </summary> |
| | 105 | 125 | | public Dictionary<string, string> RouteValues { get; init; } |
| | | 126 | | |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Creates a new <see cref="KestrunRequest"/> instance from the specified <see cref="Microsoft.AspNetCore.Http.Http |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="context">The HTTP context containing the request information.</param> |
| | | 132 | | /// <returns>A task that represents the asynchronous operation. The task result contains the constructed <see cref=" |
| | | 133 | | public static async Task<KestrunRequest> NewRequest(HttpContext context) |
| | | 134 | | { |
| | | 135 | | // ① Allow the body to be read multiple times |
| | 104 | 136 | | context.Request.EnableBuffering(); |
| | | 137 | | |
| | | 138 | | // ② Read the raw body into a string, then rewind |
| | | 139 | | string body; |
| | 104 | 140 | | using (var reader = new StreamReader( |
| | 104 | 141 | | context.Request.Body, |
| | 104 | 142 | | encoding: Encoding.UTF8, |
| | 104 | 143 | | detectEncodingFromByteOrderMarks: false, |
| | 104 | 144 | | leaveOpen: true)) |
| | | 145 | | { |
| | 104 | 146 | | body = await reader.ReadToEndAsync(); |
| | 104 | 147 | | context.Request.Body.Position = 0; |
| | 104 | 148 | | } |
| | | 149 | | |
| | | 150 | | // ③ If it's a form, read it safely |
| | 104 | 151 | | var formDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 104 | 152 | | if (context.Request.HasFormContentType) |
| | | 153 | | { |
| | 1 | 154 | | var form = await context.Request.ReadFormAsync(); |
| | 6 | 155 | | foreach (var kv in form) |
| | | 156 | | { |
| | 2 | 157 | | formDict[kv.Key] = kv.Value.ToString(); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | 104 | 161 | | return new KestrunRequest(context: context, formDict: formDict, body: body); |
| | 104 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Synchronous helper for tests and simple call sites that prefer not to use async/await. |
| | | 166 | | /// Avoid in ASP.NET request pipelines; intended for unit tests only. |
| | | 167 | | /// </summary> |
| | 60 | 168 | | public static KestrunRequest NewRequestSync(HttpContext context) => NewRequest(context).GetAwaiter().GetResult(); |
| | | 169 | | } |