2 // System.Web.HttpRequest.jvm.cs
6 // Eyal Alaluf <eyala@mainsoft.com>
10 // Copyright (C) 2006 Mainsoft, Co. (http://www.mainsoft.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Web.Hosting;
32 using javax.servlet.http;
33 using System.Web.Configuration;
35 using System.Collections;
36 using System.Collections.Specialized;
41 public sealed partial class HttpRequest
43 const string SessionLock = "vmw.session.lock";
44 const string SessionCookies = "vmw.session.cookies";
46 static object GetJavaSessionLock (HttpSession javaSession)
49 object sessionLock = javaSession.getAttribute (SessionLock);
50 if (sessionLock == null) {
51 sessionLock = String.Copy (SessionLock);
52 javaSession.setAttribute (SessionLock, sessionLock);
60 HttpServletRequest servletReq = context.ServletRequest;
61 if (servletReq == null) {
62 NameValueCollection requestParameters = context.RequestParameters;
63 if (requestParameters != null)
64 form.Add (requestParameters);
70 servletReq.setCharacterEncoding (ContentEncoding.WebName);
72 for (java.util.Enumeration e = servletReq.getParameterNames(); e.hasMoreElements() ;) {
73 string key = (string) e.nextElement();
74 string [] qvalue = QueryString.GetValues (key);
75 string [] qfvalue = servletReq.getParameterValues (key);
77 for (int i = (qvalue != null) ? qvalue.Length : 0; i < qfvalue.Length; i++)
78 form.Add (key, qfvalue [i]);
82 const int INPUT_BUFFER_SIZE = 1024;
84 void MakeInputStream ()
86 if (worker_request == null)
87 throw new HttpException ("No HttpWorkerRequest");
90 // return ((ServletWorkerRequest)worker_request).InputStream();
93 // Use an unmanaged memory block as this might be a large
96 int content_length = ContentLength;
98 HttpRuntimeSection config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");
100 HttpRuntimeConfig config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");
102 if (content_length > (config.MaxRequestLength * 1024))
103 throw new HttpException ("File exceeds httpRuntime limit");
105 byte[] content = new byte[content_length];
107 throw new HttpException (String.Format ("Not enough memory to allocate {0} bytes", content_length));
111 buffer = worker_request.GetPreloadedEntityBody ();
113 total = buffer.Length;
114 if (content_length > 0)
115 total = Math.Min (content_length, total);
116 Array.Copy (buffer, content, total);
121 buffer = new byte [INPUT_BUFFER_SIZE];
122 while (total < content_length) {
124 n = worker_request.ReadEntityBody (buffer, Math.Min (content_length-total, INPUT_BUFFER_SIZE));
127 Array.Copy (buffer, 0, content, total, n);
130 if (total < content_length)
131 throw new HttpException (411, "The uploaded file is incomplete");
133 input_stream = new MemoryStream (content, 0, content.Length, false, true);
138 internal void GetSessionCookiesForPortal (HttpCookieCollection cookies)
142 if (!(context.WorkerRequest is IHttpExtendedWorkerRequest))
144 IHttpExtendedWorkerRequest exWorker = (IHttpExtendedWorkerRequest) context.WorkerRequest;
145 HttpSession javaSession = exWorker.GetSession (false);
146 if (javaSession == null)
149 object sessionLock = GetJavaSessionLock (javaSession);
151 Hashtable sessionCookies = (Hashtable) javaSession.getAttribute (SessionCookies);
152 if (sessionCookies == null)
155 ArrayList expiredCookies = null;
156 foreach (string key in sessionCookies.Keys) {
157 HttpCookie sessionCookie = (HttpCookie) sessionCookies [key];
158 if (sessionCookie.Expires.Ticks != 0 &&
159 sessionCookie.Expires.Ticks < DateTime.Now.Ticks) {
160 if (cookies [key] != null)
161 cookies.Remove (key);
163 if (expiredCookies == null)
164 expiredCookies = new ArrayList();
165 expiredCookies.Add (key);
169 cookies.Set (sessionCookie);
172 if (expiredCookies != null)
173 foreach (object key in expiredCookies)
174 sessionCookies.Remove (key);
178 internal void SetSessionCookiesForPortal (HttpCookieCollection cookies)
180 if (cookies == null || cookies.Count == 0)
183 if (!(context.WorkerRequest is IHttpExtendedWorkerRequest))
185 IHttpExtendedWorkerRequest exWorker = (IHttpExtendedWorkerRequest) context.WorkerRequest;
186 bool inPortletMode = !context.IsServletRequest;
187 bool shouldStoreCookiesCollection = false;
188 HttpSession javaSession = exWorker.GetSession (false);
190 if (javaSession == null && inPortletMode)
191 javaSession = exWorker.GetSession (true);
193 if (javaSession == null)
196 object sessionLock = GetJavaSessionLock (javaSession);
198 Hashtable sessionCookies = (Hashtable)javaSession.getAttribute (SessionCookies);
199 if (sessionCookies == null)
201 sessionCookies = new Hashtable ();
202 shouldStoreCookiesCollection = true;
207 ArrayList sessionStoredCookies = null;
208 for (int i=0; i < cookies.Count; i++) {
209 HttpCookie cookie = cookies[i];
210 if (sessionCookies [cookie.Name] != null || inPortletMode) {
211 sessionCookies [cookie.Name] = cookie;
212 if (sessionStoredCookies == null)
213 sessionStoredCookies = new ArrayList();
214 sessionStoredCookies. Add (cookie.Name);
218 if (sessionStoredCookies != null)
219 foreach (object key in sessionStoredCookies)
220 cookies.Remove ((string) key);
222 if (shouldStoreCookiesCollection)
223 javaSession.setAttribute (SessionCookies, sessionCookies);
227 internal void SetWorkerRequest (HttpWorkerRequest wr) {
229 current_exe_path = null;
231 base_virtual_dir = null;