GetMeshHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Specialized;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Web;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Services.Interfaces;
  41. using Caps = OpenSim.Framework.Capabilities.Caps;
  42. namespace OpenSim.Capabilities.Handlers
  43. {
  44. public class GetMeshHandler
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IAssetService m_assetService;
  49. public const string DefaultFormat = "vnd.ll.mesh";
  50. public GetMeshHandler(IAssetService assService)
  51. {
  52. m_assetService = assService;
  53. }
  54. public Hashtable Handle(Hashtable request)
  55. {
  56. Hashtable ret = new Hashtable();
  57. ret["int_response_code"] = (int)System.Net.HttpStatusCode.NotFound;
  58. ret["content_type"] = "text/plain";
  59. ret["keepalive"] = false;
  60. ret["reusecontext"] = false;
  61. ret["int_bytes"] = 0;
  62. ret["int_lod"] = 0;
  63. string MeshStr = (string)request["mesh_id"];
  64. //m_log.DebugFormat("[GETMESH]: called {0}", MeshStr);
  65. if (m_assetService == null)
  66. {
  67. m_log.Error("[GETMESH]: Cannot fetch mesh " + MeshStr + " without an asset service");
  68. }
  69. UUID meshID;
  70. if (!String.IsNullOrEmpty(MeshStr) && UUID.TryParse(MeshStr, out meshID))
  71. {
  72. // m_log.DebugFormat("[GETMESH]: Received request for mesh id {0}", meshID);
  73. ret = ProcessGetMesh(request, UUID.Zero, null);
  74. }
  75. else
  76. {
  77. m_log.Warn("[GETMESH]: Failed to parse a mesh_id from GetMesh request: " + (string)request["uri"]);
  78. }
  79. return ret;
  80. }
  81. public Hashtable ProcessGetMesh(Hashtable request, UUID AgentId, Caps cap)
  82. {
  83. Hashtable responsedata = new Hashtable();
  84. responsedata["int_response_code"] = 400; //501; //410; //404;
  85. responsedata["content_type"] = "text/plain";
  86. responsedata["keepalive"] = false;
  87. responsedata["str_response_string"] = "Request wasn't what was expected";
  88. responsedata["reusecontext"] = false;
  89. responsedata["int_lod"] = 0;
  90. responsedata["int_bytes"] = 0;
  91. string meshStr = string.Empty;
  92. if (request.ContainsKey("mesh_id"))
  93. meshStr = request["mesh_id"].ToString();
  94. UUID meshID = UUID.Zero;
  95. if (!String.IsNullOrEmpty(meshStr) && UUID.TryParse(meshStr, out meshID))
  96. {
  97. if (m_assetService == null)
  98. {
  99. responsedata["int_response_code"] = 404; //501; //410; //404;
  100. responsedata["content_type"] = "text/plain";
  101. responsedata["keepalive"] = false;
  102. responsedata["str_response_string"] = "The asset service is unavailable. So is your mesh.";
  103. responsedata["reusecontext"] = false;
  104. return responsedata;
  105. }
  106. AssetBase mesh = m_assetService.Get(meshID.ToString());
  107. if (mesh != null)
  108. {
  109. if (mesh.Type == (SByte)AssetType.Mesh)
  110. {
  111. Hashtable headers = new Hashtable();
  112. responsedata["headers"] = headers;
  113. string range = String.Empty;
  114. if (((Hashtable)request["headers"])["range"] != null)
  115. range = (string)((Hashtable)request["headers"])["range"];
  116. else if (((Hashtable)request["headers"])["Range"] != null)
  117. range = (string)((Hashtable)request["headers"])["Range"];
  118. if (!String.IsNullOrEmpty(range)) // Mesh Asset LOD // Physics
  119. {
  120. // Range request
  121. int start, end;
  122. if (TryParseRange(range, out start, out end))
  123. {
  124. // Before clamping start make sure we can satisfy it in order to avoid
  125. // sending back the last byte instead of an error status
  126. if (start >= mesh.Data.Length)
  127. {
  128. responsedata["int_response_code"] = 404; //501; //410; //404;
  129. responsedata["content_type"] = "text/plain";
  130. responsedata["keepalive"] = false;
  131. responsedata["str_response_string"] = "This range doesnt exist.";
  132. responsedata["reusecontext"] = false;
  133. responsedata["int_lod"] = 3;
  134. return responsedata;
  135. }
  136. else
  137. {
  138. end = Utils.Clamp(end, 0, mesh.Data.Length - 1);
  139. start = Utils.Clamp(start, 0, end);
  140. int len = end - start + 1;
  141. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  142. if (start > 20000)
  143. {
  144. responsedata["int_lod"] = 3;
  145. }
  146. else if (start < 4097)
  147. {
  148. responsedata["int_lod"] = 1;
  149. }
  150. else
  151. {
  152. responsedata["int_lod"] = 2;
  153. }
  154. if (start == 0 && len == mesh.Data.Length) // well redudante maybe
  155. {
  156. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
  157. responsedata["bin_response_data"] = mesh.Data;
  158. responsedata["int_bytes"] = mesh.Data.Length;
  159. responsedata["reusecontext"] = false;
  160. responsedata["int_lod"] = 3;
  161. }
  162. else
  163. {
  164. responsedata["int_response_code"] =
  165. (int)System.Net.HttpStatusCode.PartialContent;
  166. headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end,
  167. mesh.Data.Length);
  168. byte[] d = new byte[len];
  169. Array.Copy(mesh.Data, start, d, 0, len);
  170. responsedata["bin_response_data"] = d;
  171. responsedata["int_bytes"] = len;
  172. responsedata["reusecontext"] = false;
  173. }
  174. }
  175. }
  176. else
  177. {
  178. m_log.Warn("[GETMESH]: Failed to parse a range from GetMesh request, sending full asset: " + (string)request["uri"]);
  179. responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
  180. responsedata["content_type"] = "application/vnd.ll.mesh";
  181. responsedata["int_response_code"] = 200;
  182. responsedata["reusecontext"] = false;
  183. responsedata["int_lod"] = 3;
  184. }
  185. }
  186. else
  187. {
  188. responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
  189. responsedata["content_type"] = "application/vnd.ll.mesh";
  190. responsedata["int_response_code"] = 200;
  191. responsedata["reusecontext"] = false;
  192. responsedata["int_lod"] = 3;
  193. }
  194. }
  195. // Optionally add additional mesh types here
  196. else
  197. {
  198. responsedata["int_response_code"] = 404; //501; //410; //404;
  199. responsedata["content_type"] = "text/plain";
  200. responsedata["keepalive"] = false;
  201. responsedata["str_response_string"] = "Unfortunately, this asset isn't a mesh.";
  202. responsedata["reusecontext"] = false;
  203. responsedata["int_lod"] = 1;
  204. return responsedata;
  205. }
  206. }
  207. else
  208. {
  209. responsedata["int_response_code"] = 404; //501; //410; //404;
  210. responsedata["content_type"] = "text/plain";
  211. responsedata["keepalive"] = false;
  212. responsedata["str_response_string"] = "Your Mesh wasn't found. Sorry!";
  213. responsedata["reusecontext"] = false;
  214. responsedata["int_lod"] = 0;
  215. return responsedata;
  216. }
  217. }
  218. return responsedata;
  219. }
  220. private bool TryParseRange(string header, out int start, out int end)
  221. {
  222. if (header.StartsWith("bytes="))
  223. {
  224. string[] rangeValues = header.Substring(6).Split('-');
  225. if (rangeValues.Length == 2)
  226. {
  227. if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end))
  228. return true;
  229. }
  230. }
  231. start = end = 0;
  232. return false;
  233. }
  234. }
  235. }