diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs | 9 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Messaging/MessagingUtilities.cs | 25 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs | 47 |
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> |