|
| 1 | +using System.Net; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | +using Tigernet.Hosting.Enums; |
| 5 | + |
| 6 | +namespace Tigernet.Hosting.Extensions; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Extension class for HttpListenerRequest that provides methods for retrieving data from different parts of the request. |
| 10 | +/// </summary> |
| 11 | +internal static class RequestListenerExtension |
| 12 | +{ |
| 13 | + internal static object? GetInjectingRequestData( |
| 14 | + this HttpListenerRequest request, |
| 15 | + Type returnType, Transfer transfer, |
| 16 | + string selector = null!) |
| 17 | + { |
| 18 | + return transfer switch |
| 19 | + { |
| 20 | + Transfer.FromBody => request.GetDataFromRequestBody(returnType), |
| 21 | + Transfer.FromHeader => request.GetDataFromRequestHeader(selector, returnType), |
| 22 | + Transfer.FromRoute => request.GetDataFromRequestRoute(selector, returnType), |
| 23 | + Transfer.FromQuery => request.GetDataFromRequestQuery(returnType), |
| 24 | + _ => default |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + internal static T? GetInjectingRequestData<T>( |
| 29 | + this HttpListenerRequest request, Transfer transfer, |
| 30 | + string selector = null!) |
| 31 | + { |
| 32 | + return transfer switch |
| 33 | + { |
| 34 | + Transfer.FromBody => request.GetDataFromRequestBody<T>(), |
| 35 | + Transfer.FromHeader => request.GetDataFromRequestHeader<T>(selector), |
| 36 | + Transfer.FromRoute => request.GetDataFromRequestRoute<T>(selector), |
| 37 | + Transfer.FromQuery => request.GetDataFromRequestQuery<T>(), |
| 38 | + _ => default |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + #region Private methods |
| 43 | + |
| 44 | + #region Map from request body |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Retrieves data from the request body and returns it as the specified type. |
| 48 | + /// </summary> |
| 49 | + /// <typeparam name="T">The type to return the data as.</typeparam> |
| 50 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 51 | + /// <returns>The data extracted from the request body as the specified type.</returns> |
| 52 | + private static T? GetDataFromRequestBody<T>(this HttpListenerRequest request) |
| 53 | + { |
| 54 | + using Stream bodyStream = request.InputStream; |
| 55 | + // Determine the encoding and content type of the request |
| 56 | + Encoding encoding = request.ContentEncoding; |
| 57 | + |
| 58 | + // Read the data from the request body |
| 59 | + using StreamReader reader = new StreamReader(bodyStream, encoding); |
| 60 | + string requestBody = reader.ReadToEnd(); |
| 61 | + |
| 62 | + if (requestBody != String.Empty) |
| 63 | + return JsonSerializer.Deserialize<T>(requestBody); |
| 64 | + |
| 65 | + throw new NullReferenceException("In request body value is not given. "); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Retrieves data from the request body and returns it as the specified type. |
| 70 | + /// </summary> |
| 71 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 72 | + /// <param name="returnType">Returned type of the data.</param> |
| 73 | + /// <returns>The data extracted from the request body as the specified type.</returns> |
| 74 | + private static object? GetDataFromRequestBody(this HttpListenerRequest request, Type returnType) |
| 75 | + { |
| 76 | + using Stream bodyStream = request.InputStream; |
| 77 | + // Determine the encoding and content type of the request |
| 78 | + Encoding encoding = request.ContentEncoding; |
| 79 | + |
| 80 | + // Read the data from the request body |
| 81 | + using StreamReader reader = new StreamReader(bodyStream, encoding); |
| 82 | + string requestBody = reader.ReadToEnd(); |
| 83 | + |
| 84 | + if (requestBody != String.Empty) |
| 85 | + return JsonSerializer.Deserialize(requestBody, returnType); |
| 86 | + |
| 87 | + throw new NullReferenceException("In request body value is not given. "); |
| 88 | + } |
| 89 | + |
| 90 | + #endregion |
| 91 | + |
| 92 | + #region Map from request header |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Retrieves data from the specified header in the request and returns it as the specified type. |
| 96 | + /// </summary> |
| 97 | + /// <typeparam name="T">The type to return the data as.</typeparam> |
| 98 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 99 | + /// <param name="key">The key of the header to extract data from.</param> |
| 100 | + /// <returns>The data extracted from the specified header as the specified type.</returns> |
| 101 | + private static T? GetDataFromRequestHeader<T>( |
| 102 | + this HttpListenerRequest request, |
| 103 | + string key) |
| 104 | + { |
| 105 | + var header = request.Headers.Get(key); |
| 106 | + if (header != null) |
| 107 | + return JsonSerializer.Deserialize<T>(header); |
| 108 | + |
| 109 | + throw new NullReferenceException($"In request header with given key '{key}' is not value is not detected"); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Retrieves data from the specified header in the request and returns it as the specified type. |
| 114 | + /// </summary> |
| 115 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 116 | + /// <param name="key">The key of the header to extract data from.</param> |
| 117 | + /// <param name="returnType">return type of the request data</param> |
| 118 | + /// <returns>The data extracted from the specified header as the specified type.</returns> |
| 119 | + private static object? GetDataFromRequestHeader( |
| 120 | + this HttpListenerRequest request, |
| 121 | + string key, Type returnType) |
| 122 | + { |
| 123 | + string? header = request.Headers.Get(key); |
| 124 | + return JsonSerializer.Deserialize(header ?? "", returnType); |
| 125 | + } |
| 126 | + |
| 127 | + #endregion |
| 128 | + |
| 129 | + #region Map from request route |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Retrieves data from the specified route in the request and returns it as the specified type. |
| 133 | + /// </summary> |
| 134 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 135 | + /// <param name="key">The key of the route to extract data from.</param> |
| 136 | + /// <returns>The data extracted from the specified route as the specified type.</returns> |
| 137 | + private static T? GetDataFromRequestRoute<T>( |
| 138 | + this HttpListenerRequest request, |
| 139 | + string key) |
| 140 | + { |
| 141 | + var routeValue = request.Url?.Segments[^1]; |
| 142 | + return JsonSerializer.Deserialize<T>(routeValue ?? ""); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Retrieves data from the specified route in the request and returns it as the specified type. |
| 147 | + /// </summary> |
| 148 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 149 | + /// <param name="key">The key of the route to extract data from.</param> |
| 150 | + /// <param name="returnType">return type of the request data value</param> |
| 151 | + /// <returns>The data extracted from the specified route as the specified type.</returns> |
| 152 | + private static object? GetDataFromRequestRoute( |
| 153 | + this HttpListenerRequest request, |
| 154 | + string key, Type returnType) |
| 155 | + { |
| 156 | + var routeValue = request.Url?.Segments[^1]; |
| 157 | + return JsonSerializer.Deserialize(routeValue ?? "", returnType); |
| 158 | + } |
| 159 | + |
| 160 | + #endregion |
| 161 | + |
| 162 | + #region Map from request query |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Retrieves data from the request query and returns it as the specified type. |
| 166 | + /// </summary> |
| 167 | + /// <typeparam name="T">The type to return the data as.</typeparam> |
| 168 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 169 | + /// <returns>The data extracted from the request query as the specified type.</returns> |
| 170 | + private static T? GetDataFromRequestQuery<T>( |
| 171 | + this HttpListenerRequest request) |
| 172 | + { |
| 173 | + var queryValue = request.QueryString.ToString(); |
| 174 | + return JsonSerializer.Deserialize<T>(queryValue ?? ""); |
| 175 | + } |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Retrieves data from the request query and returns it as the specified type. |
| 179 | + /// </summary> |
| 180 | + /// <param name="request">The HttpListenerRequest to extract data from.</param> |
| 181 | + /// <param name="returnType">returned type of the request coming data</param> |
| 182 | + /// <returns>The data extracted from the request query as the specified type.</returns> |
| 183 | + private static object? GetDataFromRequestQuery( |
| 184 | + this HttpListenerRequest request, |
| 185 | + Type returnType) |
| 186 | + { |
| 187 | + var queryValue = request.QueryString.ToString(); |
| 188 | + return JsonSerializer.Deserialize(queryValue ?? "", returnType); |
| 189 | + } |
| 190 | + |
| 191 | + #endregion |
| 192 | + |
| 193 | + #endregion |
| 194 | +} |
| 195 | + |
0 commit comments