| | 1 | |
|
| | 2 | | using System.Xml.Linq; |
| | 3 | | using Newtonsoft.Json; |
| | 4 | | using Newtonsoft.Json.Serialization; |
| | 5 | | using Microsoft.AspNetCore.StaticFiles; |
| | 6 | | using System.Text; |
| | 7 | | using Serilog; |
| | 8 | | using Serilog.Events; |
| | 9 | | using System.Buffers; |
| | 10 | | using Microsoft.Extensions.FileProviders; |
| | 11 | | using Microsoft.AspNetCore.WebUtilities; |
| | 12 | | using System.Net; |
| | 13 | | using MongoDB.Bson; |
| | 14 | | using Kestrun.Utilities; |
| | 15 | | using System.Collections; |
| | 16 | | using CsvHelper.Configuration; |
| | 17 | | using System.Globalization; |
| | 18 | | using CsvHelper; |
| | 19 | | using System.Reflection; |
| | 20 | |
|
| | 21 | | namespace Kestrun.Models; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Represents an HTTP response in the Kestrun framework, providing methods to write various content types and manage he |
| | 25 | | /// </summary> |
| | 26 | | /// <remarks> |
| | 27 | | /// Initializes a new instance of the <see cref="KestrunResponse"/> class with the specified request and optional body a |
| | 28 | | /// </remarks> |
| | 29 | | /// <param name="request">The associated <see cref="KestrunRequest"/> for this response.</param> |
| | 30 | | /// <param name="bodyAsyncThreshold">The threshold in bytes for using async body write operations. Defaults to 8192.</pa |
| 72 | 31 | | public class KestrunResponse(KestrunRequest request, int bodyAsyncThreshold = 8192) |
| | 32 | | { |
| | 33 | | /// <summary> |
| | 34 | | /// A set of MIME types that are considered text-based for response content. |
| | 35 | | /// </summary> |
| 1 | 36 | | public static readonly HashSet<string> TextBasedMimeTypes = |
| 1 | 37 | | new(StringComparer.OrdinalIgnoreCase) |
| 1 | 38 | | { |
| 1 | 39 | | "application/json", |
| 1 | 40 | | "application/xml", |
| 1 | 41 | | "application/javascript", |
| 1 | 42 | | "application/xhtml+xml", |
| 1 | 43 | | "application/x-www-form-urlencoded", |
| 1 | 44 | | "application/yaml", |
| 1 | 45 | | "application/graphql" |
| 1 | 46 | | }; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets or sets the HTTP status code for the response. |
| | 50 | | /// </summary> |
| 162 | 51 | | public int StatusCode { get; set; } = 200; |
| | 52 | | /// <summary> |
| | 53 | | /// Gets or sets the collection of HTTP headers for the response. |
| | 54 | | /// </summary> |
| 112 | 55 | | public Dictionary<string, string> Headers { get; set; } = []; |
| | 56 | | /// <summary> |
| | 57 | | /// Gets or sets the MIME content type of the response. |
| | 58 | | /// </summary> |
| 219 | 59 | | public string ContentType { get; set; } = "text/plain"; |
| | 60 | | /// <summary> |
| | 61 | | /// Gets or sets the body of the response, which can be a string, byte array, stream, or file info. |
| | 62 | | /// </summary> |
| 120 | 63 | | public object? Body { get; set; } |
| | 64 | | /// <summary> |
| | 65 | | /// Gets or sets the URL to redirect the response to, if an HTTP redirect is required. |
| | 66 | | /// </summary> |
| 35 | 67 | | public string? RedirectUrl { get; set; } // For HTTP redirects |
| | 68 | | /// <summary> |
| | 69 | | /// Gets or sets the list of Set-Cookie header values for the response. |
| | 70 | | /// </summary> |
| 15 | 71 | | public List<string>? Cookies { get; set; } // For Set-Cookie headers |
| | 72 | |
|
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Text encoding for textual MIME types. |
| | 76 | | /// </summary> |
| 96 | 77 | | public Encoding Encoding { get; set; } = Encoding.UTF8; |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Content-Disposition header value. |
| | 81 | | /// </summary> |
| 102 | 82 | | public ContentDispositionOptions ContentDisposition { get; set; } = new ContentDispositionOptions(); |
| | 83 | | /// <summary> |
| | 84 | | /// Gets the associated KestrunRequest for this response. |
| | 85 | | /// </summary> |
| 107 | 86 | | public KestrunRequest Request { get; private set; } = request ?? throw new ArgumentNullException(nameof(request)); |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Global text encoding for all responses. Defaults to UTF-8. |
| | 90 | | /// </summary> |
| 86 | 91 | | public Encoding AcceptCharset { get; private set; } = request.Headers.TryGetValue("Accept-Charset", out var value) ? |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// If the response body is larger than this threshold (in bytes), async write will be used. |
| | 95 | | /// </summary> |
| 72 | 96 | | public int BodyAsyncThreshold { get; set; } = bodyAsyncThreshold; |
| | 97 | |
|
| | 98 | | #region Constructors |
| | 99 | | #endregion |
| | 100 | |
|
| | 101 | | #region Helpers |
| | 102 | | /// <summary> |
| | 103 | | /// Retrieves the value of the specified header from the response headers. |
| | 104 | | /// </summary> |
| | 105 | | /// <param name="key">The name of the header to retrieve.</param> |
| | 106 | | /// <returns>The value of the header if found; otherwise, null.</returns> |
| 0 | 107 | | public string? GetHeader(string key) => Headers.TryGetValue(key, out var value) ? value : null; |
| | 108 | |
|
| | 109 | | private string DetermineContentType(string? contentType, string defaultType = "text/plain") |
| | 110 | | { |
| 1 | 111 | | if (string.IsNullOrWhiteSpace(contentType)) |
| | 112 | | { |
| 1 | 113 | | _ = Request.Headers.TryGetValue("Accept", out var acceptHeader); |
| 1 | 114 | | contentType = (acceptHeader ?? defaultType) |
| 1 | 115 | | .ToLowerInvariant(); |
| | 116 | | } |
| | 117 | |
|
| 1 | 118 | | return contentType; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Determines whether the specified content type is text-based or supports a charset. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="type">The MIME content type to check.</param> |
| | 125 | | /// <returns>True if the content type is text-based; otherwise, false.</returns> |
| | 126 | | public static bool IsTextBasedContentType(string type) |
| | 127 | | { |
| 24 | 128 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 129 | | { |
| 24 | 130 | | Log.Debug("Checking if content type is text-based: {ContentType}", type); |
| | 131 | | } |
| | 132 | |
|
| | 133 | | // Check if the content type is text-based or has a charset |
| 24 | 134 | | if (string.IsNullOrEmpty(type)) |
| | 135 | | { |
| 1 | 136 | | return false; |
| | 137 | | } |
| | 138 | |
|
| 23 | 139 | | if (type.StartsWith("text/", StringComparison.OrdinalIgnoreCase)) |
| | 140 | | { |
| 14 | 141 | | return true; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | // Include structured types using XML or JSON suffixes |
| 9 | 145 | | if (type.EndsWith("+xml", StringComparison.OrdinalIgnoreCase) || |
| 9 | 146 | | type.EndsWith("+json", StringComparison.OrdinalIgnoreCase)) |
| | 147 | | { |
| 2 | 148 | | return true; |
| | 149 | | } |
| | 150 | |
|
| | 151 | | // Common application types where charset makes sense |
| 7 | 152 | | return TextBasedMimeTypes.Contains(type); |
| | 153 | | } |
| | 154 | | #endregion |
| | 155 | |
|
| | 156 | | #region Response Writers |
| | 157 | | /// <summary> |
| | 158 | | /// Writes a file response with the specified file path, content type, and HTTP status code. |
| | 159 | | /// </summary> |
| | 160 | | /// <param name="filePath">The path to the file to be sent in the response.</param> |
| | 161 | | /// <param name="contentType">The MIME type of the file content.</param> |
| | 162 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 163 | | public void WriteFileResponse( |
| | 164 | | string? filePath, |
| | 165 | | string? contentType, |
| | 166 | | int statusCode = StatusCodes.Status200OK |
| | 167 | | ) |
| | 168 | | { |
| 2 | 169 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 170 | | { |
| 2 | 171 | | Log.Debug("Writing file response,FilePath={FilePath} StatusCode={StatusCode}, ContentType={ContentType}, Cur |
| 2 | 172 | | filePath, statusCode, contentType, Directory.GetCurrentDirectory()); |
| | 173 | | } |
| | 174 | |
|
| 2 | 175 | | if (string.IsNullOrEmpty(filePath)) |
| | 176 | | { |
| 0 | 177 | | throw new ArgumentException("File path cannot be null or empty.", nameof(filePath)); |
| | 178 | | } |
| | 179 | |
|
| 2 | 180 | | if (!File.Exists(filePath)) |
| | 181 | | { |
| 1 | 182 | | StatusCode = StatusCodes.Status404NotFound; |
| 1 | 183 | | Body = $"File not found: {filePath}"; |
| 1 | 184 | | ContentType = $"text/plain; charset={Encoding.WebName}"; |
| 1 | 185 | | return; |
| | 186 | | } |
| | 187 | | // 1. Make sure you have an absolute file path |
| 1 | 188 | | var fullPath = Path.GetFullPath(filePath); |
| | 189 | |
|
| | 190 | | // 2. Extract the directory to use as the "root" |
| 1 | 191 | | var directory = Path.GetDirectoryName(fullPath) |
| 1 | 192 | | ?? throw new InvalidOperationException("Could not determine directory from file path"); |
| | 193 | |
|
| 1 | 194 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 195 | | { |
| 1 | 196 | | Log.Debug("Serving file: {FilePath}", fullPath); |
| | 197 | | } |
| | 198 | |
|
| | 199 | | // Create a physical file provider for the directory |
| 1 | 200 | | var physicalProvider = new PhysicalFileProvider(directory); |
| 1 | 201 | | var fi = physicalProvider.GetFileInfo(Path.GetFileName(filePath)); |
| 1 | 202 | | var provider = new FileExtensionContentTypeProvider(); |
| 1 | 203 | | contentType ??= provider.TryGetContentType(fullPath, out var ct) |
| 1 | 204 | | ? ct |
| 1 | 205 | | : "application/octet-stream"; |
| 1 | 206 | | Body = fi; |
| | 207 | |
|
| | 208 | | // headers & metadata |
| 1 | 209 | | StatusCode = statusCode; |
| 1 | 210 | | ContentType = contentType; |
| 1 | 211 | | Log.Debug("File response prepared: FileName={FileName}, Length={Length}, ContentType={ContentType}", |
| 1 | 212 | | fi.Name, fi.Length, ContentType); |
| 1 | 213 | | } |
| | 214 | |
|
| | 215 | | /// <summary> |
| | 216 | | /// Writes a JSON response with the specified input object and HTTP status code. |
| | 217 | | /// </summary> |
| | 218 | | /// <param name="inputObject">The object to be converted to JSON.</param> |
| | 219 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| 1 | 220 | | public void WriteJsonResponse(object? inputObject, int statusCode = StatusCodes.Status200OK) => WriteJsonResponseAsy |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Asynchronously writes a JSON response with the specified input object and HTTP status code. |
| | 224 | | /// </summary> |
| | 225 | | /// <param name="inputObject">The object to be converted to JSON.</param> |
| | 226 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| 3 | 227 | | public async Task WriteJsonResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK) => await Wri |
| | 228 | |
|
| | 229 | | /// <summary> |
| | 230 | | /// Writes a JSON response using the specified input object and serializer settings. |
| | 231 | | /// </summary> |
| | 232 | | /// <param name="inputObject">The object to be converted to JSON.</param> |
| | 233 | | /// <param name="serializerSettings">The settings to use for JSON serialization.</param> |
| | 234 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 235 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 0 | 236 | | public void WriteJsonResponse(object? inputObject, JsonSerializerSettings serializerSettings, int statusCode = Statu |
| | 237 | |
|
| | 238 | | /// <summary> |
| | 239 | | /// Asynchronously writes a JSON response using the specified input object and serializer settings. |
| | 240 | | /// </summary> |
| | 241 | | /// <param name="inputObject">The object to be converted to JSON.</param> |
| | 242 | | /// <param name="serializerSettings">The settings to use for JSON serialization.</param> |
| | 243 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 244 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 245 | | public async Task WriteJsonResponseAsync(object? inputObject, JsonSerializerSettings serializerSettings, int statusC |
| | 246 | | { |
| 4 | 247 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 248 | | { |
| 4 | 249 | | Log.Debug("Writing JSON response (async), StatusCode={StatusCode}, ContentType={ContentType}", statusCode, c |
| | 250 | | } |
| | 251 | |
|
| 8 | 252 | | Body = await Task.Run(() => JsonConvert.SerializeObject(inputObject, serializerSettings)); |
| 4 | 253 | | ContentType = string.IsNullOrEmpty(contentType) ? $"application/json; charset={Encoding.WebName}" : contentType; |
| 4 | 254 | | StatusCode = statusCode; |
| 4 | 255 | | } |
| | 256 | | /// <summary> |
| | 257 | | /// Writes a JSON response with the specified input object, serialization depth, compression option, status code, an |
| | 258 | | /// </summary> |
| | 259 | | /// <param name="inputObject">The object to be converted to JSON.</param> |
| | 260 | | /// <param name="depth">The maximum depth for JSON serialization.</param> |
| | 261 | | /// <param name="compress">Whether to compress the JSON output (no indentation).</param> |
| | 262 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 263 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 0 | 264 | | public void WriteJsonResponse(object? inputObject, int depth, bool compress, int statusCode = StatusCodes.Status200O |
| | 265 | |
|
| | 266 | | /// <summary> |
| | 267 | | /// Asynchronously writes a JSON response with the specified input object, serialization depth, compression option, |
| | 268 | | /// </summary> |
| | 269 | | /// <param name="inputObject">The object to be converted to JSON.</param> |
| | 270 | | /// <param name="depth">The maximum depth for JSON serialization.</param> |
| | 271 | | /// <param name="compress">Whether to compress the JSON output (no indentation).</param> |
| | 272 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 273 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 274 | | public async Task WriteJsonResponseAsync(object? inputObject, int depth, bool compress, int statusCode = StatusCodes |
| | 275 | | { |
| 4 | 276 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 277 | | { |
| 4 | 278 | | Log.Debug("Writing JSON response (async), StatusCode={StatusCode}, ContentType={ContentType}, Depth={Depth}, |
| 4 | 279 | | statusCode, contentType, depth, compress); |
| | 280 | | } |
| | 281 | |
|
| 4 | 282 | | var serializerSettings = new JsonSerializerSettings |
| 4 | 283 | | { |
| 4 | 284 | | Formatting = compress ? Formatting.None : Formatting.Indented, |
| 4 | 285 | | ContractResolver = new CamelCasePropertyNamesContractResolver(), |
| 4 | 286 | | ReferenceLoopHandling = ReferenceLoopHandling.Ignore, |
| 4 | 287 | | NullValueHandling = NullValueHandling.Ignore, |
| 4 | 288 | | DefaultValueHandling = DefaultValueHandling.Ignore, |
| 4 | 289 | | MaxDepth = depth |
| 4 | 290 | | }; |
| 4 | 291 | | await WriteJsonResponseAsync(inputObject, serializerSettings: serializerSettings, statusCode: statusCode, conten |
| 4 | 292 | | } |
| | 293 | | /// <summary> |
| | 294 | | /// Writes a CBOR response (binary, efficient, not human-readable). |
| | 295 | | /// </summary> |
| | 296 | | public async Task WriteCborResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK, string? cont |
| | 297 | | { |
| 2 | 298 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 299 | | { |
| 2 | 300 | | Log.Debug("Writing CBOR response, StatusCode={StatusCode}, ContentType={ContentType}", statusCode, contentTy |
| | 301 | | } |
| | 302 | |
|
| | 303 | | // Serialize to CBOR using PeterO.Cbor |
| 4 | 304 | | Body = await Task.Run(() => inputObject != null |
| 4 | 305 | | ? PeterO.Cbor.CBORObject.FromObject(inputObject).EncodeToBytes() |
| 4 | 306 | | : []); |
| 2 | 307 | | ContentType = string.IsNullOrEmpty(contentType) ? "application/cbor" : contentType; |
| 2 | 308 | | StatusCode = statusCode; |
| 2 | 309 | | } |
| | 310 | |
|
| | 311 | | /// <summary> |
| | 312 | | /// Writes a CBOR response (binary, efficient, not human-readable). |
| | 313 | | /// </summary> |
| | 314 | | /// <param name="inputObject">The object to be converted to CBOR.</param> |
| | 315 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 316 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 0 | 317 | | public void WriteCborResponse(object? inputObject, int statusCode = StatusCodes.Status200OK, string? contentType = n |
| | 318 | |
|
| | 319 | | /// <summary> |
| | 320 | | /// Asynchronously writes a BSON response with the specified input object, status code, and content type. |
| | 321 | | /// </summary> |
| | 322 | | /// <param name="inputObject">The object to be converted to BSON.</param> |
| | 323 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 324 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 325 | | public async Task WriteBsonResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK, string? cont |
| | 326 | | { |
| 1 | 327 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 328 | | { |
| 1 | 329 | | Log.Debug("Writing BSON response, StatusCode={StatusCode}, ContentType={ContentType}", statusCode, contentTy |
| | 330 | | } |
| | 331 | |
|
| | 332 | | // Serialize to BSON (as byte[]) |
| 2 | 333 | | Body = await Task.Run(() => inputObject != null ? inputObject.ToBson() : []); |
| 1 | 334 | | ContentType = string.IsNullOrEmpty(contentType) ? "application/bson" : contentType; |
| 1 | 335 | | StatusCode = statusCode; |
| 1 | 336 | | } |
| | 337 | |
|
| | 338 | | /// <summary> |
| | 339 | | /// Writes a BSON response with the specified input object, status code, and content type. |
| | 340 | | /// </summary> |
| | 341 | | /// <param name="inputObject">The object to be converted to BSON.</param> |
| | 342 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 343 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 0 | 344 | | public void WriteBsonResponse(object? inputObject, int statusCode = StatusCodes.Status200OK, string? contentType = n |
| | 345 | |
|
| | 346 | | /// <summary> |
| | 347 | | /// Asynchronously writes a response with the specified input object and HTTP status code. |
| | 348 | | /// Chooses the response format based on the Accept header or defaults to text/plain. |
| | 349 | | /// </summary> |
| | 350 | | /// <param name="inputObject">The object to be sent in the response body.</param> |
| | 351 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 352 | | public async Task WriteResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK) |
| | 353 | | { |
| 1 | 354 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 355 | | { |
| 1 | 356 | | Log.Debug("Writing response, StatusCode={StatusCode}", statusCode); |
| | 357 | | } |
| | 358 | |
|
| 1 | 359 | | Body = inputObject; |
| 1 | 360 | | ContentType = DetermineContentType(contentType: string.Empty); // Ensure ContentType is set based on Accept head |
| | 361 | |
|
| 1 | 362 | | if (ContentType.Contains("json")) |
| | 363 | | { |
| 1 | 364 | | await WriteJsonResponseAsync(inputObject: inputObject, statusCode: statusCode); |
| | 365 | | } |
| 0 | 366 | | else if (ContentType.Contains("yaml") || ContentType.Contains("yml")) |
| | 367 | | { |
| 0 | 368 | | await WriteYamlResponseAsync(inputObject: inputObject, statusCode: statusCode); |
| | 369 | | } |
| 0 | 370 | | else if (ContentType.Contains("xml")) |
| | 371 | | { |
| 0 | 372 | | await WriteXmlResponseAsync(inputObject: inputObject, statusCode: statusCode); |
| | 373 | | } |
| | 374 | | else |
| | 375 | | { |
| 0 | 376 | | await WriteTextResponseAsync(inputObject: inputObject, statusCode: statusCode); |
| | 377 | | } |
| 1 | 378 | | } |
| | 379 | |
|
| | 380 | | /// <summary> |
| | 381 | | /// Writes a response with the specified input object and HTTP status code. |
| | 382 | | /// Chooses the response format based on the Accept header or defaults to text/plain. |
| | 383 | | /// </summary> |
| | 384 | | /// <param name="inputObject">The object to be sent in the response body.</param> |
| | 385 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| 0 | 386 | | public void WriteResponse(object? inputObject, int statusCode = StatusCodes.Status200OK) => WriteResponseAsync(input |
| | 387 | |
|
| | 388 | | /// <summary> |
| | 389 | | /// Writes a CSV response with the specified input object, status code, content type, and optional CsvConfiguration. |
| | 390 | | /// </summary> |
| | 391 | | /// <param name="inputObject">The object to be converted to CSV.</param> |
| | 392 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 393 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 394 | | /// <param name="config">An optional CsvConfiguration to customize CSV output.</param> |
| | 395 | | public void WriteCsvResponse( |
| | 396 | | object? inputObject, |
| | 397 | | int statusCode = StatusCodes.Status200OK, |
| | 398 | | string? contentType = null, |
| | 399 | | CsvConfiguration? config = null) |
| | 400 | | { |
| 2 | 401 | | Action<CsvConfiguration>? tweaker = null; |
| | 402 | |
|
| 2 | 403 | | if (config is not null) |
| | 404 | | { |
| 1 | 405 | | tweaker = target => |
| 1 | 406 | | { |
| 90 | 407 | | foreach (var prop in typeof(CsvConfiguration) |
| 1 | 408 | | .GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| 1 | 409 | | { |
| 44 | 410 | | if (prop.CanRead && prop.CanWrite) |
| 1 | 411 | | { |
| 44 | 412 | | var value = prop.GetValue(config); |
| 44 | 413 | | prop.SetValue(target, value); |
| 1 | 414 | | } |
| 1 | 415 | | } |
| 2 | 416 | | }; |
| | 417 | | } |
| 2 | 418 | | WriteCsvResponseAsync(inputObject, statusCode, contentType, tweaker).GetAwaiter().GetResult(); |
| 2 | 419 | | } |
| | 420 | |
|
| | 421 | | /// <summary> |
| | 422 | | /// Asynchronously writes a CSV response with the specified input object, status code, content type, and optional co |
| | 423 | | /// </summary> |
| | 424 | | /// <param name="inputObject">The object to be converted to CSV.</param> |
| | 425 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 426 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 427 | | /// <param name="tweak">An optional action to tweak the CsvConfiguration.</param> |
| | 428 | | public async Task WriteCsvResponseAsync( |
| | 429 | | object? inputObject, |
| | 430 | | int statusCode = StatusCodes.Status200OK, |
| | 431 | | string? contentType = null, |
| | 432 | | Action<CsvConfiguration>? tweak = null) |
| | 433 | | { |
| 3 | 434 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 435 | | { |
| 3 | 436 | | Log.Debug("Writing CSV response (async), StatusCode={StatusCode}, ContentType={ContentType}", |
| 3 | 437 | | statusCode, contentType); |
| | 438 | | } |
| | 439 | |
|
| | 440 | | // Serialize inside a background task so heavy reflection never blocks the caller |
| 3 | 441 | | Body = await Task.Run(() => |
| 3 | 442 | | { |
| 3 | 443 | | var cfg = new CsvConfiguration(CultureInfo.InvariantCulture) |
| 3 | 444 | | { |
| 3 | 445 | | HasHeaderRecord = true, |
| 3 | 446 | | NewLine = Environment.NewLine |
| 3 | 447 | | }; |
| 3 | 448 | | tweak?.Invoke(cfg); // let the caller flirt with the config |
| 3 | 449 | |
|
| 3 | 450 | | using var sw = new StringWriter(); |
| 3 | 451 | | using var csv = new CsvWriter(sw, cfg); |
| 3 | 452 | |
|
| 3 | 453 | | // CsvHelper insists on an enumerable; wrap single objects so it stays happy |
| 3 | 454 | | if (inputObject is IEnumerable records and not string) |
| 3 | 455 | | { |
| 3 | 456 | | csv.WriteRecords(records); // whole collections (IEnumerable<T>) |
| 3 | 457 | | } |
| 0 | 458 | | else if (inputObject is not null) |
| 3 | 459 | | { |
| 0 | 460 | | csv.WriteRecords([inputObject]); // lone POCO |
| 3 | 461 | | } |
| 3 | 462 | | else |
| 3 | 463 | | { |
| 0 | 464 | | csv.WriteHeader<object>(); // nothing? write only headers for an empty file |
| 3 | 465 | | } |
| 3 | 466 | |
|
| 3 | 467 | | return sw.ToString(); |
| 6 | 468 | | }).ConfigureAwait(false); |
| | 469 | |
|
| 3 | 470 | | ContentType = string.IsNullOrEmpty(contentType) |
| 3 | 471 | | ? $"text/csv; charset={Encoding.WebName}" |
| 3 | 472 | | : contentType; |
| 3 | 473 | | StatusCode = statusCode; |
| 3 | 474 | | } |
| | 475 | | /// <summary> |
| | 476 | | /// Writes a YAML response with the specified input object, status code, and content type. |
| | 477 | | /// </summary> |
| | 478 | | /// <param name="inputObject">The object to be converted to YAML.</param> |
| | 479 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 480 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 1 | 481 | | public void WriteYamlResponse(object? inputObject, int statusCode = StatusCodes.Status200OK, string? contentType = n |
| | 482 | |
|
| | 483 | | /// <summary> |
| | 484 | | /// Asynchronously writes a YAML response with the specified input object, status code, and content type. |
| | 485 | | /// </summary> |
| | 486 | | /// <param name="inputObject">The object to be converted to YAML.</param> |
| | 487 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 488 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 489 | | public async Task WriteYamlResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK, string? cont |
| | 490 | | { |
| 3 | 491 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 492 | | { |
| 3 | 493 | | Log.Debug("Writing YAML response (async), StatusCode={StatusCode}, ContentType={ContentType}", statusCode, c |
| | 494 | | } |
| | 495 | |
|
| 6 | 496 | | Body = await Task.Run(() => YamlHelper.ToYaml(inputObject)); |
| 3 | 497 | | ContentType = string.IsNullOrEmpty(contentType) ? $"application/yaml; charset={Encoding.WebName}" : contentType; |
| 3 | 498 | | StatusCode = statusCode; |
| 3 | 499 | | } |
| | 500 | |
|
| | 501 | | /// <summary> |
| | 502 | | /// Writes an XML response with the specified input object, status code, and content type. |
| | 503 | | /// </summary> |
| | 504 | | /// <param name="inputObject">The object to be converted to XML.</param> |
| | 505 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 506 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 1 | 507 | | public void WriteXmlResponse(object? inputObject, int statusCode = StatusCodes.Status200OK, string? contentType = nu |
| | 508 | |
|
| | 509 | | /// <summary> |
| | 510 | | /// Asynchronously writes an XML response with the specified input object, status code, and content type. |
| | 511 | | /// </summary> |
| | 512 | | /// <param name="inputObject">The object to be converted to XML.</param> |
| | 513 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 514 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 515 | | public async Task WriteXmlResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK, string? conte |
| | 516 | | { |
| 3 | 517 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 518 | | { |
| 3 | 519 | | Log.Debug("Writing XML response (async), StatusCode={StatusCode}, ContentType={ContentType}", statusCode, co |
| | 520 | | } |
| | 521 | |
|
| 6 | 522 | | var xml = await Task.Run(() => XmlHelper.ToXml("Response", inputObject)); |
| 6 | 523 | | Body = await Task.Run(() => xml.ToString(SaveOptions.DisableFormatting)); |
| 3 | 524 | | ContentType = string.IsNullOrEmpty(contentType) ? $"application/xml; charset={Encoding.WebName}" : contentType; |
| 3 | 525 | | StatusCode = statusCode; |
| 3 | 526 | | } |
| | 527 | | /// <summary> |
| | 528 | | /// Writes a text response with the specified input object, status code, and content type. |
| | 529 | | /// </summary> |
| | 530 | | /// <param name="inputObject">The object to be converted to a text response.</param> |
| | 531 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 532 | | /// <param name="contentType">The MIME type of the response content.</param> |
| 4 | 533 | | public void WriteTextResponse(object? inputObject, int statusCode = StatusCodes.Status200OK, string? contentType = n |
| | 534 | |
|
| | 535 | | /// <summary> |
| | 536 | | /// Asynchronously writes a text response with the specified input object, status code, and content type. |
| | 537 | | /// </summary> |
| | 538 | | /// <param name="inputObject">The object to be converted to a text response.</param> |
| | 539 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 540 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 541 | | public async Task WriteTextResponseAsync(object? inputObject, int statusCode = StatusCodes.Status200OK, string? cont |
| | 542 | | { |
| 17 | 543 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 544 | | { |
| 17 | 545 | | Log.Debug("Writing text response (async), StatusCode={StatusCode}, ContentType={ContentType}", statusCode, c |
| | 546 | | } |
| | 547 | |
|
| 17 | 548 | | if (inputObject is null) |
| | 549 | | { |
| 0 | 550 | | throw new ArgumentNullException(nameof(inputObject), "Input object cannot be null for text response."); |
| | 551 | | } |
| | 552 | |
|
| 34 | 553 | | Body = await Task.Run(() => inputObject?.ToString() ?? string.Empty); |
| 17 | 554 | | ContentType = string.IsNullOrEmpty(contentType) ? $"text/plain; charset={Encoding.WebName}" : contentType; |
| 17 | 555 | | StatusCode = statusCode; |
| 17 | 556 | | } |
| | 557 | |
|
| | 558 | | /// <summary> |
| | 559 | | /// Writes an HTTP redirect response with the specified URL and optional message. |
| | 560 | | /// </summary> |
| | 561 | | /// <param name="url">The URL to redirect to.</param> |
| | 562 | | /// <param name="message">An optional message to include in the response body.</param> |
| | 563 | | public void WriteRedirectResponse(string url, string? message = null) |
| | 564 | | { |
| 5 | 565 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 566 | | { |
| 5 | 567 | | Log.Debug("Writing redirect response, StatusCode={StatusCode}, Location={Location}", StatusCode, url); |
| | 568 | | } |
| | 569 | |
|
| 5 | 570 | | if (string.IsNullOrEmpty(url)) |
| | 571 | | { |
| 0 | 572 | | throw new ArgumentNullException(nameof(url), "URL cannot be null for redirect response."); |
| | 573 | | } |
| | 574 | | // framework hook |
| 5 | 575 | | RedirectUrl = url; |
| | 576 | |
|
| | 577 | | // HTTP status + Location header |
| 5 | 578 | | StatusCode = StatusCodes.Status302Found; |
| 5 | 579 | | Headers["Location"] = url; |
| | 580 | |
|
| 5 | 581 | | if (message is not null) |
| | 582 | | { |
| | 583 | | // include a body |
| 1 | 584 | | Body = message; |
| 1 | 585 | | ContentType = $"text/plain; charset={Encoding.WebName}"; |
| | 586 | | } |
| | 587 | | else |
| | 588 | | { |
| | 589 | | // no body: clear any existing body/headers |
| 4 | 590 | | Body = null; |
| | 591 | | //ContentType = null; |
| 4 | 592 | | _ = Headers.Remove("Content-Length"); |
| | 593 | | } |
| 4 | 594 | | } |
| | 595 | |
|
| | 596 | | /// <summary> |
| | 597 | | /// Writes a binary response with the specified data, status code, and content type. |
| | 598 | | /// </summary> |
| | 599 | | /// <param name="data">The binary data to send in the response.</param> |
| | 600 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 601 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 602 | | public void WriteBinaryResponse(byte[] data, int statusCode = StatusCodes.Status200OK, string contentType = "applica |
| | 603 | | { |
| 1 | 604 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 605 | | { |
| 1 | 606 | | Log.Debug("Writing binary response, StatusCode={StatusCode}, ContentType={ContentType}", statusCode, content |
| | 607 | | } |
| | 608 | |
|
| 1 | 609 | | Body = data ?? throw new ArgumentNullException(nameof(data), "Data cannot be null for binary response."); |
| 1 | 610 | | ContentType = contentType; |
| 1 | 611 | | StatusCode = statusCode; |
| 1 | 612 | | } |
| | 613 | | /// <summary> |
| | 614 | | /// Writes a stream response with the specified stream, status code, and content type. |
| | 615 | | /// </summary> |
| | 616 | | /// <param name="stream">The stream to send in the response.</param> |
| | 617 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 618 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 619 | | public void WriteStreamResponse(Stream stream, int statusCode = StatusCodes.Status200OK, string contentType = "appli |
| | 620 | | { |
| 3 | 621 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 622 | | { |
| 3 | 623 | | Log.Debug("Writing stream response, StatusCode={StatusCode}, ContentType={ContentType}", statusCode, content |
| | 624 | | } |
| | 625 | |
|
| 3 | 626 | | Body = stream; |
| 3 | 627 | | ContentType = contentType; |
| 3 | 628 | | StatusCode = statusCode; |
| 3 | 629 | | } |
| | 630 | | #endregion |
| | 631 | |
|
| | 632 | | #region Error Responses |
| | 633 | | /// <summary> |
| | 634 | | /// Structured payload for error responses. |
| | 635 | | /// </summary> |
| | 636 | | internal record ErrorPayload |
| | 637 | | { |
| 24 | 638 | | public string Error { get; init; } = default!; |
| 25 | 639 | | public string? Details { get; init; } |
| 27 | 640 | | public string? Exception { get; init; } |
| 26 | 641 | | public string? StackTrace { get; init; } |
| 48 | 642 | | public int Status { get; init; } |
| 24 | 643 | | public string Reason { get; init; } = default!; |
| 24 | 644 | | public string Timestamp { get; init; } = default!; |
| 18 | 645 | | public string? Path { get; init; } |
| 18 | 646 | | public string? Method { get; init; } |
| | 647 | | } |
| | 648 | |
|
| | 649 | | /// <summary> |
| | 650 | | /// Write an error response with a custom message. |
| | 651 | | /// Chooses JSON/YAML/XML/plain-text based on override → Accept → default JSON. |
| | 652 | | /// </summary> |
| | 653 | | public async Task WriteErrorResponseAsync( |
| | 654 | | string message, |
| | 655 | | int statusCode = StatusCodes.Status500InternalServerError, |
| | 656 | | string? contentType = null, |
| | 657 | | string? details = null) |
| | 658 | | { |
| 9 | 659 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 660 | | { |
| 9 | 661 | | Log.Debug("Writing error response, StatusCode={StatusCode}, ContentType={ContentType}, Message={Message}", |
| 9 | 662 | | statusCode, contentType, message); |
| | 663 | | } |
| | 664 | |
|
| 9 | 665 | | if (string.IsNullOrWhiteSpace(message)) |
| | 666 | | { |
| 0 | 667 | | throw new ArgumentNullException(nameof(message)); |
| | 668 | | } |
| | 669 | |
|
| 9 | 670 | | Log.Warning("Writing error response with status {StatusCode}: {Message}", statusCode, message); |
| | 671 | |
|
| 9 | 672 | | var payload = new ErrorPayload |
| 9 | 673 | | { |
| 9 | 674 | | Error = message, |
| 9 | 675 | | Details = details, |
| 9 | 676 | | Exception = null, |
| 9 | 677 | | StackTrace = null, |
| 9 | 678 | | Status = statusCode, |
| 9 | 679 | | Reason = ReasonPhrases.GetReasonPhrase(statusCode), |
| 9 | 680 | | Timestamp = DateTime.UtcNow.ToString("o"), |
| 9 | 681 | | Path = Request?.Path, |
| 9 | 682 | | Method = Request?.Method |
| 9 | 683 | | }; |
| | 684 | |
|
| 9 | 685 | | await WriteFormattedErrorResponseAsync(payload, contentType); |
| 9 | 686 | | } |
| | 687 | |
|
| | 688 | | /// <summary> |
| | 689 | | /// Writes an error response with a custom message. |
| | 690 | | /// Chooses JSON/YAML/XML/plain-text based on override → Accept → default JSON. |
| | 691 | | /// </summary> |
| | 692 | | /// <param name="message">The error message to include in the response.</param> |
| | 693 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 694 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 695 | | /// <param name="details">Optional details to include in the response.</param> |
| | 696 | | public void WriteErrorResponse( |
| | 697 | | string message, |
| | 698 | | int statusCode = StatusCodes.Status500InternalServerError, |
| | 699 | | string? contentType = null, |
| 1 | 700 | | string? details = null) => WriteErrorResponseAsync(message, statusCode, contentType, details).GetAwaiter().GetResu |
| | 701 | |
|
| | 702 | |
|
| | 703 | | /// <summary> |
| | 704 | | /// Asynchronously writes an error response based on an exception. |
| | 705 | | /// Chooses JSON/YAML/XML/plain-text based on override → Accept → default JSON. |
| | 706 | | /// </summary> |
| | 707 | | /// <param name="ex">The exception to report.</param> |
| | 708 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 709 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 710 | | /// <param name="includeStack">Whether to include the stack trace in the response.</param> |
| | 711 | | public async Task WriteErrorResponseAsync( |
| | 712 | | Exception ex, |
| | 713 | | int statusCode = StatusCodes.Status500InternalServerError, |
| | 714 | | string? contentType = null, |
| | 715 | | bool includeStack = true) |
| | 716 | | { |
| 3 | 717 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 718 | | { |
| 3 | 719 | | Log.Debug("Writing error response from exception, StatusCode={StatusCode}, ContentType={ContentType}, Includ |
| 3 | 720 | | statusCode, contentType, includeStack); |
| | 721 | | } |
| | 722 | |
|
| 3 | 723 | | ArgumentNullException.ThrowIfNull(ex); |
| | 724 | |
|
| 3 | 725 | | Log.Warning(ex, "Writing error response with status {StatusCode}", statusCode); |
| | 726 | |
|
| 3 | 727 | | var payload = new ErrorPayload |
| 3 | 728 | | { |
| 3 | 729 | | Error = ex.Message, |
| 3 | 730 | | Details = null, |
| 3 | 731 | | Exception = ex.GetType().Name, |
| 3 | 732 | | StackTrace = includeStack ? ex.ToString() : null, |
| 3 | 733 | | Status = statusCode, |
| 3 | 734 | | Reason = ReasonPhrases.GetReasonPhrase(statusCode), |
| 3 | 735 | | Timestamp = DateTime.UtcNow.ToString("o"), |
| 3 | 736 | | Path = Request?.Path, |
| 3 | 737 | | Method = Request?.Method |
| 3 | 738 | | }; |
| | 739 | |
|
| 3 | 740 | | await WriteFormattedErrorResponseAsync(payload, contentType); |
| 3 | 741 | | } |
| | 742 | | /// <summary> |
| | 743 | | /// Writes an error response based on an exception. |
| | 744 | | /// Chooses JSON/YAML/XML/plain-text based on override → Accept → default JSON. |
| | 745 | | /// </summary> |
| | 746 | | /// <param name="ex">The exception to report.</param> |
| | 747 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 748 | | /// <param name="contentType">The MIME type of the response content.</param> |
| | 749 | | /// <param name="includeStack">Whether to include the stack trace in the response.</param> |
| | 750 | | public void WriteErrorResponse( |
| | 751 | | Exception ex, |
| | 752 | | int statusCode = StatusCodes.Status500InternalServerError, |
| | 753 | | string? contentType = null, |
| 1 | 754 | | bool includeStack = true) => WriteErrorResponseAsync(ex, statusCode, contentType, includeStack).GetAwaiter() |
| | 755 | |
|
| | 756 | | /// <summary> |
| | 757 | | /// Internal dispatcher: serializes the payload according to the chosen content-type. |
| | 758 | | /// </summary> |
| | 759 | | private async Task WriteFormattedErrorResponseAsync(ErrorPayload payload, string? contentType = null) |
| | 760 | | { |
| 12 | 761 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 762 | | { |
| 12 | 763 | | Log.Debug("Writing formatted error response, ContentType={ContentType}, Status={Status}", contentType, paylo |
| | 764 | | } |
| | 765 | |
|
| 12 | 766 | | if (string.IsNullOrWhiteSpace(contentType)) |
| | 767 | | { |
| 10 | 768 | | _ = Request.Headers.TryGetValue("Accept", out var acceptHeader); |
| 10 | 769 | | contentType = (acceptHeader ?? "text/plain") |
| 10 | 770 | | .ToLowerInvariant(); |
| | 771 | | } |
| 12 | 772 | | if (contentType.Contains("json")) |
| | 773 | | { |
| 2 | 774 | | await WriteJsonResponseAsync(payload, payload.Status); |
| | 775 | | } |
| 10 | 776 | | else if (contentType.Contains("yaml") || contentType.Contains("yml")) |
| | 777 | | { |
| 2 | 778 | | await WriteYamlResponseAsync(payload, payload.Status); |
| | 779 | | } |
| 8 | 780 | | else if (contentType.Contains("xml")) |
| | 781 | | { |
| 2 | 782 | | await WriteXmlResponseAsync(payload, payload.Status); |
| | 783 | | } |
| | 784 | | else |
| | 785 | | { |
| | 786 | | // Plain-text fallback |
| 6 | 787 | | var lines = new List<string> |
| 6 | 788 | | { |
| 6 | 789 | | $"Status: {payload.Status} ({payload.Reason})", |
| 6 | 790 | | $"Error: {payload.Error}", |
| 6 | 791 | | $"Time: {payload.Timestamp}" |
| 6 | 792 | | }; |
| | 793 | |
|
| 6 | 794 | | if (!string.IsNullOrWhiteSpace(payload.Details)) |
| | 795 | | { |
| 1 | 796 | | lines.Add("Details:\n" + payload.Details); |
| | 797 | | } |
| | 798 | |
|
| 6 | 799 | | if (!string.IsNullOrWhiteSpace(payload.Exception)) |
| | 800 | | { |
| 3 | 801 | | lines.Add($"Exception: {payload.Exception}"); |
| | 802 | | } |
| | 803 | |
|
| 6 | 804 | | if (!string.IsNullOrWhiteSpace(payload.StackTrace)) |
| | 805 | | { |
| 2 | 806 | | lines.Add("StackTrace:\n" + payload.StackTrace); |
| | 807 | | } |
| | 808 | |
|
| 6 | 809 | | var text = string.Join("\n", lines); |
| 6 | 810 | | await WriteTextResponseAsync(text, payload.Status, "text/plain"); |
| | 811 | | } |
| 12 | 812 | | } |
| | 813 | |
|
| | 814 | | #endregion |
| | 815 | | #region HTML Response Helpers |
| | 816 | |
|
| | 817 | | /// <summary> |
| | 818 | | /// Renders a template string by replacing placeholders in the format {{key}} with corresponding values from the pro |
| | 819 | | /// </summary> |
| | 820 | | /// <param name="template">The template string containing placeholders.</param> |
| | 821 | | /// <param name="vars">A dictionary of variables to replace in the template.</param> |
| | 822 | | /// <returns>The rendered string with placeholders replaced by variable values.</returns> |
| | 823 | | private static string RenderInlineTemplate( |
| | 824 | | string template, |
| | 825 | | IReadOnlyDictionary<string, object?> vars) |
| | 826 | | { |
| 2 | 827 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 828 | | { |
| 2 | 829 | | Log.Debug("Rendering inline template, TemplateLength={TemplateLength}, VarsCount={VarsCount}", |
| 2 | 830 | | template?.Length ?? 0, vars?.Count ?? 0); |
| | 831 | | } |
| | 832 | |
|
| 2 | 833 | | if (string.IsNullOrEmpty(template)) |
| | 834 | | { |
| 0 | 835 | | return string.Empty; |
| | 836 | | } |
| | 837 | |
|
| 2 | 838 | | if (vars is null || vars.Count == 0) |
| | 839 | | { |
| 0 | 840 | | return template; |
| | 841 | | } |
| | 842 | |
|
| 2 | 843 | | var render = RenderInline(template, vars); |
| | 844 | |
|
| 2 | 845 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 846 | | { |
| 2 | 847 | | Log.Debug("Rendered template length: {RenderedLength}", render.Length); |
| | 848 | | } |
| | 849 | |
|
| 2 | 850 | | return render; |
| | 851 | | } |
| | 852 | |
|
| | 853 | | /// <summary> |
| | 854 | | /// Renders a template string by replacing placeholders in the format {{key}} with corresponding values from the pro |
| | 855 | | /// </summary> |
| | 856 | | /// <param name="template">The template string containing placeholders.</param> |
| | 857 | | /// <param name="vars">A dictionary of variables to replace in the template.</param> |
| | 858 | | /// <returns>The rendered string with placeholders replaced by variable values.</returns> |
| | 859 | | private static string RenderInline(string template, IReadOnlyDictionary<string, object?> vars) |
| | 860 | | { |
| 2 | 861 | | var sb = new StringBuilder(template.Length); |
| | 862 | |
|
| | 863 | | // Iterate through the template |
| 2 | 864 | | var i = 0; |
| 39 | 865 | | while (i < template.Length) |
| | 866 | | { |
| | 867 | | // opening “{{” |
| 37 | 868 | | if (template[i] == '{' && i + 1 < template.Length && template[i + 1] == '{') |
| | 869 | | { |
| 3 | 870 | | var start = i + 2; // after “{{” |
| 3 | 871 | | var end = template.IndexOf("}}", start, StringComparison.Ordinal); |
| | 872 | |
|
| 3 | 873 | | if (end > start) // found closing “}}” |
| | 874 | | { |
| 3 | 875 | | var rawKey = template[start..end].Trim(); |
| | 876 | |
|
| 3 | 877 | | if (TryResolveValue(rawKey, vars, out var value) && value is not null) |
| | 878 | | { |
| 3 | 879 | | _ = sb.Append(value); // append resolved value |
| | 880 | | } |
| | 881 | | else |
| | 882 | | { |
| 0 | 883 | | _ = sb.Append("{{").Append(rawKey).Append("}}"); // leave it as-is if unknown |
| | 884 | | } |
| | 885 | |
|
| 3 | 886 | | i = end + 2; // jump past the “}}” |
| 3 | 887 | | continue; |
| | 888 | | } |
| | 889 | | } |
| | 890 | |
|
| | 891 | | // ordinary character |
| 34 | 892 | | _ = sb.Append(template[i]); |
| 34 | 893 | | i++; // move to the next character |
| | 894 | | } |
| 2 | 895 | | return sb.ToString(); |
| | 896 | | } |
| | 897 | |
|
| | 898 | |
|
| | 899 | |
|
| | 900 | | /// <summary> |
| | 901 | | /// Resolves a dotted path like “Request.Path” through nested dictionaries |
| | 902 | | /// and/or object properties (case-insensitive). |
| | 903 | | /// </summary> |
| | 904 | | private static bool TryResolveValue( |
| | 905 | | string path, |
| | 906 | | IReadOnlyDictionary<string, object?> root, |
| | 907 | | out object? value) |
| | 908 | | { |
| 3 | 909 | | value = null; |
| | 910 | |
|
| 3 | 911 | | if (string.IsNullOrWhiteSpace(path)) |
| | 912 | | { |
| 0 | 913 | | return false; |
| | 914 | | } |
| | 915 | |
|
| 3 | 916 | | object? current = root; |
| 16 | 917 | | foreach (var segment in path.Split('.')) |
| | 918 | | { |
| 5 | 919 | | if (current is null) |
| | 920 | | { |
| 0 | 921 | | return false; |
| | 922 | | } |
| | 923 | |
|
| | 924 | | // ① Handle dictionary look-ups (IReadOnlyDictionary or IDictionary) |
| 5 | 925 | | if (current is IReadOnlyDictionary<string, object?> roDict) |
| | 926 | | { |
| 3 | 927 | | if (!roDict.TryGetValue(segment, out current)) |
| | 928 | | { |
| 0 | 929 | | return false; |
| | 930 | | } |
| | 931 | |
|
| | 932 | | continue; |
| | 933 | | } |
| | 934 | |
|
| 2 | 935 | | if (current is IDictionary dict) |
| | 936 | | { |
| 0 | 937 | | if (!dict.Contains(segment)) |
| | 938 | | { |
| 0 | 939 | | return false; |
| | 940 | | } |
| | 941 | |
|
| 0 | 942 | | current = dict[segment]; |
| 0 | 943 | | continue; |
| | 944 | | } |
| | 945 | |
|
| | 946 | | // ② Handle property look-ups via reflection |
| 2 | 947 | | var prop = current.GetType().GetProperty( |
| 2 | 948 | | segment, |
| 2 | 949 | | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); |
| | 950 | |
|
| 2 | 951 | | if (prop is null) |
| | 952 | | { |
| 0 | 953 | | return false; |
| | 954 | | } |
| | 955 | |
|
| 2 | 956 | | current = prop.GetValue(current); |
| | 957 | | } |
| | 958 | |
|
| 3 | 959 | | value = current; |
| 3 | 960 | | return true; |
| | 961 | | } |
| | 962 | |
|
| | 963 | |
|
| | 964 | | /// <summary> |
| | 965 | | /// Asynchronously writes an HTML response, rendering the provided template string and replacing placeholders with v |
| | 966 | | /// </summary> |
| | 967 | | /// <param name="template">The HTML template string containing placeholders.</param> |
| | 968 | | /// <param name="vars">A dictionary of variables to replace in the template.</param> |
| | 969 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 970 | | public async Task WriteHtmlResponseAsync( |
| | 971 | | string template, |
| | 972 | | IReadOnlyDictionary<string, object?>? vars, |
| | 973 | | int statusCode = 200) |
| | 974 | | { |
| 2 | 975 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 976 | | { |
| 2 | 977 | | Log.Debug("Writing HTML response (async), StatusCode={StatusCode}, TemplateLength={TemplateLength}", statusC |
| | 978 | | } |
| | 979 | |
|
| 2 | 980 | | if (vars is null || vars.Count == 0) |
| | 981 | | { |
| 0 | 982 | | await WriteTextResponseAsync(template, statusCode, "text/html"); |
| | 983 | | } |
| | 984 | | else |
| | 985 | | { |
| 2 | 986 | | await WriteTextResponseAsync(RenderInlineTemplate(template, vars), statusCode, "text/html"); |
| | 987 | | } |
| 2 | 988 | | } |
| | 989 | |
|
| | 990 | | /// <summary> |
| | 991 | | /// Asynchronously reads an HTML file, merges in placeholders from the provided dictionary, and writes the result as |
| | 992 | | /// </summary> |
| | 993 | | /// <param name="filePath">The path to the HTML file to read.</param> |
| | 994 | | /// <param name="vars">A dictionary of variables to replace in the template.</param> |
| | 995 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 996 | | public async Task WriteHtmlResponseFromFileAsync( |
| | 997 | | string filePath, |
| | 998 | | IReadOnlyDictionary<string, object?> vars, |
| | 999 | | int statusCode = 200) |
| | 1000 | | { |
| 1 | 1001 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 1002 | | { |
| 1 | 1003 | | Log.Debug("Writing HTML response from file (async), FilePath={FilePath}, StatusCode={StatusCode}", filePath, |
| | 1004 | | } |
| | 1005 | |
|
| 1 | 1006 | | if (!File.Exists(filePath)) |
| | 1007 | | { |
| 0 | 1008 | | WriteTextResponse($"<!-- File not found: {filePath} -->", 404, "text/html"); |
| 0 | 1009 | | return; |
| | 1010 | | } |
| | 1011 | |
|
| 1 | 1012 | | var template = await File.ReadAllTextAsync(filePath); |
| 1 | 1013 | | WriteHtmlResponseAsync(template, vars, statusCode).GetAwaiter().GetResult(); |
| 1 | 1014 | | } |
| | 1015 | |
|
| | 1016 | |
|
| | 1017 | | /// <summary> |
| | 1018 | | /// Renders the given HTML string with placeholders and writes it as a response. |
| | 1019 | | /// </summary> |
| | 1020 | | /// <param name="template">The HTML template string containing placeholders.</param> |
| | 1021 | | /// <param name="vars">A dictionary of variables to replace in the template.</param> |
| | 1022 | | /// <param name="statusCode">The HTTP status code for the response.</param> |
| | 1023 | | public void WriteHtmlResponse( |
| | 1024 | | string template, |
| | 1025 | | IReadOnlyDictionary<string, object?>? vars, |
| 0 | 1026 | | int statusCode = 200) => WriteHtmlResponseAsync(template, vars, statusCode).GetAwaiter().GetResult(); |
| | 1027 | |
|
| | 1028 | | /// <summary> |
| | 1029 | | /// Reads an .html file, merges in placeholders, and writes it. |
| | 1030 | | /// </summary> |
| | 1031 | | public void WriteHtmlResponseFromFile( |
| | 1032 | | string filePath, |
| | 1033 | | IReadOnlyDictionary<string, object?> vars, |
| 0 | 1034 | | int statusCode = 200) => WriteHtmlResponseFromFileAsync(filePath, vars, statusCode).GetAwaiter().GetResult(); |
| | 1035 | |
|
| | 1036 | | #endregion |
| | 1037 | |
|
| | 1038 | | #region Apply to HttpResponse |
| | 1039 | | /// <summary> |
| | 1040 | | /// Applies the current KestrunResponse to the specified HttpResponse, setting status, headers, cookies, and writing |
| | 1041 | | /// </summary> |
| | 1042 | | /// <param name="response">The HttpResponse to apply the response to.</param> |
| | 1043 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | 1044 | | public async Task ApplyTo(HttpResponse response) |
| | 1045 | | { |
| 16 | 1046 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 1047 | | { |
| 16 | 1048 | | Log.Debug("Applying KestrunResponse to HttpResponse, StatusCode={StatusCode}, ContentType={ContentType}, Bod |
| 16 | 1049 | | StatusCode, ContentType, Body?.GetType().Name ?? "null"); |
| | 1050 | | } |
| | 1051 | |
|
| 16 | 1052 | | if (!string.IsNullOrEmpty(RedirectUrl)) |
| | 1053 | | { |
| 1 | 1054 | | response.Redirect(RedirectUrl); |
| 1 | 1055 | | return; |
| | 1056 | | } |
| | 1057 | |
|
| | 1058 | | try |
| | 1059 | | { |
| 15 | 1060 | | EnsureStatusAndContentType(response); |
| 15 | 1061 | | ApplyContentDispositionHeader(response); |
| 15 | 1062 | | ApplyHeadersAndCookies(response); |
| 15 | 1063 | | if (Body is not null) |
| | 1064 | | { |
| 13 | 1065 | | await WriteBodyAsync(response).ConfigureAwait(false); |
| | 1066 | | } |
| 15 | 1067 | | } |
| 0 | 1068 | | catch (Exception ex) |
| | 1069 | | { |
| 0 | 1070 | | Console.WriteLine($"Error applying response: {ex.Message}"); |
| | 1071 | | // Optionally, you can log the exception or handle it as needed |
| 0 | 1072 | | throw; |
| | 1073 | | } |
| 16 | 1074 | | } |
| | 1075 | |
|
| | 1076 | | /// <summary> |
| | 1077 | | /// Ensures the HTTP response has the correct status code and content type. |
| | 1078 | | /// </summary> |
| | 1079 | | /// <param name="response">The HTTP response to apply the status and content type to.</param> |
| | 1080 | | private void EnsureStatusAndContentType(HttpResponse response) |
| | 1081 | | { |
| 15 | 1082 | | response.StatusCode = StatusCode; |
| 15 | 1083 | | if (!string.IsNullOrEmpty(ContentType) && |
| 15 | 1084 | | IsTextBasedContentType(ContentType) && |
| 15 | 1085 | | !ContentType.Contains("charset=", StringComparison.OrdinalIgnoreCase)) |
| | 1086 | | { |
| 4 | 1087 | | ContentType = ContentType.TrimEnd(';') + $"; charset={AcceptCharset.WebName}"; |
| | 1088 | | } |
| 15 | 1089 | | response.ContentType = ContentType; |
| 15 | 1090 | | } |
| | 1091 | |
|
| | 1092 | | /// <summary> |
| | 1093 | | /// Applies the Content-Disposition header to the HTTP response. |
| | 1094 | | /// </summary> |
| | 1095 | | /// <param name="response">The HTTP response to apply the header to.</param> |
| | 1096 | | private void ApplyContentDispositionHeader(HttpResponse response) |
| | 1097 | | { |
| 15 | 1098 | | if (ContentDisposition.Type == ContentDispositionType.NoContentDisposition) |
| | 1099 | | { |
| 13 | 1100 | | return; |
| | 1101 | | } |
| | 1102 | |
|
| 2 | 1103 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | 1104 | | { |
| 2 | 1105 | | Log.Debug("Setting Content-Disposition header, Type={Type}, FileName={FileName}", |
| 2 | 1106 | | ContentDisposition.Type, ContentDisposition.FileName); |
| | 1107 | | } |
| | 1108 | |
|
| 2 | 1109 | | var dispositionValue = ContentDisposition.Type switch |
| 2 | 1110 | | { |
| 2 | 1111 | | ContentDispositionType.Attachment => "attachment", |
| 0 | 1112 | | ContentDispositionType.Inline => "inline", |
| 0 | 1113 | | _ => throw new InvalidOperationException("Invalid Content-Disposition type") |
| 2 | 1114 | | }; |
| | 1115 | |
|
| 2 | 1116 | | if (string.IsNullOrEmpty(ContentDisposition.FileName) && Body is IFileInfo fi) |
| | 1117 | | { |
| | 1118 | | // default filename: use the file's name |
| 1 | 1119 | | ContentDisposition.FileName = fi.Name; |
| | 1120 | | } |
| | 1121 | |
|
| 2 | 1122 | | if (!string.IsNullOrEmpty(ContentDisposition.FileName)) |
| | 1123 | | { |
| 2 | 1124 | | var escapedFileName = WebUtility.UrlEncode(ContentDisposition.FileName); |
| 2 | 1125 | | dispositionValue += $"; filename=\"{escapedFileName}\""; |
| | 1126 | | } |
| | 1127 | |
|
| 2 | 1128 | | response.Headers.Append("Content-Disposition", dispositionValue); |
| 2 | 1129 | | } |
| | 1130 | |
|
| | 1131 | | /// <summary> |
| | 1132 | | /// Applies headers and cookies to the HTTP response. |
| | 1133 | | /// </summary> |
| | 1134 | | /// <param name="response">The HTTP response to apply the headers and cookies to.</param> |
| | 1135 | | private void ApplyHeadersAndCookies(HttpResponse response) |
| | 1136 | | { |
| 15 | 1137 | | if (Headers is not null) |
| | 1138 | | { |
| 30 | 1139 | | foreach (var kv in Headers) |
| | 1140 | | { |
| 0 | 1141 | | response.Headers[kv.Key] = kv.Value; |
| | 1142 | | } |
| | 1143 | | } |
| 15 | 1144 | | if (Cookies is not null) |
| | 1145 | | { |
| 0 | 1146 | | foreach (var cookie in Cookies) |
| | 1147 | | { |
| 0 | 1148 | | response.Headers.Append("Set-Cookie", cookie); |
| | 1149 | | } |
| | 1150 | | } |
| 15 | 1151 | | } |
| | 1152 | |
|
| | 1153 | | /// <summary> |
| | 1154 | | /// Writes the response body to the HTTP response. |
| | 1155 | | /// </summary> |
| | 1156 | | /// <param name="response">The HTTP response to write to.</param> |
| | 1157 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | 1158 | | private async Task WriteBodyAsync(HttpResponse response) |
| | 1159 | | { |
| 13 | 1160 | | var bodyValue = Body; // capture to avoid nullability warnings when mutated in default |
| | 1161 | | switch (bodyValue) |
| | 1162 | | { |
| | 1163 | | case IFileInfo fileInfo: |
| 1 | 1164 | | Log.Debug("Sending file {FileName} (Length={Length})", fileInfo.Name, fileInfo.Length); |
| 1 | 1165 | | response.ContentLength = fileInfo.Length; |
| 1 | 1166 | | response.Headers.LastModified = fileInfo.LastModified.ToString("R"); |
| 1 | 1167 | | await response.SendFileAsync( |
| 1 | 1168 | | file: fileInfo, |
| 1 | 1169 | | offset: 0, |
| 1 | 1170 | | count: fileInfo.Length, |
| 1 | 1171 | | cancellationToken: response.HttpContext.RequestAborted |
| 1 | 1172 | | ); |
| 1 | 1173 | | break; |
| | 1174 | |
|
| | 1175 | | case byte[] bytes: |
| 1 | 1176 | | response.ContentLength = bytes.LongLength; |
| 1 | 1177 | | await response.Body.WriteAsync(bytes, response.HttpContext.RequestAborted); |
| 1 | 1178 | | await response.Body.FlushAsync(response.HttpContext.RequestAborted); |
| 1 | 1179 | | break; |
| | 1180 | |
|
| | 1181 | | case Stream stream: |
| 2 | 1182 | | var seekable = stream.CanSeek; |
| 2 | 1183 | | Log.Debug("Sending stream (seekable={Seekable}, len={Len})", |
| 2 | 1184 | | seekable, seekable ? stream.Length : -1); |
| | 1185 | |
|
| 2 | 1186 | | if (seekable) |
| | 1187 | | { |
| 1 | 1188 | | response.ContentLength = stream.Length; |
| 1 | 1189 | | stream.Position = 0; |
| | 1190 | | } |
| | 1191 | | else |
| | 1192 | | { |
| 1 | 1193 | | response.ContentLength = null; |
| | 1194 | | } |
| | 1195 | |
|
| | 1196 | | const int BufferSize = 64 * 1024; // 64 KB |
| 2 | 1197 | | var buffer = ArrayPool<byte>.Shared.Rent(BufferSize); |
| | 1198 | | try |
| | 1199 | | { |
| | 1200 | | int bytesRead; |
| 4 | 1201 | | while ((bytesRead = await stream.ReadAsync(buffer.AsMemory(0, BufferSize), response.HttpContext.Requ |
| | 1202 | | { |
| 2 | 1203 | | await response.Body.WriteAsync(buffer.AsMemory(0, bytesRead), response.HttpContext.RequestAborte |
| | 1204 | | } |
| 2 | 1205 | | } |
| | 1206 | | finally |
| | 1207 | | { |
| 2 | 1208 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 1209 | | } |
| 2 | 1210 | | await response.Body.FlushAsync(response.HttpContext.RequestAborted); |
| 2 | 1211 | | break; |
| | 1212 | |
|
| | 1213 | | case string str: |
| 9 | 1214 | | var data = AcceptCharset.GetBytes(str); |
| 9 | 1215 | | response.ContentLength = data.Length; |
| 9 | 1216 | | await response.Body.WriteAsync(data, response.HttpContext.RequestAborted); |
| 9 | 1217 | | await response.Body.FlushAsync(response.HttpContext.RequestAborted); |
| 9 | 1218 | | break; |
| | 1219 | |
|
| | 1220 | | default: |
| 0 | 1221 | | var bodyType = bodyValue?.GetType().Name ?? "null"; |
| 0 | 1222 | | Body = "Unsupported body type: " + bodyType; |
| 0 | 1223 | | Log.Warning("Unsupported body type: {BodyType}", bodyType); |
| 0 | 1224 | | response.StatusCode = StatusCodes.Status500InternalServerError; |
| 0 | 1225 | | response.ContentType = "text/plain; charset=utf-8"; |
| 0 | 1226 | | response.ContentLength = Body.ToString()?.Length ?? null; |
| | 1227 | | break; |
| | 1228 | | } |
| 13 | 1229 | | } |
| | 1230 | | #endregion |
| | 1231 | | } |