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
|
//-----------------------------------------------------------------------
// <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.Contracts;
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="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) {
if (request == null) {
throw new ArgumentNullException("request");
}
this.HttpMethod = request.HttpMethod;
this.Url = request.Url;
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="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, WebHeaderCollection headers, Stream inputStream) {
ErrorUtilities.VerifyNonZeroLength(httpMethod, "httpMethod");
ErrorUtilities.VerifyArgumentNotNull(requestUrl, "requestUrl");
ErrorUtilities.VerifyArgumentNotNull(headers, "headers");
this.HttpMethod = httpMethod;
this.Url = requestUrl;
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) {
ErrorUtilities.VerifyArgumentNotNull(listenerRequest, "listenerRequest");
this.HttpMethod = listenerRequest.HttpMethod;
this.Url = listenerRequest.Url;
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) {
if (request == null) {
throw new ArgumentNullException("request");
}
this.HttpMethod = request.Method;
this.Headers = request.Headers;
this.Url = 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) {
this.HttpMethod = request.Method;
this.Url = 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.
/// </summary>
internal Uri Url { get; set; }
/// <summary>
/// Gets the query part of the URL (The ? and everything after it).
/// </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] == "application/x-www-form-urlencoded") {
StreamReader reader = new StreamReader(this.InputStream);
long 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>
/// 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) {
headers.Add(key, pairs[key]);
}
return headers;
}
}
}
|