correctly check for request locality
[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 vmw.@internal.j2ee;\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                                 RawLoadWwwForm ();\r
62                                 return;\r
63                         }\r
64 \r
65                         servletReq.setCharacterEncoding (ContentEncoding.WebName);\r
66 \r
67                         for (java.util.Enumeration e = servletReq.getParameterNames(); e.hasMoreElements() ;) {\r
68                                 string key = (string) e.nextElement();\r
69                                 string [] qvalue = QueryString.GetValues (key);\r
70                                 string [] qfvalue = servletReq.getParameterValues (key);\r
71 \r
72                                 for (int i = (qvalue != null) ? qvalue.Length : 0; i < qfvalue.Length; i++)\r
73                                         form.Add (key, qfvalue [i]);\r
74                         }\r
75                 }\r
76 \r
77                 const int INPUT_BUFFER_SIZE = 1024;\r
78 \r
79                 void MakeInputStream ()\r
80                 {\r
81                         if (worker_request == null)\r
82                                 throw new HttpException ("No HttpWorkerRequest");\r
83 \r
84                         // consider for perf:\r
85                         //    return ((ServletWorkerRequest)worker_request).InputStream();\r
86 \r
87                         //\r
88                         // Use an unmanaged memory block as this might be a large\r
89                         // upload\r
90                         //\r
91                         int content_length = ContentLength;\r
92 #if NET_2_0\r
93                         HttpRuntimeSection config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");\r
94 #else\r
95                         HttpRuntimeConfig config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");\r
96 #endif\r
97                         if (content_length > (config.MaxRequestLength * 1024))\r
98                                 throw new HttpException ("File exceeds httpRuntime limit");\r
99                         \r
100                         byte[] content = new byte[content_length];\r
101                         if (content == null)\r
102                                 throw new HttpException (String.Format ("Not enough memory to allocate {0} bytes", content_length));\r
103 \r
104                         int total;\r
105                         byte [] buffer;\r
106                         buffer = worker_request.GetPreloadedEntityBody ();\r
107                         if (buffer != null){\r
108                                 total = buffer.Length;\r
109                                 if (content_length > 0)\r
110                                         total = Math.Min (content_length, total);\r
111                                 Array.Copy (buffer, content, total);\r
112                         }\r
113                         else\r
114                                 total = 0;\r
115 \r
116                         buffer = new byte [INPUT_BUFFER_SIZE];\r
117                         while (total < content_length) {\r
118                                 int n;\r
119                                 n = worker_request.ReadEntityBody (buffer, Math.Min (content_length-total, INPUT_BUFFER_SIZE));\r
120                                 if (n <= 0)\r
121                                         break;\r
122                                 Array.Copy (buffer, 0, content, total, n);\r
123                                 total += n;\r
124                         } \r
125                         if (total < content_length)\r
126                                 throw new HttpException (411, "The uploaded file is incomplete");\r
127                                                          \r
128                         input_stream = new MemoryStream (content, 0, content.Length, false, true);\r
129 \r
130                         DoFilter (buffer);\r
131                 }\r
132 \r
133                 internal void GetSessionCookiesForPortal (HttpCookieCollection cookies)\r
134                 {\r
135                         if (context == null)\r
136                                 return;\r
137                         HttpServletRequest servletReq = context.ServletRequest;\r
138                         if (servletReq == null)\r
139                                 return;\r
140                         HttpSession javaSession = servletReq.getSession(false);\r
141                         if (javaSession == null)\r
142                                 return;\r
143 \r
144                         object sessionLock = GetJavaSessionLock (javaSession);\r
145                         lock (sessionLock) {\r
146                                 Hashtable sessionCookies = (Hashtable) javaSession.getAttribute (SessionCookies);\r
147                                 if (sessionCookies == null)\r
148                                         return;\r
149 \r
150                                 ArrayList expiredCookies = null;\r
151                                 foreach (string key in sessionCookies.Keys) {\r
152                                         HttpCookie sessionCookie = (HttpCookie) sessionCookies [key];\r
153                                         if (sessionCookie.Expires.Ticks != 0 &&\r
154                                                 sessionCookie.Expires.Ticks < DateTime.Now.Ticks) {\r
155                                                 if (cookies [key] != null)\r
156                                                         cookies.Remove (key);\r
157                                                 else {\r
158                                                         if (expiredCookies == null)\r
159                                                                 expiredCookies = new ArrayList();\r
160                                                         expiredCookies.Add (key);\r
161                                                 }\r
162                                         }\r
163                                         else\r
164                                                 cookies.Set (sessionCookie);\r
165                                 }\r
166 \r
167                                 if (expiredCookies != null)\r
168                                         foreach (object key in expiredCookies)\r
169                                                 sessionCookies.Remove (key);\r
170                         }\r
171                 }\r
172 \r
173                 internal void SetSessionCookiesForPortal (HttpCookieCollection cookies)\r
174                 {\r
175                         if (cookies == null || cookies.Count == 0)\r
176                                 return;\r
177 \r
178                         HttpServletRequest servletReq = context.ServletRequest;\r
179                         if (servletReq == null)\r
180                                 return;\r
181                         bool inPortletMode = servletReq is IPortletRequest;\r
182                         bool shouldStoreCookiesCollection = false;\r
183                         HttpSession javaSession = servletReq.getSession(false);\r
184 \r
185                         if (javaSession == null && inPortletMode)\r
186                                 javaSession = servletReq.getSession(true);\r
187 \r
188                         if (javaSession == null)\r
189                                 return;\r
190 \r
191                         object sessionLock = GetJavaSessionLock (javaSession);\r
192                         lock (sessionLock) {\r
193                                 Hashtable sessionCookies = (Hashtable)javaSession.getAttribute (SessionCookies);                        \r
194                                 if (sessionCookies == null)\r
195                                         if (inPortletMode) {\r
196                                                 sessionCookies = new Hashtable ();\r
197                                                 shouldStoreCookiesCollection = true;\r
198                                         }\r
199                                         else\r
200                                                 return;\r
201 \r
202                                 ArrayList sessionStoredCookies = null;\r
203                                 for (int i=0; i < cookies.Count; i++) {\r
204                                         HttpCookie cookie = cookies[i];\r
205                                         if (sessionCookies [cookie.Name] != null || inPortletMode) {\r
206                                                 sessionCookies [cookie.Name] = cookie;\r
207                                                 if (sessionStoredCookies == null)\r
208                                                         sessionStoredCookies = new ArrayList();\r
209                                                 sessionStoredCookies. Add (cookie.Name);\r
210                                         }\r
211                                 }\r
212 \r
213                                 if (sessionStoredCookies != null)\r
214                                         foreach (object key in sessionStoredCookies)\r
215                                                 cookies.Remove ((string) key);\r
216 \r
217                                 if (shouldStoreCookiesCollection)\r
218                                         javaSession.setAttribute (SessionCookies, sessionCookies);\r
219                         }\r
220                 }\r
221         }\r
222 }\r