| | | 1 | | /******************************************************************************** |
| | | 2 | | * HttpMethodExtensions.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="HttpMethod"/> helpers. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <example> |
| | | 20 | | /// <code> |
| | | 21 | | /// HttpMethod method = HttpMethod.For("PATCH"); |
| | | 22 | | /// </code> |
| | | 23 | | /// </example> |
| | | 24 | | public static class HttpMethodExtensions |
| | | 25 | | { |
| | | 26 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 2 | 27 | | private static readonly FrozenDictionary<string, HttpMethod> s_httpMethods = new Dictionary<string, HttpMethod>( |
| | 2 | 28 | | { |
| | 2 | 29 | | ["DELETE"] = HttpMethod.Delete, |
| | 2 | 30 | | ["GET"] = HttpMethod.Get, |
| | 2 | 31 | | ["HEAD"] = HttpMethod.Head, |
| | 2 | 32 | | ["OPTIONS"] = HttpMethod.Options, |
| | 2 | 33 | | #if NETSTANDARD2_0 |
| | 2 | 34 | | ["PATCH"] = new HttpMethod("PATCH"), |
| | 2 | 35 | | #else |
| | 2 | 36 | | ["PATCH"] = HttpMethod.Patch, |
| | 2 | 37 | | #endif |
| | 2 | 38 | | ["POST"] = HttpMethod.Post, |
| | 2 | 39 | | ["PUT"] = HttpMethod.Put, |
| | 2 | 40 | | ["TRACE"] = HttpMethod.Trace |
| | 2 | 41 | | }.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); |
| | | 42 | | |
| | | 43 | | extension(HttpMethod) |
| | | 44 | | { |
| | | 45 | | /// <summary> |
| | | 46 | | /// Returns the shared <see cref="HttpMethod"/> instance for a known method name, or creates one for a custo |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="method">The HTTP method name.</param> |
| | | 49 | | /// <returns>The matching <see cref="HttpMethod"/>.</returns> |
| | | 50 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="method"/> is <see langword="null"/>. |
| | | 51 | | /// <exception cref="ArgumentException">Thrown when <paramref name="method"/> is empty or whitespace.</excep |
| | | 52 | | /// <exception cref="FormatException">Thrown when <paramref name="method"/> is not a valid HTTP method token |
| | | 53 | | /// <example> |
| | | 54 | | /// <code> |
| | | 55 | | /// HttpMethod method = HttpMethod.For("GET"); |
| | | 56 | | /// </code> |
| | | 57 | | /// </example> |
| | | 58 | | public static HttpMethod For(string method) |
| | 2 | 59 | | { |
| | 2 | 60 | | Ensure.NotNull(method); |
| | 2 | 61 | | return s_httpMethods.TryGetValue(method, out HttpMethod? httpMethod) ? httpMethod : new HttpMethod(metho |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |