GetMeshHandler.cs 10 KB

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