| | | 1 | | /******************************************************************************** |
| | | 2 | | * HttpRequestMessageExtensions.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Frozen; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | using System.Net.Http; |
| | | 11 | | |
| | | 12 | | namespace NanoRoute |
| | | 13 | | { |
| | | 14 | | using Internals; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// <see cref="HttpRequestMessage"/> extensions. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <example> |
| | | 20 | | /// <code> |
| | | 21 | | /// bool isContentHeader = HttpRequestMessage.ContentHeaders.Contains("Content-Type"); |
| | | 22 | | /// </code> |
| | | 23 | | /// </example> |
| | | 24 | | public static class HttpRequestMessageExtensions |
| | | 25 | | { |
| | | 26 | | #region Private |
| | | 27 | | private const string |
| | | 28 | | OriginalRequestPropertyKey = "OriginalRequest", |
| | | 29 | | TraceIdPropertyKey = "TraceId"; |
| | | 30 | | |
| | | 31 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 2 | 32 | | private static readonly FrozenSet<string> s_contentHeaders = new List<string> |
| | 2 | 33 | | { |
| | 2 | 34 | | "Allow", |
| | 2 | 35 | | "Content-Disposition", |
| | 2 | 36 | | "Content-Encoding", |
| | 2 | 37 | | "Content-Language", |
| | 2 | 38 | | "Content-Length", |
| | 2 | 39 | | "Content-Location", |
| | 2 | 40 | | "Content-MD5", |
| | 2 | 41 | | "Content-Range", |
| | 2 | 42 | | "Content-Type", |
| | 2 | 43 | | "Expires", |
| | 2 | 44 | | "Last-Modified" |
| | 2 | 45 | | }.ToFrozenSet(StringComparer.OrdinalIgnoreCase); |
| | | 46 | | |
| | | 47 | | private static object? GetProperty(HttpRequestMessage request, string key) |
| | 2 | 48 | | { |
| | 2 | 49 | | Ensure.NotNull(request); |
| | 2 | 50 | | return request.Properties.TryGetValue(key, out object? value) |
| | 2 | 51 | | ? value |
| | 2 | 52 | | : null; |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | private static void SetProperty(HttpRequestMessage request, string key, object? value) |
| | 2 | 56 | | { |
| | 2 | 57 | | Ensure.NotNull(request); |
| | 2 | 58 | | if (value is null) |
| | 1 | 59 | | request.Properties.Remove(key); |
| | | 60 | | else |
| | 2 | 61 | | request.Properties[key] = value; |
| | 2 | 62 | | } |
| | | 63 | | #endregion |
| | | 64 | | |
| | | 65 | | extension(HttpRequestMessage) |
| | | 66 | | { |
| | | 67 | | /// <summary> |
| | | 68 | | /// Content header names. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <example> |
| | | 71 | | /// <code> |
| | | 72 | | /// if (HttpRequestMessage.ContentHeaders.Contains(headerName)) |
| | | 73 | | /// request.Content!.Headers.TryAddWithoutValidation(headerName, values); |
| | | 74 | | /// </code> |
| | | 75 | | /// </example> |
| | 2 | 76 | | public static FrozenSet<string> ContentHeaders => s_contentHeaders; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | extension(HttpRequestMessage request) |
| | | 80 | | { |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets or sets the transport-specific request object that was converted into this <see cref="HttpRequestMe |
| | | 83 | | /// </summary> |
| | | 84 | | /// <exception cref="ArgumentNullException">Thrown when the extended request is <see langword="null"/>.</exc |
| | | 85 | | /// <example> |
| | | 86 | | /// <code> |
| | | 87 | | /// request.OriginalRequest = listenerRequest; |
| | | 88 | | /// HttpListenerRequest? original = request.OriginalRequest as HttpListenerRequest; |
| | | 89 | | /// </code> |
| | | 90 | | /// </example> |
| | | 91 | | public object? OriginalRequest |
| | | 92 | | { |
| | 2 | 93 | | get => GetProperty(request, OriginalRequestPropertyKey); |
| | 2 | 94 | | set => SetProperty(request, OriginalRequestPropertyKey, value); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets or sets the trace identifier associated with this request. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <exception cref="ArgumentNullException">Thrown when the extended request is <see langword="null"/>.</exc |
| | | 101 | | /// <example> |
| | | 102 | | /// <code> |
| | | 103 | | /// request.TraceId = "trace-1"; |
| | | 104 | | /// string? traceId = request.TraceId; |
| | | 105 | | /// </code> |
| | | 106 | | /// </example> |
| | | 107 | | public string? TraceId |
| | | 108 | | { |
| | 2 | 109 | | get => GetProperty(request, TraceIdPropertyKey)?.ToString(); |
| | 2 | 110 | | set => SetProperty(request, TraceIdPropertyKey, value); |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |