OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
OSHttpRequest.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Specialized;
32 using System.IO;
33 using System.Net;
34 using System.Reflection;
35 using System.Text;
36 using System.Web;
37 using HttpServer;
38 using log4net;
39 
40 namespace OpenSim.Framework.Servers.HttpServer
41 {
43  {
44  private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45 
46  protected IHttpRequest _request = null;
47  protected IHttpClientContext _context = null;
48 
49  public string[] AcceptTypes
50  {
51  get { return _request.AcceptTypes; }
52  }
53 
54  public Encoding ContentEncoding
55  {
56  get { return _contentEncoding; }
57  }
58  private Encoding _contentEncoding;
59 
60  public long ContentLength
61  {
62  get { return _request.ContentLength; }
63  }
64 
65  public long ContentLength64
66  {
67  get { return ContentLength; }
68  }
69 
70  public string ContentType
71  {
72  get { return _contentType; }
73  }
74  private string _contentType;
75 
76  public HttpCookieCollection Cookies
77  {
78  get
79  {
80  RequestCookies cookies = _request.Cookies;
81  HttpCookieCollection httpCookies = new HttpCookieCollection();
82  foreach (RequestCookie cookie in cookies)
83  httpCookies.Add(new HttpCookie(cookie.Name, cookie.Value));
84  return httpCookies;
85  }
86  }
87 
88  public bool HasEntityBody
89  {
90  get { return _request.ContentLength != 0; }
91  }
92 
93  public NameValueCollection Headers
94  {
95  get { return _request.Headers; }
96  }
97 
98  public string HttpMethod
99  {
100  get { return _request.Method; }
101  }
102 
103  public Stream InputStream
104  {
105  get { return _request.Body; }
106  }
107 
108  public bool IsSecured
109  {
110  get { return _context.IsSecured; }
111  }
112 
113  public bool KeepAlive
114  {
115  get { return ConnectionType.KeepAlive == _request.Connection; }
116  }
117 
118  public NameValueCollection QueryString
119  {
120  get { return _queryString; }
121  }
122  private NameValueCollection _queryString;
123 
124  public Hashtable Query
125  {
126  get { return _query; }
127  }
128  private Hashtable _query;
129 
133 // public Hashtable Form { get; private set; }
134 
135  public string RawUrl
136  {
137  get { return _request.Uri.AbsolutePath; }
138  }
139 
140  public IPEndPoint RemoteIPEndPoint
141  {
142  get { return _remoteIPEndPoint; }
143  }
144  private IPEndPoint _remoteIPEndPoint;
145 
146  public Uri Url
147  {
148  get { return _request.Uri; }
149  }
150 
151  public string UserAgent
152  {
153  get { return _userAgent; }
154  }
155  private string _userAgent;
156 
157  internal IHttpRequest IHttpRequest
158  {
159  get { return _request; }
160  }
161 
162  internal IHttpClientContext IHttpClientContext
163  {
164  get { return _context; }
165  }
166 
171  internal Dictionary<string, object> Whiteboard
172  {
173  get { return _whiteboard; }
174  }
175  private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
176 
177  public OSHttpRequest() {}
178 
179  public OSHttpRequest(IHttpClientContext context, IHttpRequest req)
180  {
181  _request = req;
182  _context = context;
183 
184  if (null != req.Headers["content-encoding"])
185  {
186  try
187  {
188  _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
189  }
190  catch (Exception)
191  {
192  // ignore
193  }
194  }
195 
196  if (null != req.Headers["content-type"])
197  _contentType = _request.Headers["content-type"];
198  if (null != req.Headers["user-agent"])
199  _userAgent = req.Headers["user-agent"];
200 
201  if (null != req.Headers["remote_addr"])
202  {
203  try
204  {
205  IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
206  // sometimes req.Headers["remote_port"] returns a comma separated list, so use
207  // the first one in the list and log it
208  string[] strPorts = req.Headers["remote_port"].Split(new char[] { ',' });
209  if (strPorts.Length > 1)
210  {
211  _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
212  req.Headers["remote_addr"], req.Headers["remote_port"]);
213  }
214  int port = Int32.Parse(strPorts[0]);
215  _remoteIPEndPoint = new IPEndPoint(addr, port);
216  }
217  catch (FormatException)
218  {
219  _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
220  req.Headers["remote_addr"], req.Headers["remote_port"]);
221  }
222  }
223 
224  _queryString = new NameValueCollection();
225  _query = new Hashtable();
226  try
227  {
228  foreach (HttpInputItem item in req.QueryString)
229  {
230  try
231  {
232  _queryString.Add(item.Name, item.Value);
233  _query[item.Name] = item.Value;
234  }
235  catch (InvalidCastException)
236  {
237  _log.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
238  continue;
239  }
240  }
241  }
242  catch (Exception)
243  {
244  _log.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
245  }
246 
247 // Form = new Hashtable();
248 // foreach (HttpInputItem item in req.Form)
249 // {
250 // _log.DebugFormat("[OSHttpRequest]: Got form item {0}={1}", item.Name, item.Value);
251 // Form.Add(item.Name, item.Value);
252 // }
253  }
254 
255  public override string ToString()
256  {
257  StringBuilder me = new StringBuilder();
258  me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
259  foreach (string k in Headers.AllKeys)
260  {
261  me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
262  }
263  if (null != RemoteIPEndPoint)
264  {
265  me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
266  }
267 
268  return me.ToString();
269  }
270  }
271 }
OSHttpRequest(IHttpClientContext context, IHttpRequest req)