New test.
[mono.git] / mcs / class / System.Web / System.Web.Hosting / ServletWorkerRequest.jvm.cs
1 //
2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
3 //
4
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 using System;
26 using System.IO;
27 using System.Text;
28 using System.Runtime.InteropServices;
29 using System.Web.Util;
30 using vmw.common;
31 using System.Web.J2EE;
32 using System.Collections;
33 using System.Web;
34 using javax.servlet;
35 using javax.servlet.http;
36 using System.Collections.Specialized;
37 using System.Globalization;
38
39 namespace System.Web.Hosting {
40         [MonoTODO("Implement security demands on the path usage functions (and review)")]
41         [ComVisible (false)]
42         internal sealed class ServletWorkerRequest : HttpWorkerRequest {
43                 readonly HttpServlet _HttpServlet;
44                 readonly HttpServletRequest _HttpServletRequest;
45                 readonly HttpServletResponse _HttpServletResponse;
46
47                 readonly string _requestUri;
48                 readonly string _pathInfo;
49
50                 static readonly StringDictionary _srvVarsToHeaderMap;
51
52                 private string [][] unknownHeaders;
53                 string _rawUrl;
54
55                 private HttpWorkerRequest.EndOfSendNotification _endOfSendCallback;
56                 private object _endOfSendArgs;
57
58                 enum KnownServerVariable {
59                         AUTH_TYPE,
60                         CONTENT_LENGTH,
61                         CONTENT_TYPE,
62                         QUERY_STRING,
63                         REMOTE_ADDR,
64                         REMOTE_HOST,
65                         REMOTE_USER,
66                         REQUEST_METHOD,
67                         REQUEST_URI,
68                         SCRIPT_NAME,
69                         SERVER_NAME,
70                         SERVER_PORT,
71                         SERVER_PROTOCOL,
72                         SERVER_SOFTWARE,
73                         PATH_INFO
74                 };
75
76                 static readonly Hashtable KnownServerVariableMap;
77
78                 static ServletWorkerRequest() {
79                         _srvVarsToHeaderMap = new StringDictionary();
80                         _srvVarsToHeaderMap.Add("HTTP_ACCEPT", "Accept");
81                         _srvVarsToHeaderMap.Add("HTTP_REFERER", "Referer");
82                         _srvVarsToHeaderMap.Add("HTTP_ACCEPT_LANGUAGE", "Accept-Language");
83                         _srvVarsToHeaderMap.Add("HTTP_ACCEPT_ENCODING", "Accept-Encoding");
84                         _srvVarsToHeaderMap.Add("HTTP_CONNECTION", "Connection");
85                         _srvVarsToHeaderMap.Add("HTTP_HOST", "Host");
86                         _srvVarsToHeaderMap.Add("HTTP_USER_AGENT", "User-Agent");
87                         _srvVarsToHeaderMap.Add("HTTP_SOAPACTION", "SOAPAction");
88
89                         string[] knownServerVariableNames = Enum.GetNames(typeof(KnownServerVariable));
90                         KnownServerVariableMap = CollectionsUtil.CreateCaseInsensitiveHashtable(knownServerVariableNames.Length);
91                         for (int i = 0; i < knownServerVariableNames.Length; i++)
92                                 KnownServerVariableMap[knownServerVariableNames[i]] = (KnownServerVariable)i;
93                 }
94
95                 public ServletWorkerRequest (HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp) {
96                         _HttpServlet = servlet;
97                         _HttpServletRequest = req;
98                         _HttpServletResponse = resp;
99
100                         string contextPath = req.getContextPath();
101                         string requestURI = req.getRequestURI();
102                         if (String.CompareOrdinal(requestURI, contextPath) == 0 ||
103                                 (((requestURI.Length - contextPath.Length) == 1) &&
104                                         requestURI[contextPath.Length] == '/' &&
105                                         String.CompareOrdinal(requestURI, 0, contextPath, 0, contextPath.Length) == 0 ))
106                                 requestURI = contextPath + req.getServletPath();        
107
108                         _requestUri = HttpUtility.UrlDecode(requestURI);
109                         const int dotInvokeLength = 7; //".invoke".Length
110                         if (_requestUri.Length > dotInvokeLength &&
111                                 String.CompareOrdinal(".invoke", 0, _requestUri, 
112                                 _requestUri.Length - dotInvokeLength, dotInvokeLength) == 0) {
113
114                                 _requestUri = _requestUri.Substring(0, _requestUri.Length - dotInvokeLength);
115                                 
116                                 int paramNameStart = _requestUri.LastIndexOf('/');
117                                 _pathInfo = _requestUri.Substring(paramNameStart, _requestUri.Length - paramNameStart);
118                         }
119                 }
120
121                 public HttpServlet Servlet {
122                         get {
123                                 return _HttpServlet;
124                         }
125                 }
126                 
127                 public HttpServletRequest ServletRequest {
128                         get{
129                                 return _HttpServletRequest;
130                         }
131                 }
132
133                 public HttpServletResponse ServletResponse {
134                         get{
135                                 return _HttpServletResponse;
136                         }
137                 }
138                 
139                 [MonoTODO("Implement security")]
140                 public override string MachineInstallDirectory {
141                         get {
142                                 return ICalls.GetMachineInstallDirectory ();
143                         }
144                 }
145
146                 public override string MachineConfigPath {
147                         get { return ICalls.GetMachineConfigPath (); }
148                 }
149
150                 public override void EndOfRequest () {
151                         if (_endOfSendCallback != null)
152                                 _endOfSendCallback(this, _endOfSendArgs);
153                 }
154
155                 public override void FlushResponse (bool finalFlush) {
156                         ServletOutputStream servletOutputStream = _HttpServletResponse.getOutputStream();
157                         servletOutputStream.flush();
158                         if (finalFlush)
159                                 servletOutputStream.close();
160                 }
161
162                 public override string GetAppPath () {
163                         return _HttpServletRequest.getContextPath();
164                 }
165                 public override string GetAppPathTranslated () {
166                         return J2EEUtils.GetApplicationRealPath(_HttpServlet.getServletConfig());;
167                 }
168
169                 public override string GetFilePath () {
170                         string uri = GetUriPath();
171                         string pathInfo = GetPathInfo();
172                         if (pathInfo != null && pathInfo.Length > 0)
173                                 uri = uri.Substring(0, uri.Length - pathInfo.Length);
174
175                         return uri;
176                 }
177
178                 public override string GetFilePathTranslated () {
179                         string page = GetFilePath ();
180
181                         if (Path.DirectorySeparatorChar != '/')
182                                 page = page.Replace ('/', Path.DirectorySeparatorChar);
183
184                         if (page [0] == Path.DirectorySeparatorChar)
185                                 page = page.Substring (1);
186                         
187                         return Path.Combine (GetAppPathTranslated (), page);
188                 }
189
190                 public override string GetHttpVerbName () {
191                         return _HttpServletRequest.getMethod();
192                 }
193
194                 public override string GetHttpVersion () {
195                         return _HttpServletRequest.getProtocol();
196                 }
197
198                 public override string GetLocalAddress () {
199                         return _HttpServletRequest.getLocalAddr();
200                 }
201
202                 public override int GetLocalPort () {
203                         return _HttpServletRequest.getLocalPort();
204                 }
205
206                 public override string GetPathInfo () {
207                         return _pathInfo != null ? _pathInfo : _HttpServletRequest.getPathInfo();
208                 }
209
210                 public override string GetQueryString () {
211                         return _HttpServletRequest.getQueryString();
212                 }
213
214                 public override string GetRawUrl () {
215                         if (_rawUrl == null) {
216                                 StringBuilder builder = new StringBuilder();
217                                 builder.Append(GetUriPath());
218                                 string pathInfo = GetPathInfo();
219                                 string query = GetQueryString();
220                                 if (query != null && query.Length > 0) {
221                                         builder.Append('?');
222                                         builder.Append(query);
223                                 }
224
225                                 _rawUrl = builder.ToString();
226                         }
227
228                         return _rawUrl;
229                 }
230
231                 public override string GetRemoteAddress() {
232                         return _HttpServletRequest.getRemoteAddr();
233                 }
234
235                 public override string GetRemoteName() {
236                         return _HttpServletRequest.getRemoteHost();
237                 }
238
239
240                 public override int GetRemotePort() {
241                         try {
242                                 return _HttpServletRequest.getRemotePort();
243                         }
244                         catch(Exception e) { //should catch also java.lang.Throwable
245                                 //if servlet API is 2.3 and below - there is no
246                                 //method getRemotePort in ServletRequest interface...
247                                 //should be described as limitation.
248                                 return 0;
249                         }
250                 }
251
252                 public override string GetServerVariable(string name) {
253                         // FIXME: We need to make a proper mapping between the standard server
254                         // variables and java equivalent. probably we have to have a configuration file 
255                         // which associates between the two. Pay a special attention on GetUnknownRequestHeader/s
256                         // while implementing. Ensure that system web "common" code correctly calls each method.
257
258                         string headerName = _srvVarsToHeaderMap[name];
259
260                         if (headerName != null)
261                                 return _HttpServletRequest.getHeader( headerName );
262
263                         object knownVariable = KnownServerVariableMap[name];
264                         if (knownVariable != null)
265                                 return GetKnownServerVariable((KnownServerVariable)knownVariable);
266
267                         return _HttpServletRequest.getHeader( name );
268                 }
269
270                 string GetKnownServerVariable(KnownServerVariable index) {
271                         switch (index) {
272                                 case KnownServerVariable.AUTH_TYPE : return _HttpServletRequest.getAuthType();
273                                 case KnownServerVariable.CONTENT_LENGTH : return Convert.ToString(_HttpServletRequest.getContentLength());
274                                 case KnownServerVariable.CONTENT_TYPE : return _HttpServletRequest.getContentType();
275                                 case KnownServerVariable.QUERY_STRING : return GetQueryString();
276                                 case KnownServerVariable.REMOTE_ADDR : return GetRemoteAddress();
277                                 case KnownServerVariable.REMOTE_HOST : return GetRemoteName();
278                                 case KnownServerVariable.REMOTE_USER : return _HttpServletRequest.getRemoteUser();
279                                 case KnownServerVariable.REQUEST_METHOD : return GetHttpVerbName ();
280                                 case KnownServerVariable.REQUEST_URI : return GetUriPath();
281                                 case KnownServerVariable.SCRIPT_NAME : return GetFilePath ();
282                                 case KnownServerVariable.SERVER_NAME : return GetServerName();
283                                 case KnownServerVariable.SERVER_PORT : return Convert.ToString(_HttpServletRequest.getServerPort());
284                                 case KnownServerVariable.SERVER_PROTOCOL : return GetHttpVersion ();
285                                 case KnownServerVariable.SERVER_SOFTWARE : return Servlet.getServletContext().getServerInfo();
286                                 case KnownServerVariable.PATH_INFO : return GetPathInfo();
287                                 default: throw new IndexOutOfRangeException("index");
288                         }
289                 }
290
291                 public override string GetUriPath() {
292                         return _requestUri;
293                 }
294
295                 public override IntPtr GetUserToken() {
296                         return IntPtr.Zero;
297                 }
298
299                 public override string MapPath (string path) {
300                         string appVirtualPath = GetAppPath();
301                         if (path.StartsWith(appVirtualPath)) {
302                                 path = path.Remove(0,appVirtualPath.Length);
303                                 if (path.StartsWith("/"))
304                                         path = path.Remove(0,1);
305                         }
306                         //string realPath = Servlet.getServletContext().getRealPath(path);
307                         //                      if (Path.IsPathRooted(path))
308                         //                              return path;
309                         //                      if (!path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)&& 
310                         //                              !path.StartsWith("/") && !path.StartsWith("\\")&& !Path.IsPathRooted(path))            
311                         //                              return IAppDomainConfig.WAR_ROOT_SYMBOL + "/" + path;
312                         //                      else if (!path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)&& !Path.IsPathRooted(path))
313                         //                              return IAppDomainConfig.WAR_ROOT_SYMBOL + path;
314                         //                      else 
315                         //                              return path;
316
317                         if (path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)) {
318                                 return path;
319                         }
320
321                         string retVal =  IAppDomainConfig.WAR_ROOT_SYMBOL;
322
323                         if (!path.StartsWith("/") && !path.StartsWith("\\"))
324                                 retVal += "/";
325
326                         retVal += path;
327
328                         return retVal;
329                 }
330
331                 public override void SendResponseFromFile (IntPtr handle, long offset, long length) {
332                         throw new NotSupportedException();
333                 }
334
335                 public override void SendResponseFromFile (string filename, long offset, long length) {
336                         using (FileStream fs = File.OpenRead (filename)) {
337                                 byte [] buffer = new byte [4 * 1024];
338
339                                 if (offset != 0)
340                                         fs.Position = offset;
341
342                                 long remain = length;
343                                 int n;
344                                 while (remain > 0 && (n = fs.Read (buffer, 0, (int) Math.Min (remain, buffer.Length))) != 0){
345                                         remain -= n;
346                                         SendResponseFromMemory(buffer, n);
347                                 }
348                         }
349                 }
350
351                 public override void SendResponseFromMemory (byte [] data, int length) {
352                         sbyte [] sdata = vmw.common.TypeUtils.ToSByteArray(data);
353                         _HttpServletResponse.getOutputStream().write(sdata, 0 , length);
354                 }
355
356                 public override void SendStatus(int statusCode, string statusDescription) {
357                         // setStatus(int, string) is deprecated
358                         _HttpServletResponse.setStatus(statusCode/*, statusDescription*/);
359                 }
360
361                 public override void SendUnknownResponseHeader(string name, string value) {
362                         if (HeadersSent ())
363                                 return;
364
365                         _HttpServletResponse.addHeader(name, value);
366                 }
367
368                 public override bool HeadersSent () {
369                         return _HttpServletResponse.isCommitted();
370                 }
371
372                 public override void SendCalculatedContentLength (int contentLength) {
373                         _HttpServletResponse.setContentLength(contentLength);
374                 }
375
376                 public override void SendKnownResponseHeader (int index, string value) {
377                         SendUnknownResponseHeader (GetKnownResponseHeaderName (index), value);
378                 }
379
380                 public override string GetKnownRequestHeader (int index) {      
381                         return GetUnknownRequestHeader(GetKnownRequestHeaderName (index));
382                 }
383
384                 public override string GetUnknownRequestHeader (string name) {
385                         return _HttpServletRequest.getHeader(name);
386                 }
387
388                 public override string [][] GetUnknownRequestHeaders () {
389                         if (unknownHeaders == null) {
390                                 ArrayList pairs = new ArrayList ();
391                                 for (java.util.Enumeration he = _HttpServletRequest.getHeaderNames(); he.hasMoreElements() ;) {
392                                         string key = (string) he.nextElement();
393                                         int index = HttpWorkerRequest.GetKnownRequestHeaderIndex (key);
394                                         if (index != -1)
395                                                 continue;
396                                         pairs.Add (new string [] {key, _HttpServletRequest.getHeader(key)});
397                                 }
398                                 
399                                 if (pairs.Count != 0) {
400                                         unknownHeaders = new string [pairs.Count][];
401                                         for (int i = 0; i < pairs.Count; i++)
402                                                 unknownHeaders [i] = (string []) pairs [i];
403                                 }
404                         }
405                         if (unknownHeaders == null) unknownHeaders = new string [0][];
406
407                         return unknownHeaders;
408                 }
409
410                 public override int ReadEntityBody (byte [] buffer, int size) {
411                         if (buffer == null || size == 0)
412                                 return 0;
413                         sbyte [] sbuffer = vmw.common.TypeUtils.ToSByteArray(buffer);
414                         int r = _HttpServletRequest.getInputStream().read(sbuffer, 0, size);    
415                         return (r==-1)?0:r;
416                 }
417
418                 public override void SetEndOfSendNotification(System.Web.HttpWorkerRequest.EndOfSendNotification callback, object extraData) {
419                         _endOfSendCallback = callback;
420                         _endOfSendArgs = extraData;
421                 }
422
423                 public override string GetProtocol() {
424                         return _HttpServletRequest.getScheme();
425                 }
426
427                 public override string GetServerName() {
428                         return _HttpServletRequest.getServerName();
429                 }
430
431                 public override bool IsSecure() {
432                         return _HttpServletRequest.isSecure();
433                 }
434         }
435 }
436