forked from microsoft/referencesource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServerVarsCollection.cs
415 lines (342 loc) · 14.4 KB
/
HttpServerVarsCollection.cs
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//------------------------------------------------------------------------------
// <copyright file="HttpServerVarsCollection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* Collection of server variables with callback to HttpRequest for 'dynamic' ones
*
* Copyright (c) 2000 Microsoft Corporation
*/
namespace System.Web {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using System.Web.Hosting;
using System.Web.Util;
internal class HttpServerVarsCollection : HttpValueCollection {
private bool _populated;
private HttpRequest _request;
private IIS7WorkerRequest _iis7workerRequest;
private List<HttpServerVarsCollectionEntry> _unsyncedEntries;
// We preallocate the base collection with a size that should be sufficient
// to store all server variables w/o having to expand
internal HttpServerVarsCollection(HttpWorkerRequest wr, HttpRequest request) : base(59) {
// if this is an IIS7WorkerRequest, then the collection will be writeable and we will
// call into IIS7 to update the server var block when changes are made.
_iis7workerRequest = wr as IIS7WorkerRequest;
_request = request;
_populated = false;
Debug.Assert( _request != null );
}
[SuppressMessage("Microsoft.Usage", "CA2236:CallBaseClassMethodsOnISerializableTypes",
Justification = "this class, while derived from class implementing ISerializable, is not serializable")]
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
throw new SerializationException();
}
internal void Dispose() {
_request = null;
}
internal void AddStatic(String name, String value) {
if (value == null)
value = String.Empty;
InvalidateCachedArrays();
BaseAdd(name, new HttpServerVarsCollectionEntry(name, value));
}
internal void AddDynamic(String name, DynamicServerVariable var) {
InvalidateCachedArrays();
BaseAdd(name, new HttpServerVarsCollectionEntry(name, var));
}
private String GetServerVar(Object e) {
HttpServerVarsCollectionEntry entry = (HttpServerVarsCollectionEntry)e;
return (entry != null) ? entry.GetValue(_request) : null;
}
//
// Support for deferred population of the collection
//
private void Populate() {
if (!_populated) {
if (_request != null) {
MakeReadWrite();
_request.FillInServerVariablesCollection();
// Add all unsynchronized entries, if any
if (_unsyncedEntries != null) {
foreach (var entry in _unsyncedEntries) {
var existingEntry = (HttpServerVarsCollectionEntry)BaseGet(entry.Name);
if (existingEntry != null && existingEntry.IsDynamic) {
// Exisiting dynamic server variables cannot be modified - ignore the new value
continue;
}
InvalidateCachedArrays();
BaseSet(entry.Name, entry); // Update an existing entry, or create one if it's new
}
_unsyncedEntries.Clear();
}
if (_iis7workerRequest == null) {
MakeReadOnly();
}
}
_populated = true;
}
}
private String GetSimpleServerVar(String name) {
// get server var without population of the collection
// only most popular are included
if (name != null && name.Length > 1 && _request != null) {
switch (name[0]) {
case 'A':
case 'a':
if (StringUtil.EqualsIgnoreCase(name, "AUTH_TYPE"))
return _request.CalcDynamicServerVariable(DynamicServerVariable.AUTH_TYPE);
else if (StringUtil.EqualsIgnoreCase(name, "AUTH_USER"))
return _request.CalcDynamicServerVariable(DynamicServerVariable.AUTH_USER);
break;
case 'H':
case 'h':
if (StringUtil.EqualsIgnoreCase(name, "HTTP_USER_AGENT"))
return _request.UserAgent;
break;
case 'Q':
case 'q':
if (StringUtil.EqualsIgnoreCase(name, "QUERY_STRING"))
return _request.QueryStringText;
break;
case 'P':
case 'p':
if (StringUtil.EqualsIgnoreCase(name, "PATH_INFO"))
return _request.Path;
else if (StringUtil.EqualsIgnoreCase(name, "PATH_TRANSLATED"))
return _request.PhysicalPath;
break;
case 'R':
case 'r':
if (StringUtil.EqualsIgnoreCase(name, "REQUEST_METHOD"))
return _request.HttpMethod;
else if (StringUtil.EqualsIgnoreCase(name, "REMOTE_USER"))
return _request.CalcDynamicServerVariable(DynamicServerVariable.AUTH_USER);
else if (StringUtil.EqualsIgnoreCase(name, "REMOTE_HOST"))
return _request.UserHostName;
else if (StringUtil.EqualsIgnoreCase(name, "REMOTE_ADDRESS"))
return _request.UserHostAddress;
break;
case 'S':
case 's':
if (StringUtil.EqualsIgnoreCase(name, "SCRIPT_NAME"))
return _request.FilePath;
break;
}
}
// do the default processing (populate the collection)
return null;
}
//
// Enumerator must pre-populate the collection
//
public override IEnumerator GetEnumerator() {
Populate();
return base.GetEnumerator();
}
//
// NameValueCollection overrides
//
public override int Count {
get {
Populate();
return base.Count;
}
}
public override void Add(String name, String value) {
// not supported because it appends the value to a comma separated list
throw new NotSupportedException();
}
public override void Clear() {
throw new NotSupportedException();
}
public override String Get(String name) {
if (!_populated) {
String value = GetSimpleServerVar(name);
if (value != null)
return value;
Populate();
}
if (_iis7workerRequest != null) {
string var = GetServerVar(BaseGet(name));
if (String.IsNullOrEmpty(var)) {
var = _request.FetchServerVariable(name);
}
return var;
}
else {
return GetServerVar(BaseGet(name));
}
}
public override String[] GetValues(String name) {
String s = Get(name);
return(s != null) ? new String[1] { s} : null;
}
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.High)]
public override void Set(String name, String value) {
if (_iis7workerRequest == null) {
throw new PlatformNotSupportedException();
}
if (name == null) {
throw new ArgumentNullException("name");
}
SetNoDemand(name, value);
}
internal void SetNoDemand(String name, String value) {
if (value == null) {
value = String.Empty;
}
_iis7workerRequest.SetServerVariable(name, value);
SetServerVariableManagedOnly(name, value);
SynchronizeHeader(name, value);
_request.InvalidateParams();
}
private void SynchronizeHeader(String name, String value) {
if (StringUtil.StringStartsWith(name, "HTTP_"))
{
// update managed copy of header
string headerName = name.Substring("HTTP_".Length);
headerName = headerName.Replace('_', '-');
int knownIndex = HttpWorkerRequest.GetKnownRequestHeaderIndex(headerName);
if (knownIndex > -1) {
headerName = HttpWorkerRequest.GetKnownRequestHeaderName(knownIndex);
}
HttpHeaderCollection headers = _request.Headers as HttpHeaderCollection;
if (headers != null) {
headers.SynchronizeHeader(headerName, value);
}
}
}
// updates managed copy of server variable with current value from native header block
internal void SynchronizeServerVariable(String name, String value, bool ensurePopulated = true) {
if (name == null) {
throw new ArgumentNullException("name");
}
if (value != null) {
if (this._populated || ensurePopulated) {
SetServerVariableManagedOnly(name, value);
}
else {
// Lazy synchronization - when populate is indeed required
if (_unsyncedEntries == null) {
_unsyncedEntries = new List<HttpServerVarsCollectionEntry>();
}
_unsyncedEntries.Add(new HttpServerVarsCollectionEntry(name, value));
}
}
else {
base.Remove(name);
}
_request.InvalidateParams();
}
// updates managed copy of server variable with current value from native header block
private void SetServerVariableManagedOnly(String name, String value) {
Debug.Assert(name != null);
Debug.Assert(value != null);
// populate in order to identify dynamic variables
Populate();
// dynamic server variables cannot be modified
HttpServerVarsCollectionEntry entry = (HttpServerVarsCollectionEntry) BaseGet(name);
if (entry != null && entry.IsDynamic) {
throw new HttpException(SR.GetString(SR.Server_variable_cannot_be_modified));
}
InvalidateCachedArrays();
// this will update an existing entry, or create one if it's new
BaseSet(name, new HttpServerVarsCollectionEntry(name, value));
}
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.High)]
public override void Remove(String name) {
if (_iis7workerRequest == null) {
throw new PlatformNotSupportedException();
}
if (name == null) {
throw new ArgumentNullException("name");
}
RemoveNoDemand(name);
}
internal void RemoveNoDemand(String name) {
// delete by sending null value
_iis7workerRequest.SetServerVariable(name, null /*value*/);
base.Remove(name);
SynchronizeHeader(name, null);
_request.InvalidateParams();
}
public override String Get(int index) {
Populate();
return GetServerVar(BaseGet(index));
}
public override String[] GetValues(int index) {
String s = Get(index);
return(s != null) ? new String[1] { s} : null;
}
public override String GetKey(int index) {
Populate();
return base.GetKey(index);
}
public override string[] AllKeys {
get {
Populate();
return base.AllKeys;
}
}
//
// HttpValueCollection overrides
//
internal override string ToString(bool urlencoded) {
Populate();
StringBuilder s = new StringBuilder();
int n = Count;
String key, value;
for (int i = 0; i < n; i++) {
if (i > 0)
s.Append('&');
key = GetKey(i);
if (urlencoded)
key = UrlEncodeForToString(key);
s.Append(key);
s.Append('=');
value = Get(i);
if (urlencoded)
value = UrlEncodeForToString(value);
s.Append(value);
}
return s.ToString();
}
}
/*
* Entry in a server vars colleciton
*/
internal class HttpServerVarsCollectionEntry {
internal readonly String Name;
internal readonly bool IsDynamic;
internal readonly String Value;
internal readonly DynamicServerVariable Var;
internal HttpServerVarsCollectionEntry(String name, String value) {
Name = name;
Value = value;
IsDynamic = false;
}
internal HttpServerVarsCollectionEntry(String name, DynamicServerVariable var) {
Name = name;
Var = var;
IsDynamic = true;
}
internal String GetValue(HttpRequest request) {
String v = null;
if (IsDynamic) {
if (request != null)
v = request.CalcDynamicServerVariable(Var);
}
else {
v = Value;
}
return v;
}
}
}