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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
//------------------------------------------------------------------------------
// <copyright file="StateWorkerRequest.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* StateHttpWorkerRequest
*
* Copyright (c) 1998-1999, Microsoft Corporation
*
*/
namespace System.Web.SessionState {
using System.Text;
using System.Configuration.Assemblies;
using System.Runtime.InteropServices;
using System.Collections;
using System.Web;
using System.Web.Util;
using System.Globalization;
class StateHttpWorkerRequest : HttpWorkerRequest {
/* long enough to hold the string representation of an IPv4 or IPv6 address; keep in sync with tracker.cxx */
private const int ADDRESS_LENGTH_MAX = 64;
IntPtr _tracker;
string _uri;
UnsafeNativeMethods.StateProtocolExclusive _exclusive;
int _extraFlags;
int _timeout;
int _lockCookie;
bool _lockCookieExists;
int _contentLength;
byte[] _content;
UnsafeNativeMethods.StateProtocolVerb _methodIndex;
string _method;
string _remoteAddress;
int _remotePort;
string _localAddress;
int _localPort;
StringBuilder _status;
int _statusCode;
StringBuilder _headers;
IntPtr _unmanagedState;
bool _sent;
internal StateHttpWorkerRequest(
IntPtr tracker,
UnsafeNativeMethods.StateProtocolVerb methodIndex,
string uri,
UnsafeNativeMethods.StateProtocolExclusive exclusive,
int extraFlags,
int timeout,
int lockCookieExists,
int lockCookie,
int contentLength,
IntPtr content
) {
_tracker = tracker;
_methodIndex = methodIndex;
switch (_methodIndex) {
case UnsafeNativeMethods.StateProtocolVerb.GET:
_method = "GET";
break;
case UnsafeNativeMethods.StateProtocolVerb.PUT:
_method = "PUT";
break;
case UnsafeNativeMethods.StateProtocolVerb.HEAD:
_method = "HEAD";
break;
case UnsafeNativeMethods.StateProtocolVerb.DELETE:
_method = "DELETE";
break;
default:
Debug.Assert(false, "Shouldn't get here!");
break;
}
_uri = uri;
// Handle the ASP1.1 case which prepends an extra / to the URI
if (_uri.StartsWith("//", StringComparison.Ordinal)) {
_uri = _uri.Substring(1);
}
_exclusive = exclusive;
_extraFlags = extraFlags;
_timeout = timeout;
_lockCookie = lockCookie;
_lockCookieExists = lockCookieExists != 0;
_contentLength = contentLength;
if (contentLength != 0) {
Debug.Assert(_contentLength == IntPtr.Size);
// Need to convert 'content', which is a ptr to native StateItem,
// into a byte array because that's what GetPreloadedEntityBody
// must return, and GetPreloadedEntityBody is what the pipeline uses
// to read the body of the request, which in our case is just a pointer
// to a native StateItem object.
#if WIN64
ulong p = (ulong) content;
_content = new byte[8]
{
(byte) ((p & 0x00000000000000ff)),
(byte) ((p & 0x000000000000ff00) >> 8),
(byte) ((p & 0x0000000000ff0000) >> 16),
(byte) ((p & 0x00000000ff000000) >> 24),
(byte) ((p & 0x000000ff00000000) >> 32),
(byte) ((p & 0x0000ff0000000000) >> 40),
(byte) ((p & 0x00ff000000000000) >> 48),
(byte) ((p & 0xff00000000000000) >> 56),
};
#else
uint p = (uint) content;
_content = new byte[4]
{
(byte) ((p & 0x000000ff)),
(byte) ((p & 0x0000ff00) >> 8),
(byte) ((p & 0x00ff0000) >> 16),
(byte) ((p & 0xff000000) >> 24),
};
#endif
}
_status = new StringBuilder(256);
_headers = new StringBuilder(256);
}
public override string GetUriPath() {
return HttpUtility.UrlDecode(_uri);
}
// The file path is used as the path for configuration.
// This path should always be null, in order to retrieve
// the machine configuration.
public override string GetFilePath() {
return null;
}
public override string GetQueryString() {
return null;
}
public override string GetRawUrl() {
return _uri;
}
public override string GetHttpVerbName() {
return _method;
}
public override string GetHttpVersion() {
return "HTTP/1.0";
}
public override string GetRemoteAddress() {
StringBuilder buf;
if (_remoteAddress == null) {
buf = new StringBuilder(ADDRESS_LENGTH_MAX);
UnsafeNativeMethods.STWNDGetRemoteAddress(_tracker, buf);
_remoteAddress = buf.ToString();
}
return _remoteAddress;
}
public override int GetRemotePort() {
if (_remotePort == 0) {
_remotePort = UnsafeNativeMethods.STWNDGetRemotePort(_tracker);
}
return _remotePort;
}
public override string GetLocalAddress() {
StringBuilder buf;
if (_localAddress == null) {
buf = new StringBuilder(ADDRESS_LENGTH_MAX);
UnsafeNativeMethods.STWNDGetLocalAddress(_tracker, buf);
_localAddress = buf.ToString();
}
return _localAddress;
}
public override int GetLocalPort() {
if (_localPort == 0) {
_localPort = UnsafeNativeMethods.STWNDGetLocalPort(_tracker);
}
return _localPort;
}
public override byte[] GetPreloadedEntityBody() {
return _content;
}
public override bool IsEntireEntityBodyIsPreloaded() {
/* Request is always preloaded */
return true;
}
public override string MapPath(string virtualPath) {
/*
* Physical and virtual are identical to state server.
*/
return virtualPath;
}
public override int ReadEntityBody(byte[] buffer, int size) {
/* pretend everything is preloaded */
return 0;
}
public override long GetBytesRead() {
/* State web doesn't support partial reads */
throw new NotSupportedException(SR.GetString(SR.Not_supported));
}
public override string GetKnownRequestHeader(int index) {
string s = null;
switch (index) {
/* special case important ones */
case HeaderContentLength:
s = (_contentLength).ToString(CultureInfo.InvariantCulture);
break;
}
return s;
}
public override string GetUnknownRequestHeader(string name) {
string s = null;
if (name.Equals(StateHeaders.EXCLUSIVE_NAME)) {
switch (_exclusive) {
case UnsafeNativeMethods.StateProtocolExclusive.ACQUIRE:
s = StateHeaders.EXCLUSIVE_VALUE_ACQUIRE;
break;
case UnsafeNativeMethods.StateProtocolExclusive.RELEASE:
s = StateHeaders.EXCLUSIVE_VALUE_RELEASE;
break;
}
}
else if (name.Equals(StateHeaders.TIMEOUT_NAME)) {
if (_timeout != -1) {
s = (_timeout).ToString(CultureInfo.InvariantCulture);
}
}
else if (name.Equals(StateHeaders.LOCKCOOKIE_NAME)) {
if (_lockCookieExists) {
s = (_lockCookie).ToString(CultureInfo.InvariantCulture);
}
}
else if (name.Equals(StateHeaders.EXTRAFLAGS_NAME)) {
if (_extraFlags != -1) {
s = (_extraFlags).ToString(CultureInfo.InvariantCulture);
}
}
return s;
}
public override string[][] GetUnknownRequestHeaders() {
string [][] ret;
int c, i;
c = 0;
if (_exclusive != (UnsafeNativeMethods.StateProtocolExclusive) (-1)) {
c++;
}
if (_extraFlags != -1) {
c++;
}
if (_timeout != -1) {
c++;
}
if (_lockCookieExists) {
c++;
}
if (c == 0)
return null;
ret = new string[c][];
i = 0;
if (_exclusive != (UnsafeNativeMethods.StateProtocolExclusive) (-1)) {
ret[0] = new string[2];
ret[0][0] = StateHeaders.EXCLUSIVE_NAME;
if (_exclusive == UnsafeNativeMethods.StateProtocolExclusive.ACQUIRE) {
ret[0][1] = StateHeaders.EXCLUSIVE_VALUE_ACQUIRE;
}
else {
Debug.Assert(_exclusive == UnsafeNativeMethods.StateProtocolExclusive.RELEASE, "_exclusive == UnsafeNativeMethods.StateProtocolExclusive.RELEASE");
ret[0][1] = StateHeaders.EXCLUSIVE_VALUE_RELEASE;
}
i++;
}
if (_timeout != -1) {
ret[i] = new string[2];
ret[i][0] = StateHeaders.TIMEOUT_NAME;
ret[i][1] = (_timeout).ToString(CultureInfo.InvariantCulture);
i++;
}
if (_lockCookieExists) {
ret[i] = new string[2];
ret[i][0] = StateHeaders.LOCKCOOKIE_NAME;
ret[i][1] = (_lockCookie).ToString(CultureInfo.InvariantCulture);
i++;
}
if (_extraFlags != -1) {
ret[i] = new string[2];
ret[i][0] = StateHeaders.EXTRAFLAGS_NAME;
ret[i][1] = (_extraFlags).ToString(CultureInfo.InvariantCulture);
i++;
}
return ret;
}
public override void SendStatus(int statusCode, string statusDescription) {
Debug.Assert(!_sent);
_statusCode = statusCode;
_status.Append((statusCode).ToString(CultureInfo.InvariantCulture) + " " + statusDescription + "\r\n");
}
public override void SendKnownResponseHeader(int index, string value) {
Debug.Assert(!_sent);
_headers.Append(GetKnownResponseHeaderName(index));
_headers.Append(": ");
_headers.Append(value);
_headers.Append("\r\n");
}
public override void SendUnknownResponseHeader(string name, string value) {
Debug.Assert(!_sent);
_headers.Append(name);
_headers.Append(": ");
_headers.Append(value);
_headers.Append("\r\n");
}
public override void SendCalculatedContentLength(int contentLength) {
Debug.Assert(!_sent);
/*
* Do nothing - we append the content-length in STWNDSendResponse.
*/
}
public override bool HeadersSent() {
return _sent;
}
public override bool IsClientConnected() {
return UnsafeNativeMethods.STWNDIsClientConnected(_tracker);
}
public override void CloseConnection() {
UnsafeNativeMethods.STWNDCloseConnection(_tracker);
}
private void SendResponse() {
if (!_sent) {
_sent = true;
UnsafeNativeMethods.STWNDSendResponse(
_tracker,
_status,
_status.Length,
_headers,
_headers.Length,
_unmanagedState);
}
}
public override void SendResponseFromMemory(byte[] data, int length) {
/*
* The only content besides error message text is the pointer
* to the state item in unmanaged memory.
*/
if (_statusCode == 200) {
Debug.Assert(_unmanagedState == IntPtr.Zero, "_unmanagedState == 0");
Debug.Assert(length == IntPtr.Size, "length == IntPtr.Size");
Debug.Assert(_methodIndex == UnsafeNativeMethods.StateProtocolVerb.GET, "verb == GET");
Debug.Assert(_exclusive != UnsafeNativeMethods.StateProtocolExclusive.RELEASE,
"correct exclusive method");
if (IntPtr.Size == 4) {
_unmanagedState = (IntPtr)
(((int)data[0]) |
((int)data[1] << 8) |
((int)data[2] << 16) |
((int)data[3] << 24));
}
else {
_unmanagedState = (IntPtr)
(((long)data[0]) |
((long)data[1] << 8) |
((long)data[2] << 16) |
((long)data[3] << 24) |
((long)data[4] << 32) |
((long)data[5] << 40) |
((long)data[6] << 48) |
((long)data[7] << 56));
}
Debug.Assert(_unmanagedState != IntPtr.Zero, "_unmanagedState != 0");
}
SendResponse();
}
public override void SendResponseFromFile(string filename, long offset, long length) {
/* Not needed by state application */
throw new NotSupportedException(SR.GetString(SR.Not_supported));
}
public override void SendResponseFromFile(IntPtr handle, long offset, long length) {
/* Not needed by state application */
throw new NotSupportedException(SR.GetString(SR.Not_supported));
}
public override void FlushResponse(bool finalFlush) {
SendResponse();
}
public override void EndOfRequest() {
SendResponse();
UnsafeNativeMethods.STWNDEndOfRequest(_tracker);
}
}
}
|