Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Adapters / RequestAdapter.cs
blob27876558563abf70e54c71356124f9c015e64434
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 using Castle.MonoRail.Framework;
17 namespace Castle.MonoRail.Framework.Adapters
19 using System;
20 using System.Collections;
21 using System.Collections.Specialized;
22 using System.IO;
23 using System.Web;
24 using Castle.Components.Binder;
26 /// <summary>
27 /// This class adapts the <c>HttpRequest</c> to a MonoRail <c>IRequest</c>.
28 /// </summary>
29 public class RequestAdapter : IRequest
31 private TreeBuilder treeBuilder = new TreeBuilder();
32 private HttpRequest request;
33 private FileDictionaryAdapter files;
35 /// <summary>
36 /// Lazy initialized property with a hierarchical
37 /// representation of the flat data on <see cref="Controller.Params"/>
38 /// </summary>
39 protected CompositeNode paramsCompositeNode;
41 /// <summary>
42 /// Lazy initialized property with a hierarchical
43 /// representation of the flat data on <see cref="IRequest.Form"/>
44 /// </summary>
45 protected CompositeNode formCompositeNode;
47 /// <summary>
48 /// Lazy initialized property with a hierarchical
49 /// representation of the flat data on <see cref="IRequest.QueryString"/>
50 /// </summary>
51 protected CompositeNode queryStringCompositeNode;
53 /// <summary>
54 /// Initializes a new instance of the <see cref="RequestAdapter"/> class.
55 /// </summary>
56 /// <param name="request">The request.</param>
57 public RequestAdapter(HttpRequest request)
59 this.request = request;
62 /// <summary>
63 /// Gets the request URL.
64 /// </summary>
65 /// <value></value>
66 public string Url
68 get { return RawUrl; }
71 /// <summary>
72 /// Gets the accept header.
73 /// </summary>
74 /// <value>The accept header.</value>
75 public string AcceptHeader
77 get { return request.Headers["Accept"]; }
80 /// <summary>
81 /// Gets the referring URL.
82 /// </summary>
83 /// <value></value>
84 public String UrlReferrer
86 get
88 Uri referrer = request.UrlReferrer;
90 if (referrer != null)
92 return referrer.ToString();
94 else
96 return null;
101 /// <summary>
102 /// Gets a value indicating whether this requeest is from a local address.
103 /// </summary>
104 /// <value><c>true</c> if this instance is local; otherwise, <c>false</c>.</value>
105 public bool IsLocal
107 get { return request.Url.IsLoopback; }
110 /// <summary>
111 /// Gets the contents of the incoming HTTP entity body.
112 /// </summary>
113 /// <value></value>
114 public Stream InputStream
116 get { return request.InputStream; }
119 /// <summary>
120 /// Gets or sets the MIME content type of the incoming request.
121 /// </summary>
122 /// <value></value>
123 public string ContentType
125 get { return request.ContentType; }
126 set { request.ContentType = value; }
129 /// <summary>
130 /// Gets the request type (GET, POST, etc)
131 /// </summary>
132 /// <value></value>
133 public string RequestType
135 get { return HttpMethod; }
138 /// <summary>
139 /// Gets the HTTP method (GET, POST, etc).
140 /// </summary>
141 /// <value>The HTTP method.</value>
142 public string HttpMethod
144 get { return request.HttpMethod; }
147 /// <summary>
148 /// Gets the URI.
149 /// </summary>
150 /// <value>The URI.</value>
151 public Uri Uri
153 get { return request.Url; }
156 /// <summary>
157 /// Gets additional path information for
158 /// a resource with a URL extension.
159 /// </summary>
160 /// <value>The path info.</value>
161 public String PathInfo
163 get { return request.PathInfo; }
166 /// <summary>
167 /// Gets the raw URL.
168 /// </summary>
169 /// <value>The raw URL.</value>
170 public String RawUrl
172 get { return request.RawUrl; }
175 /// <summary>
176 /// Gets the file path.
177 /// </summary>
178 /// <value>The file path.</value>
179 public String FilePath
181 get { return request.FilePath; }
184 /// <summary>
185 /// Gets the Http headers.
186 /// </summary>
187 /// <value>The Http headers.</value>
188 public NameValueCollection Headers
190 get { return request.Headers; }
193 /// <summary>
194 /// Gets the params which accumulates headers, post, querystring and cookies.
195 /// </summary>
196 /// <value>The params.</value>
197 public NameValueCollection Params
199 get { return request.Params; }
202 /// <summary>
203 /// Gets the query string.
204 /// </summary>
205 /// <value>The query string.</value>
206 public NameValueCollection QueryString
208 get { return request.QueryString; }
211 /// <summary>
212 /// Gets the form.
213 /// </summary>
214 /// <value>The form.</value>
215 public NameValueCollection Form
217 get { return request.Form; }
220 /// <summary>
221 /// Indexer to access <see cref="Params"/> entries.
222 /// </summary>
223 /// <value></value>
224 public string this[string name]
226 get { return request[name]; }
229 // /// <summary>
230 // /// Reads the request data as a byte array.
231 // /// </summary>
232 // /// <param name="count">How many bytes.</param>
233 // /// <returns></returns>
234 // public byte[] BinaryRead(int count)
235 // {
236 // return request.BinaryRead(count);
237 // }
240 /// <summary>
241 /// Gets the <see cref="HttpPostedFile"/> per key.
242 /// </summary>
243 /// <value></value>
244 public IDictionary Files
248 if (files == null)
250 files = new FileDictionaryAdapter(request.Files);
252 return files;
256 /// <summary>
257 /// Gets the user languages.
258 /// </summary>
259 /// <value>The user languages.</value>
260 public String[] UserLanguages
262 get { return request.UserLanguages; }
265 /// <summary>
266 /// Gets the IP host address of the remote client.
267 /// </summary>
268 /// <value>The IP address of the remote client.</value>
269 public string UserHostAddress
271 get { return request.UserHostAddress; }
274 /// <summary>
275 /// Reads the cookie.
276 /// </summary>
277 /// <param name="name">The cookie name.</param>
278 /// <returns></returns>
279 public String ReadCookie(String name)
281 HttpCookie cookie = request.Cookies[name];
282 if (cookie == null)
284 return null;
286 return cookie.Value;
289 /// <summary>
290 /// Lazy initialized property with a hierarchical
291 /// representation of the flat data on <see cref="Controller.Params"/>
292 /// </summary>
293 public virtual CompositeNode ParamsNode
297 if (paramsCompositeNode == null)
299 paramsCompositeNode = treeBuilder.BuildSourceNode(Params);
300 treeBuilder.PopulateTree(paramsCompositeNode, request.Files);
303 return paramsCompositeNode;
307 /// <summary>
308 /// Lazy initialized property with a hierarchical
309 /// representation of the flat data on <see cref="IRequest.Form"/>
310 /// </summary>
311 public virtual CompositeNode FormNode
315 if (formCompositeNode == null)
317 formCompositeNode = treeBuilder.BuildSourceNode(Form);
318 treeBuilder.PopulateTree(formCompositeNode, request.Files);
321 return formCompositeNode;
325 /// <summary>
326 /// Lazy initialized property with a hierarchical
327 /// representation of the flat data on <see cref="IRequest.QueryString"/>
328 /// </summary>
329 public virtual CompositeNode QueryStringNode
333 if (queryStringCompositeNode == null)
335 queryStringCompositeNode = treeBuilder.BuildSourceNode(QueryString);
336 treeBuilder.PopulateTree(queryStringCompositeNode, request.Files);
339 return queryStringCompositeNode;
343 /// <summary>
344 /// This method is for internal use only
345 /// </summary>
346 /// <param name="from"></param>
347 /// <returns></returns>
348 public CompositeNode ObtainParamsNode(ParamStore from)
350 switch (from)
352 case ParamStore.Form:
353 return FormNode;
354 case ParamStore.QueryString:
355 return QueryStringNode;
356 default:
357 return ParamsNode;
361 /// <summary>
362 /// Validates the input.
363 /// </summary>
364 public void ValidateInput()
366 request.ValidateInput();