summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/HttpRequestInfo.cs
blob: f3a1ba8b42445530c85b5cdf2fef72d7d4c6c925 (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
//-----------------------------------------------------------------------
// <copyright file="HttpRequestInfo.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging {
	using System;
	using System.Collections.Specialized;
	using System.Diagnostics;
	using System.Diagnostics.CodeAnalysis;
	using System.Globalization;
	using System.IO;
	using System.Net;
	using System.Net.Http;
	using System.Net.Http.Headers;
	using System.Net.Mime;
	using System.ServiceModel.Channels;
	using System.Web;
	using Validation;

	/// <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 : HttpRequestBase {
		/// <summary>
		/// The HTTP verb in the request.
		/// </summary>
		private readonly string httpMethod;

		/// <summary>
		/// The full request URL.
		/// </summary>
		private readonly Uri requestUri;

		/// <summary>
		/// The HTTP headers.
		/// </summary>
		private readonly NameValueCollection headers;

		/// <summary>
		/// The variables defined in the query part of the URL.
		/// </summary>
		private readonly NameValueCollection queryString;

		/// <summary>
		/// The POSTed form variables.
		/// </summary>
		private readonly NameValueCollection form;

		/// <summary>
		/// The server variables collection.
		/// </summary>
		private readonly NameValueCollection serverVariables;

		/// <summary>
		/// The backing field for the <see cref="Cookies"/> property.
		/// </summary>
		private readonly HttpCookieCollection cookies;

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="requestUri">The request URI.</param>
		internal HttpRequestInfo(HttpRequestMessageProperty request, Uri requestUri) {
			Requires.NotNull(request, "request");
			Requires.NotNull(requestUri, "requestUri");

			this.httpMethod = request.Method;
			this.headers = request.Headers;
			this.requestUri = requestUri;
			this.form = new NameValueCollection();
			this.queryString = HttpUtility.ParseQueryString(requestUri.Query);
			this.serverVariables = new NameValueCollection();
			this.cookies = new HttpCookieCollection();

			Reporting.RecordRequestStatistics(this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="httpMethod">The HTTP method.</param>
		/// <param name="requestUri">The request URI.</param>
		/// <param name="form">The form variables.</param>
		/// <param name="headers">The HTTP headers.</param>
		/// <param name="cookies">The cookies in the request.</param>
		internal HttpRequestInfo(string httpMethod, Uri requestUri, NameValueCollection form = null, NameValueCollection headers = null, HttpCookieCollection cookies = null) {
			Requires.NotNullOrEmpty(httpMethod, "httpMethod");
			Requires.NotNull(requestUri, "requestUri");

			this.httpMethod = httpMethod;
			this.requestUri = requestUri;
			this.form = form ?? new NameValueCollection();
			this.queryString = HttpUtility.ParseQueryString(requestUri.Query);
			this.headers = headers ?? new WebHeaderCollection();
			this.serverVariables = new NameValueCollection();
			this.cookies = cookies ?? new HttpCookieCollection();
		}

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

			this.httpMethod = listenerRequest.HttpMethod;
			this.requestUri = listenerRequest.Url;
			this.queryString = listenerRequest.QueryString;
			this.headers = listenerRequest.Headers;
			this.form = ParseFormData(listenerRequest.HttpMethod, listenerRequest.Headers, () => listenerRequest.InputStream);
			this.serverVariables = new NameValueCollection();
			this.cookies = new HttpCookieCollection();

			Reporting.RecordRequestStatistics(this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo" /> class.
		/// </summary>
		/// <param name="request">The request.</param>
		internal HttpRequestInfo(HttpRequestMessage request) {
			Requires.NotNull(request, "request");

			this.httpMethod = request.Method.ToString();
			this.requestUri = request.RequestUri;
			this.queryString = HttpUtility.ParseQueryString(request.RequestUri.Query);
			this.headers = new NameValueCollection();
			AddHeaders(this.headers, request.Headers);
			AddHeaders(this.headers, request.Content.Headers);
			this.form = ParseFormData(this.httpMethod, this.headers, () => request.Content.ReadAsStreamAsync().Result);
			this.serverVariables = new NameValueCollection();
			this.cookies = new HttpCookieCollection();

			Reporting.RecordRequestStatistics(this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="HttpRequestInfo"/> class.
		/// </summary>
		/// <param name="httpMethod">The HTTP method.</param>
		/// <param name="requestUri">The request URI.</param>
		/// <param name="headers">The headers.</param>
		/// <param name="inputStream">The input stream.</param>
		internal HttpRequestInfo(string httpMethod, Uri requestUri, NameValueCollection headers, Stream inputStream) {
			Requires.NotNullOrEmpty(httpMethod, "httpMethod");
			Requires.NotNull(requestUri, "requestUri");

			this.httpMethod = httpMethod;
			this.requestUri = requestUri;
			this.headers = headers;
			this.queryString = HttpUtility.ParseQueryString(requestUri.Query);
			this.form = ParseFormData(httpMethod, headers, () => inputStream);
			this.serverVariables = new NameValueCollection();
			this.cookies = new HttpCookieCollection();

			Reporting.RecordRequestStatistics(this);
		}

		/// <summary>
		/// Gets the HTTP method.
		/// </summary>
		public override string HttpMethod {
			get { return this.httpMethod; }
		}

		/// <summary>
		/// Gets the headers.
		/// </summary>
		public override NameValueCollection Headers {
			get { return this.headers; }
		}

		/// <summary>
		/// Gets the URL.
		/// </summary>
		public override Uri Url {
			get { return this.requestUri; }
		}

		/// <summary>
		/// Gets the raw URL.
		/// </summary>
		public override string RawUrl {
			get { return this.requestUri.AbsolutePath + this.requestUri.Query; }
		}

		/// <summary>
		/// Gets the form.
		/// </summary>
		public override NameValueCollection Form {
			get { return this.form; }
		}

		/// <summary>
		/// Gets the query string.
		/// </summary>
		public override NameValueCollection QueryString {
			get { return this.queryString; }
		}

		/// <summary>
		/// Gets the server variables.
		/// </summary>
		public override NameValueCollection ServerVariables {
			get { return this.serverVariables; }
		}

		/// <summary>
		/// Gets the collection of cookies that were sent by the client.
		/// </summary>
		/// <returns>The client's cookies.</returns>
		public override HttpCookieCollection Cookies {
			get { return this.cookies; }
		}

		/// <summary>
		/// Creates an <see cref="HttpRequestBase"/> instance that describes the specified HTTP request.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="requestUri">The request URI.</param>
		/// <returns>An instance of <see cref="HttpRequestBase"/>.</returns>
		public static HttpRequestBase Create(HttpRequestMessageProperty request, Uri requestUri) {
			return new HttpRequestInfo(request, requestUri);
		}

		/// <summary>
		/// Creates an <see cref="HttpRequestBase"/> instance that describes the specified HTTP request.
		/// </summary>
		/// <param name="listenerRequest">The listener request.</param>
		/// <returns>An instance of <see cref="HttpRequestBase"/>.</returns>
		public static HttpRequestBase Create(HttpListenerRequest listenerRequest) {
			return new HttpRequestInfo(listenerRequest);
		}

#if CLR4
		/// <summary>
		/// Creates an <see cref="HttpRequestBase"/> instance that describes the specified HTTP request.
		/// </summary>
		/// <param name="request">The HTTP request.</param>
		/// <returns>An instance of <see cref="HttpRequestBase"/>.</returns>
		public static HttpRequestBase Create(HttpRequestMessage request) {
			return new HttpRequestInfo(request);
		}
#endif

		/// <summary>
		/// Creates an <see cref="HttpRequestBase"/> instance that describes the specified HTTP request.
		/// </summary>
		/// <param name="httpMethod">The HTTP method.</param>
		/// <param name="requestUri">The request URI.</param>
		/// <param name="form">The form variables.</param>
		/// <param name="headers">The HTTP headers.</param>
		/// <returns>An instance of <see cref="HttpRequestBase"/>.</returns>
		public static HttpRequestBase Create(string httpMethod, Uri requestUri, NameValueCollection form = null, NameValueCollection headers = null) {
			return new HttpRequestInfo(httpMethod, requestUri, form, headers);
		}

		/// <summary>
		/// Creates an <see cref="HttpRequestBase"/> instance that describes the specified HTTP request.
		/// </summary>
		/// <param name="httpMethod">The HTTP method.</param>
		/// <param name="requestUri">The request URI.</param>
		/// <param name="headers">The headers.</param>
		/// <param name="inputStream">The input stream.</param>
		/// <returns>An instance of <see cref="HttpRequestBase"/>.</returns>
		public static HttpRequestBase Create(string httpMethod, Uri requestUri, NameValueCollection headers, Stream inputStream) {
			return new HttpRequestInfo(httpMethod, requestUri, headers, inputStream);
		}

		/// <summary>
		/// Reads name=value pairs from the POSTed form entity when the HTTP headers indicate that that is the payload of the entity.
		/// </summary>
		/// <param name="httpMethod">The HTTP method.</param>
		/// <param name="headers">The headers.</param>
		/// <param name="inputStreamFunc">A function that returns the input stream.</param>
		/// <returns>The non-null collection of form variables.</returns>
		private static NameValueCollection ParseFormData(string httpMethod, NameValueCollection headers, Func<Stream> inputStreamFunc) {
			Requires.NotNullOrEmpty(httpMethod, "httpMethod");
			Requires.NotNull(headers, "headers");

			ContentType contentType = string.IsNullOrEmpty(headers[HttpRequestHeaders.ContentType]) ? null : new ContentType(headers[HttpRequestHeaders.ContentType]);
			if (httpMethod == "POST" && contentType != null && string.Equals(contentType.MediaType, Channel.HttpFormUrlEncoded, StringComparison.Ordinal) && inputStreamFunc != null) {
				var inputStream = inputStreamFunc();
				var reader = new StreamReader(inputStream);
				long originalPosition = 0;
				if (inputStream.CanSeek) {
					originalPosition = inputStream.Position;
				}
				string postEntity = reader.ReadToEnd();
				if (inputStream.CanSeek) {
					inputStream.Seek(originalPosition, SeekOrigin.Begin);
				}

				return HttpUtility.ParseQueryString(postEntity);
			}

			return new NameValueCollection();
		}

		/// <summary>
		/// Adds HTTP headers to a <see cref="NameValueCollection"/>.
		/// </summary>
		/// <param name="collectionToFill">The collection to be modified with added entries.</param>
		/// <param name="headers">The collection to read from.</param>
		private static void AddHeaders(NameValueCollection collectionToFill, HttpHeaders headers) {
			Requires.NotNull(collectionToFill, "collectionToFill");
			Requires.NotNull(headers, "headers");

			foreach (var header in headers) {
				foreach (var value in header.Value) {
					collectionToFill.Add(header.Key, value);
				}
			}
		}
	}
}