summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/IncomingWebResponse.cs
blob: 90d2f1fcb15d393b532fbf14ef104952b3833b8b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//-----------------------------------------------------------------------
// <copyright file="IncomingWebResponse.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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

	/// <summary>
	/// Details on the incoming response from a direct web request to a remote party.
	/// </summary>
	[ContractVerification(true)]
	[ContractClass(typeof(IncomingWebResponseContract))]
	public abstract class IncomingWebResponse : IDisposable {
		/// <summary>
		/// The encoding to use in reading a response that does not declare its own content encoding.
		/// </summary>
		private const string DefaultContentEncoding = "ISO-8859-1";

		/// <summary>
		/// Initializes a new instance of the <see cref="IncomingWebResponse"/> class.
		/// </summary>
		protected internal IncomingWebResponse() {
			this.Status = HttpStatusCode.OK;
			this.Headers = new WebHeaderCollection();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="IncomingWebResponse"/> class.
		/// </summary>
		/// <param name="requestUri">The original request URI.</param>
		/// <param name="response">The response to initialize from.  The network stream is used by this class directly.</param>
		protected IncomingWebResponse(Uri requestUri, HttpWebResponse response) {
			Requires.NotNull(requestUri, "requestUri");
			Requires.NotNull(response, "response");

			this.RequestUri = requestUri;
			if (!string.IsNullOrEmpty(response.ContentType)) {
				try {
					this.ContentType = new ContentType(response.ContentType);
				} catch (FormatException) {
					Logger.Messaging.ErrorFormat("HTTP response to {0} included an invalid Content-Type header value: {1}", response.ResponseUri.AbsoluteUri, response.ContentType);
				}
			}
			this.ContentEncoding = string.IsNullOrEmpty(response.ContentEncoding) ? DefaultContentEncoding : response.ContentEncoding;
			this.FinalUri = response.ResponseUri;
			this.Status = response.StatusCode;
			this.Headers = response.Headers;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="IncomingWebResponse"/> class.
		/// </summary>
		/// <param name="requestUri">The request URI.</param>
		/// <param name="responseUri">The final URI to respond to the request.</param>
		/// <param name="headers">The headers.</param>
		/// <param name="statusCode">The status code.</param>
		/// <param name="contentType">Type of the content.</param>
		/// <param name="contentEncoding">The content encoding.</param>
		protected IncomingWebResponse(Uri requestUri, Uri responseUri, WebHeaderCollection headers, HttpStatusCode statusCode, string contentType, string contentEncoding) {
			Requires.NotNull(requestUri, "requestUri");

			this.RequestUri = requestUri;
			this.Status = statusCode;
			if (!string.IsNullOrEmpty(contentType)) {
				try {
					this.ContentType = new ContentType(contentType);
				} catch (FormatException) {
					Logger.Messaging.ErrorFormat("HTTP response to {0} included an invalid Content-Type header value: {1}", responseUri.AbsoluteUri, contentType);
				}
			}
			this.ContentEncoding = string.IsNullOrEmpty(contentEncoding) ? DefaultContentEncoding : contentEncoding;
			this.Headers = headers;
			this.FinalUri = responseUri;
		}

		/// <summary>
		/// Gets the type of the content.
		/// </summary>
		public ContentType ContentType { get; private set; }

		/// <summary>
		/// Gets the content encoding.
		/// </summary>
		public string ContentEncoding { get; private set; }

		/// <summary>
		/// Gets the URI of the initial request.
		/// </summary>
		public Uri RequestUri { get; private set; }

		/// <summary>
		/// Gets the URI that finally responded to the request.
		/// </summary>
		/// <remarks>
		/// This can be different from the <see cref="RequestUri"/> in cases of 
		/// redirection during the request.
		/// </remarks>
		public Uri FinalUri { get; internal set; }

		/// <summary>
		/// Gets the headers that must be included in the response to the user agent.
		/// </summary>
		/// <remarks>
		/// The headers in this collection are not meant to be a comprehensive list
		/// of exactly what should be sent, but are meant to augment whatever headers
		/// are generally included in a typical response.
		/// </remarks>
		public WebHeaderCollection Headers { get; internal set; }

		/// <summary>
		/// Gets the HTTP status code to use in the HTTP response.
		/// </summary>
		public HttpStatusCode Status { get; internal set; }

		/// <summary>
		/// Gets the body of the HTTP response.
		/// </summary>
		public abstract Stream ResponseStream { get; }

		/// <summary>
		/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
		/// </returns>
		public override string ToString() {
			StringBuilder sb = new StringBuilder();
			sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "RequestUri = {0}", this.RequestUri));
			sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "ResponseUri = {0}", this.FinalUri));
			sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "StatusCode = {0}", this.Status));
			sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "ContentType = {0}", this.ContentType));
			sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "ContentEncoding = {0}", this.ContentEncoding));
			sb.AppendLine("Headers:");
			foreach (string header in this.Headers) {
				sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "\t{0}: {1}", header, this.Headers[header]));
			}

			return sb.ToString();
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public void Dispose() {
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Creates a text reader for the response stream.
		/// </summary>
		/// <returns>The text reader, initialized for the proper encoding.</returns>
		[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Costly operation")]
		public abstract StreamReader GetResponseReader();

		/// <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 abstract CachedDirectWebResponse GetSnapshot(int maximumBytesToCache);

		/// <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 virtual void Dispose(bool disposing) {
			if (disposing) {
				Stream responseStream = this.ResponseStream;
				if (responseStream != null) {
					responseStream.Dispose();
				}
			}
		}
	}
}