GetMeshHandler.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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["int_bytes"] = 0;
  60. string MeshStr = (string)request["mesh_id"];
  61. //m_log.DebugFormat("[GETMESH]: called {0}", MeshStr);
  62. if (m_assetService == null)
  63. {
  64. m_log.Error("[GETMESH]: Cannot fetch mesh " + MeshStr + " without an asset service");
  65. ret["keepalive"] = false;
  66. return ret;
  67. }
  68. UUID meshID;
  69. if (!String.IsNullOrEmpty(MeshStr) && UUID.TryParse(MeshStr, out meshID))
  70. {
  71. // m_log.DebugFormat("[GETMESH]: Received request for mesh id {0}", meshID);
  72. ret = ProcessGetMesh(request, UUID.Zero, null);
  73. }
  74. else
  75. {
  76. m_log.Warn("[GETMESH]: Failed to parse a mesh_id from GetMesh request: " + (string)request["uri"]);
  77. }
  78. return ret;
  79. }
  80. public Hashtable ProcessGetMesh(Hashtable request, UUID AgentId, Caps cap)
  81. {
  82. Hashtable responsedata = new Hashtable();
  83. responsedata["int_response_code"] = 400; //501; //410; //404;
  84. responsedata["content_type"] = "text/plain";
  85. responsedata["int_bytes"] = 0;
  86. string meshStr = string.Empty;
  87. if (request.ContainsKey("mesh_id"))
  88. meshStr = request["mesh_id"].ToString();
  89. UUID meshID = UUID.Zero;
  90. if (!String.IsNullOrEmpty(meshStr) && UUID.TryParse(meshStr, out meshID))
  91. {
  92. if (m_assetService == null)
  93. {
  94. responsedata["int_response_code"] = 404; //501; //410; //404;
  95. responsedata["keepalive"] = false;
  96. responsedata["str_response_string"] = "The asset service is unavailable. So is your mesh.";
  97. return responsedata;
  98. }
  99. AssetBase mesh = m_assetService.Get(meshID.ToString());
  100. if (mesh != null)
  101. {
  102. if (mesh.Type == (SByte)AssetType.Mesh)
  103. {
  104. Hashtable headers = new Hashtable();
  105. responsedata["headers"] = headers;
  106. string range = String.Empty;
  107. if (((Hashtable)request["headers"])["range"] != null)
  108. range = (string)((Hashtable)request["headers"])["range"];
  109. else if (((Hashtable)request["headers"])["Range"] != null)
  110. range = (string)((Hashtable)request["headers"])["Range"];
  111. if (!String.IsNullOrEmpty(range)) // Mesh Asset LOD // Physics
  112. {
  113. // Range request
  114. int start, end;
  115. if (TryParseRange(range, out start, out end))
  116. {
  117. // Before clamping start make sure we can satisfy it in order to avoid
  118. // sending back the last byte instead of an error status
  119. if (start >= mesh.Data.Length)
  120. {
  121. responsedata["int_response_code"] = 404; //501; //410; //404;
  122. responsedata["content_type"] = "text/plain";
  123. responsedata["str_response_string"] = "This range doesnt exist.";
  124. return responsedata;
  125. }
  126. else
  127. {
  128. end = Utils.Clamp(end, 0, mesh.Data.Length - 1);
  129. start = Utils.Clamp(start, 0, end);
  130. int len = end - start + 1;
  131. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  132. responsedata["int_response_code"] =
  133. (int)System.Net.HttpStatusCode.PartialContent;
  134. headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, mesh.Data.Length);
  135. byte[] d = new byte[len];
  136. Array.Copy(mesh.Data, start, d, 0, len);
  137. responsedata["bin_response_data"] = d;
  138. responsedata["int_bytes"] = len;
  139. }
  140. }
  141. else
  142. {
  143. m_log.Warn("[GETMESH]: Failed to parse a range from GetMesh request, sending full asset: " + (string)request["uri"]);
  144. responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
  145. responsedata["content_type"] = "application/vnd.ll.mesh";
  146. responsedata["int_response_code"] = 200;
  147. }
  148. }
  149. else
  150. {
  151. responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
  152. responsedata["content_type"] = "application/vnd.ll.mesh";
  153. responsedata["int_response_code"] = 200;
  154. }
  155. }
  156. // Optionally add additional mesh types here
  157. else
  158. {
  159. responsedata["int_response_code"] = 404; //501; //410; //404;
  160. responsedata["content_type"] = "text/plain";
  161. responsedata["str_response_string"] = "Unfortunately, this asset isn't a mesh.";
  162. return responsedata;
  163. }
  164. }
  165. else
  166. {
  167. responsedata["int_response_code"] = 404; //501; //410; //404;
  168. responsedata["content_type"] = "text/plain";
  169. responsedata["str_response_string"] = "Your Mesh wasn't found. Sorry!";
  170. return responsedata;
  171. }
  172. }
  173. return responsedata;
  174. }
  175. private bool TryParseRange(string header, out int start, out int end)
  176. {
  177. if (header.StartsWith("bytes="))
  178. {
  179. string[] rangeValues = header.Substring(6).Split('-');
  180. if (rangeValues.Length == 2)
  181. {
  182. if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end))
  183. return true;
  184. }
  185. }
  186. start = end = 0;
  187. return false;
  188. }
  189. }
  190. }