Created
April 26, 2017 19:17
-
-
Save josheinstein/3f0c4e0f3da22c2a708b1bd56bc2f546 to your computer and use it in GitHub Desktop.
A C# script that contains helper extension methods for the HttpRequestMessage class, to simplify Azure Function development.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "System.Management.Automation" | |
#r "System.Web" | |
using System.Net; | |
using System.Net.Http; | |
public static string Query(this HttpRequestMessage request, string name, string defaultValue = null) { | |
var pairs = request.GetQueryNameValuePairs().Where(q => String.Equals(q.Key, name, StringComparison.OrdinalIgnoreCase)); | |
if (pairs.Any()) { return pairs.First().Value; } | |
return defaultValue; | |
} | |
public static T Query<T>(this HttpRequestMessage request, string name, T defaultValue = default(T)) { | |
string valueAsString = Query(request, name); | |
if (!String.IsNullOrWhiteSpace(valueAsString)) { | |
// Leverages PowerShell's flexible type conversion semantics. | |
// For example, makes it trivial to return a parsed IPAddress from a query string parameter. | |
return (T)System.Management.Automation.LanguagePrimitives.ConvertTo(valueAsString, typeof(T)); | |
} | |
return defaultValue; | |
} | |
public static string Header(this HttpRequestMessage request, string name, string defaultValue = null) { | |
var pairs = request.Headers.Where(q => String.Equals(q.Key, name, StringComparison.OrdinalIgnoreCase)); | |
if (pairs.Any()) { return pairs.First().Value.FirstOrDefault(); } // what a disgusting api | |
return defaultValue; | |
} | |
public static T Property<T>(this HttpRequestMessage request, string name, T defaultValue = default(T)) { | |
object value; | |
if (request.Properties.TryGetValue(name, out value)) { | |
return (T)value; | |
} | |
return defaultValue; | |
} | |
public static async Task<dynamic> Body(this HttpRequestMessage request) { | |
return await request.Content.ReadAsAsync<object>(); | |
} | |
public static async Task<T> Body<T>(this HttpRequestMessage request) { | |
return await request.Content.ReadAsAsync<T>(); | |
} | |
public static System.Net.IPAddress ClientIP(this HttpRequestMessage request) { | |
// First, prefer the X-Forwarded-For header | |
string valueAsString = Header(request, "X-Forwarded-For"); | |
if (valueAsString != null) { | |
valueAsString = valueAsString.Split(':',',')[0]; | |
} | |
else { | |
// If there's no header indicating a proxy forwarded request, | |
// revert to the actual IP address of the client. | |
var httpContext = Property<System.Web.HttpContextBase>(request, "MS_HttpContext"); | |
if (httpContext != null) { | |
valueAsString = httpContext.Request.UserHostAddress; | |
} | |
} | |
IPAddress addr; | |
if (IPAddress.TryParse(valueAsString, out addr)) { | |
return addr; | |
} | |
return null; | |
} | |
public static void Trace(this HttpRequestMessage request, TraceWriter log) { | |
log.Info("HttpRequestMessage Trace"); | |
log.Info(" "); | |
log.Info($"Method : {request.Method}"); | |
log.Info($"URI : {request.RequestUri}"); | |
log.Info($"Client IP : {ClientIP(request)}"); | |
log.Info(" "); | |
log.Info("- HEADERS"); | |
foreach (var pair in request.Headers) { | |
log.Info($"{pair.Key} : {pair.Value.FirstOrDefault()}"); | |
} | |
log.Info(" "); | |
log.Info("- PROPERTIES"); | |
foreach (var pair in request.Properties) { | |
log.Info($"{pair.Key} : {pair.Value}"); | |
} | |
log.Info(" "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment