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
|
//
// System.Web.Hosting.SimpleWorkerRequest.cs
//
// Authors:
// Patrik Torstensson (Patrik.Torstensson@labs2.com)
// (class signature from Bob Smith <bob@thestuff.net> (C) )
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Web.Util;
namespace System.Web.Hosting
{
[MonoTODO("Implement security demands on the path usage functions (and review)")]
[ComVisible (false)]
public class SimpleWorkerRequest : HttpWorkerRequest
{
private string _Page;
private string _Query;
private string _PathInfo = String.Empty;
private string _AppVirtualPath;
private string _AppPhysicalPath;
private string _AppInstallPath;
private TextWriter _Output;
private bool _HasInstallInfo;
private SimpleWorkerRequest ()
{
}
public SimpleWorkerRequest (string Page, string Query, TextWriter Output)
{
_Page = Page;
_Query = Query;
AppDomain current = AppDomain.CurrentDomain;
object o = current.GetData (".appPath");
if (o == null)
throw new HttpException ("Cannot get .appPath");
_AppPhysicalPath = o.ToString ();
o = current.GetData (".hostingVirtualPath");
if (o == null)
throw new HttpException ("Cannot get .hostingVirtualPath");
_AppVirtualPath = o.ToString ();
o = current.GetData (".hostingInstallDir");
if (o == null)
throw new HttpException ("Cannot get .hostingInstallDir");
_AppInstallPath = o.ToString ();
_Output = Output;
if (_AppPhysicalPath == null)
throw new HttpException ("Invalid app domain");
_HasInstallInfo = true;
ExtractPagePathInfo();
}
public SimpleWorkerRequest (string AppVirtualPath,
string AppPhysicalPath,
string Page,
string Query,
TextWriter Output)
{
if (AppDomain.CurrentDomain.GetData (".appPath") == null)
throw new HttpException ("Invalid app domain");
_Page = Page;
_Query = Query;
_AppVirtualPath = AppVirtualPath;
_AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
_Output = Output;
_HasInstallInfo = false;
ExtractPagePathInfo();
}
[MonoTODO("Implement security")]
public override string MachineInstallDirectory
{
get {
if (_HasInstallInfo)
return _AppInstallPath;
return ICalls.GetMachineInstallDirectory ();
}
}
public override string MachineConfigPath
{
get { return ICalls.GetMachineConfigPath (); }
}
public override void EndOfRequest ()
{
}
public override void FlushResponse (bool finalFlush)
{
}
public override string GetAppPath ()
{
return _AppVirtualPath;
}
public override string GetAppPathTranslated ()
{
return _AppPhysicalPath;
}
public override string GetFilePath ()
{
return CreatePath (false);
}
public override string GetFilePathTranslated ()
{
string page = _Page;
if (Path.DirectorySeparatorChar != '/')
page = _Page.Replace ('/', Path.DirectorySeparatorChar);
if (page [0] == Path.DirectorySeparatorChar)
page = page.Substring (1);
return (Path.Combine (_AppPhysicalPath, page));
}
public override string GetHttpVerbName ()
{
return "GET";
}
public override string GetHttpVersion ()
{
return "HTTP/1.0";
}
public override string GetLocalAddress ()
{
return "127.0.0.1";
}
public override int GetLocalPort ()
{
return 80;
}
public override string GetPathInfo ()
{
return _PathInfo;
}
public override string GetQueryString ()
{
return _Query;
}
public override string GetRawUrl ()
{
string path = CreatePath (true);
if (null != _Query && _Query.Length > 0)
return path + "?" + _Query;
return path;
}
public override string GetRemoteAddress()
{
return "127.0.0.1";
}
public override int GetRemotePort()
{
return 0;
}
public override string GetServerVariable(string name)
{
return String.Empty;
}
public override string GetUriPath()
{
return CreatePath (true);
}
public override IntPtr GetUserToken()
{
return IntPtr.Zero;
}
public override string MapPath (string path)
{
string sPath = _AppPhysicalPath.Substring (0, _AppPhysicalPath.Length - 1);
if (path != null && path.Length > 0 && path [0] != '/')
return sPath;
char sep = Path.DirectorySeparatorChar;
if (path.StartsWith(_AppVirtualPath)) {
if (sep == '/')
return sPath + path.Substring (_AppVirtualPath.Length);
else
return sPath + path.Substring (_AppVirtualPath.Length).Replace ('/', sep);
}
return null;
}
public override void SendKnownResponseHeader (int index, string value)
{
}
public override void SendResponseFromFile (IntPtr handle, long offset, long length)
{
}
public override void SendResponseFromFile (string filename, long offset, long length)
{
}
public override void SendResponseFromMemory (byte [] data, int length)
{
_Output.Write (Encoding.Default.GetChars (data, 0, length));
}
public override void SendStatus(int statusCode, string statusDescription)
{
}
public override void SendUnknownResponseHeader(string name, string value)
{
}
// Create's a path string
private string CheckAndAddSlash(string sPath)
{
if (null == sPath)
return null;
if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
return sPath + Path.DirectorySeparatorChar;
return sPath;
}
// Create's a path string
private string CreatePath (bool bIncludePathInfo)
{
string sPath = Path.Combine (_AppVirtualPath, _Page);
if (bIncludePathInfo)
{
sPath += _PathInfo;
}
return sPath;
}
// "The extra path information, as given by the client. In
// other words, scripts can be accessed by their virtual
// pathname, followed by extra information at the end of this
// path. The extra information is sent as PATH_INFO."
private void ExtractPagePathInfo ()
{
if (_Page == null || _Page == String.Empty)
return;
string FullPath = GetFilePathTranslated ();
int PathInfoLength = 0;
string LastFile = String.Empty;
while (PathInfoLength < _Page.Length) {
if (LastFile.Length > 0) {
// increase it by the length of the file plus
// a "/"
//
PathInfoLength += LastFile.Length + 1;
}
if (File.Exists (FullPath) == true)
break;
if (Directory.Exists (FullPath) == true) {
PathInfoLength -= (LastFile.Length + 1);
break;
}
LastFile = Path.GetFileName (FullPath);
FullPath = Path.GetDirectoryName (FullPath);
}
if (PathInfoLength <= 0 || PathInfoLength > _Page.Length)
return;
_PathInfo = _Page.Substring (_Page.Length - PathInfoLength);
_Page = _Page.Substring (0, _Page.Length - PathInfoLength);
}
}
}
|