summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth/Messaging/HttpRequestInfo.cs
blob: 0693926e8107179c8f468bd027aef9f75ed1b925 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//-----------------------------------------------------------------------
// <copyright file="HttpRequestInfo.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging {
	using System;
	using System.Collections.Specialized;
	using System.Diagnostics;
	using System.Diagnostics.CodeAnalysis;
	using System.Diagnostics.Contracts;
	using System.Globalization;
	using System.IO;
	using System.Net;
	using System.ServiceModel.Channels;
	using System.Web;

	/// <summary>
	/// A property store of details of an incoming HTTP request.
	/// </summary>
	/// <remarks>
	/// This serves a very similar purpose to <see cref="HttpRequest"/>, except that
	/// ASP.NET does not let us fully initialize that class, so we have to write one
	/// of our one.
	/// </remarks>
	public class HttpRequestInfo {
		/// <summary>
		/// The key/value pairs found in the entity of a POST request.
		/// </summary>
		private NameValueCollection form;

		/// <summary>
		/// The key/value pairs found in the querystring of the incoming request.
		/// </summary>
		private NameValueCollection queryString;

		/// <summary>
		/// Backing field for the <see cref="QueryStringBeforeRewriting"/> property.
		/// </summary>
		private NameValueCollection queryStringBeforeRewriting;

		/// <summary>
		/// Backing field for the <see cref="Message"/> property.
		/// </summary>
		private IDirectedProtocolMessage message;

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="request">The ASP.NET structure to copy from.</param>
		public HttpRequestInfo(HttpRequest request) {
			Contract.Requires(request != null);
			ErrorUtilities.VerifyArgumentNotNull(request, "request");

			this.HttpMethod = request.HttpMethod;
			this.Url = request.Url;
			this.UrlBeforeRewriting = GetPublicFacingUrl(request);
			this.RawUrl = request.RawUrl;
			this.Headers = GetHeaderCollection(request.Headers);
			this.InputStream = request.InputStream;

			// These values would normally be calculated, but we'll reuse them from
			// HttpRequest since they're already calculated, and there's a chance (<g>)
			// that ASP.NET does a better job of being comprehensive about gathering
			// these as well.
			this.form = request.Form;
			this.queryString = request.QueryString;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="httpMethod">The HTTP method (i.e. GET or POST) of the incoming request.</param>
		/// <param name="requestUrl">The URL being requested.</param>
		/// <param name="rawUrl">The raw URL that appears immediately following the HTTP verb in the request,
		/// before any URL rewriting takes place.</param>
		/// <param name="headers">Headers in the HTTP request.</param>
		/// <param name="inputStream">The entity stream, if any.  (POST requests typically have these).  Use <c>null</c> for GET requests.</param>
		public HttpRequestInfo(string httpMethod, Uri requestUrl, string rawUrl, WebHeaderCollection headers, Stream inputStream) {
			Contract.Requires(!string.IsNullOrEmpty(httpMethod));
			Contract.Requires(requestUrl != null);
			Contract.Requires(rawUrl != null);
			Contract.Requires(headers != null);
			ErrorUtilities.VerifyNonZeroLength(httpMethod, "httpMethod");
			ErrorUtilities.VerifyArgumentNotNull(requestUrl, "requestUrl");
			ErrorUtilities.VerifyArgumentNotNull(rawUrl, "rawUrl");
			ErrorUtilities.VerifyArgumentNotNull(headers, "headers");

			this.HttpMethod = httpMethod;
			this.Url = requestUrl;
			this.UrlBeforeRewriting = requestUrl;
			this.RawUrl = rawUrl;
			this.Headers = headers;
			this.InputStream = inputStream;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="listenerRequest">Details on the incoming HTTP request.</param>
		public HttpRequestInfo(HttpListenerRequest listenerRequest) {
			Contract.Requires(listenerRequest != null);
			ErrorUtilities.VerifyArgumentNotNull(listenerRequest, "listenerRequest");

			this.HttpMethod = listenerRequest.HttpMethod;
			this.Url = listenerRequest.Url;
			this.UrlBeforeRewriting = listenerRequest.Url;
			this.RawUrl = listenerRequest.RawUrl;
			this.Headers = new WebHeaderCollection();
			foreach (string key in listenerRequest.Headers) {
				this.Headers[key] = listenerRequest.Headers[key];
			}

			this.InputStream = listenerRequest.InputStream;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="request">The WCF incoming request structure to get the HTTP information from.</param>
		/// <param name="requestUri">The URI of the service endpoint.</param>
		public HttpRequestInfo(HttpRequestMessageProperty request, Uri requestUri) {
			Contract.Requires(request != null);
			Contract.Requires(requestUri != null);
			ErrorUtilities.VerifyArgumentNotNull(request, "request");
			ErrorUtilities.VerifyArgumentNotNull(requestUri, "requestUri");

			this.HttpMethod = request.Method;
			this.Headers = request.Headers;
			this.Url = requestUri;
			this.UrlBeforeRewriting = requestUri;
			this.RawUrl = MakeUpRawUrlFromUrl(requestUri);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		internal HttpRequestInfo() {
			this.HttpMethod = "GET";
			this.Headers = new WebHeaderCollection();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="request">The HttpWebRequest (that was never used) to copy from.</param>
		internal HttpRequestInfo(WebRequest request) {
			Contract.Requires(request != null);
			ErrorUtilities.VerifyArgumentNotNull(request, "request");

			this.HttpMethod = request.Method;
			this.Url = request.RequestUri;
			this.UrlBeforeRewriting = request.RequestUri;
			this.RawUrl = MakeUpRawUrlFromUrl(request.RequestUri);
			this.Headers = GetHeaderCollection(request.Headers);
			this.InputStream = null;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="message">The message being passed in through a mock transport.  May be null.</param>
		/// <param name="httpMethod">The HTTP method that the incoming request came in on, whether or not <paramref name="message"/> is null.</param>
		internal HttpRequestInfo(IDirectedProtocolMessage message, HttpDeliveryMethods httpMethod) {
			this.message = message;
			if ((httpMethod & HttpDeliveryMethods.GetRequest) != 0) {
				this.HttpMethod = "GET";
			} else if ((httpMethod & HttpDeliveryMethods.PostRequest) != 0) {
				this.HttpMethod = "POST";
			}
		}

		/// <summary>
		/// Gets or sets the message that is being sent over a mock transport (for testing).
		/// </summary>
		internal virtual IDirectedProtocolMessage Message {
			get { return this.message; }
			set { this.message = value; }
		}

		/// <summary>
		/// Gets or sets the verb in the request (i.e. GET, POST, etc.)
		/// </summary>
		internal string HttpMethod { get; set; }

		/// <summary>
		/// Gets or sets the entire URL of the request, after any URL rewriting.
		/// </summary>
		internal Uri Url { get; set; }

		/// <summary>
		/// Gets or sets the raw URL that appears immediately following the HTTP verb in the request,
		/// before any URL rewriting takes place.
		/// </summary>
		internal string RawUrl { get; set; }

		/// <summary>
		/// Gets or sets the full public URL used by the remote client to initiate this request,
		/// before any URL rewriting and before any changes made by web farm load distributors.
		/// </summary>
		internal Uri UrlBeforeRewriting { get; set; }

		/// <summary>
		/// Gets the query part of the URL (The ? and everything after it), after URL rewriting.
		/// </summary>
		internal string Query {
			get { return this.Url != null ? this.Url.Query : null; }
		}

		/// <summary>
		/// Gets or sets the collection of headers that came in with the request.
		/// </summary>
		internal WebHeaderCollection Headers { get; set; }

		/// <summary>
		/// Gets or sets the entity, or body of the request, if any.
		/// </summary>
		internal Stream InputStream { get; set; }

		/// <summary>
		/// Gets the key/value pairs found in the entity of a POST request.
		/// </summary>
		internal NameValueCollection Form {
			get {
				Contract.Ensures(Contract.Result<NameValueCollection>() != null);
				if (this.form == null) {
					if (this.HttpMethod == "POST" && this.Headers[HttpRequestHeader.ContentType] == Channel.HttpFormUrlEncoded) {
						StreamReader reader = new StreamReader(this.InputStream);
						long originalPosition = 0;
						if (this.InputStream.CanSeek) {
							originalPosition = this.InputStream.Position;
						}
						this.form = HttpUtility.ParseQueryString(reader.ReadToEnd());
						if (this.InputStream.CanSeek) {
							this.InputStream.Seek(originalPosition, SeekOrigin.Begin);
						}
					} else {
						this.form = new NameValueCollection();
					}
				}

				return this.form;
			}
		}

		/// <summary>
		/// Gets the key/value pairs found in the querystring of the incoming request.
		/// </summary>
		internal NameValueCollection QueryString {
			get {
				if (this.queryString == null) {
					this.queryString = this.Query != null ? HttpUtility.ParseQueryString(this.Query) : new NameValueCollection();
				}

				return this.queryString;
			}
		}

		/// <summary>
		/// Gets the query data from the original request (before any URL rewriting has occurred.)
		/// </summary>
		/// <returns>A <see cref="NameValueCollection"/> containing all the parameters in the query string.</returns>
		internal NameValueCollection QueryStringBeforeRewriting {
			get {
				if (this.queryStringBeforeRewriting == null) {
					// This request URL may have been rewritten by the host site.
					// For openid protocol purposes, we really need to look at 
					// the original query parameters before any rewriting took place.
					if (!this.IsUrlRewritten) {
						// No rewriting has taken place.
						this.queryStringBeforeRewriting = this.QueryString;
					} else {
						// Rewriting detected!  Recover the original request URI.
						ErrorUtilities.VerifyInternal(this.UrlBeforeRewriting != null, "UrlBeforeRewriting is null, so the query string cannot be determined.");
						this.queryStringBeforeRewriting = HttpUtility.ParseQueryString(this.UrlBeforeRewriting.Query);
					}
				}

				return this.queryStringBeforeRewriting;
			}
		}

		/// <summary>
		/// Gets a value indicating whether the request's URL was rewritten by ASP.NET
		/// or some other module.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this request's URL was rewritten; otherwise, <c>false</c>.
		/// </value>
		internal bool IsUrlRewritten {
			get { return this.Url != this.UrlBeforeRewriting; }
		}

		/// <summary>
		/// Gets the query or form data from the original request (before any URL rewriting has occurred.)
		/// </summary>
		/// <returns>A set of name=value pairs.</returns>
		[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Expensive call")]
		internal NameValueCollection GetQueryOrFormFromContext() {
			NameValueCollection query;
			if (this.HttpMethod == "GET") {
				query = this.QueryStringBeforeRewriting;
			} else {
				query = this.Form;
			}
			return query;
		}

		/// <summary>
		/// Gets the public facing URL for the given incoming HTTP request.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <returns>The URI that the outside world used to create this request.</returns>
		private static Uri GetPublicFacingUrl(HttpRequest request) {
			Contract.Requires(request != null);
			ErrorUtilities.VerifyArgumentNotNull(request, "request");

			// Due to URL rewriting, cloud computing (i.e. Azure)
			// and web farms, etc., we have to be VERY careful about what
			// we consider the incoming URL.  We want to see the URL as it would
			// appear on the public-facing side of the hosting web site.
			// HttpRequest.Url gives us the internal URL in a cloud environment,
			// So we use a variable that (at least from what I can tell) gives us
			// the public URL:
			if (request.ServerVariables["HTTP_HOST"] != null) {
				ErrorUtilities.VerifySupported(request.Url.Scheme == Uri.UriSchemeHttps || request.Url.Scheme == Uri.UriSchemeHttp, "Only HTTP and HTTPS are supported protocols.");
				UriBuilder publicRequestUri = new UriBuilder(request.Url);
				Uri hostAndPort = new Uri(request.Url.Scheme + Uri.SchemeDelimiter + request.ServerVariables["HTTP_HOST"]);
				publicRequestUri.Host = hostAndPort.Host;
				publicRequestUri.Port = hostAndPort.Port;
				if (request.ServerVariables["HTTP_X_FORWARDED_PROTO"] != null) {
					publicRequestUri.Scheme = request.ServerVariables["HTTP_X_FORWARDED_PROTO"];
				}
				return publicRequestUri.Uri;
			} else {
				// Failover to the method that works for non-web farm enviroments.
				// We use Request.Url for the full path to the server, and modify it
				// with Request.RawUrl to capture both the cookieless session "directory" if it exists
				// and the original path in case URL rewriting is going on.  We don't want to be
				// fooled by URL rewriting because we're comparing the actual URL with what's in
				// the return_to parameter in some cases.
				// Response.ApplyAppPathModifier(builder.Path) would have worked for the cookieless
				// session, but not the URL rewriting problem.
				return new Uri(request.Url, request.RawUrl);
			}
		}

		/// <summary>
		/// Makes up a reasonable guess at the raw URL from the possibly rewritten URL.
		/// </summary>
		/// <param name="url">A full URL.</param>
		/// <returns>A raw URL that might have come in on the HTTP verb.</returns>
		private static string MakeUpRawUrlFromUrl(Uri url) {
			Contract.Requires(url != null);
			return url.AbsolutePath + url.Query + url.Fragment;
		}

		/// <summary>
		/// Converts a NameValueCollection to a WebHeaderCollection.
		/// </summary>
		/// <param name="pairs">The collection a HTTP headers.</param>
		/// <returns>A new collection of the given headers.</returns>
		private static WebHeaderCollection GetHeaderCollection(NameValueCollection pairs) {
			Debug.Assert(pairs != null, "pairs == null");

			WebHeaderCollection headers = new WebHeaderCollection();
			foreach (string key in pairs) {
				try {
					headers.Add(key, pairs[key]);
				} catch (ArgumentException ex) {
					Logger.Messaging.WarnFormat(
						"{0} thrown when trying to add web header \"{1}: {2}\".  {3}",
						ex.GetType().Name,
						key,
						pairs[key],
						ex.Message);
				}
			}

			return headers;
		}
	}
}