29 using System.Collections.Generic;
31 using System.Reflection;
38 using OpenSim.Framework;
39 using OpenSim.Server.Base;
40 using OpenSim.Services.Interfaces;
41 using OpenSim.Framework.Servers.HttpServer;
42 using OpenSim.Server.Handlers.Base;
46 namespace OpenSim.Server.Handlers.MapImage
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 private string m_ConfigName =
"MapImageService";
57 base(config, server, configName)
59 IConfig serverConfig = config.Configs[m_ConfigName];
60 if (serverConfig == null)
61 throw new Exception(String.Format(
"No section {0} in config file", m_ConfigName));
63 string mapService = serverConfig.GetString(
"LocalServiceModule",
66 if (mapService == String.Empty)
67 throw new Exception(
"No LocalServiceModule in config file");
69 Object[] args =
new Object[] { config };
72 string gridService = serverConfig.GetString(
"GridService", String.Empty);
73 if (gridService !=
string.Empty)
74 m_GridService = ServerUtils.LoadPlugin<
IGridService>(gridService, args);
76 if (m_GridService != null)
77 m_log.InfoFormat(
"[MAP IMAGE HANDLER]: GridService check is ON");
79 m_log.InfoFormat(
"[MAP IMAGE HANDLER]: GridService check is OFF");
81 bool proxy = serverConfig.GetBoolean(
"HasProxy",
false);
89 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
95 base(
"POST",
"/removemap")
97 m_MapService = service;
105 StreamReader sr =
new StreamReader(requestData);
106 string body = sr.ReadToEnd();
112 Dictionary<string, object> request = ServerUtils.ParseQueryString(body);
114 if (!request.ContainsKey(
"X") || !request.ContainsKey(
"Y"))
117 return FailureResult(
"Bad request.");
120 Int32.TryParse(request[
"X"].ToString(), out x);
121 Int32.TryParse(request[
"Y"].ToString(), out y);
123 UUID scopeID = UUID.Zero;
124 if (request.ContainsKey(
"SCOPE"))
125 UUID.TryParse(request[
"SCOPE"].ToString(), out scopeID);
127 m_log.DebugFormat(
"[MAP REMOVE SERVER CONNECTOR]: Received position data for region at {0}-{1}", x, y);
129 if (m_GridService != null)
131 System.Net.IPAddress ipAddr = GetCallerIP(httpRequest);
132 GridRegion r = m_GridService.GetRegionByPosition(UUID.Zero, (int)Util.RegionToWorldLoc((uint)x), (int)Util.RegionToWorldLoc((uint)y));
137 m_log.WarnFormat(
"[MAP IMAGE HANDLER]: IP address {0} may be trying to impersonate region in IP {1}", ipAddr, r.ExternalEndPoint.Address);
138 return FailureResult(
"IP address of caller does not match IP address of registered region");
144 m_log.WarnFormat(
"[MAP IMAGE HANDLER]: IP address {0} may be rogue. Region not found at coordinates {1}-{2}",
146 return FailureResult(
"Region not found at given coordinates");
150 string reason = string.Empty;
151 bool result = m_MapService.RemoveMapTile(x, y, scopeID, out reason);
154 return SuccessResult();
156 return FailureResult(reason);
161 m_log.ErrorFormat(
"[MAP SERVICE IMAGE HANDLER]: Exception {0} {1}", e.Message, e.StackTrace);
164 return FailureResult(
"Unexpected server error");
167 private byte[] SuccessResult()
169 XmlDocument doc =
new XmlDocument();
171 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
174 doc.AppendChild(xmlnode);
176 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
179 doc.AppendChild(rootElement);
181 XmlElement result = doc.CreateElement(
"",
"Result",
"");
182 result.AppendChild(doc.CreateTextNode(
"Success"));
184 rootElement.AppendChild(result);
186 return DocToBytes(doc);
189 private byte[] FailureResult(
string msg)
191 XmlDocument doc =
new XmlDocument();
193 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
196 doc.AppendChild(xmlnode);
198 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
201 doc.AppendChild(rootElement);
203 XmlElement result = doc.CreateElement(
"",
"Result",
"");
204 result.AppendChild(doc.CreateTextNode(
"Failure"));
206 rootElement.AppendChild(result);
208 XmlElement message = doc.CreateElement(
"",
"Message",
"");
209 message.AppendChild(doc.CreateTextNode(msg));
211 rootElement.AppendChild(message);
213 return DocToBytes(doc);
216 private byte[] DocToBytes(XmlDocument doc)
218 MemoryStream ms =
new MemoryStream();
219 XmlTextWriter xw =
new XmlTextWriter(ms, null);
220 xw.Formatting = Formatting.Indented;
230 return request.RemoteIPEndPoint.Address;
233 string xff =
"X-Forwarded-For";
234 string xffValue = request.Headers[xff.ToLower()];
235 if (xffValue == null || (xffValue != null && xffValue ==
string.Empty))
236 xffValue = request.
Headers[xff];
238 if (xffValue == null || (xffValue != null && xffValue ==
string.Empty))
240 m_log.WarnFormat(
"[MAP IMAGE HANDLER]: No XFF header");
241 return request.RemoteIPEndPoint.Address;
244 System.Net.IPEndPoint ep = Util.GetClientIPFromXFF(xffValue);
249 return request.RemoteIPEndPoint.Address;
Base streamed request handler.
override byte[] Handle(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
MapRemoveServiceConnector(IConfigSource config, IHttpServer server, string configName)
MapServerRemoveHandler(IMapImageService service, IGridService grid, bool proxy)
IPEndPoint ExternalEndPoint
NameValueCollection Headers
OpenSim.Services.Interfaces.GridRegion GridRegion
OSHttpStatusCode
HTTP status codes (almost) as defined by W3C in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html and IETF in http://tools.ietf.org/html/rfc6585