Implemented Servlet session management. Servlet hosting moved to Mainsoft.Web package
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / BaseStaticHttpServlet.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
26 using System;
27 using System.IO;
28 using System.Configuration;
29 using System.Web.Configuration;
30 using System.Threading;
31
32 using javax.servlet;
33 using javax.servlet.http;
34 using vmw.common;
35
36 namespace System.Web.J2EE
37 {
38         public class BaseStaticHttpServlet : HttpServlet
39         {
40                 public BaseStaticHttpServlet()
41                 {
42                 }
43
44                 override public void init(ServletConfig config)
45                 {
46                         base.init(config);
47                         ServletContext context = config.getServletContext();
48                         AppDir = config.getInitParameter(IAppDomainConfig.APP_DIR_NAME);
49                         if (AppDir != null) {
50                                 AppDir = AppDir.Replace('\\', '/');
51                                 if (AppDir[AppDir.Length - 1] != '/')
52                                         AppDir += '/';
53                         }
54                 }
55
56                 override protected void service(HttpServletRequest req, HttpServletResponse resp)
57                 {
58                         String pathInfo = req.getRequestURI();
59                         String contextPath = req.getContextPath();
60                         if (pathInfo.Equals(contextPath) ||
61                                 ((pathInfo.Length - contextPath.Length) == 1) && pathInfo[pathInfo.Length-1] == '/' && pathInfo.StartsWith(contextPath))
62                                 pathInfo = contextPath + req.getServletPath();
63                         resp.setHeader("X-Powered-By", "ASP.NET");
64                         resp.setHeader("X-AspNet-Version", "1.1.4322");
65
66                         ServletOutputStream hos = resp.getOutputStream();
67                         String filename = "";
68                         try 
69                         {
70                                 pathInfo = pathInfo.Substring(contextPath.Length);
71                                 if (pathInfo.StartsWith("/") || pathInfo.StartsWith("\\"))
72                                         pathInfo = pathInfo.Substring(1);
73                                 filename = AppDir + pathInfo;
74                                 resp.setContentType(this.getServletContext().getMimeType(filename));
75                                 FileStream fis = null;
76                                 try {
77                                         fis = new FileStream(filename,FileMode.Open,FileAccess.Read);
78                                         byte[] buf = new byte[4 * 1024];  // 4K buffer
79                                         int bytesRead;
80                                         while ((bytesRead = fis.Read(buf,0,buf.Length)) != -1 &&
81                                                    bytesRead != 0) {
82                                                 hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
83                                         }
84                                 }
85                                 finally {
86                                         if (fis != null) fis.Close();
87                                 }
88                         }
89                         catch (System.IO.FileNotFoundException e) 
90                         {
91                                 resp.setStatus(404,"Object Not Found.");
92                                 HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
93                                 hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
94                                 hos.flush();
95                         }
96                         catch(Exception e) 
97                         {
98                                 Console.WriteLine("ERROR in Static File Reading {0},{1}",e.GetType(), e.Message);
99                                 resp.setStatus(500);
100                                 HttpException myExp = new HttpException ("Exception in Reading static file", e);
101                                 hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
102                                 hos.flush();
103                         }
104                 }
105
106                 override public void destroy()
107                 {
108                         base.destroy();
109                 }
110
111                 private string AppDir;
112         }
113 }
114
115 namespace System.Web.GH
116 {
117         public class BaseStaticHttpServlet : System.Web.J2EE.BaseStaticHttpServlet
118         {
119         }
120
121 }