opensim-development – 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.Specialized;
31 using System.IO;
32 using System.IO.Compression;
33 using System.Reflection;
34 using System.Net;
35 using System.Text;
36 using System.Web;
37  
38 using OpenSim.Server.Base;
39 using OpenSim.Server.Handlers.Base;
40 using OpenSim.Services.Interfaces;
41 using GridRegion = OpenSim.Services.Interfaces.GridRegion;
42 using OpenSim.Framework;
43 using OpenSim.Framework.Servers.HttpServer;
44  
45 using OpenMetaverse;
46 using OpenMetaverse.StructuredData;
47 using Nini.Config;
48 using log4net;
49  
50  
51 namespace OpenSim.Server.Handlers.Simulation
52 {
53 public class AgentHandler
54 {
55 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
56  
57 private ISimulationService m_SimulationService;
58  
59 public AgentHandler() { }
60  
61 public AgentHandler(ISimulationService sim)
62 {
63 m_SimulationService = sim;
64 }
65  
66 public Hashtable Handler(Hashtable request)
67 {
68 // m_log.Debug("[CONNECTION DEBUGGING]: AgentHandler Called");
69 //
70 // m_log.Debug("---------------------------");
71 // m_log.Debug(" >> uri=" + request["uri"]);
72 // m_log.Debug(" >> content-type=" + request["content-type"]);
73 // m_log.Debug(" >> http-method=" + request["http-method"]);
74 // m_log.Debug("---------------------------\n");
75  
76 Hashtable responsedata = new Hashtable();
77 responsedata["content_type"] = "text/html";
78 responsedata["keepalive"] = false;
79  
80  
81 UUID agentID;
82 UUID regionID;
83 string action;
84 if (!Utils.GetParams((string)request["uri"], out agentID, out regionID, out action))
85 {
86 m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", request["uri"]);
87 responsedata["int_response_code"] = 404;
88 responsedata["str_response_string"] = "false";
89  
90 return responsedata;
91 }
92  
93 // Next, let's parse the verb
94 string method = (string)request["http-method"];
95 if (method.Equals("DELETE"))
96 {
97 string auth_token = string.Empty;
98 if (request.ContainsKey("auth"))
99 auth_token = request["auth"].ToString();
100  
101 DoAgentDelete(request, responsedata, agentID, action, regionID, auth_token);
102 return responsedata;
103 }
104 else if (method.Equals("QUERYACCESS"))
105 {
106 DoQueryAccess(request, responsedata, agentID, regionID);
107 return responsedata;
108 }
109 else
110 {
111 m_log.ErrorFormat("[AGENT HANDLER]: method {0} not supported in agent message {1} (caller is {2})", method, (string)request["uri"], Util.GetCallerIP(request));
112 responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
113 responsedata["str_response_string"] = "Method not allowed";
114  
115 return responsedata;
116 }
117  
118 }
119  
120 protected virtual void DoQueryAccess(Hashtable request, Hashtable responsedata, UUID id, UUID regionID)
121 {
122 if (m_SimulationService == null)
123 {
124 m_log.Debug("[AGENT HANDLER]: Agent QUERY called. Harmless but useless.");
125 responsedata["content_type"] = "application/json";
126 responsedata["int_response_code"] = HttpStatusCode.NotImplemented;
127 responsedata["str_response_string"] = string.Empty;
128  
129 return;
130 }
131  
132 // m_log.DebugFormat("[AGENT HANDLER]: Received QUERYACCESS with {0}", (string)request["body"]);
133 OSDMap args = Utils.GetOSDMap((string)request["body"]);
134  
135 Vector3 position = Vector3.Zero;
136 if (args.ContainsKey("position"))
137 position = Vector3.Parse(args["position"].AsString());
138  
139 GridRegion destination = new GridRegion();
140 destination.RegionID = regionID;
141  
142 string reason;
143 string version;
144 bool result = m_SimulationService.QueryAccess(destination, id, position, out version, out reason);
145  
146 responsedata["int_response_code"] = HttpStatusCode.OK;
147  
148 OSDMap resp = new OSDMap(3);
149  
150 resp["success"] = OSD.FromBoolean(result);
151 resp["reason"] = OSD.FromString(reason);
152 resp["version"] = OSD.FromString(version);
153  
154 // We must preserve defaults here, otherwise a false "success" will not be put into the JSON map!
155 responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp, true);
156  
157 // Console.WriteLine("str_response_string [{0}]", responsedata["str_response_string"]);
158 }
159  
160 protected void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID id, string action, UUID regionID, string auth_token)
161 {
162 if (string.IsNullOrEmpty(action))
163 m_log.DebugFormat("[AGENT HANDLER]: >>> DELETE <<< RegionID: {0}; from: {1}; auth_code: {2}", regionID, Util.GetCallerIP(request), auth_token);
164 else
165 m_log.DebugFormat("[AGENT HANDLER]: Release {0} to RegionID: {1}", id, regionID);
166  
167 GridRegion destination = new GridRegion();
168 destination.RegionID = regionID;
169  
170 if (action.Equals("release"))
171 ReleaseAgent(regionID, id);
172 else
173 Util.FireAndForget(delegate { m_SimulationService.CloseAgent(destination, id, auth_token); });
174  
175 responsedata["int_response_code"] = HttpStatusCode.OK;
176 responsedata["str_response_string"] = "OpenSim agent " + id.ToString();
177  
178 //m_log.DebugFormat("[AGENT HANDLER]: Agent {0} Released/Deleted from region {1}", id, regionID);
179 }
180  
181 protected virtual void ReleaseAgent(UUID regionID, UUID id)
182 {
183 m_SimulationService.ReleaseAgent(regionID, id, "");
184 }
185 }
186  
187 public class AgentPostHandler : BaseStreamHandler
188 {
189 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
190  
191 private ISimulationService m_SimulationService;
192 protected bool m_Proxy = false;
193  
194 public AgentPostHandler(ISimulationService service) :
195 base("POST", "/agent")
196 {
197 m_SimulationService = service;
198 }
199  
200 public AgentPostHandler(string path) :
201 base("POST", path)
202 {
203 m_SimulationService = null;
204 }
205  
206 protected override byte[] ProcessRequest(string path, Stream request,
207 IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
208 {
209 // m_log.DebugFormat("[SIMULATION]: Stream handler called");
210  
211 Hashtable keysvals = new Hashtable();
212 Hashtable headervals = new Hashtable();
213  
214 string[] querystringkeys = httpRequest.QueryString.AllKeys;
215 string[] rHeaders = httpRequest.Headers.AllKeys;
216  
217 keysvals.Add("uri", httpRequest.RawUrl);
218 keysvals.Add("content-type", httpRequest.ContentType);
219 keysvals.Add("http-method", httpRequest.HttpMethod);
220  
221 foreach (string queryname in querystringkeys)
222 keysvals.Add(queryname, httpRequest.QueryString[queryname]);
223  
224 foreach (string headername in rHeaders)
225 headervals[headername] = httpRequest.Headers[headername];
226  
227 keysvals.Add("headers", headervals);
228 keysvals.Add("querystringkeys", querystringkeys);
229  
230 httpResponse.StatusCode = 200;
231 httpResponse.ContentType = "text/html";
232 httpResponse.KeepAlive = false;
233 Encoding encoding = Encoding.UTF8;
234  
235 Stream inputStream = null;
236 if (httpRequest.ContentType == "application/x-gzip")
237 inputStream = new GZipStream(request, CompressionMode.Decompress);
238 else if (httpRequest.ContentType == "application/json")
239 inputStream = request;
240 else // no go
241 {
242 httpResponse.StatusCode = 406;
243 return encoding.GetBytes("false");
244 }
245  
246 StreamReader reader = new StreamReader(inputStream, encoding);
247  
248 string requestBody = reader.ReadToEnd();
249 reader.Close();
250 keysvals.Add("body", requestBody);
251  
252 Hashtable responsedata = new Hashtable();
253  
254 UUID agentID;
255 UUID regionID;
256 string action;
257  
258 if (!Utils.GetParams((string)keysvals["uri"], out agentID, out regionID, out action))
259 {
260 m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", keysvals["uri"]);
261  
262 httpResponse.StatusCode = 404;
263  
264 return encoding.GetBytes("false");
265 }
266  
267 DoAgentPost(keysvals, responsedata, agentID);
268  
269 httpResponse.StatusCode = (int)responsedata["int_response_code"];
270 return encoding.GetBytes((string)responsedata["str_response_string"]);
271 }
272  
273 protected void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
274 {
275 OSDMap args = Utils.GetOSDMap((string)request["body"]);
276 if (args == null)
277 {
278 responsedata["int_response_code"] = HttpStatusCode.BadRequest;
279 responsedata["str_response_string"] = "Bad request";
280 return;
281 }
282  
283 AgentDestinationData data = CreateAgentDestinationData();
284 UnpackData(args, data, request);
285  
286 GridRegion destination = new GridRegion();
287 destination.RegionID = data.uuid;
288 destination.RegionLocX = data.x;
289 destination.RegionLocY = data.y;
290 destination.RegionName = data.name;
291  
292 GridRegion gatekeeper = ExtractGatekeeper(data);
293  
294 AgentCircuitData aCircuit = new AgentCircuitData();
295 try
296 {
297 aCircuit.UnpackAgentCircuitData(args);
298 }
299 catch (Exception ex)
300 {
301 m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
302 responsedata["int_response_code"] = HttpStatusCode.BadRequest;
303 responsedata["str_response_string"] = "Bad request";
304 return;
305 }
306  
307 OSDMap resp = new OSDMap(2);
308 string reason = String.Empty;
309  
310 // This is the meaning of POST agent
311 //m_regionClient.AdjustUserInformation(aCircuit);
312 //bool result = m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
313 bool result = CreateAgent(gatekeeper, destination, aCircuit, data.flags, data.fromLogin, out reason);
314  
315 resp["reason"] = OSD.FromString(reason);
316 resp["success"] = OSD.FromBoolean(result);
317 // Let's also send out the IP address of the caller back to the caller (HG 1.5)
318 resp["your_ip"] = OSD.FromString(GetCallerIP(request));
319  
320 // TODO: add reason if not String.Empty?
321 responsedata["int_response_code"] = HttpStatusCode.OK;
322 responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp);
323 }
324  
325 protected virtual AgentDestinationData CreateAgentDestinationData()
326 {
327 return new AgentDestinationData();
328 }
329  
330 protected virtual void UnpackData(OSDMap args, AgentDestinationData data, Hashtable request)
331 {
332 // retrieve the input arguments
333 if (args.ContainsKey("destination_x") && args["destination_x"] != null)
334 Int32.TryParse(args["destination_x"].AsString(), out data.x);
335 else
336 m_log.WarnFormat(" -- request didn't have destination_x");
337 if (args.ContainsKey("destination_y") && args["destination_y"] != null)
338 Int32.TryParse(args["destination_y"].AsString(), out data.y);
339 else
340 m_log.WarnFormat(" -- request didn't have destination_y");
341 if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
342 UUID.TryParse(args["destination_uuid"].AsString(), out data.uuid);
343 if (args.ContainsKey("destination_name") && args["destination_name"] != null)
344 data.name = args["destination_name"].ToString();
345 if (args.ContainsKey("teleport_flags") && args["teleport_flags"] != null)
346 data.flags = args["teleport_flags"].AsUInteger();
347 }
348  
349 protected virtual GridRegion ExtractGatekeeper(AgentDestinationData data)
350 {
351 return null;
352 }
353  
354 protected string GetCallerIP(Hashtable request)
355 {
356 if (!m_Proxy)
357 return Util.GetCallerIP(request);
358  
359 // We're behind a proxy
360 Hashtable headers = (Hashtable)request["headers"];
361  
362 //// DEBUG
363 //foreach (object o in headers.Keys)
364 // m_log.DebugFormat("XXX {0} = {1}", o.ToString(), (headers[o] == null? "null" : headers[o].ToString()));
365  
366 string xff = "X-Forwarded-For";
367 if (headers.ContainsKey(xff.ToLower()))
368 xff = xff.ToLower();
369  
370 if (!headers.ContainsKey(xff) || headers[xff] == null)
371 {
372 m_log.WarnFormat("[AGENT HANDLER]: No XFF header");
373 return Util.GetCallerIP(request);
374 }
375  
376 m_log.DebugFormat("[AGENT HANDLER]: XFF is {0}", headers[xff]);
377  
378 IPEndPoint ep = Util.GetClientIPFromXFF((string)headers[xff]);
379 if (ep != null)
380 return ep.Address.ToString();
381  
382 // Oops
383 return Util.GetCallerIP(request);
384 }
385  
386 // subclasses can override this
387 protected virtual bool CreateAgent(GridRegion gatekeeper, GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, bool fromLogin, out string reason)
388 {
389 return m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
390 }
391 }
392  
393 public class AgentPutHandler : BaseStreamHandler
394 {
395 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
396  
397 private ISimulationService m_SimulationService;
398 protected bool m_Proxy = false;
399  
400 public AgentPutHandler(ISimulationService service) :
401 base("PUT", "/agent")
402 {
403 m_SimulationService = service;
404 }
405  
406 public AgentPutHandler(string path) :
407 base("PUT", path)
408 {
409 m_SimulationService = null;
410 }
411  
412 protected override byte[] ProcessRequest(string path, Stream request,
413 IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
414 {
415 // m_log.DebugFormat("[SIMULATION]: Stream handler called");
416  
417 Hashtable keysvals = new Hashtable();
418 Hashtable headervals = new Hashtable();
419  
420 string[] querystringkeys = httpRequest.QueryString.AllKeys;
421 string[] rHeaders = httpRequest.Headers.AllKeys;
422  
423 keysvals.Add("uri", httpRequest.RawUrl);
424 keysvals.Add("content-type", httpRequest.ContentType);
425 keysvals.Add("http-method", httpRequest.HttpMethod);
426  
427 foreach (string queryname in querystringkeys)
428 keysvals.Add(queryname, httpRequest.QueryString[queryname]);
429  
430 foreach (string headername in rHeaders)
431 headervals[headername] = httpRequest.Headers[headername];
432  
433 keysvals.Add("headers", headervals);
434 keysvals.Add("querystringkeys", querystringkeys);
435  
436 Stream inputStream;
437 if (httpRequest.ContentType == "application/x-gzip")
438 inputStream = new GZipStream(request, CompressionMode.Decompress);
439 else
440 inputStream = request;
441  
442 Encoding encoding = Encoding.UTF8;
443 StreamReader reader = new StreamReader(inputStream, encoding);
444  
445 string requestBody = reader.ReadToEnd();
446 reader.Close();
447 keysvals.Add("body", requestBody);
448  
449 httpResponse.StatusCode = 200;
450 httpResponse.ContentType = "text/html";
451 httpResponse.KeepAlive = false;
452  
453 Hashtable responsedata = new Hashtable();
454  
455 UUID agentID;
456 UUID regionID;
457 string action;
458  
459 if (!Utils.GetParams((string)keysvals["uri"], out agentID, out regionID, out action))
460 {
461 m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", keysvals["uri"]);
462  
463 httpResponse.StatusCode = 404;
464  
465 return encoding.GetBytes("false");
466 }
467  
468 DoAgentPut(keysvals, responsedata);
469  
470 httpResponse.StatusCode = (int)responsedata["int_response_code"];
471 return encoding.GetBytes((string)responsedata["str_response_string"]);
472 }
473  
474 protected void DoAgentPut(Hashtable request, Hashtable responsedata)
475 {
476 OSDMap args = Utils.GetOSDMap((string)request["body"]);
477 if (args == null)
478 {
479 responsedata["int_response_code"] = HttpStatusCode.BadRequest;
480 responsedata["str_response_string"] = "Bad request";
481 return;
482 }
483  
484 // retrieve the input arguments
485 int x = 0, y = 0;
486 UUID uuid = UUID.Zero;
487 string regionname = string.Empty;
488 if (args.ContainsKey("destination_x") && args["destination_x"] != null)
489 Int32.TryParse(args["destination_x"].AsString(), out x);
490 if (args.ContainsKey("destination_y") && args["destination_y"] != null)
491 Int32.TryParse(args["destination_y"].AsString(), out y);
492 if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
493 UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
494 if (args.ContainsKey("destination_name") && args["destination_name"] != null)
495 regionname = args["destination_name"].ToString();
496  
497 GridRegion destination = new GridRegion();
498 destination.RegionID = uuid;
499 destination.RegionLocX = x;
500 destination.RegionLocY = y;
501 destination.RegionName = regionname;
502  
503 string messageType;
504 if (args["message_type"] != null)
505 messageType = args["message_type"].AsString();
506 else
507 {
508 m_log.Warn("[AGENT HANDLER]: Agent Put Message Type not found. ");
509 messageType = "AgentData";
510 }
511  
512 bool result = true;
513 if ("AgentData".Equals(messageType))
514 {
515 AgentData agent = new AgentData();
516 try
517 {
518 agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID));
519 }
520 catch (Exception ex)
521 {
522 m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
523 responsedata["int_response_code"] = HttpStatusCode.BadRequest;
524 responsedata["str_response_string"] = "Bad request";
525 return;
526 }
527  
528 //agent.Dump();
529 // This is one of the meanings of PUT agent
530 result = UpdateAgent(destination, agent);
531 }
532 else if ("AgentPosition".Equals(messageType))
533 {
534 AgentPosition agent = new AgentPosition();
535 try
536 {
537 agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID));
538 }
539 catch (Exception ex)
540 {
541 m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
542 return;
543 }
544 //agent.Dump();
545 // This is one of the meanings of PUT agent
546 result = m_SimulationService.UpdateAgent(destination, agent);
547  
548 }
549  
550 responsedata["int_response_code"] = HttpStatusCode.OK;
551 responsedata["str_response_string"] = result.ToString();
552 //responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp); ??? instead
553 }
554  
555 // subclasses can override this
556 protected virtual bool UpdateAgent(GridRegion destination, AgentData agent)
557 {
558 return m_SimulationService.UpdateAgent(destination, agent);
559 }
560 }
561  
562 public class AgentDestinationData
563 {
564 public int x;
565 public int y;
566 public string name;
567 public UUID uuid;
568 public uint flags;
569 public bool fromLogin;
570 }
571 }