opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 {
42 public class OSHttpRequest : IOSHttpRequest
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  
130 /// <value>
131 /// POST request values, if applicable
132 /// </value>
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  
167 /// <summary>
168 /// Internal whiteboard for handlers to store temporary stuff
169 /// into.
170 /// </summary>
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 _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
186 if (null != req.Headers["content-type"])
187 _contentType = _request.Headers["content-type"];
188 if (null != req.Headers["user-agent"])
189 _userAgent = req.Headers["user-agent"];
190 if (null != req.Headers["remote_addr"])
191 {
192 try
193 {
194 IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
195 // sometimes req.Headers["remote_port"] returns a comma separated list, so use
196 // the first one in the list and log it
197 string[] strPorts = req.Headers["remote_port"].Split(new char[] { ',' });
198 if (strPorts.Length > 1)
199 {
200 _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
201 req.Headers["remote_addr"], req.Headers["remote_port"]);
202 }
203 int port = Int32.Parse(strPorts[0]);
204 _remoteIPEndPoint = new IPEndPoint(addr, port);
205 }
206 catch (FormatException)
207 {
208 _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
209 req.Headers["remote_addr"], req.Headers["remote_port"]);
210 }
211 }
212  
213 _queryString = new NameValueCollection();
214 _query = new Hashtable();
215 try
216 {
217 foreach (HttpInputItem item in req.QueryString)
218 {
219 try
220 {
221 _queryString.Add(item.Name, item.Value);
222 _query[item.Name] = item.Value;
223 }
224 catch (InvalidCastException)
225 {
226 _log.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
227 continue;
228 }
229 }
230 }
231 catch (Exception)
232 {
233 _log.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
234 }
235  
236 // Form = new Hashtable();
237 // foreach (HttpInputItem item in req.Form)
238 // {
239 // _log.DebugFormat("[OSHttpRequest]: Got form item {0}={1}", item.Name, item.Value);
240 // Form.Add(item.Name, item.Value);
241 // }
242 }
243  
244 public override string ToString()
245 {
246 StringBuilder me = new StringBuilder();
247 me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
248 foreach (string k in Headers.AllKeys)
249 {
250 me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
251 }
252 if (null != RemoteIPEndPoint)
253 {
254 me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
255 }
256  
257 return me.ToString();
258 }
259 }
260 }