summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2009-03-27 20:49:23 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2009-03-27 20:50:14 -0700
commitda6d943b86c20f5eb7deafeb1bfb2f8cf7ab44d8 (patch)
tree8313a21fdbdbd77770b49e4409dea057d731fbb4 /src
parentc6c6eb53789e30f6ba48cee44edbcc5e91467480 (diff)
downloadDotNetOpenAuth-da6d943b86c20f5eb7deafeb1bfb2f8cf7ab44d8.zip
DotNetOpenAuth-da6d943b86c20f5eb7deafeb1bfb2f8cf7ab44d8.tar.gz
DotNetOpenAuth-da6d943b86c20f5eb7deafeb1bfb2f8cf7ab44d8.tar.bz2
Added convenience method for sending OutgoingWebResponse instances to an HttpListenerResponse.
Diffstat (limited to 'src')
-rw-r--r--src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs9
-rw-r--r--src/DotNetOpenAuth/Messaging/MessagingUtilities.cs25
-rw-r--r--src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs47
3 files changed, 72 insertions, 9 deletions
diff --git a/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs b/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs
index 782ce0d..2b1d961 100644
--- a/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs
+++ b/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs
@@ -95,8 +95,13 @@ namespace DotNetOpenAuth.Test.Messaging
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void ApplyHeadersToResponseNullResponse() {
- MessagingUtilities.ApplyHeadersToResponse(new WebHeaderCollection(), null);
+ public void ApplyHeadersToResponseNullAspNetResponse() {
+ MessagingUtilities.ApplyHeadersToResponse(new WebHeaderCollection(), (HttpResponse)null);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void ApplyHeadersToResponseNullListenerResponse() {
+ MessagingUtilities.ApplyHeadersToResponse(new WebHeaderCollection(), (HttpListenerResponse)null);
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
diff --git a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs
index 852d79d..c024cdc 100644
--- a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs
+++ b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs
@@ -184,6 +184,31 @@ namespace DotNetOpenAuth.Messaging {
}
/// <summary>
+ /// Adds a set of HTTP headers to an <see cref="HttpResponse"/> instance,
+ /// taking care to set some headers to the appropriate properties of
+ /// <see cref="HttpResponse" />
+ /// </summary>
+ /// <param name="headers">The headers to add.</param>
+ /// <param name="response">The <see cref="HttpListenerResponse"/> instance to set the appropriate values to.</param>
+ internal static void ApplyHeadersToResponse(WebHeaderCollection headers, HttpListenerResponse response) {
+ ErrorUtilities.VerifyArgumentNotNull(headers, "headers");
+ ErrorUtilities.VerifyArgumentNotNull(response, "response");
+
+ foreach (string headerName in headers) {
+ switch (headerName) {
+ case "Content-Type":
+ response.ContentType = headers[HttpResponseHeader.ContentType];
+ break;
+
+ // Add more special cases here as necessary.
+ default:
+ response.AddHeader(headerName, headers[headerName]);
+ break;
+ }
+ }
+ }
+
+ /// <summary>
/// Copies the contents of one stream to another.
/// </summary>
/// <param name="copyFrom">The stream to copy from, at the position where copying should begin.</param>
diff --git a/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs b/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs
index 55cb4b6..bad582c 100644
--- a/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs
+++ b/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs
@@ -123,23 +123,56 @@ namespace DotNetOpenAuth.Messaging {
Contract.Requires(HttpContext.Current != null);
ErrorUtilities.VerifyHttpContext();
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.StatusCode = (int)this.Status;
- MessagingUtilities.ApplyHeadersToResponse(this.Headers, HttpContext.Current.Response);
+ this.Send(HttpContext.Current);
+ }
+
+ /// <summary>
+ /// Automatically sends the appropriate response to the user agent
+ /// and ends execution on the current page or handler.
+ /// </summary>
+ /// <param name="context">The context of the HTTP request whose response should be set.
+ /// Typically this is <see cref="HttpContext.Current"/>.</param>
+ /// <exception cref="ThreadAbortException">Thrown by ASP.NET in order to prevent additional data from the page being sent to the client and corrupting the response.</exception>
+ public virtual void Send(HttpContext context) {
+ Contract.Requires(context != null);
+ ErrorUtilities.VerifyArgumentNotNull(context, "context");
+
+ context.Response.Clear();
+ context.Response.StatusCode = (int)this.Status;
+ MessagingUtilities.ApplyHeadersToResponse(this.Headers, context.Response);
if (this.ResponseStream != null) {
try {
- this.ResponseStream.CopyTo(HttpContext.Current.Response.OutputStream);
+ this.ResponseStream.CopyTo(context.Response.OutputStream);
} catch (HttpException ex) {
- if (ex.ErrorCode == -2147467259 && HttpContext.Current.Response.Output != null) {
+ if (ex.ErrorCode == -2147467259 && context.Response.Output != null) {
// Test scenarios can generate this, since the stream is being spoofed:
// System.Web.HttpException: OutputStream is not available when a custom TextWriter is used.
- HttpContext.Current.Response.Output.Write(this.Body);
+ context.Response.Output.Write(this.Body);
} else {
throw;
}
}
}
- HttpContext.Current.Response.End();
+
+ context.Response.End();
+ }
+
+ /// <summary>
+ /// Automatically sends the appropriate response to the user agent.
+ /// </summary>
+ /// <param name="response">The response to set to this message.</param>
+ public virtual void Send(HttpListenerResponse response) {
+ Contract.Requires(response != null);
+ ErrorUtilities.VerifyArgumentNotNull(response, "response");
+
+ response.StatusCode = (int)this.Status;
+ MessagingUtilities.ApplyHeadersToResponse(this.Headers, response);
+ if (this.ResponseStream != null) {
+ response.ContentLength64 = this.ResponseStream.Length;
+ this.ResponseStream.CopyTo(response.OutputStream);
+ }
+
+ response.OutputStream.Close();
}
/// <summary>