2008-01-23 Olivier Dufour <olivier.duff@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpRequest.jvm.cs
1 //\r
2 // System.Web.HttpRequest.jvm.cs \r
3 //\r
4 // \r
5 // Author:\r
6 //      Eyal Alaluf <eyala@mainsoft.com>\r
7 //\r
8 \r
9 //\r
10 // Copyright (C) 2006 Mainsoft, Co. (http://www.mainsoft.com)\r
11 //\r
12 // Permission is hereby granted, free of charge, to any person obtaining\r
13 // a copy of this software and associated documentation files (the\r
14 // "Software"), to deal in the Software without restriction, including\r
15 // without limitation the rights to use, copy, modify, merge, publish,\r
16 // distribute, sublicense, and/or sell copies of the Software, and to\r
17 // permit persons to whom the Software is furnished to do so, subject to\r
18 // the following conditions:\r
19 // \r
20 // The above copyright notice and this permission notice shall be\r
21 // included in all copies or substantial portions of the Software.\r
22 // \r
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
30 //\r
31 using System.Web.Hosting;\r
32 using javax.servlet.http;\r
33 using System.Web.Configuration;\r
34 using System.IO;\r
35 using System.Collections;\r
36 using System.Collections.Specialized;\r
37 \r
38 namespace System.Web\r
39 {\r
40         public sealed partial class HttpRequest\r
41         {\r
42                 private const string SessionLock = "vmw.session.lock";\r
43                 private const string SessionCookies = "vmw.session.cookies";\r
44 \r
45                 private static object GetJavaSessionLock (HttpSession javaSession)\r
46                 {\r
47                         lock (SessionLock) {\r
48                                 object sessionLock = javaSession.getAttribute (SessionLock);\r
49                                 if (sessionLock == null) {                              \r
50                                         sessionLock = String.Copy (SessionLock);\r
51                                         javaSession.setAttribute (SessionLock, sessionLock);\r
52                                 }\r
53                                 return sessionLock;\r
54                         }\r
55                 }\r
56 \r
57                 void LoadWwwForm ()\r
58                 {\r
59                         HttpServletRequest servletReq = context.ServletRequest;\r
60                         if (servletReq == null) {\r
61                                 NameValueCollection requestParameters = context.RequestParameters;\r
62                                 if (requestParameters != null)\r
63                                         form.Add (requestParameters);\r
64                                 else\r
65                                         RawLoadWwwForm ();\r
66                                 return;\r
67                         }\r
68 \r
69                         servletReq.setCharacterEncoding (ContentEncoding.WebName);\r
70 \r
71                         for (java.util.Enumeration e = servletReq.getParameterNames(); e.hasMoreElements() ;) {\r
72                                 string key = (string) e.nextElement();\r
73                                 string [] qvalue = QueryString.GetValues (key);\r
74                                 string [] qfvalue = servletReq.getParameterValues (key);\r
75 \r
76                                 for (int i = (qvalue != null) ? qvalue.Length : 0; i < qfvalue.Length; i++)\r
77                                         form.Add (key, qfvalue [i]);\r
78                         }\r
79                 }\r
80 \r
81                 const int INPUT_BUFFER_SIZE = 1024;\r
82 \r
83                 void MakeInputStream ()\r
84                 {\r
85                         if (worker_request == null)\r
86                                 throw new HttpException ("No HttpWorkerRequest");\r
87 \r
88                         // consider for perf:\r
89                         //    return ((ServletWorkerRequest)worker_request).InputStream();\r
90 \r
91                         //\r
92                         // Use an unmanaged memory block as this might be a large\r
93                         // upload\r
94                         //\r
95                         int content_length = ContentLength;\r
96 #if NET_2_0\r
97                         HttpRuntimeSection config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");\r
98 #else\r
99                         HttpRuntimeConfig config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");\r
100 #endif\r
101                         if (content_length > (config.MaxRequestLength * 1024))\r
102                                 throw new HttpException ("File exceeds httpRuntime limit");\r
103                         \r
104                         byte[] content = new byte[content_length];\r
105                         if (content == null)\r
106                                 throw new HttpException (String.Format ("Not enough memory to allocate {0} bytes", content_length));\r
107 \r
108                         int total;\r
109                         byte [] buffer;\r
110                         buffer = worker_request.GetPreloadedEntityBody ();\r
111                         if (buffer != null){\r
112                                 total = buffer.Length;\r
113                                 if (content_length > 0)\r
114                                         total = Math.Min (content_length, total);\r
115                                 Array.Copy (buffer, content, total);\r
116                         }\r
117                         else\r
118                                 total = 0;\r
119 \r
120                         buffer = new byte [INPUT_BUFFER_SIZE];\r
121                         while (total < content_length) {\r
122                                 int n;\r
123                                 n = worker_request.ReadEntityBody (buffer, Math.Min (content_length-total, INPUT_BUFFER_SIZE));\r
124                                 if (n <= 0)\r
125                                         break;\r
126                                 Array.Copy (buffer, 0, content, total, n);\r
127                                 total += n;\r
128                         } \r
129                         if (total < content_length)\r
130                                 throw new HttpException (411, "The uploaded file is incomplete");\r
131                                                          \r
132                         input_stream = new MemoryStream (content, 0, content.Length, false, true);\r
133 \r
134                         DoFilter (buffer);\r
135                 }\r
136 \r
137                 internal void GetSessionCookiesForPortal (HttpCookieCollection cookies)\r
138                 {\r
139                         if (context == null)\r
140                                 return;\r
141                         HttpServletRequest servletReq = context.ServletRequest;\r
142                         if (servletReq == null)\r
143                                 return;\r
144                         HttpSession javaSession = servletReq.getSession(false);\r
145                         if (javaSession == null)\r
146                                 return;\r
147 \r
148                         object sessionLock = GetJavaSessionLock (javaSession);\r
149                         lock (sessionLock) {\r
150                                 Hashtable sessionCookies = (Hashtable) javaSession.getAttribute (SessionCookies);\r
151                                 if (sessionCookies == null)\r
152                                         return;\r
153 \r
154                                 ArrayList expiredCookies = null;\r
155                                 foreach (string key in sessionCookies.Keys) {\r
156                                         HttpCookie sessionCookie = (HttpCookie) sessionCookies [key];\r
157                                         if (sessionCookie.Expires.Ticks != 0 &&\r
158                                                 sessionCookie.Expires.Ticks < DateTime.Now.Ticks) {\r
159                                                 if (cookies [key] != null)\r
160                                                         cookies.Remove (key);\r
161                                                 else {\r
162                                                         if (expiredCookies == null)\r
163                                                                 expiredCookies = new ArrayList();\r
164                                                         expiredCookies.Add (key);\r
165                                                 }\r
166                                         }\r
167                                         else\r
168                                                 cookies.Set (sessionCookie);\r
169                                 }\r
170 \r
171                                 if (expiredCookies != null)\r
172                                         foreach (object key in expiredCookies)\r
173                                                 sessionCookies.Remove (key);\r
174                         }\r
175                 }\r
176 \r
177                 internal void SetSessionCookiesForPortal (HttpCookieCollection cookies)\r
178                 {\r
179                         if (cookies == null || cookies.Count == 0)\r
180                                 return;\r
181 \r
182                         HttpServletRequest servletReq = context.ServletRequest;\r
183                         if (servletReq == null)\r
184                                 return;\r
185                         bool inPortletMode = true; //servletReq is IPortletRequest;\r
186                         bool shouldStoreCookiesCollection = false;\r
187                         HttpSession javaSession = servletReq.getSession(false);\r
188 \r
189                         if (javaSession == null && inPortletMode)\r
190                                 javaSession = servletReq.getSession(true);\r
191 \r
192                         if (javaSession == null)\r
193                                 return;\r
194 \r
195                         object sessionLock = GetJavaSessionLock (javaSession);\r
196                         lock (sessionLock) {\r
197                                 Hashtable sessionCookies = (Hashtable)javaSession.getAttribute (SessionCookies);                        \r
198                                 if (sessionCookies == null)\r
199                                         if (inPortletMode) {\r
200                                                 sessionCookies = new Hashtable ();\r
201                                                 shouldStoreCookiesCollection = true;\r
202                                         }\r
203                                         else\r
204                                                 return;\r
205 \r
206                                 ArrayList sessionStoredCookies = null;\r
207                                 for (int i=0; i < cookies.Count; i++) {\r
208                                         HttpCookie cookie = cookies[i];\r
209                                         if (sessionCookies [cookie.Name] != null || inPortletMode) {\r
210                                                 sessionCookies [cookie.Name] = cookie;\r
211                                                 if (sessionStoredCookies == null)\r
212                                                         sessionStoredCookies = new ArrayList();\r
213                                                 sessionStoredCookies. Add (cookie.Name);\r
214                                         }\r
215                                 }\r
216 \r
217                                 if (sessionStoredCookies != null)\r
218                                         foreach (object key in sessionStoredCookies)\r
219                                                 cookies.Remove ((string) key);\r
220 \r
221                                 if (shouldStoreCookiesCollection)\r
222                                         javaSession.setAttribute (SessionCookies, sessionCookies);\r
223                         }\r
224                 }\r
225 \r
226                 internal void SetWorkerRequest (HttpWorkerRequest wr) {\r
227                         worker_request = wr;\r
228                         current_exe_path = null;\r
229                         file_path = null;\r
230                         base_virtual_dir = null;\r
231                         form = null;\r
232                 }\r
233 \r
234         }\r
235 }\r