Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/unosquare/embedio
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Apr 12, 2016
2 parents 2ee0732 + 7378ea4 commit 8ba960c
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions Unosquare.Labs.EmbedIO/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,38 @@ public static bool JsonResponse(this HttpListenerContext context, string json)
context.Response.OutputStream.Write(buffer, 0, buffer.Length);

return true;
}
}

/// <summary>
/// Parses the json as a given type from the request body.
/// Please note the underlying input stream is not rewindable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context.</param>
/// <returns></returns>
public static T ParseJson<T>(this HttpListenerContext context)
where T : class
{
var body = context.RequestBody();
return body == null ? null : JsonConvert.DeserializeObject<T>(body);
var requestBody = context.RequestBody();
return requestBody == null ? null : JsonConvert.DeserializeObject<T>(requestBody);
}

/// <summary>
/// Parses the json as a given type from the request body string.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="requestBody">The request body.</param>
/// <returns></returns>
public static T ParseJson<T>(this string requestBody)
where T : class
{
return requestBody == null ? null : JsonConvert.DeserializeObject<T>(requestBody);
}

/// <summary>
/// Retrieves the request body
/// Retrieves the request body as a string.
/// Note that once this method returns, the underlying input stream cannot be read again as
/// it is not rewindable for obvious reasons. This functionality is by design.
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
Expand Down Expand Up @@ -263,6 +278,7 @@ public static bool HasRequestHeader(this HttpListenerContext context, string hea

/// <summary>
/// Returns dictionary from Request POST data
/// Please note the underlying input stream is not rewindable.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
Expand All @@ -276,16 +292,35 @@ public static Dictionary<string, string> RequestFormData(this HttpListenerContex
using (var reader = new StreamReader(body, request.ContentEncoding))
{
var stringData = reader.ReadToEnd();

if (string.IsNullOrWhiteSpace(stringData)) return null;

return stringData.Split('&')
.ToDictionary(c => c.Split('=')[0],
c => WebUtility.UrlDecode(c.Split('=')[1]));
return ParseFormData(stringData);
}
}
}

}

/// <summary>
/// Returns dictionary from Request POST data
/// </summary>
/// <param name="requestBody">The request body.</param>
/// <returns></returns>
public static Dictionary<string, string> RequestFormData(this string requestBody)
{
return ParseFormData(requestBody);
}

/// <summary>
/// Parses the form data given the request body string.
/// </summary>
/// <param name="requestBody">The request body.</param>
/// <returns></returns>
private static Dictionary<string, string> ParseFormData(string requestBody)
{
if (string.IsNullOrWhiteSpace(requestBody)) return null;

return requestBody.Split('&')
.ToDictionary(c => c.Split('=')[0],
c => WebUtility.UrlDecode(c.Split('=')[1]));
}

/// <summary>
/// Compresses the specified buffer using the G-Zip compression algorithm.
/// </summary>
Expand Down

0 comments on commit 8ba960c

Please sign in to comment.