fix of 7784
[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;
30 using System.Threading;
31
32 using javax.servlet;
33 using javax.servlet.http;
34 using vmw.common;
35 using System.Diagnostics;
36
37 namespace Mainsoft.Web.Hosting
38 {
39         /// <summary>
40         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
41         /// </summary>
42         public class BaseStaticHttpServlet : HttpServlet
43         {
44                 public BaseStaticHttpServlet()
45                 {
46                 }
47
48                 override public void init(ServletConfig config)
49                 {
50                         base.init(config);
51                         AppDir = config.getServletContext ().getInitParameter (IAppDomainConfig.APP_DIR_NAME);
52                         if (AppDir != null) {
53                                 AppDir = AppDir.Replace('\\', '/');
54                                 if (AppDir[AppDir.Length - 1] != '/')
55                                         AppDir += '/';
56                         }
57                 }
58
59                 override protected void service(HttpServletRequest req, HttpServletResponse resp)
60                 {
61                         resp.setHeader("X-Powered-By", "ASP.NET");
62                         resp.setHeader("X-AspNet-Version", "1.1.4322");
63
64                         String filename = getServletContext().getRealPath(req.getServletPath());
65                         ServletOutputStream hos;
66                         try {
67                                 hos = resp.getOutputStream();
68                         }
69                         catch (java.lang.IllegalStateException e)
70                         {
71                                 string mimeType = getServletContext().getMimeType(filename);
72                                 if (mimeType == null || mimeType.StartsWith("text")) {
73                                         sendFileUsingWriter(resp, filename);
74                                         return;
75                                 }
76                                 else
77                                         throw e;
78                         }
79                         try 
80                         {
81                                 string mimeType = this.getServletContext().getMimeType(filename);
82                                 if (mimeType == null)
83                                         mimeType = "text/plain";
84                                 
85                                 resp.setContentType(mimeType);
86
87                                 FileStream fis = null;
88                                 try {
89                                         fis = new FileStream(filename,FileMode.Open,FileAccess.Read);
90                                         byte[] buf = new byte[4 * 1024];  // 4K buffer
91                                         int bytesRead;
92                                         while ((bytesRead = fis.Read(buf,0,buf.Length)) != -1 &&
93                                                    bytesRead != 0) {
94                                                 hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
95                                         }
96                                 }
97                                 finally {
98                                         if (fis != null) fis.Close();
99                                 }
100                         }
101                         catch (System.IO.FileNotFoundException e) 
102                         {
103                                 resp.setStatus(404,"Object Not Found.");
104                                 HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
105                                 hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
106                                 hos.flush();
107                         }
108                         catch(Exception e) 
109                         {
110                                 Trace.WriteLine (String.Format ("ERROR in Static File Reading {0},{1}", e.GetType (), e.Message));
111                                 resp.setStatus(500);
112                                 HttpException myExp = new HttpException ("Exception in Reading static file", e);
113                                 hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
114                                 hos.flush();
115                         }
116                 }
117
118                 void sendFileUsingWriter(HttpServletResponse resp, string filename)
119                 {
120                         java.io.PrintWriter writer = resp.getWriter();
121                         try 
122                         {
123                                 resp.setContentType(this.getServletContext().getMimeType(filename));
124                                 StreamReader fis = null;
125                                 char[] buf = new char[4 * 1024];  // 4K buffer
126                                 try {
127                                         fis = new StreamReader(filename);
128                                         int charsRead;
129                                         while ((charsRead = fis.Read(buf,0,buf.Length)) != -1 &&
130                                                 charsRead != 0) {
131                                                 writer.write(buf, 0, charsRead);
132                                         }
133                                 }
134                                 finally {
135                                         if (fis != null) fis.Close();
136                                 }
137                         }
138                         catch (System.IO.FileNotFoundException e) 
139                         {
140                                 resp.setStatus(404,"Object Not Found.");
141                                 HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
142                                 writer.print(((HttpException) myExp).GetHtmlErrorMessage ());
143                                 writer.flush();
144                         }
145                         catch(Exception e) 
146                         {
147                                 Trace.WriteLine (String.Format ("ERROR in Static File Reading {0},{1}", e.GetType (), e.Message));
148                                 resp.setStatus(500);
149                                 HttpException myExp = new HttpException ("Exception in Reading static file", e);
150                                 writer.print(((HttpException) myExp).GetHtmlErrorMessage ());
151                                 writer.flush();
152                         }
153                 }
154
155                 override public void destroy()
156                 {
157                         base.destroy();
158                 }
159
160                 private string AppDir;
161         }
162 }
163
164 namespace System.Web.J2EE
165 {
166         /// <summary>
167         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
168         /// </summary>
169         public class BaseStaticHttpServlet : Mainsoft.Web.Hosting.BaseStaticHttpServlet
170         {
171         }
172
173 }
174
175
176 namespace System.Web.GH
177 {
178         /// <summary>
179         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
180         /// </summary>
181         public class BaseStaticHttpServlet : Mainsoft.Web.Hosting.BaseStaticHttpServlet
182         {
183         }
184
185 }
186