summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/NetworkDirectWebResponse.cs
blob: 8fb69a164c2019a913e4b27a9febc4c903d70d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//-----------------------------------------------------------------------
// <copyright file="NetworkDirectWebResponse.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging {
	using System;
	using System.Diagnostics;
	using System.Diagnostics.Contracts;
	using System.IO;
	using System.Net;
	using System.Text;

	/// <summary>
	/// A live network HTTP response
	/// </summary>
	[DebuggerDisplay("{Status} {ContentType.MediaType}")]
	[ContractVerification(true)]
	internal class NetworkDirectWebResponse : IncomingWebResponse, IDisposable {
		/// <summary>
		/// The network response object, used to initialize this instance, that still needs 
		/// to be closed if applicable.
		/// </summary>
		private HttpWebResponse httpWebResponse;

		/// <summary>
		/// The incoming network response stream.
		/// </summary>
		private Stream responseStream;

		/// <summary>
		/// A value indicating whether a stream reader has already been
		/// created on this instance.
		/// </summary>
		private bool streamReadBegun;

		/// <summary>
		/// Initializes a new instance of the <see cref="NetworkDirectWebResponse"/> class.
		/// </summary>
		/// <param name="requestUri">The request URI.</param>
		/// <param name="response">The response.</param>
		internal NetworkDirectWebResponse(Uri requestUri, HttpWebResponse response)
			: base(requestUri, response) {
			Requires.NotNull(requestUri, "requestUri");
			Requires.NotNull(response, "response");
			this.httpWebResponse = response;
			this.responseStream = response.GetResponseStream();
		}

		/// <summary>
		/// Gets the body of the HTTP response.
		/// </summary>
		public override Stream ResponseStream {
			get { return this.responseStream; }
		}

		/// <summary>
		/// Creates a text reader for the response stream.
		/// </summary>
		/// <returns>The text reader, initialized for the proper encoding.</returns>
		public override StreamReader GetResponseReader() {
			this.streamReadBegun = true;
			if (this.responseStream == null) {
				throw new ObjectDisposedException(GetType().Name);
			}

			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>
		/// Gets an offline snapshot version of this instance.
		/// </summary>
		/// <param name="maximumBytesToCache">The maximum bytes from the response stream to cache.</param>
		/// <returns>A snapshot version of this instance.</returns>
		/// <remarks>
		/// If this instance is a <see cref="NetworkDirectWebResponse"/> creating a snapshot
		/// will automatically close and dispose of the underlying response stream.
		/// If this instance is a <see cref="CachedDirectWebResponse"/>, the result will
		/// be the self same instance.
		/// </remarks>
		internal override CachedDirectWebResponse GetSnapshot(int maximumBytesToCache) {
			ErrorUtilities.VerifyOperation(!this.streamReadBegun, "Network stream reading has already begun.");
			ErrorUtilities.VerifyOperation(this.httpWebResponse != null, "httpWebResponse != null");

			this.streamReadBegun = true;
			var result = new CachedDirectWebResponse(this.RequestUri, this.httpWebResponse, maximumBytesToCache);
			this.Dispose();
			return result;
		}

		/// <summary>
		/// Releases unmanaged and - optionally - managed resources
		/// </summary>
		/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
		protected override void Dispose(bool disposing) {
			if (disposing) {
				if (this.responseStream != null) {
					this.responseStream.Dispose();
					this.responseStream = null;
				}
				if (this.httpWebResponse != null) {
					this.httpWebResponse.Close();
					this.httpWebResponse = null;
				}
			}

			base.Dispose(disposing);
		}
	}
}