~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/SimiasApp/.svn/text-base/MonoWorkerRequest.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// MonoWorkerRequest.cs
3
 
//
4
 
// Authors:
5
 
//      Daniel Lopez Ridruejo
6
 
//      Gonzalo Paniagua Javier
7
 
//
8
 
// Copyright (c) 2002 Daniel Lopez Ridruejo.
9
 
//           (c) 2002,2003 Ximian, Inc.
10
 
//           All rights reserved.
11
 
// (C) Copyright 2004 Novell, Inc. (http://www.novell.com)
12
 
//
13
 
// Permission is hereby granted, free of charge, to any person obtaining
14
 
// a copy of this software and associated documentation files (the
15
 
// "Software"), to deal in the Software without restriction, including
16
 
// without limitation the rights to use, copy, modify, merge, publish,
17
 
// distribute, sublicense, and/or sell copies of the Software, and to
18
 
// permit persons to whom the Software is furnished to do so, subject to
19
 
// the following conditions:
20
 
// 
21
 
// The above copyright notice and this permission notice shall be
22
 
// included in all copies or substantial portions of the Software.
23
 
// 
24
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
 
//
32
 
 
33
 
 
34
 
using System;
35
 
using System.Collections;
36
 
using System.Diagnostics;
37
 
using System.IO;
38
 
using System.Net;
39
 
using System.Net.Sockets;
40
 
using System.Reflection;
41
 
using System.Text;
42
 
using System.Threading;
43
 
using System.Web;
44
 
using System.Web.Hosting;
45
 
 
46
 
namespace Mono.ASPNET
47
 
{
48
 
        public class MapPathEventArgs : EventArgs
49
 
        {
50
 
                string path;
51
 
                string mapped;
52
 
                bool isMapped;
53
 
 
54
 
                public MapPathEventArgs (string path)
55
 
                {
56
 
                        this.path = path;
57
 
                        isMapped = false;
58
 
                }
59
 
 
60
 
                public string Path {
61
 
                        get { return path; }
62
 
                }
63
 
                
64
 
                public bool IsMapped {
65
 
                        get { return isMapped; }
66
 
                }
67
 
 
68
 
                public string MappedPath {
69
 
                        get { return mapped; }
70
 
                        set {
71
 
                                mapped = value;
72
 
                                isMapped = (value != null && value != "");
73
 
                        }
74
 
                }
75
 
        }
76
 
 
77
 
        public delegate void MapPathEventHandler (object sender, MapPathEventArgs args);
78
 
        public delegate void EndOfRequestHandler (MonoWorkerRequest request);
79
 
        
80
 
        public abstract class MonoWorkerRequest : SimpleWorkerRequest
81
 
        {
82
 
                IApplicationHost appHostBase;
83
 
                Encoding encoding;
84
 
                byte [] queryStringBytes;
85
 
                string hostVPath;
86
 
                string hostPath;
87
 
 
88
 
                public MonoWorkerRequest (IApplicationHost appHost)
89
 
                        : base (String.Empty, String.Empty, null)
90
 
                {
91
 
                        if (appHost == null)
92
 
                                throw new ArgumentNullException ("appHost");
93
 
 
94
 
                        appHostBase = appHost;
95
 
                }
96
 
 
97
 
                public event MapPathEventHandler MapPathEvent;
98
 
                public event EndOfRequestHandler EndOfRequestEvent;
99
 
                
100
 
                string HostPath {
101
 
                        get { 
102
 
                                if (hostPath == null)
103
 
                                        hostPath = appHostBase.Path;
104
 
 
105
 
                                return hostPath;
106
 
                        }
107
 
                }
108
 
 
109
 
                string HostVPath {
110
 
                        get { 
111
 
                                if (hostVPath == null)
112
 
                                        hostVPath = appHostBase.VPath;
113
 
 
114
 
                                return hostVPath;
115
 
                        }
116
 
                }
117
 
 
118
 
                protected virtual Encoding Encoding {
119
 
                        get {
120
 
                                if (encoding == null)
121
 
                                        encoding = new UTF8Encoding (false);
122
 
 
123
 
                                return encoding;
124
 
                        }
125
 
 
126
 
                        set { encoding = value; }
127
 
                }
128
 
 
129
 
                public override string GetAppPath ()
130
 
                {
131
 
                        return HostVPath;
132
 
                }
133
 
 
134
 
                public override string GetAppPathTranslated ()
135
 
                {
136
 
                        return HostPath;
137
 
                }
138
 
 
139
 
                public override string GetFilePathTranslated ()
140
 
                {
141
 
                        return MapPath (GetFilePath ());
142
 
                }
143
 
 
144
 
                public override string GetLocalAddress ()
145
 
                {
146
 
                        return "localhost";
147
 
                }
148
 
 
149
 
                public override string GetServerName ()
150
 
                {
151
 
                        string hostHeader = GetKnownRequestHeader(HeaderHost);
152
 
                        if (hostHeader == null || hostHeader.Length == 0) {
153
 
                                hostHeader = GetLocalAddress ();
154
 
                        } else {
155
 
                                int colonIndex = hostHeader.IndexOf (':');
156
 
                                if (colonIndex > 0) {
157
 
                                        hostHeader = hostHeader.Substring (0, colonIndex);
158
 
                                } else if (colonIndex == 0) {
159
 
                                        hostHeader = GetLocalAddress ();
160
 
                                }
161
 
                        }
162
 
                        return hostHeader;
163
 
                }
164
 
 
165
 
 
166
 
                public override int GetLocalPort ()
167
 
                {
168
 
                        return 0;
169
 
                }
170
 
 
171
 
                public override byte [] GetPreloadedEntityBody ()
172
 
                {
173
 
                        return null;
174
 
                }
175
 
 
176
 
                public override byte [] GetQueryStringRawBytes ()
177
 
                {
178
 
                        if (queryStringBytes == null) {
179
 
                                string queryString = GetQueryString ();
180
 
                                if (queryString != null)
181
 
                                        queryStringBytes = Encoding.GetBytes (queryString);
182
 
                        }
183
 
 
184
 
                        return queryStringBytes;
185
 
                }
186
 
 
187
 
                string DoMapPathEvent (string path)
188
 
                {
189
 
                        if (MapPathEvent != null) {
190
 
                                MapPathEventArgs args = new MapPathEventArgs (path);
191
 
                                foreach (MapPathEventHandler evt in MapPathEvent.GetInvocationList ()) {
192
 
                                        evt (this, args);
193
 
                                        if (args.IsMapped)
194
 
                                                return args.MappedPath;
195
 
                                }
196
 
                        }
197
 
 
198
 
                        return null;
199
 
                }
200
 
                
201
 
                public override string MapPath (string path)
202
 
                {
203
 
                        string eventResult = DoMapPathEvent (path);
204
 
                        if (eventResult != null)
205
 
                                return eventResult;
206
 
 
207
 
                        if (path == null || path.Length == 0 || path == HostVPath)
208
 
                                return HostPath.Replace ('/', Path.DirectorySeparatorChar);
209
 
 
210
 
                        if (path [0] == '~' && path.Length > 2 && path [1] == '/')
211
 
                                path = path.Substring (1);
212
 
 
213
 
                        int len = HostVPath.Length;
214
 
                        if (path.StartsWith (HostVPath) && (path.Length == len || path [len] == '/'))
215
 
                                path = path.Substring (len + 1);
216
 
 
217
 
                        if (path.Length > 0 && path [0] == '/')
218
 
                                path = path.Substring (1);
219
 
 
220
 
                        return Path.Combine (HostPath, path.Replace ('/', Path.DirectorySeparatorChar));
221
 
                }
222
 
 
223
 
                protected abstract bool GetRequestData ();
224
 
                public abstract int RequestId { get; }
225
 
 
226
 
                public bool ReadRequestData ()
227
 
                {
228
 
                        return GetRequestData ();
229
 
                }
230
 
 
231
 
                public void ProcessRequest ()
232
 
                {
233
 
                        HttpRuntime.ProcessRequest (this);
234
 
                }
235
 
 
236
 
                public override void EndOfRequest ()
237
 
                {
238
 
                        if (EndOfRequestEvent != null)
239
 
                                EndOfRequestEvent (this);
240
 
                }
241
 
 
242
 
                public override void SendCalculatedContentLength (int contentLength)
243
 
                {
244
 
                        //FIXME: Should we ignore this for apache2?
245
 
                        SendUnknownResponseHeader ("Content-Length", contentLength.ToString ());
246
 
                }
247
 
 
248
 
                public override void SendKnownResponseHeader (int index, string value)
249
 
                {
250
 
                        if (HeadersSent ())
251
 
                                return;
252
 
 
253
 
                        string headerName = HttpWorkerRequest.GetKnownResponseHeaderName (index);
254
 
                        SendUnknownResponseHeader (headerName, value);
255
 
                }
256
 
 
257
 
                private void SendStream (Stream stream, long offset, long length)
258
 
                {
259
 
                        if (offset < 0 || length <= 0)
260
 
                                return;
261
 
                        
262
 
                        long stLength = stream.Length;
263
 
                        if (offset + length > stLength)
264
 
                                length = stLength - offset;
265
 
 
266
 
                        if (offset > 0)
267
 
                                stream.Seek (offset, SeekOrigin.Begin);
268
 
 
269
 
                        byte [] fileContent = new byte [8192];
270
 
                        int count = fileContent.Length;
271
 
                        while (length > 0 && (count = stream.Read (fileContent, 0, count)) != 0) {
272
 
                                SendResponseFromMemory (fileContent, count);
273
 
                                length -= count;
274
 
                                count = (int) System.Math.Min (length, fileContent.Length);
275
 
                        }
276
 
                }
277
 
 
278
 
                public override void SendResponseFromFile (string filename, long offset, long length)
279
 
                {
280
 
                        Stream file = null;
281
 
                        try {
282
 
                                file = File.OpenRead (filename);
283
 
                                SendStream (file, offset, length);
284
 
                        } finally {
285
 
                                if (file != null)
286
 
                                        file.Close ();
287
 
                        }
288
 
                }
289
 
 
290
 
                public override void SendResponseFromFile (IntPtr handle, long offset, long length)
291
 
                {
292
 
                        Stream file = null;
293
 
                        try {
294
 
                                file = new FileStream (handle, FileAccess.Read);
295
 
                                SendStream (file, offset, length);
296
 
                        } finally {
297
 
                                if (file != null)
298
 
                                        file.Close ();
299
 
                        }
300
 
                }
301
 
        }
302
 
}
303