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