29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Specialized;
33 using System.IO.Compression;
34 using System.Reflection;
39 using OpenSim.Server.Base;
40 using OpenSim.Server.Handlers.Base;
41 using OpenSim.Services.Interfaces;
43 using OpenSim.Framework;
44 using OpenSim.Framework.Servers.HttpServer;
47 using OpenMetaverse.StructuredData;
52 namespace OpenSim.Server.Handlers.Simulation
56 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
64 m_SimulationService = sim;
67 public Hashtable
Handler(Hashtable request)
77 Hashtable responsedata =
new Hashtable();
78 responsedata[
"content_type"] =
"text/html";
79 responsedata[
"keepalive"] =
false;
85 if (!
Utils.
GetParams((
string)request[
"uri"], out agentID, out regionID, out action))
87 m_log.InfoFormat(
"[AGENT HANDLER]: Invalid parameters for agent message {0}", request[
"uri"]);
88 responsedata[
"int_response_code"] = 404;
89 responsedata[
"str_response_string"] =
"false";
95 string method = (string)request[
"http-method"];
96 if (method.Equals(
"DELETE"))
98 string auth_token = string.Empty;
99 if (request.ContainsKey(
"auth"))
100 auth_token = request[
"auth"].ToString();
102 DoAgentDelete(request, responsedata, agentID, action, regionID, auth_token);
105 else if (method.Equals(
"QUERYACCESS"))
107 DoQueryAccess(request, responsedata, agentID, regionID);
112 m_log.ErrorFormat(
"[AGENT HANDLER]: method {0} not supported in agent message {1} (caller is {2})", method, (string)request[
"uri"], Util.GetCallerIP(request));
113 responsedata[
"int_response_code"] = HttpStatusCode.MethodNotAllowed;
114 responsedata[
"str_response_string"] =
"Method not allowed";
121 protected virtual void DoQueryAccess(Hashtable request, Hashtable responsedata, UUID agentID, UUID regionID)
125 if (m_SimulationService == null)
127 m_log.Debug(
"[AGENT HANDLER]: Agent QUERY called. Harmless but useless.");
128 responsedata[
"content_type"] =
"application/json";
129 responsedata[
"int_response_code"] = HttpStatusCode.NotImplemented;
130 responsedata[
"str_response_string"] = string.Empty;
136 OSDMap args = Utils.GetOSDMap((string)request[
"body"]);
138 bool viaTeleport =
true;
139 if (args.ContainsKey(
"viaTeleport"))
140 viaTeleport = args[
"viaTeleport"].AsBoolean();
142 Vector3 position = Vector3.Zero;
143 if (args.ContainsKey(
"position"))
144 position = Vector3.Parse(args[
"position"].AsString());
146 string agentHomeURI = null;
147 if (args.ContainsKey(
"agent_home_uri"))
148 agentHomeURI = args[
"agent_home_uri"].AsString();
151 float theirVersion = 0f;
152 if (args.ContainsKey(
"my_version"))
154 string theirVersionStr = args[
"my_version"].AsString();
155 string[] parts = theirVersionStr.Split(
new char[] {
'/'});
156 if (parts.Length > 1)
157 theirVersion = float.Parse(parts[1]);
160 if (args.ContainsKey(
"context"))
164 float minVersionRequired = 0f;
165 float maxVersionRequired = 0f;
166 float minVersionProvided = 0f;
167 float maxVersionProvided = 0f;
169 if (args.ContainsKey(
"simulation_service_supported_min"))
170 minVersionProvided = (
float)args[
"simulation_service_supported_min"].AsReal();
171 if (args.ContainsKey(
"simulation_service_supported_max"))
172 maxVersionProvided = (
float)args[
"simulation_service_supported_max"].AsReal();
174 if (args.ContainsKey(
"simulation_service_accepted_min"))
175 minVersionRequired = (
float)args[
"simulation_service_accepted_min"].AsReal();
176 if (args.ContainsKey(
"simulation_service_accepted_max"))
177 maxVersionRequired = (
float)args[
"simulation_service_accepted_max"].AsReal();
179 responsedata[
"int_response_code"] = HttpStatusCode.OK;
184 float outboundVersion = 0f;
185 float inboundVersion = 0f;
187 if (minVersionProvided == 0f)
191 if(theirVersion == 0f)
193 resp[
"success"] = OSD.FromBoolean(
false);
194 resp[
"reason"] = OSD.FromString(
"Your region is running a old version of opensim no longer supported. Consider updating it");
195 responsedata[
"str_response_string"] = OSDParser.SerializeJsonString(resp,
true);
199 version = theirVersion;
204 resp[
"success"] = OSD.FromBoolean(
false);
206 responsedata[
"str_response_string"] = OSDParser.SerializeJsonString(resp,
true);
216 resp[
"success"] = OSD.FromBoolean(
false);
218 responsedata[
"str_response_string"] = OSDParser.SerializeJsonString(resp,
true);
224 resp[
"success"] = OSD.FromBoolean(
false);
226 responsedata[
"str_response_string"] = OSDParser.SerializeJsonString(resp,
true);
238 List<UUID> features =
new List<UUID>();
240 if (args.ContainsKey(
"features"))
244 foreach (
OSD o
in array)
245 features.Add(
new UUID(o.AsString()));
249 destination.RegionID = regionID;
253 ctx.InboundVersion = inboundVersion;
254 ctx.OutboundVersion = outboundVersion;
255 if (minVersionProvided == 0f)
257 ctx.InboundVersion = version;
258 ctx.OutboundVersion = version;
261 bool result = m_SimulationService.QueryAccess(destination, agentID, agentHomeURI, viaTeleport, position, features, ctx, out reason);
263 resp[
"success"] = OSD.FromBoolean(result);
264 resp[
"reason"] = OSD.FromString(reason);
265 string legacyVersion = String.Format(
"SIMULATION/{0}", version);
266 resp[
"version"] = OSD.FromString(legacyVersion);
267 resp[
"negotiated_inbound_version"] = OSD.FromReal(inboundVersion);
268 resp[
"negotiated_outbound_version"] = OSD.FromReal(outboundVersion);
271 foreach (UUID feature
in features)
272 featuresWanted.Add(OSD.FromString(feature.ToString()));
274 resp[
"features"] = featuresWanted;
277 responsedata[
"str_response_string"] = OSDParser.SerializeJsonString(resp,
true);
282 protected void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID
id,
string action, UUID regionID,
string auth_token)
284 if (
string.IsNullOrEmpty(action))
285 m_log.DebugFormat(
"[AGENT HANDLER]: >>> DELETE <<< RegionID: {0}; from: {1}; auth_code: {2}", regionID, Util.GetCallerIP(request), auth_token);
287 m_log.DebugFormat(
"[AGENT HANDLER]: Release {0} to RegionID: {1}", id, regionID);
290 destination.RegionID = regionID;
292 if (action.Equals(
"release"))
293 ReleaseAgent(regionID,
id);
296 o => m_SimulationService.CloseAgent(destination, id, auth_token), null,
"AgentHandler.DoAgentDelete");
298 responsedata[
"int_response_code"] = HttpStatusCode.OK;
299 responsedata[
"str_response_string"] =
"OpenSim agent " + id.ToString();
306 m_SimulationService.ReleaseAgent(regionID, id,
"");
312 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
315 protected bool m_Proxy =
false;
318 base(
"POST",
"/agent")
320 m_SimulationService = service;
326 m_SimulationService = null;
334 Hashtable keysvals =
new Hashtable();
335 Hashtable headervals =
new Hashtable();
337 string[] querystringkeys = httpRequest.QueryString.AllKeys;
338 string[] rHeaders = httpRequest.Headers.AllKeys;
340 keysvals.Add(
"uri", httpRequest.RawUrl);
341 keysvals.Add(
"content-type", httpRequest.ContentType);
342 keysvals.Add(
"http-method", httpRequest.HttpMethod);
344 foreach (
string queryname
in querystringkeys)
345 keysvals.Add(queryname, httpRequest.QueryString[queryname]);
347 foreach (
string headername
in rHeaders)
348 headervals[headername] = httpRequest.Headers[headername];
350 keysvals.Add(
"headers", headervals);
351 keysvals.Add(
"querystringkeys", querystringkeys);
353 httpResponse.StatusCode = 200;
354 httpResponse.ContentType =
"text/html";
355 httpResponse.KeepAlive =
false;
356 Encoding encoding = Encoding.UTF8;
358 if (httpRequest.ContentType !=
"application/json")
360 httpResponse.StatusCode = 406;
361 return encoding.GetBytes(
"false");
366 Stream inputStream = request;
367 Stream innerStream = null;
370 if ((httpRequest.ContentType ==
"application/x-gzip" || httpRequest.Headers[
"Content-Encoding"] ==
"gzip") || (httpRequest.Headers[
"X-Content-Encoding"] ==
"gzip"))
372 innerStream = inputStream;
376 using (StreamReader reader =
new StreamReader(inputStream, encoding))
378 requestBody = reader.ReadToEnd();
383 if (innerStream != null)
384 innerStream.Dispose();
385 inputStream.Dispose();
388 keysvals.Add(
"body", requestBody);
390 Hashtable responsedata =
new Hashtable();
396 if (!
Utils.
GetParams((
string)keysvals[
"uri"], out agentID, out regionID, out action))
398 m_log.InfoFormat(
"[AGENT HANDLER]: Invalid parameters for agent message {0}", keysvals[
"uri"]);
400 httpResponse.StatusCode = 404;
402 return encoding.GetBytes(
"false");
405 DoAgentPost(keysvals, responsedata, agentID);
407 httpResponse.StatusCode = (int)responsedata[
"int_response_code"];
408 return encoding.GetBytes((string)responsedata[
"str_response_string"]);
411 protected void DoAgentPost(Hashtable request, Hashtable responsedata, UUID
id)
415 OSDMap args = Utils.GetOSDMap((string)request[
"body"]);
418 responsedata[
"int_response_code"] = HttpStatusCode.BadRequest;
419 responsedata[
"str_response_string"] =
"Bad request";
423 if (args.ContainsKey(
"context"))
427 UnpackData(args, data, request);
430 destination.RegionID = data.uuid;
431 destination.RegionLocX = data.x;
432 destination.RegionLocY = data.y;
433 destination.RegionName = data.name;
435 GridRegion gatekeeper = ExtractGatekeeper(data);
440 aCircuit.UnpackAgentCircuitData(args);
444 m_log.InfoFormat(
"[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
445 responsedata[
"int_response_code"] = HttpStatusCode.BadRequest;
446 responsedata[
"str_response_string"] =
"Bad request";
452 if (args.ContainsKey(
"source_uuid"))
455 source.RegionLocX = Int32.Parse(args[
"source_x"].AsString());
456 source.RegionLocY = Int32.Parse(args[
"source_y"].AsString());
457 source.RegionName = args[
"source_name"].AsString();
458 source.RegionID = UUID.Parse(args[
"source_uuid"].AsString());
460 if (args.ContainsKey(
"source_server_uri"))
461 source.
RawServerURI = args[
"source_server_uri"].AsString();
463 source.RawServerURI = null;
467 string reason = String.Empty;
473 bool result = CreateAgent(source, gatekeeper, destination, aCircuit, data.
flags, data.
fromLogin, ctx, out reason);
475 resp[
"reason"] = OSD.FromString(reason);
476 resp[
"success"] = OSD.FromBoolean(result);
478 resp[
"your_ip"] = OSD.FromString(GetCallerIP(request));
481 responsedata[
"int_response_code"] = HttpStatusCode.OK;
482 responsedata[
"str_response_string"] = OSDParser.SerializeJsonString(resp);
493 if (args.ContainsKey(
"destination_x") && args[
"destination_x"] != null)
494 Int32.TryParse(args[
"destination_x"].AsString(), out data.x);
496 m_log.WarnFormat(
" -- request didn't have destination_x");
497 if (args.ContainsKey(
"destination_y") && args[
"destination_y"] != null)
498 Int32.TryParse(args[
"destination_y"].AsString(), out data.y);
500 m_log.WarnFormat(
" -- request didn't have destination_y");
501 if (args.ContainsKey(
"destination_uuid") && args[
"destination_uuid"] != null)
502 UUID.TryParse(args[
"destination_uuid"].AsString(), out data.uuid);
503 if (args.ContainsKey(
"destination_name") && args[
"destination_name"] != null)
504 data.
name = args[
"destination_name"].ToString();
505 if (args.ContainsKey(
"teleport_flags") && args[
"teleport_flags"] != null)
506 data.
flags = args[
"teleport_flags"].AsUInteger();
517 return Util.GetCallerIP(request);
520 Hashtable headers = (Hashtable)request[
"headers"];
526 string xff =
"X-Forwarded-For";
527 if (headers.ContainsKey(xff.ToLower()))
530 if (!headers.ContainsKey(xff) || headers[xff] == null)
532 m_log.WarnFormat(
"[AGENT HANDLER]: No XFF header");
533 return Util.GetCallerIP(request);
536 m_log.DebugFormat(
"[AGENT HANDLER]: XFF is {0}", headers[xff]);
538 IPEndPoint ep = Util.GetClientIPFromXFF((string)headers[xff]);
540 return ep.Address.ToString();
543 return Util.GetCallerIP(request);
550 reason = String.Empty;
570 bool ret = m_SimulationService.CreateAgent(source, destination, aCircuit, teleportFlags, ctx, out reason);
579 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
582 protected bool m_Proxy =
false;
585 base(
"PUT",
"/agent")
587 m_SimulationService = service;
593 m_SimulationService = null;
601 Hashtable keysvals =
new Hashtable();
602 Hashtable headervals =
new Hashtable();
604 string[] querystringkeys = httpRequest.QueryString.AllKeys;
605 string[] rHeaders = httpRequest.Headers.AllKeys;
607 keysvals.Add(
"uri", httpRequest.RawUrl);
608 keysvals.Add(
"content-type", httpRequest.ContentType);
609 keysvals.Add(
"http-method", httpRequest.HttpMethod);
611 foreach (
string queryname
in querystringkeys)
612 keysvals.Add(queryname, httpRequest.QueryString[queryname]);
614 foreach (
string headername
in rHeaders)
615 headervals[headername] = httpRequest.Headers[headername];
617 keysvals.Add(
"headers", headervals);
618 keysvals.Add(
"querystringkeys", querystringkeys);
621 Encoding encoding = Encoding.UTF8;
623 Stream inputStream = request;
624 Stream innerStream = null;
627 if ((httpRequest.ContentType ==
"application/x-gzip" || httpRequest.Headers[
"Content-Encoding"] ==
"gzip") || (httpRequest.Headers[
"X-Content-Encoding"] ==
"gzip"))
629 innerStream = inputStream;
633 using (StreamReader reader =
new StreamReader(inputStream, encoding))
635 requestBody = reader.ReadToEnd();
640 if (innerStream != null)
641 innerStream.Dispose();
642 inputStream.Dispose();
645 keysvals.Add(
"body", requestBody);
647 httpResponse.StatusCode = 200;
648 httpResponse.ContentType =
"text/html";
649 httpResponse.KeepAlive =
false;
651 Hashtable responsedata =
new Hashtable();
657 if (!
Utils.
GetParams((
string)keysvals[
"uri"], out agentID, out regionID, out action))
659 m_log.InfoFormat(
"[AGENT HANDLER]: Invalid parameters for agent message {0}", keysvals[
"uri"]);
661 httpResponse.StatusCode = 404;
663 return encoding.GetBytes(
"false");
666 DoAgentPut(keysvals, responsedata);
668 httpResponse.StatusCode = (int)responsedata[
"int_response_code"];
669 return encoding.GetBytes((string)responsedata[
"str_response_string"]);
672 protected void DoAgentPut(Hashtable request, Hashtable responsedata)
677 OSDMap args = Utils.GetOSDMap((string)request[
"body"]);
680 responsedata[
"int_response_code"] = HttpStatusCode.BadRequest;
681 responsedata[
"str_response_string"] =
"Bad request";
687 UUID uuid = UUID.Zero;
688 string regionname = string.Empty;
689 if (args.ContainsKey(
"destination_x") && args[
"destination_x"] != null)
690 Int32.TryParse(args[
"destination_x"].AsString(), out x);
691 if (args.ContainsKey(
"destination_y") && args[
"destination_y"] != null)
692 Int32.TryParse(args[
"destination_y"].AsString(), out y);
693 if (args.ContainsKey(
"destination_uuid") && args[
"destination_uuid"] != null)
694 UUID.TryParse(args[
"destination_uuid"].AsString(), out uuid);
695 if (args.ContainsKey(
"destination_name") && args[
"destination_name"] != null)
696 regionname = args[
"destination_name"].ToString();
697 if (args.ContainsKey(
"context"))
701 destination.RegionID = uuid;
702 destination.RegionLocX = x;
703 destination.RegionLocY = y;
704 destination.RegionName = regionname;
707 if (args[
"message_type"] != null)
708 messageType = args[
"message_type"].AsString();
711 m_log.Warn(
"[AGENT HANDLER]: Agent Put Message Type not found. ");
712 messageType =
"AgentData";
716 if (
"AgentData".Equals(messageType))
721 agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID), ctx);
725 m_log.InfoFormat(
"[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
726 responsedata[
"int_response_code"] = HttpStatusCode.BadRequest;
727 responsedata[
"str_response_string"] =
"Bad request";
735 else if (
"AgentPosition".Equals(messageType))
740 agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID), ctx);
744 m_log.InfoFormat(
"[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
749 result = m_SimulationService.UpdateAgent(destination, agent);
753 responsedata[
"int_response_code"] = HttpStatusCode.OK;
754 responsedata[
"str_response_string"] = result.ToString();
764 return m_SimulationService.UpdateAgent(destination, agent, ctx);
virtual GridRegion ExtractGatekeeper(AgentDestinationData data)
OpenMetaverse.StructuredData.OSDArray OSDArray
virtual bool UpdateAgent(GridRegion destination, AgentData agent)
void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Base streamed request handler.
OpenMetaverse.StructuredData.OSDMap OSDMap
static readonly float SimulationServiceVersionAcceptedMax
virtual void ReleaseAgent(UUID regionID, UUID id)
static readonly float SimulationServiceVersionSupportedMin
Hashtable Handler(Hashtable request)
Ionic.Zlib.GZipStream GZipStream
virtual bool CreateAgent(GridRegion source, GridRegion gatekeeper, GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, bool fromLogin, EntityTransferContext ctx, out string reason)
string RawServerURI
Provides direct access to the 'm_serverURI' field, without returning a generated URL if m_serverURI i...
void DoAgentPut(Hashtable request, Hashtable responsedata)
virtual void DoQueryAccess(Hashtable request, Hashtable responsedata, UUID agentID, UUID regionID)
static bool GetParams(string uri, out UUID uuid, out UUID regionID, out string action)
Extract the param from an uri.
virtual void UnpackData(OSDMap args, AgentDestinationData data, Hashtable request)
static readonly float SimulationServiceVersionSupportedMax
Circuit data for an agent. Connection information shared between regions that accept UDP connections ...
virtual AgentDestinationData CreateAgentDestinationData()
OpenMetaverse.StructuredData.OSD OSD
delegate void UpdateAgent(IClientAPI remoteClient, AgentUpdateArgs agentData)
string GetCallerIP(Hashtable request)
Replacement for ChildAgentDataUpdate. Used over RESTComms and LocalComms.
AgentHandler(ISimulationService sim)
AgentPostHandler(string path)
static readonly float SimulationServiceVersionAcceptedMin
This rules versioning regarding teleports, and compatibility between simulators in that regard...
Interactive OpenSim region server
override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Ionic.Zlib.CompressionMode CompressionMode
AgentPostHandler(ISimulationService service)
OpenSim.Services.Interfaces.GridRegion GridRegion
void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID id, string action, UUID regionID, string auth_token)
AgentPutHandler(string path)
AgentPutHandler(ISimulationService service)