Download MovieTube.Web/Extensions/HttpRequestMessageExtensions.cs from niobures/VideoSearch: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/niobures/VideoSearch/resolve/main/MovieTube.Web/Extensions/HttpRequestMessageExtensions.cs
- Command line
-
hf download hf://niobures/VideoSearch/MovieTube.Web/Extensions/HttpRequestMessageExtensions.cs
-
curl -L -o HttpRequestMessageExtensions.cs https://huggingface.co/niobures/VideoSearch/resolve/main/MovieTube.Web/Extensions/HttpRequestMessageExtensions.cs
3.71 kB
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Net.Http; | |
| using System.Net.Http.Headers; | |
| namespace System.Web.Http | |
| { | |
| /// <summary> | |
| /// Extends the HttpRequestMessage collection | |
| /// </summary> | |
| public static class HttpRequestMessageExtensions | |
| { | |
| private const string HttpContext = "MS_HttpContext"; | |
| private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; | |
| /// <summary> | |
| /// Returns a dictionary of QueryStrings that's easier to work with | |
| /// than GetQueryNameValuePairs KevValuePairs collection. | |
| /// | |
| /// If you need to pull a few single values use GetQueryString instead. | |
| /// </summary> | |
| /// <param name="request"></param> | |
| /// <returns></returns> | |
| public static Dictionary<string, string> GetQueryStrings(this HttpRequestMessage request) | |
| { | |
| return request.GetQueryNameValuePairs() | |
| .ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase); | |
| } | |
| /// <summary> | |
| /// Returns an individual querystring value | |
| /// </summary> | |
| /// <param name="request"></param> | |
| /// <param name="key"></param> | |
| /// <returns></returns> | |
| public static string GetQueryString(this HttpRequestMessage request, string key) | |
| { | |
| // IEnumerable<KeyValuePair<string,string>> - right! | |
| var queryStrings = request.GetQueryNameValuePairs(); | |
| if (queryStrings == null) | |
| return null; | |
| var match = queryStrings.FirstOrDefault(kv => string.Compare(kv.Key, key, true) == 0); | |
| if (string.IsNullOrEmpty(match.Value)) | |
| return null; | |
| return match.Value; | |
| } | |
| /// <summary> | |
| /// Returns an individual HTTP Header value | |
| /// </summary> | |
| /// <param name="request"></param> | |
| /// <param name="key"></param> | |
| /// <returns></returns> | |
| public static string GetHeader(this HttpRequestMessage request, string key) | |
| { | |
| IEnumerable<string> keys = null; | |
| if (!request.Headers.TryGetValues(key, out keys)) | |
| return null; | |
| return keys.First(); | |
| } | |
| /// <summary> | |
| /// Retrieves an individual cookie from the cookies collection | |
| /// </summary> | |
| /// <param name="request"></param> | |
| /// <param name="cookieName"></param> | |
| /// <returns></returns> | |
| public static string GetCookie(this HttpRequestMessage request, string cookieName) | |
| { | |
| CookieHeaderValue cookie = request.Headers.GetCookies(cookieName).FirstOrDefault(); | |
| if (cookie != null) | |
| return cookie[cookieName].Value; | |
| return null; | |
| } | |
| public static string GetClientIpAddress(this HttpRequestMessage request) | |
| { | |
| if (request.Properties.ContainsKey(HttpContext)) | |
| { | |
| dynamic ctx = request.Properties[HttpContext]; | |
| if (ctx != null) | |
| { | |
| return ctx.Request.UserHostAddress; | |
| } | |
| } | |
| if (request.Properties.ContainsKey(RemoteEndpointMessage)) | |
| { | |
| dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; | |
| if (remoteEndpoint != null) | |
| { | |
| return remoteEndpoint.Address; | |
| } | |
| } | |
| return null; | |
| } | |
| } | |
| } |