diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-09-27 07:40:58 -0700 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-09-27 07:40:58 -0700 |
commit | faa8a59cf77470e092ce9c2fe99f9ed7432567b3 (patch) | |
tree | 272e2d32d34c3b83829df0452241a68435a29d59 /src/DotNetOAuth/Messaging/Response.cs | |
parent | d5e2339b50047e961a661f03d882dd555c45fd74 (diff) | |
download | DotNetOpenAuth-faa8a59cf77470e092ce9c2fe99f9ed7432567b3.zip DotNetOpenAuth-faa8a59cf77470e092ce9c2fe99f9ed7432567b3.tar.gz DotNetOpenAuth-faa8a59cf77470e092ce9c2fe99f9ed7432567b3.tar.bz2 |
Added final protected resource request to Appendix scenario test.
Diffstat (limited to 'src/DotNetOAuth/Messaging/Response.cs')
-rw-r--r-- | src/DotNetOAuth/Messaging/Response.cs | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/src/DotNetOAuth/Messaging/Response.cs b/src/DotNetOAuth/Messaging/Response.cs index 8a7c2e1..c02acc2 100644 --- a/src/DotNetOAuth/Messaging/Response.cs +++ b/src/DotNetOAuth/Messaging/Response.cs @@ -8,6 +8,7 @@ namespace DotNetOAuth.Messaging { using System;
using System.IO;
using System.Net;
+ using System.Text;
using System.Web;
/// <summary>
@@ -41,8 +42,10 @@ namespace DotNetOAuth.Messaging { internal Response(HttpWebResponse response) {
this.Status = response.StatusCode;
this.Headers = response.Headers;
- using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
- this.Body = reader.ReadToEnd();
+ this.ResponseStream = new MemoryStream();
+ using (Stream responseStream = response.GetResponseStream()) {
+ responseStream.CopyTo(this.ResponseStream);
+ this.ResponseStream.Seek(0, SeekOrigin.Begin);
}
}
@@ -59,7 +62,21 @@ namespace DotNetOAuth.Messaging { /// <summary>
/// Gets the body of the HTTP response.
/// </summary>
- public string Body { get; internal set; }
+ public Stream ResponseStream { get; internal set; }
+
+ /// <summary>
+ /// Gets a value indicating whether the response stream is incomplete due
+ /// to a length limitation imposed by the HttpWebRequest or calling method.
+ /// </summary>
+ public bool IsResponseTruncated { get; internal set; }
+
+ /// <summary>
+ /// Gets or sets the body of the response as a string.
+ /// </summary>
+ public string Body {
+ get { return this.ResponseStream != null ? this.GetResponseReader().ReadToEnd() : null; }
+ set { this.SetResponse(value); }
+ }
/// <summary>
/// Gets the HTTP status code to use in the HTTP response.
@@ -73,6 +90,20 @@ namespace DotNetOAuth.Messaging { internal IProtocolMessage OriginalMessage { get; set; }
/// <summary>
+ /// Creates a text reader for the response stream.
+ /// </summary>
+ /// <returns>The text reader, initialized for the proper encoding.</returns>
+ public StreamReader GetResponseReader() {
+ this.ResponseStream.Seek(0, SeekOrigin.Begin);
+ string contentEncoding = this.Headers[HttpResponseHeader.ContentEncoding];
+ if (string.IsNullOrEmpty(contentEncoding)) {
+ return new StreamReader(this.ResponseStream);
+ } else {
+ return new StreamReader(this.ResponseStream, Encoding.GetEncoding(contentEncoding));
+ }
+ }
+
+ /// <summary>
/// Automatically sends the appropriate response to the user agent.
/// Requires a current HttpContext.
/// </summary>
@@ -84,10 +115,39 @@ namespace DotNetOAuth.Messaging { HttpContext.Current.Response.Clear();
HttpContext.Current.Response.StatusCode = (int)this.Status;
MessagingUtilities.ApplyHeadersToResponse(this.Headers, HttpContext.Current.Response);
- if (this.Body != null) {
- HttpContext.Current.Response.Output.Write(this.Body);
+ if (this.ResponseStream != null) {
+ try {
+ this.ResponseStream.CopyTo(HttpContext.Current.Response.OutputStream);
+ } catch (HttpException ex) {
+ if (ex.ErrorCode == -2147467259 && HttpContext.Current.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);
+ } else {
+ throw;
+ }
+ }
}
HttpContext.Current.Response.End();
}
+
+ /// <summary>
+ /// Sets the response to some string, encoded as UTF-8.
+ /// </summary>
+ /// <param name="body">The string to set the response to.</param>
+ internal void SetResponse(string body) {
+ if (body == null) {
+ this.ResponseStream = null;
+ return;
+ }
+
+ Encoding encoding = Encoding.UTF8;
+ this.Headers[HttpResponseHeader.ContentEncoding] = encoding.HeaderName;
+ this.ResponseStream = new MemoryStream();
+ StreamWriter writer = new StreamWriter(this.ResponseStream, encoding);
+ writer.Write(body);
+ writer.Flush();
+ this.ResponseStream.Seek(0, SeekOrigin.Begin);
+ }
}
}
|