Normalize line endings.
[mono.git] / mcs / class / System.Web / System.Web / HttpRequest.jvm.cs
1 //
2 // System.Web.HttpRequest.jvm.cs 
3 //
4 // 
5 // Author:
6 //      Eyal Alaluf <eyala@mainsoft.com>
7 //
8
9 //
10 // Copyright (C) 2006 Mainsoft, Co. (http://www.mainsoft.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 using System.Web.Hosting;
32 using javax.servlet.http;
33 using System.Web.Configuration;
34 using System.IO;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using Mainsoft.Web;
38
39 namespace System.Web
40 {
41         public sealed partial class HttpRequest
42         {
43                 const string SessionLock = "vmw.session.lock";
44                 const string SessionCookies = "vmw.session.cookies";
45
46                 static object GetJavaSessionLock (HttpSession javaSession)
47                 {
48                         lock (SessionLock) {
49                                 object sessionLock = javaSession.getAttribute (SessionLock);
50                                 if (sessionLock == null) {                              
51                                         sessionLock = String.Copy (SessionLock);
52                                         javaSession.setAttribute (SessionLock, sessionLock);
53                                 }
54                                 return sessionLock;
55                         }
56                 }
57
58                 void LoadWwwForm ()
59                 {
60                         HttpServletRequest servletReq = context.ServletRequest;
61                         if (servletReq == null) {
62                                 NameValueCollection requestParameters = context.RequestParameters;
63                                 if (requestParameters != null)
64                                         form.Add (requestParameters);
65                                 else
66                                         RawLoadWwwForm ();
67                                 return;
68                         }
69
70                         servletReq.setCharacterEncoding (ContentEncoding.WebName);
71
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);
76
77                                 for (int i = (qvalue != null) ? qvalue.Length : 0; i < qfvalue.Length; i++)
78                                         form.Add (key, qfvalue [i]);
79                         }
80                 }
81
82                 const int INPUT_BUFFER_SIZE = 1024;
83
84                 void MakeInputStream ()
85                 {
86                         if (worker_request == null)
87                                 throw new HttpException ("No HttpWorkerRequest");
88
89                         // consider for perf:
90                         //    return ((ServletWorkerRequest)worker_request).InputStream();
91
92                         //
93                         // Use an unmanaged memory block as this might be a large
94                         // upload
95                         //
96                         int content_length = ContentLength;
97 #if NET_2_0
98                         HttpRuntimeSection config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");
99 #else
100                         HttpRuntimeConfig config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");
101 #endif
102                         if (content_length > (config.MaxRequestLength * 1024))
103                                 throw new HttpException ("File exceeds httpRuntime limit");
104                         
105                         byte[] content = new byte[content_length];
106                         if (content == null)
107                                 throw new HttpException (String.Format ("Not enough memory to allocate {0} bytes", content_length));
108
109                         int total;
110                         byte [] buffer;
111                         buffer = worker_request.GetPreloadedEntityBody ();
112                         if (buffer != null){
113                                 total = buffer.Length;
114                                 if (content_length > 0)
115                                         total = Math.Min (content_length, total);
116                                 Array.Copy (buffer, content, total);
117                         }
118                         else
119                                 total = 0;
120
121                         buffer = new byte [INPUT_BUFFER_SIZE];
122                         while (total < content_length) {
123                                 int n;
124                                 n = worker_request.ReadEntityBody (buffer, Math.Min (content_length-total, INPUT_BUFFER_SIZE));
125                                 if (n <= 0)
126                                         break;
127                                 Array.Copy (buffer, 0, content, total, n);
128                                 total += n;
129                         } 
130                         if (total < content_length)
131                                 throw new HttpException (411, "The uploaded file is incomplete");
132                                                          
133                         input_stream = new MemoryStream (content, 0, content.Length, false, true);
134
135                         DoFilter (buffer);
136                 }
137
138                 internal void GetSessionCookiesForPortal (HttpCookieCollection cookies)
139                 {
140                         if (context == null)
141                                 return;
142                         if (!(context.WorkerRequest is IHttpExtendedWorkerRequest))
143                                 return;
144                         IHttpExtendedWorkerRequest exWorker = (IHttpExtendedWorkerRequest) context.WorkerRequest;
145                         HttpSession javaSession = exWorker.GetSession (false);
146                         if (javaSession == null)
147                                 return;
148
149                         object sessionLock = GetJavaSessionLock (javaSession);
150                         lock (sessionLock) {
151                                 Hashtable sessionCookies = (Hashtable) javaSession.getAttribute (SessionCookies);
152                                 if (sessionCookies == null)
153                                         return;
154
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);
162                                                 else {
163                                                         if (expiredCookies == null)
164                                                                 expiredCookies = new ArrayList();
165                                                         expiredCookies.Add (key);
166                                                 }
167                                         }
168                                         else
169                                                 cookies.Set (sessionCookie);
170                                 }
171
172                                 if (expiredCookies != null)
173                                         foreach (object key in expiredCookies)
174                                                 sessionCookies.Remove (key);
175                         }
176                 }
177
178                 internal void SetSessionCookiesForPortal (HttpCookieCollection cookies)
179                 {
180                         if (cookies == null || cookies.Count == 0)
181                                 return;
182
183                         if (!(context.WorkerRequest is IHttpExtendedWorkerRequest))
184                                 return;
185                         IHttpExtendedWorkerRequest exWorker = (IHttpExtendedWorkerRequest) context.WorkerRequest;
186                         bool inPortletMode = !context.IsServletRequest;
187                         bool shouldStoreCookiesCollection = false;
188                         HttpSession javaSession = exWorker.GetSession (false);
189
190                         if (javaSession == null && inPortletMode)
191                                 javaSession = exWorker.GetSession (true);
192
193                         if (javaSession == null)
194                                 return;
195
196                         object sessionLock = GetJavaSessionLock (javaSession);
197                         lock (sessionLock) {
198                                 Hashtable sessionCookies = (Hashtable)javaSession.getAttribute (SessionCookies);                        
199                                 if (sessionCookies == null)
200                                         if (inPortletMode) {
201                                                 sessionCookies = new Hashtable ();
202                                                 shouldStoreCookiesCollection = true;
203                                         }
204                                         else
205                                                 return;
206
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);
215                                         }
216                                 }
217
218                                 if (sessionStoredCookies != null)
219                                         foreach (object key in sessionStoredCookies)
220                                                 cookies.Remove ((string) key);
221
222                                 if (shouldStoreCookiesCollection)
223                                         javaSession.setAttribute (SessionCookies, sessionCookies);
224                         }
225                 }
226
227                 internal void SetWorkerRequest (HttpWorkerRequest wr) {
228                         worker_request = wr;
229                         current_exe_path = null;
230                         file_path = null;
231                         base_virtual_dir = null;
232                         form = null;
233                         all_params = null;
234                 }
235
236         }
237 }