using System.Globalization;
using System.IO;
namespace System.Web.ExtensionMethods
{
///
/// Extension methods for HTTP Response.
///
public static class HttpResponseExtensions
{
///
/// Set the response to be JSON format, and add appropriate headers.
///
/// None.
public static void SetJson (this HttpResponse response)
{
response.ContentType = "application/json";
response.CacheControl = "no-cache";
response.AddHeader ("Last-Modified", DateTime.UtcNow.ToString (CultureInfo.InvariantCulture));
response.AddHeader ("Pragma", "no-cache");
}
}
///
/// Extension methods for HTTP Request.
///
/// See the HTTP 1.1 specification http://www.w3.org/Protocols/rfc2616/rfc2616.html
/// for details of implementation decisions.
///
///
public static class HttpRequestExtensions
{
///
/// Dump the raw http request to a string.
///
/// The that should be dumped.
/// The raw HTTP request.
public static string ToRaw (this HttpRequest request)
{
StringWriter writer = new StringWriter();
WriteStartLine (request, writer);
WriteHeaders (request, writer);
WriteBody (request, writer);
return writer.ToString();
}
public static string GetBody (this HttpRequest request)
{
StringWriter writer = new StringWriter();
WriteBody (request, writer);
return writer.ToString();
}
private static void WriteStartLine (HttpRequest request, StringWriter writer)
{
const string SPACE = " ";
writer.Write (request.HttpMethod);
writer.Write (SPACE + request.Url);
writer.WriteLine (SPACE + request.ServerVariables["SERVER_PROTOCOL"]);
}
private static void WriteHeaders (HttpRequest request, StringWriter writer)
{
foreach (string key in request.Headers.AllKeys)
{
writer.WriteLine ("{0}: {1}", key, request.Headers[key]);
}
writer.WriteLine();
}
private static void WriteBody (HttpRequest request, StringWriter writer)
{
StreamReader reader = new StreamReader (request.InputStream);
try
{
string body = reader.ReadToEnd();
writer.WriteLine (body);
}
finally
{
reader.BaseStream.Position = 0;
}
}
}
}